2022-01-27 12:21:02 +00:00
|
|
|
/* global FIREFOX UA */// toolbox.js
|
2018-11-07 06:09:29 +00:00
|
|
|
/* global prefs */
|
2017-03-25 05:54:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/* exported
|
|
|
|
$createLink
|
|
|
|
$isTextInput
|
2021-02-28 20:01:49 +00:00
|
|
|
$remove
|
|
|
|
$$remove
|
2021-01-01 14:27:58 +00:00
|
|
|
animateElement
|
2022-01-13 09:47:37 +00:00
|
|
|
focusAccessibility
|
2021-01-01 14:27:58 +00:00
|
|
|
getEventKeyName
|
|
|
|
messageBoxProxy
|
|
|
|
moveFocus
|
|
|
|
scrollElementIntoView
|
|
|
|
setupLivePrefs
|
2021-07-30 12:44:06 +00:00
|
|
|
showSpinner
|
|
|
|
toggleDataset
|
2021-01-01 14:27:58 +00:00
|
|
|
*/
|
2017-03-25 20:39:21 +00:00
|
|
|
|
2020-11-08 08:12:42 +00:00
|
|
|
Object.assign(EventTarget.prototype, {
|
|
|
|
on: addEventListener,
|
|
|
|
off: removeEventListener,
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
//#region Exports
|
2020-10-13 18:14:54 +00:00
|
|
|
|
2022-02-14 19:19:20 +00:00
|
|
|
$.root = document.documentElement;
|
|
|
|
$.rootCL = $.root.classList;
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
// Makes the focus outline appear on keyboard tabbing, but not on mouse clicks.
|
|
|
|
const focusAccessibility = {
|
|
|
|
// last event's focusedViaClick
|
|
|
|
lastFocusedViaClick: false,
|
2022-01-19 11:45:45 +00:00
|
|
|
get: el => el && el.dataset.focusedViaClick != null,
|
|
|
|
toggle: (el, state) => el && toggleDataset(el, 'focusedViaClick', state),
|
2021-01-01 14:27:58 +00:00
|
|
|
// to avoid a full layout recalc due to changes on body/root
|
|
|
|
// we modify the closest focusable element (like input or button or anything with tabindex=0)
|
|
|
|
closest(el) {
|
|
|
|
let labelSeen;
|
|
|
|
for (; el; el = el.parentElement) {
|
|
|
|
if (el.localName === 'label' && el.control && !labelSeen) {
|
|
|
|
el = el.control;
|
|
|
|
labelSeen = true;
|
2019-03-25 12:48:53 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (el.tabIndex >= 0) return el;
|
2020-10-05 15:37:47 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
|
|
|
};
|
2017-03-25 05:54:58 +00:00
|
|
|
|
2020-11-06 18:04:10 +00:00
|
|
|
/**
|
2021-01-01 14:27:58 +00:00
|
|
|
* Autoloads message-box.js
|
|
|
|
* @alias messageBox
|
2020-11-06 18:04:10 +00:00
|
|
|
*/
|
2021-01-01 14:27:58 +00:00
|
|
|
window.messageBoxProxy = new Proxy({}, {
|
|
|
|
get(_, name) {
|
|
|
|
return async (...args) => {
|
|
|
|
await require([
|
|
|
|
'/js/dlg/message-box', /* global messageBox */
|
|
|
|
'/js/dlg/message-box.css',
|
|
|
|
]);
|
|
|
|
window.messageBoxProxy = messageBox;
|
|
|
|
return messageBox[name](...args);
|
2020-11-06 18:04:10 +00:00
|
|
|
};
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2022-01-19 19:19:50 +00:00
|
|
|
function $(selector, base) {
|
2017-04-26 00:05:41 +00:00
|
|
|
// we have ids with . like #manage.onlyEnabled which looks like #id.class
|
2017-03-25 05:54:58 +00:00
|
|
|
// so since getElementById is superfast we'll try it anyway
|
2022-01-19 19:19:50 +00:00
|
|
|
const byId = !base && selector.startsWith('#') && document.getElementById(selector.slice(1));
|
|
|
|
return byId || (base || document).querySelector(selector);
|
2017-03-25 05:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function $$(selector, base = document) {
|
|
|
|
return [...base.querySelectorAll(selector)];
|
|
|
|
}
|
2017-04-17 20:25:32 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function $isTextInput(el = {}) {
|
|
|
|
return el.localName === 'textarea' ||
|
|
|
|
el.localName === 'input' && /^(text|search|number)$/.test(el.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function $remove(selector, base = document) {
|
|
|
|
const el = selector && typeof selector === 'string' ? $(selector, base) : selector;
|
|
|
|
if (el) {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function $$remove(selector, base = document) {
|
|
|
|
for (const el of base.querySelectorAll(selector)) {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 20:25:32 +00:00
|
|
|
|
2017-12-03 21:12:09 +00:00
|
|
|
/*
|
2021-01-01 14:27:58 +00:00
|
|
|
$create('tag#id.class.class', ?[children])
|
|
|
|
$create('tag#id.class.class', ?textContentOrChildNode)
|
|
|
|
$create('tag#id.class.class', {properties}, ?[children])
|
|
|
|
$create('tag#id.class.class', {properties}, ?textContentOrChildNode)
|
|
|
|
tag is 'div' by default, #id and .class are optional
|
|
|
|
|
|
|
|
$create([children])
|
|
|
|
|
|
|
|
$create({propertiesAndOptions})
|
|
|
|
$create({propertiesAndOptions}, ?[children])
|
|
|
|
tag: string, default 'div'
|
|
|
|
appendChild: element/string or an array of elements/strings
|
|
|
|
dataset: object
|
|
|
|
any DOM property: assigned as is
|
|
|
|
|
|
|
|
tag may include namespace like 'ns:tag'
|
|
|
|
*/
|
|
|
|
function $create(selector = 'div', properties, children) {
|
2017-12-03 21:12:09 +00:00
|
|
|
let ns, tag, opt;
|
|
|
|
if (typeof selector === 'string') {
|
|
|
|
if (Array.isArray(properties) ||
|
|
|
|
properties instanceof Node ||
|
|
|
|
typeof properties !== 'object') {
|
|
|
|
opt = {};
|
|
|
|
children = properties;
|
|
|
|
} else {
|
|
|
|
opt = properties || {};
|
2018-04-12 17:42:01 +00:00
|
|
|
children = children || opt.appendChild;
|
2017-12-03 21:12:09 +00:00
|
|
|
}
|
|
|
|
const idStart = (selector.indexOf('#') + 1 || selector.length + 1) - 1;
|
|
|
|
const classStart = (selector.indexOf('.') + 1 || selector.length + 1) - 1;
|
|
|
|
const id = selector.slice(idStart + 1, classStart);
|
|
|
|
if (id) {
|
|
|
|
opt.id = id;
|
|
|
|
}
|
|
|
|
const cls = selector.slice(classStart + 1);
|
|
|
|
if (cls) {
|
|
|
|
opt[selector.includes(':') ? 'class' : 'className'] =
|
|
|
|
cls.includes('.') ? cls.replace(/\./g, ' ') : cls;
|
|
|
|
}
|
|
|
|
tag = selector.slice(0, Math.min(idStart, classStart));
|
|
|
|
} else if (Array.isArray(selector)) {
|
|
|
|
tag = 'div';
|
|
|
|
opt = {};
|
|
|
|
children = selector;
|
|
|
|
} else {
|
|
|
|
opt = selector;
|
|
|
|
tag = opt.tag;
|
|
|
|
children = opt.appendChild || properties;
|
|
|
|
}
|
|
|
|
if (tag && tag.includes(':')) {
|
2021-01-01 14:27:58 +00:00
|
|
|
[ns, tag] = tag.split(':');
|
|
|
|
if (ns === 'SVG' || ns === 'svg') {
|
|
|
|
ns = 'http://www.w3.org/2000/svg';
|
|
|
|
}
|
2017-12-03 21:12:09 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const element = ns ? document.createElementNS(ns, tag) :
|
|
|
|
tag === 'fragment' ? document.createDocumentFragment() :
|
|
|
|
document.createElement(tag || 'div');
|
2017-12-03 21:12:09 +00:00
|
|
|
for (const child of Array.isArray(children) ? children : [children]) {
|
2017-07-19 12:09:29 +00:00
|
|
|
if (child) {
|
|
|
|
element.appendChild(child instanceof Node ? child : document.createTextNode(child));
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const [key, val] of Object.entries(opt)) {
|
|
|
|
switch (key) {
|
|
|
|
case 'dataset':
|
|
|
|
Object.assign(element.dataset, val);
|
|
|
|
break;
|
|
|
|
case 'attributes':
|
|
|
|
Object.entries(val).forEach(attr => element.setAttribute(...attr));
|
|
|
|
break;
|
|
|
|
case 'style': {
|
|
|
|
const t = typeof val;
|
|
|
|
if (t === 'string') element.style.cssText = val;
|
|
|
|
if (t === 'object') Object.assign(element.style, val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'tag':
|
|
|
|
case 'appendChild':
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
if (ns) {
|
|
|
|
const i = key.indexOf(':') + 1;
|
|
|
|
const attrNS = i && `http://www.w3.org/1999/${key.slice(0, i - 1)}`;
|
|
|
|
element.setAttributeNS(attrNS || null, key, val);
|
|
|
|
} else {
|
|
|
|
element[key] = val;
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 11:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return element;
|
2017-04-17 20:25:32 +00:00
|
|
|
}
|
2017-09-13 09:34:27 +00:00
|
|
|
|
2017-12-03 21:12:09 +00:00
|
|
|
function $createLink(href = '', content) {
|
2017-10-14 18:59:55 +00:00
|
|
|
const opt = {
|
2017-09-13 09:34:27 +00:00
|
|
|
tag: 'a',
|
|
|
|
target: '_blank',
|
2020-11-18 11:17:15 +00:00
|
|
|
rel: 'noopener',
|
2017-10-14 18:59:55 +00:00
|
|
|
};
|
|
|
|
if (typeof href === 'object') {
|
|
|
|
Object.assign(opt, href);
|
|
|
|
} else {
|
|
|
|
opt.href = href;
|
|
|
|
}
|
2017-12-03 21:12:09 +00:00
|
|
|
opt.appendChild = opt.appendChild || content;
|
|
|
|
return $create(opt);
|
2017-09-13 09:34:27 +00:00
|
|
|
}
|
2017-11-29 16:05:47 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/**
|
|
|
|
* @param {HTMLElement} el
|
|
|
|
* @param {string} [cls] - class name that defines or starts an animation
|
|
|
|
* @param [removeExtraClasses] - class names to remove at animation end in the *same* paint frame,
|
|
|
|
* which is needed in e.g. Firefox as it may call resolve() in the next frame
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function animateElement(el, cls = 'highlight', ...removeExtraClasses) {
|
|
|
|
return !el ? Promise.resolve(el) : new Promise(resolve => {
|
|
|
|
let onDone = () => {
|
|
|
|
el.classList.remove(cls, ...removeExtraClasses);
|
|
|
|
onDone = null;
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
if (onDone) {
|
|
|
|
const style = getComputedStyle(el);
|
|
|
|
if (style.animationName === 'none' || !parseFloat(style.animationDuration)) {
|
|
|
|
el.off('animationend', onDone);
|
|
|
|
onDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
el.on('animationend', onDone, {once: true});
|
|
|
|
el.classList.add(cls);
|
2017-11-29 16:05:47 +00:00
|
|
|
});
|
|
|
|
}
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function getEventKeyName(e, letterAsCode) {
|
|
|
|
const mods =
|
|
|
|
(e.shiftKey ? 'Shift-' : '') +
|
|
|
|
(e.ctrlKey ? 'Ctrl-' : '') +
|
|
|
|
(e.altKey ? 'Alt-' : '') +
|
|
|
|
(e.metaKey ? 'Meta-' : '');
|
|
|
|
return `${
|
|
|
|
mods === e.key + '-' ? '' : mods
|
|
|
|
}${
|
|
|
|
e.key
|
|
|
|
? e.key.length === 1 && letterAsCode ? e.code : e.key
|
|
|
|
: 'Mouse' + ('LMR'[e.button] || e.button)
|
|
|
|
}`;
|
2017-12-12 18:33:41 +00:00
|
|
|
}
|
2018-07-22 16:37:49 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-23 09:09:30 +00:00
|
|
|
* Switches to the next/previous keyboard-focusable element.
|
|
|
|
* Doesn't check `visibility` or `display` via getComputedStyle for simplicity.
|
2018-07-22 16:37:49 +00:00
|
|
|
* @param {HTMLElement} rootElement
|
2020-11-23 09:09:30 +00:00
|
|
|
* @param {Number} step - for exmaple 1 or -1 (or 0 to focus the first focusable el in the box)
|
2018-09-06 17:59:04 +00:00
|
|
|
* @returns {HTMLElement|false|undefined} -
|
|
|
|
* HTMLElement: focus changed,
|
|
|
|
* false: focus unchanged,
|
|
|
|
* undefined: nothing to focus
|
2018-07-22 16:37:49 +00:00
|
|
|
*/
|
|
|
|
function moveFocus(rootElement, step) {
|
|
|
|
const elements = [...rootElement.getElementsByTagName('*')];
|
2020-11-23 09:09:30 +00:00
|
|
|
const activeEl = document.activeElement;
|
|
|
|
const activeIndex = step ? Math.max(step < 0 ? 0 : -1, elements.indexOf(activeEl)) : -1;
|
2018-07-22 16:37:49 +00:00
|
|
|
const num = elements.length;
|
2020-11-23 09:09:30 +00:00
|
|
|
if (!step) step = 1;
|
2021-08-12 13:40:27 +00:00
|
|
|
for (let i = 1; i <= num; i++) {
|
2020-11-23 09:09:30 +00:00
|
|
|
const el = elements[(activeIndex + i * step + num) % num];
|
2018-07-22 16:37:49 +00:00
|
|
|
if (!el.disabled && el.tabIndex >= 0) {
|
|
|
|
el.focus();
|
2022-02-21 20:45:42 +00:00
|
|
|
// suppress focus outline when invoked via click
|
|
|
|
toggleDataset(el, 'focusedViaClick', focusAccessibility.lastFocusedViaClick);
|
2020-11-23 09:09:30 +00:00
|
|
|
return activeEl !== el && el;
|
2018-07-22 16:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2022-01-28 23:54:56 +00:00
|
|
|
/**
|
|
|
|
* Scrolls `window` or the closest parent with `class="scroller"` if the element is not visible,
|
|
|
|
* centering the element in the view
|
|
|
|
* @param {HTMLElement} element
|
|
|
|
* @param {number} [invalidMarginRatio] - for example, 0.10 will center the element if it's in the top/bottom 10% of the scroller
|
|
|
|
*/
|
2021-01-01 14:27:58 +00:00
|
|
|
function scrollElementIntoView(element, {invalidMarginRatio = 0} = {}) {
|
|
|
|
// align to the top/bottom of the visible area if wasn't visible
|
|
|
|
if (!element.parentNode) return;
|
|
|
|
const {top, height} = element.getBoundingClientRect();
|
|
|
|
const {top: parentTop, bottom: parentBottom} = element.parentNode.getBoundingClientRect();
|
|
|
|
const windowHeight = window.innerHeight;
|
|
|
|
if (top < Math.max(parentTop, windowHeight * invalidMarginRatio) ||
|
|
|
|
top > Math.min(parentBottom, windowHeight) - height - windowHeight * invalidMarginRatio) {
|
2022-02-14 19:19:20 +00:00
|
|
|
const scroller = element.closest('.scroller') || window;
|
|
|
|
scroller.scrollBy(0, top - (scroller.clientHeight || windowHeight) / 2 + height);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts an array of pref names (values are fetched via prefs.get)
|
|
|
|
* and establishes a two-way connection between the document elements and the actual prefs
|
|
|
|
*/
|
2022-02-16 19:32:38 +00:00
|
|
|
function setupLivePrefs(ids) {
|
2022-02-17 16:51:58 +00:00
|
|
|
let init = true;
|
2022-02-16 19:32:38 +00:00
|
|
|
// getElementsByTagName is cached so it's much faster than calling querySelector for each id
|
|
|
|
ids = ids ? [...ids] : prefs.knownKeys.filter(id => id in document.getElementsByTagName('*'));
|
2021-01-01 14:27:58 +00:00
|
|
|
prefs.subscribe(ids, updateElement, {runNow: true});
|
2022-02-17 16:51:58 +00:00
|
|
|
init = false;
|
2018-11-07 06:09:29 +00:00
|
|
|
function onChange() {
|
2022-02-16 19:32:38 +00:00
|
|
|
if (this.checkValidity() && (this.type !== 'radio' || this.checked)) {
|
|
|
|
prefs.set(this.id || this.name, getValue(this));
|
2021-12-02 16:49:03 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-12-02 16:49:03 +00:00
|
|
|
function getValue(el) {
|
|
|
|
const type = el.dataset.valueType || el.type;
|
|
|
|
return type === 'checkbox' ? el.checked :
|
|
|
|
// https://stackoverflow.com/questions/18062069/why-does-valueasnumber-return-nan-as-a-value
|
|
|
|
// valueAsNumber is not applicable for input[text/radio] or select
|
|
|
|
type === 'number' ? Number(el.value) :
|
|
|
|
el.value;
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-12-02 16:49:03 +00:00
|
|
|
function isSame(el, oldValue, value) {
|
2022-02-16 19:32:38 +00:00
|
|
|
return el.type === 'radio' ? el.checked === (oldValue === value) :
|
|
|
|
el.localName === 'select' && typeof value === 'boolean' && oldValue === `${value}` ||
|
|
|
|
oldValue === value;
|
2021-01-07 11:49:22 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
function updateElement(id, value) {
|
2022-02-16 19:32:38 +00:00
|
|
|
const byId = document.getElementById(id);
|
|
|
|
const els = byId ? [byId] : document.getElementsByName(id);
|
2021-12-02 16:49:03 +00:00
|
|
|
if (!els.length) {
|
2022-02-16 19:32:38 +00:00
|
|
|
prefs.unsubscribe(id, updateElement);
|
2021-12-02 16:49:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const el of els) {
|
|
|
|
const oldValue = getValue(el);
|
2022-02-17 16:51:58 +00:00
|
|
|
if (!isSame(el, oldValue, value)) {
|
2021-12-02 16:49:03 +00:00
|
|
|
if (el.type === 'radio') {
|
|
|
|
el.checked = value === oldValue;
|
|
|
|
} else if (el.type === 'checkbox') {
|
|
|
|
el.checked = value;
|
|
|
|
} else {
|
|
|
|
el.value = value;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
el.dispatchEvent(new Event('change', {bubbles: true}));
|
|
|
|
}
|
2022-02-17 16:51:58 +00:00
|
|
|
if (init) el.on('change', onChange);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
/** @param {string|Node} parent - selector or DOM node */
|
|
|
|
async function showSpinner(parent) {
|
|
|
|
await require(['/spinner.css']);
|
|
|
|
parent = parent instanceof Node ? parent : $(parent);
|
|
|
|
parent.appendChild($create('.lds-spinner',
|
|
|
|
new Array(12).fill($create('div')).map(e => e.cloneNode())));
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleDataset(el, prop, state) {
|
2021-08-13 20:03:01 +00:00
|
|
|
const wasEnabled = el.dataset[prop] != null; // avoids mutating DOM unnecessarily
|
2021-07-30 12:44:06 +00:00
|
|
|
if (state) {
|
2021-08-13 20:03:01 +00:00
|
|
|
if (!wasEnabled) el.dataset[prop] = '';
|
2021-07-30 12:44:06 +00:00
|
|
|
} else {
|
2021-08-13 20:03:01 +00:00
|
|
|
if (wasEnabled) delete el.dataset[prop];
|
2021-07-30 12:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/**
|
|
|
|
* @param {string} selector - beware of $ quirks with `#dotted.id` that won't work with $$
|
|
|
|
* @param {Object} [opt]
|
2022-02-17 21:19:03 +00:00
|
|
|
* @param {function(HTMLElement[]):boolean} [opt.recur] - called on each match until stopOnDomReady,
|
2021-01-01 14:27:58 +00:00
|
|
|
you can also return `false` to disconnect the observer
|
|
|
|
* @param {boolean} [opt.stopOnDomReady] - stop observing on DOM ready
|
|
|
|
* @returns {Promise<HTMLElement>} - resolves on first match
|
|
|
|
*/
|
|
|
|
function waitForSelector(selector, {recur, stopOnDomReady = true} = {}) {
|
|
|
|
let el = $(selector);
|
2022-02-17 21:19:03 +00:00
|
|
|
let elems;
|
|
|
|
return el && (!recur || recur(elems = $$(selector)) === false)
|
2021-01-01 14:27:58 +00:00
|
|
|
? Promise.resolve(el)
|
|
|
|
: new Promise(resolve => {
|
2022-02-17 21:19:03 +00:00
|
|
|
new MutationObserver((mutations, observer) => {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (!el) el = $(selector);
|
|
|
|
if (!el) return;
|
|
|
|
if (!recur ||
|
2022-02-24 22:26:50 +00:00
|
|
|
callRecur(mutations) === false ||
|
|
|
|
stopOnDomReady && document.readyState === 'complete') {
|
2022-02-17 21:19:03 +00:00
|
|
|
observer.disconnect();
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2022-02-17 21:19:03 +00:00
|
|
|
if (resolve) {
|
2021-01-01 14:27:58 +00:00
|
|
|
resolve(el);
|
2022-02-17 21:19:03 +00:00
|
|
|
resolve = null;
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2022-02-17 21:19:03 +00:00
|
|
|
}).observe(document, {childList: true, subtree: true});
|
|
|
|
function isMatching(n) {
|
|
|
|
return n.tagName && (n.matches(selector) || n.firstElementChild && $(selector, n));
|
|
|
|
}
|
|
|
|
function callRecur([m0, m1]) {
|
2022-02-24 22:26:50 +00:00
|
|
|
// Checking addedNodes if only 1 MutationRecord to skip simple mutations quickly
|
|
|
|
if (m1 || (m0 = m0.addedNodes)[3] || [].some.call(m0, isMatching)) {
|
2022-02-17 21:19:03 +00:00
|
|
|
const all = $$(selector); // Using one $$ call instead of ~100 calls for each node
|
|
|
|
const added = !elems ? all : all.filter(el => !elems.includes(el));
|
|
|
|
if (added.length) {
|
|
|
|
elems = all;
|
|
|
|
return recur(added);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
//#region Internals
|
|
|
|
|
2022-01-13 09:47:37 +00:00
|
|
|
const dom = {};
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(() => {
|
|
|
|
|
|
|
|
const Collapsible = {
|
2022-02-17 21:19:03 +00:00
|
|
|
bindEvents(elems) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const prefKeys = [];
|
|
|
|
for (const el of elems) {
|
|
|
|
prefKeys.push(el.dataset.pref);
|
|
|
|
($('h2', el) || el).on('click', Collapsible.saveOnClick);
|
|
|
|
}
|
|
|
|
prefs.subscribe(prefKeys, Collapsible.updateOnPrefChange, {runNow: true});
|
|
|
|
},
|
|
|
|
canSave(el) {
|
|
|
|
return !el.matches('.compact-layout .ignore-pref-if-compact');
|
|
|
|
},
|
|
|
|
async saveOnClick(event) {
|
|
|
|
if (event.target.closest('.intercepts-click')) {
|
|
|
|
event.preventDefault();
|
|
|
|
} else {
|
|
|
|
const el = event.target.closest('details');
|
|
|
|
await new Promise(setTimeout);
|
|
|
|
if (Collapsible.canSave(el)) {
|
|
|
|
prefs.set(el.dataset.pref, el.open);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateOnPrefChange(key, value) {
|
|
|
|
const el = $(`details[data-pref="${key}"]`);
|
|
|
|
if (el.open !== value && Collapsible.canSave(el)) {
|
|
|
|
el.open = value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2022-01-13 09:47:37 +00:00
|
|
|
const lazyScripts = [
|
|
|
|
'/js/dom-on-load',
|
|
|
|
];
|
2022-02-14 19:19:20 +00:00
|
|
|
if (!UA.windows) $.rootCL.add('non-windows');
|
2021-01-01 14:27:58 +00:00
|
|
|
// set language for a) CSS :lang pseudo and b) hyphenation
|
2022-02-14 19:19:20 +00:00
|
|
|
$.root.setAttribute('lang', chrome.i18n.getUILanguage());
|
2022-01-13 09:47:37 +00:00
|
|
|
// set up header width resizer
|
|
|
|
const HW = 'headerWidth.';
|
|
|
|
const HWprefId = HW + location.pathname.match(/^.(\w*)/)[1];
|
|
|
|
if (prefs.knownKeys.includes(HWprefId)) {
|
|
|
|
Object.assign(dom, {
|
|
|
|
HW,
|
|
|
|
HWprefId,
|
|
|
|
setHWProp(width) {
|
2022-01-16 12:45:55 +00:00
|
|
|
// If this is a small window on a big monitor the user can maximize it later
|
|
|
|
const max = (innerWidth < 850 ? screen.availWidth : innerWidth) / 3;
|
|
|
|
width = Math.round(Math.max(200, Math.min(max, Number(width) || 0)));
|
2022-02-14 19:19:20 +00:00
|
|
|
$.root.style.setProperty('--header-width', width + 'px');
|
2022-01-13 09:47:37 +00:00
|
|
|
return width;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
prefs.ready.then(() => dom.setHWProp(prefs.get(HWprefId)));
|
|
|
|
lazyScripts.push('/js/header-resizer');
|
|
|
|
}
|
|
|
|
// add favicon in FF
|
|
|
|
if (FIREFOX) {
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const size of [38, 32, 19, 16]) {
|
|
|
|
document.head.appendChild($create('link', {
|
|
|
|
rel: 'icon',
|
2022-02-17 00:10:59 +00:00
|
|
|
href: `/images/icon/${size}.png`,
|
2021-01-01 14:27:58 +00:00
|
|
|
sizes: size + 'x' + size,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 09:47:37 +00:00
|
|
|
prefs.ready.then(() => {
|
|
|
|
waitForSelector('details[data-pref]', {recur: Collapsible.bindEvents});
|
|
|
|
});
|
2022-02-23 02:52:04 +00:00
|
|
|
window.requestIdleCallback(() => {
|
2022-01-13 09:47:37 +00:00
|
|
|
require(lazyScripts);
|
2022-02-23 02:52:04 +00:00
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
})();
|
2020-11-23 09:09:30 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
//#endregion
|