simplify usePrefsDuringPageLoad

This commit is contained in:
tophf 2017-12-26 08:54:01 +03:00
parent 4da6eeb746
commit e4f8cf5fb5

View File

@ -21,7 +21,7 @@ const newUI = {
}, },
}; };
newUI.renderClass(); newUI.renderClass();
usePrefsDuringPageLoad(); requestAnimationFrame(usePrefsDuringPageLoad);
const TARGET_TYPES = ['domains', 'urls', 'urlPrefixes', 'regexps']; const TARGET_TYPES = ['domains', 'urls', 'urlPrefixes', 'regexps'];
const GET_FAVICON_URL = 'https://www.google.com/s2/favicons?domain='; const GET_FAVICON_URL = 'https://www.google.com/s2/favicons?domain=';
@ -600,40 +600,21 @@ function rememberScrollPosition() {
function usePrefsDuringPageLoad() { function usePrefsDuringPageLoad() {
const observer = new MutationObserver(mutations => { for (const id of Object.getOwnPropertyNames(prefs.readOnlyValues)) {
const adjustedNodes = []; const value = prefs.readOnlyValues[id];
for (const mutation of mutations) { if (value !== true) continue;
for (const node of mutation.addedNodes) { const el = document.getElementById(id) ||
// [naively] assuming each element of addedNodes is a childless element id.includes('expanded') && $(`details[data-pref="${id}"]`);
const key = node.dataset && node.dataset.pref || node.id; if (!el) continue;
const prefValue = key ? prefs.readOnlyValues[key] : undefined; if (el.type === 'checkbox') {
if (prefValue !== undefined) { el.checked = value;
if (node.type === 'checkbox') { } else if (el.localName === 'details') {
node.checked = prefValue; el.open = value;
} else if (node.localName === 'details') { } else {
node.open = prefValue; el.value = value;
} else {
node.value = prefValue;
}
if (node.adjustWidth) {
adjustedNodes.push(node);
}
}
}
} }
if (adjustedNodes.length) {
observer.disconnect();
for (const node of adjustedNodes) {
node.adjustWidth();
}
startObserver();
}
});
function startObserver() {
observer.observe(document, {subtree: true, childList: true});
} }
startObserver(); $$('#header select').forEach(el => el.adjustWidth());
onDOMready().then(() => observer.disconnect());
} }