UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

117 lines 5.28 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable dot-notation */ /** * This was originally copied from PivotTiles basicElements.tsx file * * Use like this: let thisID = findParentElementPropLikeThis(e.target, 'id', 'ButtonID', 5, 'begins'); * Will find element where id begins wtih ButtonID up to 5 parents up. * @param e: element * @param prop: 'classList' * @param value: 'ControlZone' * @param maxHops * @param search: 'contains' * @param alertError //Alert on error or not found * @param consoleResult //Console log result element * */ import { check4This, Check4 } from '../../Links/CheckSearch'; export function findParentElementLikeThis(e, prop, value, maxHops, search, alertError = true, consoleResult = true) { let result = null; let checkElement = e['parentElement']; let found = false; let foundHops = 0; for (let i = 0; i < maxHops; i++) { if (found === false) { if (checkElement[prop]) { foundHops++; let parentProp = prop === 'classList' ? checkElement['className'].split(' ') : checkElement[prop]; if (parentProp) { if (prop.toLowerCase() === 'classlist') { parentProp = JSON.parse(JSON.stringify(parentProp)); if (search === 'contains') { if (parentProp.indexOf(value) > -1) { result = checkElement; found = true; } } else if (search === 'begins') { if (parentProp.indexOf(value) === 0) { result = checkElement; found = true; } } else if (search === 'ends') { if (parentProp.indexOf(value) === parentProp.length - 1) { result = checkElement; found = true; } } else if (search === 'exact') { if (checkElement['className'] === value) { result = checkElement; found = true; } } } else if (search === 'begins') { if (parentProp.indexOf(value) === 0) { result = checkElement; found = true; } } else if (search === 'ends') { alert('findParentElementPropLikeThis: Error - feature not yet avaialble!'); } else if (search === 'contains') { if (parentProp.indexOf(value) > -1) { result = checkElement; found = true; } } else if (search === 'exact') { if (parentProp === value) { result = checkElement; found = true; } } } } if (found === false) { checkElement = checkElement['parentElement']; } } } if (found === false && alertError === true) { alert('findParentElementPropLikeThis: Could not find parent element - see console.'); console.log('findParentElementPropLikeThis: found, prop, value, foundHops, maxHops, search', found, prop, value, foundHops, maxHops, search); } if (consoleResult === true) { if (check4This(Check4.fpsDomSearch_Eq_true) === true) console.log('findParentElementPropLikeThis: found, prop, value, foundHops, maxHops, search', found, prop, value, foundHops, maxHops, search); } return result; } /** * * @param e: element * @param prop: 'classList' * @param value: 'ControlZone' * @param maxHops * @param search: 'contains' * @returns */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function findParentElementPropLikeThis(e, prop, value, maxHops, search) { const result = findParentElementLikeThis(e, prop, value, maxHops, search); const found = result === null ? false : true; const propResult = result !== null ? result[prop] : result; if (found === false) { alert('findParentElementPropLikeThis: Could not find parent element - see console.'); console.log('findParentElementPropLikeThis: Did not find: prop', prop); console.log('findParentElementPropLikeThis: Did not find: value', result[prop]); console.log('findParentElementPropLikeThis: Did not find: maxHops', maxHops); console.log('findParentElementPropLikeThis: Did not find: search', search); } return propResult; } //# sourceMappingURL=domSearch.js.map