2022-01-19 19:21:54 +00:00
|
|
|
/* global $ moveFocus setupLivePrefs */// dom.js
|
2021-12-07 11:43:21 +00:00
|
|
|
/* global API */// msg.js
|
2021-12-29 19:57:22 +00:00
|
|
|
/* global editor */
|
|
|
|
/* global helpPopup */// util.js
|
|
|
|
/* global t */// localization.js
|
2022-01-19 19:21:54 +00:00
|
|
|
/* global debounce tryURL */// toolbox.js
|
2021-12-07 04:44:49 +00:00
|
|
|
/* exported StyleSettings */
|
|
|
|
'use strict';
|
|
|
|
|
2022-01-18 13:39:33 +00:00
|
|
|
async function StyleSettings() {
|
2021-12-29 19:57:22 +00:00
|
|
|
const AUTOSAVE_DELAY = 500; // same as config-dialog.js
|
|
|
|
const SS_ID = 'styleSettings';
|
2022-01-19 19:21:54 +00:00
|
|
|
const PASS = val => val;
|
2022-01-18 13:39:33 +00:00
|
|
|
await t.fetchTemplate('/edit/settings.html', SS_ID);
|
2021-12-29 19:57:22 +00:00
|
|
|
const {style} = editor;
|
|
|
|
const ui = t.template[SS_ID].cloneNode(true);
|
2022-01-19 19:19:50 +00:00
|
|
|
const elAuto = $('#config\\.autosave', ui);
|
2021-12-29 19:57:22 +00:00
|
|
|
const elSave = $('#ss-save', ui);
|
2022-01-19 19:21:54 +00:00
|
|
|
const elUpd = $('#ss-updatable', ui);
|
2021-12-29 19:57:22 +00:00
|
|
|
const pendingSetters = new Map();
|
|
|
|
const updaters = [
|
2022-01-19 19:21:54 +00:00
|
|
|
initCheckbox(elUpd, 'updatable', tryURL(style.updateUrl).href),
|
|
|
|
initInput('#ss-update-url', 'updateUrl', '', {
|
|
|
|
validate(el) {
|
|
|
|
elUpd.disabled = !el.value || !el.validity.valid;
|
|
|
|
return el.validity.valid;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
initRadio('ss-scheme', 'preferScheme', 'none'),
|
2021-12-29 19:57:22 +00:00
|
|
|
initArea('inclusions'),
|
|
|
|
initArea('exclusions'),
|
2021-12-07 04:44:49 +00:00
|
|
|
];
|
2021-12-29 19:57:22 +00:00
|
|
|
update();
|
|
|
|
window.on(SS_ID, update);
|
|
|
|
window.on('closeHelp', () => window.off(SS_ID, update), {once: true});
|
|
|
|
helpPopup.show(t(SS_ID), ui, {
|
|
|
|
className: 'style-settings-popup',
|
|
|
|
});
|
|
|
|
elSave.onclick = save;
|
|
|
|
$('#ss-close', ui).onclick = helpPopup.close;
|
|
|
|
setupLivePrefs([elAuto.id]);
|
|
|
|
moveFocus(ui, 0);
|
2021-12-07 04:44:49 +00:00
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function autosave(el, setter) {
|
|
|
|
pendingSetters.set(el, setter);
|
|
|
|
helpPopup.div.classList.add('dirty');
|
|
|
|
elSave.disabled = false;
|
|
|
|
if (elAuto.checked) debounce(save, AUTOSAVE_DELAY);
|
2021-12-07 04:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function initArea(type) {
|
2022-01-19 19:21:54 +00:00
|
|
|
return initInput(`#ss-${type}`, type, [], {
|
|
|
|
get: textToList,
|
|
|
|
set: list => list.join('\n'),
|
|
|
|
validate(el) {
|
|
|
|
const val = el.value;
|
|
|
|
el.rows = val.match(/^/gm).length + !val.endsWith('\n');
|
2021-12-07 11:43:21 +00:00
|
|
|
},
|
2022-01-19 19:21:54 +00:00
|
|
|
});
|
2021-12-07 11:43:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 19:21:54 +00:00
|
|
|
function initCheckbox(el, key, defVal) {
|
|
|
|
return initInput(el, key, Boolean(defVal), {dom: 'checked'});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initInput(el, key, defVal, {
|
|
|
|
dom = 'value', // DOM property name
|
|
|
|
get = PASS, // transformer function(val) after getting DOM value
|
|
|
|
set = PASS, // transformer function(val) before setting DOM value
|
|
|
|
validate = PASS, // function(el) - return `false` to prevent saving
|
|
|
|
} = {}) {
|
|
|
|
if (typeof el === 'string') {
|
|
|
|
el = $(el, ui);
|
|
|
|
}
|
|
|
|
el.oninput = () => {
|
|
|
|
if (validate(el) !== false) {
|
|
|
|
autosave(el, {dom, get, key});
|
|
|
|
}
|
|
|
|
};
|
2021-12-29 19:57:22 +00:00
|
|
|
return () => {
|
2022-01-19 19:21:54 +00:00
|
|
|
let val = style[key];
|
|
|
|
val = set(val != null ? val : defVal);
|
2021-12-29 19:57:22 +00:00
|
|
|
// Skipping if unchanged to preserve the Undo history of the input
|
2022-01-19 19:21:54 +00:00
|
|
|
if (el[dom] !== val) el[dom] = val;
|
|
|
|
validate(el);
|
2021-12-07 04:44:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:21:54 +00:00
|
|
|
function initRadio(name, key, defVal) {
|
|
|
|
$(`#${name}`, ui).oninput = e => {
|
|
|
|
if (e.target.checked) {
|
|
|
|
autosave(e.target, {key});
|
|
|
|
}
|
|
|
|
};
|
2021-12-29 19:57:22 +00:00
|
|
|
return () => {
|
2022-01-19 19:21:54 +00:00
|
|
|
const val = style[key] || defVal;
|
|
|
|
const el = $(`[name="${name}"][value="${val}"]`, ui);
|
|
|
|
el.checked = true;
|
2021-12-07 04:44:49 +00:00
|
|
|
};
|
|
|
|
}
|
2021-12-29 19:57:22 +00:00
|
|
|
|
|
|
|
function save() {
|
2022-01-19 19:21:54 +00:00
|
|
|
pendingSetters.forEach(saveValue);
|
2021-12-29 19:57:22 +00:00
|
|
|
pendingSetters.clear();
|
|
|
|
helpPopup.div.classList.remove('dirty');
|
|
|
|
elSave.disabled = true;
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:21:54 +00:00
|
|
|
function saveValue({dom = 'value', get = PASS, key}, el) {
|
|
|
|
return API.styles.config(style.id, key, get(el[dom]));
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function textToList(text) {
|
|
|
|
return text.split(/\n/).map(s => s.trim()).filter(Boolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
|
|
|
updaters.forEach(fn => fn());
|
|
|
|
}
|
2021-12-07 04:44:49 +00:00
|
|
|
}
|