set even/odd class based on html { --columns: # } variable

This commit is contained in:
tophf 2018-05-15 16:24:28 +03:00
parent 1d0ca613f9
commit 349c33b478
2 changed files with 40 additions and 13 deletions

View File

@ -50,7 +50,13 @@ function onRuntimeMessage(msg) {
case 'styleDeleted':
handleDelete(msg.id);
break;
case 'styleApply':
case 'styleReplaceAll':
break;
default:
return;
}
setTimeout(sorter.updateStripes, 0, {onlyWhenColumnsChanged: true});
}
@ -117,9 +123,6 @@ function showStyles(styles = [], matchUrlIds) {
style,
name: style.name.toLocaleLowerCase() + '\n' + style.name,
})),
}).map((info, index) => {
info.index = index;
return info;
});
let index = 0;
let firstRun = true;
@ -149,7 +152,7 @@ function showStyles(styles = [], matchUrlIds) {
}
renderBin.appendChild(entry);
}
filterAndAppend({container: renderBin});
filterAndAppend({container: renderBin}).then(sorter.updateStripes);
if (index < sorted.length) {
requestAnimationFrame(renderStyles);
if (firstRun) setTimeout(recreateStyleTargets, 0, {styles, iconsOnly: true});
@ -174,7 +177,7 @@ function showStyles(styles = [], matchUrlIds) {
}
function createStyleElement({style, name, index}) {
function createStyleElement({style, name}) {
// query the sub-elements just once, then reuse the references
if ((createStyleElement.parts || {}).newUI !== newUI.enabled) {
const entry = template[`style${newUI.enabled ? 'Compact' : ''}`];
@ -227,7 +230,6 @@ function createStyleElement({style, name, index}) {
(style.enabled ? 'enabled' : 'disabled') +
(style.updateUrl ? ' updatable' : '') +
(style.usercssData ? ' usercss' : '');
if (index !== undefined) entry.classList.add(index % 2 ? 'odd' : 'even');
if (style.url) {
$('.homepage', entry).appendChild(parts.homepageIcon.cloneNode(true));

View File

@ -61,6 +61,8 @@ const sorter = (() => {
const splitRegex = /\s*,\s*/;
let columns = 1;
function addOptions() {
let container;
const select = $('#manage.newUI.sort');
@ -140,15 +142,37 @@ const sorter = (() => {
updateStripes();
}
function updateStripes() {
function updateStripes({onlyWhenColumnsChanged} = {}) {
if (onlyWhenColumnsChanged && !updateColumnCount()) return;
let index = 0;
[...installed.children].forEach(entry => {
const list = entry.classList;
if (!list.contains('hidden')) {
list.add(index % 2 ? 'odd' : 'even');
list.remove(index++ % 2 ? 'even' : 'odd');
let isOdd = false;
const flipRows = columns % 2 === 0;
for (const {classList} of installed.children) {
if (classList.contains('hidden')) continue;
classList.toggle('odd', isOdd);
classList.toggle('even', !isOdd);
if (flipRows && ++index >= columns) {
index = 0;
} else {
isOdd = !isOdd;
}
}
}
function updateColumnCount() {
let newValue = 1;
for (let el = document.documentElement.lastElementChild;
el.localName === 'style';
el = el.previousElementSibling) {
if (el.textContent.includes('--columns:')) {
newValue = Math.max(1, getComputedStyle(document.documentElement).getPropertyValue('--columns') | 0);
break;
}
}
if (columns !== newValue) {
columns = newValue;
return true;
}
});
}
function showHelp(event) {
@ -168,6 +192,7 @@ const sorter = (() => {
prefs.subscribe(['manage.newUI.sort'], update);
$('#sorter-help').onclick = showHelp;
addOptions();
updateColumnCount();
}
return {init, update, sort, updateStripes};