2017-03-25 05:54:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-21 17:35:22 +00:00
|
|
|
if (!navigator.userAgent.includes('Windows')) {
|
2017-03-25 20:39:21 +00:00
|
|
|
document.documentElement.classList.add('non-windows');
|
|
|
|
}
|
|
|
|
|
2017-04-08 10:19:44 +00:00
|
|
|
// polyfill for old browsers to enable [...results] and for-of
|
|
|
|
for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection]) {
|
|
|
|
if (!type.prototype[Symbol.iterator]) {
|
|
|
|
type.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:09:31 +00:00
|
|
|
// add favicon in Firefox
|
|
|
|
if (/Firefox/.test(navigator.userAgent)) {
|
|
|
|
for (const size of [128, 38, 32, 19, 16]) {
|
|
|
|
document.head.appendChild($element({
|
|
|
|
tag: 'link',
|
|
|
|
rel: 'icon',
|
|
|
|
href: `/images/icon/${size}.png`,
|
|
|
|
sizes: size + 'x' + size,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:39:21 +00:00
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
function onDOMready() {
|
|
|
|
if (document.readyState != 'loading') {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return new Promise(resolve => {
|
|
|
|
document.addEventListener('DOMContentLoaded', function _() {
|
|
|
|
document.removeEventListener('DOMContentLoaded', _);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function scrollElementIntoView(element) {
|
|
|
|
// align to the top/bottom of the visible area if wasn't visible
|
|
|
|
const bounds = element.getBoundingClientRect();
|
|
|
|
if (bounds.top < 0 || bounds.top > innerHeight - bounds.height) {
|
|
|
|
element.scrollIntoView(bounds.top < 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-14 11:26:51 +00:00
|
|
|
function animateElement(element, {className, removeExtraClasses = [], remove = false}) {
|
2017-03-25 05:54:58 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
element.addEventListener('animationend', function _() {
|
|
|
|
element.removeEventListener('animationend', _);
|
2017-05-14 11:26:51 +00:00
|
|
|
element.classList.remove(
|
|
|
|
className,
|
|
|
|
...removeExtraClasses // In Firefox, `resolve()` might be called one frame later. This is helpful to clean-up on the same frame
|
|
|
|
);
|
2017-03-25 05:54:58 +00:00
|
|
|
// TODO: investigate why animation restarts if the elements is removed in .then()
|
|
|
|
if (remove) {
|
|
|
|
element.remove();
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
element.classList.add(className);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function enforceInputRange(element) {
|
|
|
|
const min = Number(element.min);
|
|
|
|
const max = Number(element.max);
|
2017-04-13 06:12:40 +00:00
|
|
|
const doNotify = () => element.dispatchEvent(new Event('change', {bubbles: true}));
|
|
|
|
const onChange = ({type}) => {
|
|
|
|
if (type == 'input' && element.checkValidity()) {
|
|
|
|
doNotify();
|
|
|
|
} else if (type == 'change' && !element.checkValidity()) {
|
|
|
|
element.value = Math.max(min, Math.min(max, Number(element.value)));
|
|
|
|
doNotify();
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
element.addEventListener('change', onChange);
|
|
|
|
element.addEventListener('input', onChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
function $(selector, base = document) {
|
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
|
|
|
|
const byId = selector.startsWith('#') && document.getElementById(selector.slice(1));
|
|
|
|
return byId || base.querySelector(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function $$(selector, base = document) {
|
|
|
|
return [...base.querySelectorAll(selector)];
|
|
|
|
}
|
2017-04-17 20:25:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
function $element(opt) {
|
2017-04-24 11:07:35 +00:00
|
|
|
// tag: string, default 'div', may include namespace like 'ns#tag'
|
2017-04-17 20:25:32 +00:00
|
|
|
// appendChild: element or an array of elements
|
|
|
|
// dataset: object
|
|
|
|
// any DOM property: assigned as is
|
2017-04-24 11:07:35 +00:00
|
|
|
const [ns, tag] = opt.tag && opt.tag.includes('#')
|
|
|
|
? opt.tag.split('#')
|
|
|
|
: [null, opt.tag];
|
|
|
|
const element = ns
|
|
|
|
? document.createElementNS(ns == 'SVG' || ns == 'svg' ? 'http://www.w3.org/2000/svg' : ns, tag)
|
|
|
|
: document.createElement(tag || 'div');
|
2017-04-17 20:25:32 +00:00
|
|
|
(opt.appendChild instanceof Array ? opt.appendChild : [opt.appendChild])
|
|
|
|
.forEach(child => child && element.appendChild(child));
|
|
|
|
delete opt.appendChild;
|
|
|
|
delete opt.tag;
|
|
|
|
if (opt.dataset) {
|
|
|
|
Object.assign(element.dataset, opt.dataset);
|
|
|
|
delete opt.dataset;
|
|
|
|
}
|
2017-04-24 11:07:35 +00:00
|
|
|
if (ns) {
|
|
|
|
for (const attr in opt) {
|
|
|
|
element.setAttributeNS(null, attr, opt[attr]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Object.assign(element, opt);
|
|
|
|
}
|
|
|
|
return element;
|
2017-04-17 20:25:32 +00:00
|
|
|
}
|
2017-06-10 11:08:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
function retranslateCSS(selectorToMessageMap) {
|
|
|
|
// TODO: remove when this bug is fixed in FF
|
|
|
|
// Note: selectors must be spec-normalized e.g. ::before, not :before
|
|
|
|
for (const rule of document.styleSheets[0].cssRules) {
|
|
|
|
const msg = selectorToMessageMap[rule.selectorText];
|
|
|
|
if (msg) {
|
|
|
|
rule.style.content = '"' + msg.replace(/__MSG_(\w+)__/g, (_, id) => t(id)) + '"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|