@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
115 lines • 4 kB
JavaScript
import "core-js/modules/es.string.replace.js";
import "core-js/modules/web.dom-collections.iterator.js";
export 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) {}
}
}
//# sourceMappingURL=InteractionInvalidation.js.map