stylus/manage/manage.js

144 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

/* global API msg */// msg.js
2022-01-27 12:21:02 +00:00
/* global CHROME UA debounce */// toolbox.js
/* global Events handleBulkChange handleVisibilityChange */// events.js
/* global fitSelectBoxesIn switchUI showStyles */// render.js
/* global prefs */
/* global router */
/* global sorter */
/* global t */// localization.js
/* global $ $$ $create animateElement setupLivePrefs */// dom.js
'use strict';
2015-01-30 17:31:20 +00:00
2022-02-19 14:12:15 +00:00
document.body.appendChild(t.template.body);
const installed = $('#installed');
const changeQueue = [];
changeQueue.THROTTLE = 100; // ms
changeQueue.time = 0;
// define pref-mapped ids separately
const newUI = {
enabled: null, // the global option should come first
favicons: null,
faviconsGray: null,
targets: null,
};
// ...add utility functions
Object.assign(newUI, {
ids: Object.keys(newUI),
prefKeyForId: id => `manage.newUI.${id}`.replace(/\.enabled$/, ''),
renderClass: () => {
$.rootCL.toggle('newUI', newUI.enabled);
$.rootCL.toggle('oldUI', !newUI.enabled);
},
2022-02-21 22:27:41 +00:00
hasFavs: () => newUI.enabled && newUI.favicons,
badFavsKey: 'badFavs',
async readBadFavs() {
const key = newUI.badFavsKey;
const val = await API.prefsDb.get(key);
return (newUI[key] = Array.isArray(val) ? val : []);
},
});
// ...read the actual values
for (const id of newUI.ids) {
newUI[id] = prefs.get(newUI.prefKeyForId(id));
}
newUI.renderClass();
(async function init() {
const query = router.getSearch('search');
const [styles, ids] = await Promise.all([
API.styles.getAll(),
query && API.styles.searchDB({query, mode: router.getSearch('searchMode')}),
2022-02-21 22:27:41 +00:00
newUI.hasFavs() && newUI.readBadFavs(),
prefs.ready,
]);
installed.on('click', Events.entryClicked);
installed.on('contextmenu', Events.entryClicked);
installed.on('mouseover', Events.lazyAddEntryTitle, {passive: true});
installed.on('mouseout', Events.lazyAddEntryTitle, {passive: true});
2022-02-19 21:59:08 +00:00
$('#sync-styles').onclick =
$('#manage-options-button').onclick =
router.makeToggle('stylus-options', toggleEmbeddedOptions);
$('#injection-order-button').onclick =
router.makeToggle('injection-order', (...args) => InjectionOrder(...args), [
'/vendor/draggable-list/draggable-list.iife.min.js',
'/injection-order/injection-order.css',
'/injection-order/injection-order', /* global InjectionOrder */
]);
$('#update-history-button').onclick =
router.makeToggle('update-history', (...args) => showUpdateHistory(...args), [
'/manage/updater-ui', /* global showUpdateHistory */
]);
$$('#header a[href^="http"]').forEach(a => (a.onclick = Events.external));
window.on('pageshow', handleVisibilityChange);
window.on('pagehide', handleVisibilityChange);
setupLivePrefs();
2022-02-19 14:12:15 +00:00
sorter.init();
router.update();
prefs.subscribe(newUI.ids.map(newUI.prefKeyForId), () => switchUI());
switchUI({styleOnly: true});
// translate CSS manually
document.styleSheets[0].insertRule(
`:root {${[
'genericDisabledLabel',
'updateAllCheckSucceededSomeEdited',
'filteredStylesAllHidden',
].map(id => `--${id}:"${CSS.escape(t(id))}";`).join('')
}}`);
2022-01-27 12:21:02 +00:00
if (!UA.vivaldi) {
fitSelectBoxesIn($('#filters'));
2020-10-15 10:55:27 +00:00
}
if (CHROME >= 80 && CHROME <= 88) {
// Wrong checkboxes are randomly checked after going back in history, https://crbug.com/1138598
window.on('pagehide', () => {
2020-10-15 10:55:27 +00:00
$$('input[type=checkbox]').forEach((el, i) => (el.name = `bug${i}`));
});
}
2020-10-15 10:55:27 +00:00
showStyles(styles, ids);
setTimeout(require, 0, [
'/manage/import-export',
'/manage/incremental-search',
'/manage/updater-ui',
]);
})();
2020-10-15 10:55:27 +00:00
msg.onExtension(onRuntimeMessage);
function onRuntimeMessage(msg) {
switch (msg.method) {
case 'styleUpdated':
case 'styleAdded':
case 'styleDeleted':
changeQueue.push(msg);
if (performance.now() - (changeQueue.time || 0) < changeQueue.THROTTLE) {
debounce(handleBulkChange, changeQueue.THROTTLE);
2020-10-15 10:55:27 +00:00
} else {
handleBulkChange();
}
break;
case 'styleApply':
case 'styleReplaceAll':
break;
default:
return;
}
setTimeout(sorter.updateStripes, 0, {onlyWhenColumnsChanged: true});
2015-01-30 17:31:20 +00:00
}
2022-02-19 21:59:08 +00:00
async function toggleEmbeddedOptions(show, el, selector) {
if (show) {
$.root.appendChild($create('iframe' + selector, {src: '/options.html'}))
.focus();
2022-03-03 02:23:34 +00:00
await new Promise(resolve => window.on('closeOptions', resolve, {once: true}));
2022-02-19 21:59:08 +00:00
} else {
el.contentDocument.body.classList.add('scaleout');
await animateElement(el, 'fadeout');
el.remove();
}
}