UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

122 lines (121 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InteractionInvalidation = void 0; require("core-js/modules/es.string.replace.js"); require("core-js/modules/web.dom-collections.iterator.js"); class InteractionInvalidation { constructor() { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; this.bypassElement = null; this.bypassSelectors = []; this.options = options || {}; return this; } setBypassElement(bypassElement) { if (bypassElement instanceof HTMLElement) { this.bypassElement = bypassElement; } return this; } setBypassSelector(bypassSelector) { if (!Array.isArray(bypassSelector)) { bypassSelector = [bypassSelector]; } this.bypassSelectors = bypassSelector; return this; } activate() { let targetElement = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; if (!this._nodesToInvalidate) { this._runInvalidation(targetElement); } } revert() { this._revertInvalidation(); this._nodesToInvalidate = null; } _runInvalidation(targetElement) { if (typeof document === 'undefined') { return; } this._nodesToInvalidate = this.getNodesToInvalidate(targetElement); if (!Array.isArray(this._nodesToInvalidate)) { return; } for (const node of this._nodesToInvalidate) { if (!node) { continue; } if (this.options.tabIndex !== false) { const tabindex = node.getAttribute('tabindex'); if (tabindex !== null && typeof node.__tabindex === 'undefined') { node.__tabindex = tabindex; } node.setAttribute('tabindex', '-1'); } if (this.options.ariaHidden !== false) { const ariahidden = node.getAttribute('aria-hidden'); if (ariahidden !== null && typeof node.__ariahidden === 'undefined') { node.__ariahidden = ariahidden; } node.setAttribute('aria-hidden', 'true'); } } } _revertInvalidation() { if (!Array.isArray(this._nodesToInvalidate)) { return; } for (const node of this._nodesToInvalidate) { if (!node) { continue; } if (this.options.tabIndex !== false) { if (typeof node.__tabindex !== 'undefined') { node.setAttribute('tabindex', node.__tabindex); delete node.__tabindex; } else { node.removeAttribute('tabindex'); } } if (this.options.ariaHidden !== false) { if (typeof node.__ariahidden !== 'undefined') { node.setAttribute('aria-hidden', node.__ariahidden); delete node.__ariahidden; } else { node.removeAttribute('aria-hidden'); } } } } getNodesToInvalidate() { let targetElement = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; if (typeof document === 'undefined') { return []; } if (typeof targetElement === 'string') { targetElement = document.querySelector(targetElement); } const rootSelector = targetElement ? '*' : 'html *'; const elementSelector = this.bypassSelectors.map(s => `:not(${s})`).join(''); const selector = `${rootSelector} ${elementSelector}:not(script):not(style):not(path):not(head *)`; if (process.env.NODE_ENV === 'test') { const excludeSelectors = []; const testSelector = selector.split(':').map(localSel => { if (localSel.endsWith(' *)')) { excludeSelectors.push(...Array.from((targetElement || document.documentElement).querySelectorAll(localSel.match(/\(([^)]*)\)/)[1]))); localSel = localSel.replace(' *', ''); } return localSel; }).join(':'); return Array.from((targetElement || document.documentElement).querySelectorAll(testSelector)).filter(node => !excludeSelectors.includes(node)); } try { return Array.from((targetElement || document.documentElement).querySelectorAll(selector)); } catch (error) {} } } exports.InteractionInvalidation = InteractionInvalidation; //# sourceMappingURL=InteractionInvalidation.js.map