lisn.js
Version:
Simply handle user gestures and actions. Includes widgets.
1 lines • 13.2 kB
Source Map (JSON)
{"version":3,"file":"dom-search.cjs","names":["MH","_interopRequireWildcard","require","_cssAlter","_domEvents","_log","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","getReferenceElement","spec","thisElement","referenceElement","getElementById","slice","relation","find","p","startsWith","usageError","rest","lengthOf","matchOp","refOrCls","selector","_getData","getData","PREFIX_REF","DATA_REF","getFirstReferenceElement","getLastReferenceElement","getThisReferenceElement","getNextReferenceElement","getPrevReferenceElement","logError","bugError","exports","waitForReferenceElement","timeout","waitForElement","prefixName","prefixData","getAllReferenceElements","docQuerySelectorAll","docQuerySelector","allRefs","closest","getNextOrPrevReferenceElement","goBackward","_getThisReferenceElem","_allRefs$refIndex","getDoc","contains","numRefs","refIndex","currentIsAfter","isNodeBAfterA"],"sources":["../../../src/ts/utils/dom-search.ts"],"sourcesContent":["/**\n * @module Utils\n *\n * @categoryDescription DOM: Searching for reference elements\n * The functions allow you to find elements that match a given string\n * specification.\n */\n\nimport * as MH from \"@lisn/globals/minification-helpers\";\n\nimport { getData } from \"@lisn/utils/css-alter\";\nimport { waitForElement } from \"@lisn/utils/dom-events\";\nimport { logError } from \"@lisn/utils/log\";\n\n/**\n * Get the element that matches the given reference specification.\n *\n * The specification is of the form:\n *\n * ```\n * <FullSpec> ::=\n * <Relation> \".\" <ClassName> |\n * <Relation> [\"-\" <ReferenceName>] |\n * #<DOM_ID>\n *\n * <Relation> :==\n * \"next\" |\n * \"prev\" |\n * \"this\" |\n * \"first\" |\n * \"last\"\n * ```\n *\n * - `<DOM_ID>` is the unique ID of the element\n * - `<ClassName>` is a CSS class on the element\n * - `<ReferenceName>` is the value of the `data-lisn-ref` attribute on the\n * element you are targeting. If not given, defaults to the value of the\n * `data-lisn-ref` attribute on `thisElement`.\n *\n * There now follows an explanation of how \"next\", \"prev\", \"this\", \"first\" and\n * \"last\" search the DOM:\n * - \"next\": the tree is search in document order (depth first, then breadth),\n * so the returned element could be a descendant of `thisElement`\n * - \"prev\": the tree is search in document order (depth first, then breadth),\n * but excluding ancestors of `thisElement`, so the returned element is\n * guaranteed to _not_ be an ancestor or descendant of `thisElement`.\n * - \"this\": if the given element itself matches the specification, it is\n * returned, otherwise the closest ancestor of the given element that matches\n * the specification\n * - \"first\": the first element matching; the tree is search in document order\n * (depth first, then breadth).\n * - \"last\": the last element matching; the tree is search in document order\n * (depth first, then breadth).\n *\n * @category DOM: Searching for reference elements\n *\n * @param thisElement The element to search relative to\n *\n * @throws {@link Errors.LisnUsageError | LisnUsageError}\n * If the specification is invalid or if thisElement is\n * not given for a specification of \"next\", \"prev\" or \"this\"\n */\nexport const getReferenceElement = (\n spec: string,\n thisElement: Element,\n): Element | null => {\n if (!spec) {\n return thisElement;\n }\n\n if (spec[0] === \"#\") {\n // element ID\n const referenceElement = MH.getElementById(spec.slice(1));\n if (!referenceElement) {\n return null;\n }\n return referenceElement;\n }\n\n const relation = [\"next\", \"prev\", \"this\", \"first\", \"last\"].find(\n (p) => spec.startsWith(`${p}.`) || spec.startsWith(`${p}-`) || spec === p,\n );\n\n if (!relation) {\n throw MH.usageError(`Invalid search specification '${spec}'`);\n }\n\n const rest = spec.slice(MH.lengthOf(relation));\n const matchOp = rest.slice(0, 1);\n let refOrCls = rest.slice(1);\n\n let selector: string;\n if (matchOp === \".\") {\n selector = matchOp + refOrCls;\n } else {\n if (!refOrCls) {\n refOrCls = getData(thisElement, PREFIX_REF) ?? \"\";\n }\n\n if (!refOrCls) {\n throw MH.usageError(`No reference name in '${spec}'`);\n }\n\n selector = `[${DATA_REF}=\"${refOrCls}\"]`;\n }\n\n let referenceElement;\n if (relation === \"first\") {\n referenceElement = getFirstReferenceElement(selector);\n } else if (relation === \"last\") {\n referenceElement = getLastReferenceElement(selector);\n } else {\n if (relation === \"this\") {\n referenceElement = getThisReferenceElement(selector, thisElement);\n } else if (relation === \"next\") {\n referenceElement = getNextReferenceElement(selector, thisElement);\n } else if (relation === \"prev\") {\n referenceElement = getPrevReferenceElement(selector, thisElement);\n } else {\n /* istanbul ignore next */ {\n logError(MH.bugError(`Unhandled relation case ${relation}`));\n return null;\n }\n }\n }\n\n if (!referenceElement) {\n return null;\n }\n\n return referenceElement;\n};\n\n/**\n * Like {@link getReferenceElement} excepts if no element matches the\n * specification if will wait for at most the given time for such an element.\n *\n * @category DOM: Searching for reference elements\n */\nexport const waitForReferenceElement = (\n spec: string,\n thisElement: Element,\n timeout = 200,\n) => waitForElement(() => getReferenceElement(spec, thisElement), timeout);\n\n// ----------------------------------------\n\nconst PREFIX_REF = MH.prefixName(\"ref\");\nconst DATA_REF = MH.prefixData(PREFIX_REF);\n\nconst getAllReferenceElements = (\n selector: string,\n): NodeListOf<Element> | null => MH.docQuerySelectorAll(selector);\n\nconst getFirstReferenceElement = (selector: string): Element | null =>\n MH.docQuerySelector(selector);\n\nconst getLastReferenceElement = (selector: string): Element | null => {\n const allRefs = getAllReferenceElements(selector);\n return (allRefs && allRefs[MH.lengthOf(allRefs) - 1]) || null;\n};\n\nconst getThisReferenceElement = (\n selector: string,\n thisElement: Element,\n): Element | null => thisElement.closest(selector);\n\nconst getNextReferenceElement = (selector: string, thisElement: Element) =>\n getNextOrPrevReferenceElement(selector, thisElement, false);\n\nconst getPrevReferenceElement = (selector: string, thisElement: Element) =>\n getNextOrPrevReferenceElement(selector, thisElement, true);\n\nconst getNextOrPrevReferenceElement = (\n selector: string,\n thisElement: Element,\n goBackward: boolean,\n): Element | null => {\n thisElement = getThisReferenceElement(selector, thisElement) ?? thisElement;\n\n if (!MH.getDoc().contains(thisElement)) {\n return null;\n }\n\n const allRefs = getAllReferenceElements(selector);\n if (!allRefs) {\n return null;\n }\n\n const numRefs = MH.lengthOf(allRefs);\n let refIndex = goBackward ? numRefs - 1 : -1;\n for (let i = 0; i < numRefs; i++) {\n const currentIsAfter = MH.isNodeBAfterA(thisElement, allRefs[i]);\n\n // As soon as we find either the starting element or the first element\n // that follows it, stop iteration.\n // - If we're looking for the previous reference, then take the previous\n // element in the iteration.\n // - Otherwise, if the current element in the iteration is the same as the\n // starting one, then take either the next element in the iteration.\n // - Otherwise, (if the current element follows the starting one, as\n // would happen if the starting element was not in the list of matched\n // elements, take the current element in the iteration.\n if (allRefs[i] === thisElement || currentIsAfter) {\n refIndex = i + (goBackward ? -1 : currentIsAfter ? 0 : 1);\n break;\n }\n }\n\n return allRefs[refIndex] ?? null;\n};\n"],"mappings":";;;;;;AAQA,IAAAA,EAAA,GAAAC,uBAAA,CAAAC,OAAA;AAEA,IAAAC,SAAA,GAAAD,OAAA;AACA,IAAAE,UAAA,GAAAF,OAAA;AACA,IAAAG,IAAA,GAAAH,OAAA;AAA2C,SAAAD,wBAAAK,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAP,uBAAA,YAAAA,CAAAK,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAAA,CAAAK,GAAA,CAAAX,CAAA,UAAAM,CAAA,CAAAM,GAAA,CAAAZ,CAAA,GAAAM,CAAA,CAAAO,GAAA,CAAAb,CAAA,EAAAQ,CAAA,gBAAAP,CAAA,IAAAD,CAAA,gBAAAC,CAAA,OAAAa,cAAA,CAAAC,IAAA,CAAAf,CAAA,EAAAC,CAAA,OAAAM,CAAA,IAAAD,CAAA,GAAAU,MAAA,CAAAC,cAAA,KAAAD,MAAA,CAAAE,wBAAA,CAAAlB,CAAA,EAAAC,CAAA,OAAA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