autosize filter select element

This commit is contained in:
tophf 2017-08-22 17:22:15 +03:00
parent 64ba125601
commit 9a55e64bbb
3 changed files with 44 additions and 9 deletions

View File

@ -8,6 +8,17 @@ const filtersSelector = {
numTotal: 0, numTotal: 0,
}; };
HTMLSelectElement.prototype.adjustWidth = function () {
const parent = this.parentNode;
const singleSelect = this.cloneNode(false);
singleSelect.style.width = '';
singleSelect.appendChild(this.selectedOptions[0].cloneNode(true));
parent.replaceChild(singleSelect, this);
if (this.style.width !== singleSelect.offsetWidth + 'px') {
this.style.width = singleSelect.offsetWidth + 'px';
}
parent.replaceChild(this, singleSelect);
};
onDOMready().then(onBackgroundReady).then(() => { onDOMready().then(onBackgroundReady).then(() => {
$('#search').oninput = searchStyles; $('#search').oninput = searchStyles;
@ -19,7 +30,6 @@ onDOMready().then(onBackgroundReady).then(() => {
[false, slaveData.filter], [false, slaveData.filter],
[true, slaveData.filterHide], [true, slaveData.filterHide],
]); ]);
//
// enable slave control when user switches the value // enable slave control when user switches the value
el.oninput = () => { el.oninput = () => {
if (!slave.checked) { if (!slave.checked) {
@ -36,12 +46,18 @@ onDOMready().then(onBackgroundReady).then(() => {
el.onchange = event => { el.onchange = event => {
const value = el.value === 'true'; const value = el.value === 'true';
const filter = valueMap.get(value); const filter = valueMap.get(value);
if (slaveData.filter !== filter) { if (slaveData.filter === filter) {
return;
}
slaveData.filter = filter; slaveData.filter = filter;
slaveData.filterHide = valueMap.get(!value); slaveData.filterHide = valueMap.get(!value);
debounce(filterOnChange, 0, event); debounce(filterOnChange, 0, event);
// avoid triggering MutationObserver during page load
if (document.readyState === 'complete') {
el.adjustWidth();
} }
}; };
el.onchange({target: el});
}); });
$$('[data-filter]').forEach(el => { $$('[data-filter]').forEach(el => {
@ -51,8 +67,7 @@ onDOMready().then(onBackgroundReady).then(() => {
} }
}); });
// let manage::initGlobalEvents run onDOMready first filterOnChange({forceRefilter: true});
debounce(filterOnChange, 0, {forceRefilter: true});
}); });
@ -78,8 +93,10 @@ function filterOnChange({target: el, forceRefilter}) {
hide: buildFilter(true), hide: buildFilter(true),
unhide: buildFilter(false), unhide: buildFilter(false),
}); });
if (installed) {
reapplyFilter(); reapplyFilter();
} }
}
function filterAndAppend({entry, container}) { function filterAndAppend({entry, container}) {

View File

@ -544,6 +544,8 @@ fieldset {
border-width: 1px; border-width: 1px;
border-radius: 6px; border-radius: 6px;
margin: 1em 0; margin: 1em 0;
min-width: 0;
max-width: 250px;
} }
fieldset > *:not(legend) { fieldset > *:not(legend) {
@ -572,6 +574,8 @@ fieldset > *:not(legend) {
#manage\.onlyEnabled\.invert, #manage\.onlyEnabled\.invert,
#manage\.onlyLocal\.invert { #manage\.onlyLocal\.invert {
border: none; border: none;
max-width: calc(100% - 2em);
background-color: transparent;
} }
#search { #search {

View File

@ -507,6 +507,7 @@ function rememberScrollPosition() {
function usePrefsDuringPageLoad() { function usePrefsDuringPageLoad() {
const observer = new MutationObserver(mutations => { const observer = new MutationObserver(mutations => {
const adjustedNodes = [];
for (const mutation of mutations) { for (const mutation of mutations) {
for (const node of mutation.addedNodes) { for (const node of mutation.addedNodes) {
// [naively] assuming each element of addedNodes is a childless element // [naively] assuming each element of addedNodes is a childless element
@ -517,10 +518,23 @@ function usePrefsDuringPageLoad() {
} else { } else {
node.value = prefValue; 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}); observer.observe(document, {subtree: true, childList: true});
}
startObserver();
onDOMready().then(() => observer.disconnect()); onDOMready().then(() => observer.disconnect());
} }