2021-01-01 14:27:58 +00:00
|
|
|
/* global $ $create $createLink $remove messageBoxProxy setupLivePrefs */// dom.js
|
|
|
|
/* global API */// msg.js
|
|
|
|
/* global debounce deepCopy */// toolbox.js
|
|
|
|
/* global messageBox */
|
|
|
|
/* global prefs */
|
|
|
|
/* global t */// localization.js
|
2017-09-05 20:26:01 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/* exported configDialog */
|
|
|
|
async function configDialog(style) {
|
|
|
|
|
|
|
|
await require([
|
|
|
|
'/js/color/color-converter',
|
|
|
|
'/js/color/color-mimicry',
|
|
|
|
'/js/color/color-picker',
|
|
|
|
'/js/color/color-picker.css',
|
|
|
|
'/js/dlg/config-dialog.css',
|
|
|
|
]);
|
|
|
|
|
2017-12-07 22:08:25 +00:00
|
|
|
const AUTOSAVE_DELAY = 500;
|
2017-12-10 03:02:25 +00:00
|
|
|
let saving = false;
|
|
|
|
|
2017-12-03 21:12:09 +00:00
|
|
|
const data = style.usercssData;
|
|
|
|
const varsHash = deepCopy(data.vars) || {};
|
2017-11-27 11:51:51 +00:00
|
|
|
const varNames = Object.keys(varsHash);
|
|
|
|
const vars = varNames.map(name => varsHash[name]);
|
2017-12-05 23:59:12 +00:00
|
|
|
let varsInitial = getInitialValues(varsHash);
|
|
|
|
|
2017-11-27 11:51:51 +00:00
|
|
|
const elements = [];
|
2017-11-27 06:56:22 +00:00
|
|
|
const colorpicker = window.colorpicker();
|
2017-12-05 23:59:12 +00:00
|
|
|
const isPopup = location.href.includes('popup.html');
|
|
|
|
const buttons = {};
|
2017-09-05 20:26:01 +00:00
|
|
|
|
2017-11-27 11:51:51 +00:00
|
|
|
buildConfigForm();
|
|
|
|
renderValues();
|
2017-12-08 19:26:53 +00:00
|
|
|
vars.forEach(renderValueState);
|
2017-11-27 11:51:51 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
return messageBoxProxy.show({
|
2020-10-11 15:12:06 +00:00
|
|
|
title: `${style.customName || style.name} v${data.version}`,
|
2017-12-05 23:59:12 +00:00
|
|
|
className: 'config-dialog' + (isPopup ? ' stylus-popup' : ''),
|
2017-10-14 19:03:01 +00:00
|
|
|
contents: [
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('.config-heading', data.supportURL &&
|
|
|
|
$createLink({className: '.external-support', href: data.supportURL}, t('externalFeedback'))),
|
2020-11-18 11:17:15 +00:00
|
|
|
$create('.config-body', elements),
|
2017-10-14 19:03:01 +00:00
|
|
|
],
|
2017-12-08 19:32:35 +00:00
|
|
|
buttons: [{
|
|
|
|
textContent: t('confirmSave'),
|
2017-12-12 18:33:41 +00:00
|
|
|
dataset: {cmd: 'save'},
|
2017-12-08 19:32:35 +00:00
|
|
|
disabled: true,
|
|
|
|
onclick: save,
|
|
|
|
}, {
|
|
|
|
textContent: t('genericResetLabel'),
|
|
|
|
title: t('optionsReset'),
|
2017-12-12 18:33:41 +00:00
|
|
|
dataset: {cmd: 'default'},
|
2017-12-08 19:32:35 +00:00
|
|
|
onclick: useDefault,
|
|
|
|
}, {
|
|
|
|
textContent: t('confirmClose'),
|
|
|
|
dataset: {cmd: 'close'},
|
|
|
|
}],
|
2017-12-05 23:59:12 +00:00
|
|
|
onshow,
|
2017-12-07 05:41:52 +00:00
|
|
|
}).then(onhide);
|
2017-12-05 23:59:12 +00:00
|
|
|
|
|
|
|
function getInitialValues(source) {
|
|
|
|
const data = {};
|
|
|
|
for (const name of varNames) {
|
|
|
|
const va = source[name];
|
|
|
|
data[name] = isDefault(va) ? va.default : va.value;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onshow(box) {
|
2017-12-06 20:33:48 +00:00
|
|
|
$('button', box).insertAdjacentElement('afterend',
|
|
|
|
$create('label#config-autosave-wrapper', {
|
|
|
|
title: t('configOnChangeTooltip'),
|
|
|
|
}, [
|
|
|
|
$create('input', {id: 'config.autosave', type: 'checkbox'}),
|
|
|
|
$create('SVG:svg.svg-icon.checked',
|
|
|
|
$create('SVG:use', {'xlink:href': '#svg-icon-checked'})),
|
|
|
|
t('configOnChange'),
|
|
|
|
]));
|
|
|
|
setupLivePrefs(['config.autosave']);
|
|
|
|
|
2017-12-05 23:59:12 +00:00
|
|
|
if (isPopup) {
|
|
|
|
adjustSizeForPopup(box);
|
|
|
|
}
|
2017-12-06 20:33:48 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
box.on('change', onchange);
|
2017-12-05 23:59:12 +00:00
|
|
|
buttons.save = $('[data-cmd="save"]', box);
|
|
|
|
buttons.default = $('[data-cmd="default"]', box);
|
|
|
|
buttons.close = $('[data-cmd="close"]', box);
|
2017-12-06 08:35:04 +00:00
|
|
|
updateButtons();
|
2017-12-05 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 18:33:41 +00:00
|
|
|
function onhide() {
|
2017-12-07 05:41:52 +00:00
|
|
|
document.body.style.minWidth = '';
|
|
|
|
document.body.style.minHeight = '';
|
|
|
|
colorpicker.hide();
|
|
|
|
}
|
|
|
|
|
2017-12-08 00:23:09 +00:00
|
|
|
function onchange({target, justSaved = false}) {
|
2017-12-05 23:59:12 +00:00
|
|
|
// invoked after element's own onchange so 'va' contains the updated value
|
|
|
|
const va = target.va;
|
|
|
|
if (va) {
|
|
|
|
va.dirty = varsInitial[va.name] !== (isDefault(va) ? va.default : va.value);
|
2017-12-08 00:23:09 +00:00
|
|
|
if (prefs.get('config.autosave') && !justSaved) {
|
2017-12-10 03:02:25 +00:00
|
|
|
debounce(save, 100, {anyChangeIsDirty: true});
|
2017-12-08 00:23:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
renderValueState(va);
|
|
|
|
if (!justSaved) {
|
2017-12-06 20:33:48 +00:00
|
|
|
updateButtons();
|
|
|
|
}
|
2017-11-27 05:16:42 +00:00
|
|
|
}
|
2017-12-05 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateButtons() {
|
|
|
|
const someDirty = vars.some(va => va.dirty);
|
|
|
|
buttons.save.disabled = !someDirty;
|
|
|
|
buttons.default.disabled = vars.every(isDefault);
|
|
|
|
buttons.close.textContent = t(someDirty ? 'confirmCancel' : 'confirmClose');
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function save({anyChangeIsDirty = false} = {}, bgStyle) {
|
|
|
|
for (let delay = 1; saving && delay < 1000; delay *= 2) {
|
|
|
|
await new Promise(resolve => setTimeout(resolve, delay));
|
|
|
|
}
|
2017-12-10 03:02:25 +00:00
|
|
|
if (saving) {
|
2021-01-01 14:27:58 +00:00
|
|
|
throw 'Could not save: still saving previous results...';
|
2017-12-10 03:02:25 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (!vars.some(va => va.dirty || anyChangeIsDirty && va.value !== va.savedValue)) {
|
2017-11-27 11:51:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
if (!bgStyle) {
|
2021-01-01 14:27:58 +00:00
|
|
|
bgStyle = await API.styles.get(style.id).catch(() => ({}));
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
style = style.sections ? Object.assign({}, style) : style;
|
2017-12-05 23:59:12 +00:00
|
|
|
style.enabled = true;
|
2018-01-01 17:02:49 +00:00
|
|
|
style.sourceCode = null;
|
|
|
|
style.sections = null;
|
2017-11-27 11:51:51 +00:00
|
|
|
const styleVars = style.usercssData.vars;
|
2018-01-01 17:02:49 +00:00
|
|
|
const bgVars = (bgStyle.usercssData || {}).vars || {};
|
2017-11-27 11:51:51 +00:00
|
|
|
const invalid = [];
|
|
|
|
let numValid = 0;
|
|
|
|
for (const va of vars) {
|
|
|
|
const bgva = bgVars[va.name];
|
|
|
|
let error;
|
|
|
|
if (!bgva) {
|
2017-11-27 12:57:32 +00:00
|
|
|
error = 'deleted';
|
|
|
|
delete styleVars[va.name];
|
2021-01-01 14:27:58 +00:00
|
|
|
} else if (bgva.type !== va.type) {
|
2017-11-27 12:57:32 +00:00
|
|
|
error = ['type ', '*' + va.type, ' != ', '*' + bgva.type];
|
2021-01-01 14:27:58 +00:00
|
|
|
} else if (
|
|
|
|
(va.type === 'select' || va.type === 'dropdown') &&
|
|
|
|
!isDefault(va) && bgva.options.every(o => o.name !== va.value)
|
|
|
|
) {
|
2017-11-27 12:57:32 +00:00
|
|
|
error = `'${va.value}' not in the updated '${va.type}' list`;
|
2017-12-07 22:08:25 +00:00
|
|
|
} else if (!va.dirty && (!anyChangeIsDirty || va.value === va.savedValue)) {
|
2017-11-27 11:51:51 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
styleVars[va.name].value = va.value;
|
2017-12-07 22:08:25 +00:00
|
|
|
va.savedValue = va.value;
|
2017-11-27 11:51:51 +00:00
|
|
|
numValid++;
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-27 12:57:32 +00:00
|
|
|
invalid.push(['*' + va.name, ': ', ...error].map(e =>
|
2017-12-03 21:12:09 +00:00
|
|
|
e[0] === '*' && $create('b', e.slice(1)) || e));
|
2017-11-27 12:57:32 +00:00
|
|
|
if (bgva) {
|
|
|
|
styleVars[va.name].value = deepCopy(bgva);
|
|
|
|
}
|
2017-11-27 11:51:51 +00:00
|
|
|
}
|
|
|
|
if (invalid.length) {
|
2017-12-07 05:41:52 +00:00
|
|
|
onhide();
|
2017-11-27 11:51:51 +00:00
|
|
|
messageBox.alert([
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('div', {style: 'max-width: 34em'}, t('usercssConfigIncomplete')),
|
|
|
|
$create('ol', {style: 'text-align: left'},
|
|
|
|
invalid.map(msg =>
|
|
|
|
$create({tag: 'li', appendChild: msg}))),
|
2018-03-11 13:31:25 +00:00
|
|
|
], 'pre');
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-12-07 05:41:52 +00:00
|
|
|
if (!numValid) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-10 03:02:25 +00:00
|
|
|
saving = true;
|
2021-01-01 14:27:58 +00:00
|
|
|
try {
|
|
|
|
const newVars = await API.usercss.configVars(style.id, style.usercssData.vars);
|
|
|
|
varsInitial = getInitialValues(newVars);
|
|
|
|
vars.forEach(va => onchange({target: va.input, justSaved: true}));
|
|
|
|
renderValues();
|
|
|
|
updateButtons();
|
|
|
|
$remove('.config-error');
|
|
|
|
} catch (errors) {
|
|
|
|
const el = $('.config-error', messageBox.element) ||
|
|
|
|
$('#message-box-buttons').insertAdjacentElement('afterbegin', $create('.config-error'));
|
|
|
|
el.textContent =
|
|
|
|
el.title = (Array.isArray(errors) ? errors : [errors])
|
|
|
|
.map(e => e.message || `${e}`)
|
|
|
|
.join('\n');
|
|
|
|
}
|
|
|
|
saving = false;
|
2017-12-05 23:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function useDefault() {
|
|
|
|
for (const va of vars) {
|
|
|
|
va.value = null;
|
|
|
|
onchange({target: va.input});
|
|
|
|
}
|
|
|
|
renderValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDefault(va) {
|
2018-09-18 12:29:35 +00:00
|
|
|
return va.value === null || va.value === undefined || va.value === va.default;
|
2017-12-05 23:59:12 +00:00
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
|
|
|
|
function buildConfigForm() {
|
2017-12-12 18:33:41 +00:00
|
|
|
let resetter =
|
|
|
|
$create('a.config-reset-icon', {href: '#'}, [
|
2017-12-09 15:25:44 +00:00
|
|
|
$create('SVG:svg.svg-icon', {viewBox: '0 0 20 20'}, [
|
|
|
|
$create('SVG:title', t('genericResetLabel')),
|
|
|
|
$create('SVG:polygon', {
|
|
|
|
points: '16.2,5.5 14.5,3.8 10,8.3 5.5,3.8 3.8,5.5 8.3,10 3.8,14.5 ' +
|
|
|
|
'5.5,16.2 10,11.7 14.5,16.2 16.2,14.5 11.7,10',
|
2020-11-18 11:17:15 +00:00
|
|
|
}),
|
|
|
|
]),
|
2017-12-12 18:33:41 +00:00
|
|
|
]);
|
2017-11-27 11:51:51 +00:00
|
|
|
for (const va of vars) {
|
2018-09-05 12:43:45 +00:00
|
|
|
let children;
|
2017-11-09 01:00:46 +00:00
|
|
|
switch (va.type) {
|
|
|
|
case 'color':
|
2017-12-03 21:12:09 +00:00
|
|
|
children = [
|
2017-12-21 13:04:12 +00:00
|
|
|
$create('.colorview-swatch.config-value', [
|
2017-12-09 15:25:44 +00:00
|
|
|
va.input = $create('a.color-swatch', {
|
2017-12-05 22:17:22 +00:00
|
|
|
va,
|
2017-12-09 15:25:44 +00:00
|
|
|
href: '#',
|
2020-11-18 11:17:15 +00:00
|
|
|
onclick: showColorpicker,
|
2017-12-05 22:17:22 +00:00
|
|
|
}),
|
2017-12-03 21:12:09 +00:00
|
|
|
]),
|
|
|
|
];
|
2017-11-09 01:00:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'checkbox':
|
2017-12-03 21:12:09 +00:00
|
|
|
children = [
|
2017-12-08 00:23:09 +00:00
|
|
|
$create('span.onoffswitch.config-value', [
|
2017-12-05 22:17:22 +00:00
|
|
|
va.input = $create('input.slider', {
|
2017-12-05 23:59:12 +00:00
|
|
|
va,
|
2017-12-05 22:17:22 +00:00
|
|
|
type: 'checkbox',
|
2017-12-07 22:08:25 +00:00
|
|
|
onchange: updateVarOnChange,
|
2017-12-05 22:17:22 +00:00
|
|
|
}),
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('span'),
|
2017-12-05 22:17:22 +00:00
|
|
|
]),
|
2017-11-09 01:00:46 +00:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'select':
|
|
|
|
case 'dropdown':
|
|
|
|
case 'image':
|
|
|
|
// TODO: a image picker input?
|
2017-12-05 22:17:22 +00:00
|
|
|
children = [
|
2017-12-08 00:23:09 +00:00
|
|
|
$create('.select-resizer.config-value', [
|
2017-12-05 22:17:22 +00:00
|
|
|
va.input = $create('select', {
|
2017-12-05 23:59:12 +00:00
|
|
|
va,
|
2017-12-07 22:08:25 +00:00
|
|
|
onchange: updateVarOnChange,
|
2017-12-05 22:17:22 +00:00
|
|
|
},
|
|
|
|
va.options.map(o =>
|
|
|
|
$create('option', {value: o.name}, o.label))),
|
|
|
|
$create('SVG:svg.svg-icon.select-arrow',
|
|
|
|
$create('SVG:use', {'xlink:href': '#svg-icon-select-arrow'})),
|
|
|
|
]),
|
|
|
|
];
|
2017-11-09 01:00:46 +00:00
|
|
|
break;
|
|
|
|
|
2018-09-16 22:17:15 +00:00
|
|
|
case 'range':
|
2018-09-18 04:08:16 +00:00
|
|
|
case 'number': {
|
2018-09-05 12:43:45 +00:00
|
|
|
const options = {
|
2018-09-05 02:16:40 +00:00
|
|
|
va,
|
|
|
|
type: va.type,
|
2018-09-18 04:08:16 +00:00
|
|
|
onfocus: va.type === 'number' ? selectAllOnFocus : null,
|
2018-09-20 08:34:37 +00:00
|
|
|
onblur: va.type === 'number' ? updateVarOnBlur : null,
|
2018-09-05 02:16:40 +00:00
|
|
|
onchange: updateVarOnChange,
|
2018-09-20 09:25:45 +00:00
|
|
|
oninput: updateVarOnInput,
|
2020-11-18 11:17:15 +00:00
|
|
|
required: true,
|
2018-09-05 02:16:40 +00:00
|
|
|
};
|
2018-09-19 12:30:15 +00:00
|
|
|
if (typeof va.min === 'number') {
|
2018-09-18 04:08:16 +00:00
|
|
|
options.min = va.min;
|
|
|
|
}
|
2018-09-19 12:30:15 +00:00
|
|
|
if (typeof va.max === 'number') {
|
2018-09-18 04:08:16 +00:00
|
|
|
options.max = va.max;
|
|
|
|
}
|
2018-09-19 12:30:15 +00:00
|
|
|
if (typeof va.step === 'number' && isFinite(va.step)) {
|
2018-09-18 04:08:16 +00:00
|
|
|
options.step = va.step;
|
|
|
|
}
|
2018-09-16 22:17:15 +00:00
|
|
|
children = [
|
2018-09-18 04:08:16 +00:00
|
|
|
va.type === 'range' && $create('span.current-value'),
|
2020-11-18 11:17:15 +00:00
|
|
|
va.input = $create('input.config-value', options),
|
2018-09-16 22:17:15 +00:00
|
|
|
];
|
2018-09-18 04:08:16 +00:00
|
|
|
break;
|
2018-09-05 12:43:45 +00:00
|
|
|
}
|
2018-09-05 02:16:40 +00:00
|
|
|
|
2018-09-18 04:08:16 +00:00
|
|
|
default:
|
|
|
|
children = [
|
|
|
|
va.input = $create('input.config-value', {
|
|
|
|
va,
|
|
|
|
type: va.type,
|
|
|
|
onchange: updateVarOnChange,
|
|
|
|
oninput: updateVarOnInput,
|
|
|
|
onfocus: selectAllOnFocus,
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-12-08 00:23:09 +00:00
|
|
|
|
|
|
|
resetter = resetter.cloneNode(true);
|
2017-12-12 18:33:41 +00:00
|
|
|
resetter.va = va;
|
|
|
|
resetter.onclick = resetOnClick;
|
2017-12-08 00:23:09 +00:00
|
|
|
|
2017-12-03 21:12:09 +00:00
|
|
|
elements.push(
|
|
|
|
$create(`label.config-${va.type}`, [
|
2020-11-18 11:17:15 +00:00
|
|
|
$create('span.config-name', t.breakWord(va.label)),
|
2017-12-03 21:12:09 +00:00
|
|
|
...children,
|
2017-12-08 00:23:09 +00:00
|
|
|
resetter,
|
2017-12-03 21:12:09 +00:00
|
|
|
]));
|
2017-12-12 18:33:41 +00:00
|
|
|
|
|
|
|
va.savedValue = va.value;
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-11-27 11:51:51 +00:00
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
|
2018-09-20 09:25:45 +00:00
|
|
|
function updateVarOnBlur() {
|
|
|
|
this.value = isDefault(this.va) ? this.va.default : this.va.value;
|
2018-09-20 08:34:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 22:08:25 +00:00
|
|
|
function updateVarOnChange() {
|
2018-09-19 12:09:10 +00:00
|
|
|
if (this.type === 'range') {
|
2018-09-18 04:08:16 +00:00
|
|
|
this.va.value = Number(this.value);
|
2018-09-19 13:35:52 +00:00
|
|
|
updateRangeCurrentValue(this.va, this.va.value);
|
2018-09-19 12:44:51 +00:00
|
|
|
} else if (this.type === 'number') {
|
2018-09-20 09:25:45 +00:00
|
|
|
if (this.reportValidity()) {
|
|
|
|
this.va.value = Number(this.value);
|
|
|
|
}
|
2018-09-09 12:37:25 +00:00
|
|
|
} else {
|
|
|
|
this.va.value = this.type !== 'checkbox' ? this.value : this.checked ? '1' : '0';
|
2018-09-05 02:16:40 +00:00
|
|
|
}
|
2017-12-07 22:08:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 13:31:52 +00:00
|
|
|
function updateRangeCurrentValue(va, value) {
|
|
|
|
const span = $('.current-value', va.input.closest('.config-range'));
|
|
|
|
if (span) {
|
|
|
|
span.textContent = value + (va.units || '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 22:08:25 +00:00
|
|
|
function updateVarOnInput(event, debounced = false) {
|
|
|
|
if (debounced) {
|
|
|
|
event.target.dispatchEvent(new Event('change', {bubbles: true}));
|
|
|
|
} else {
|
|
|
|
debounce(updateVarOnInput, AUTOSAVE_DELAY, event, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 12:28:55 +00:00
|
|
|
function selectAllOnFocus(event) {
|
2018-09-05 02:17:36 +00:00
|
|
|
event.target.select();
|
|
|
|
}
|
|
|
|
|
2017-12-08 00:23:09 +00:00
|
|
|
function renderValues(varsToRender = vars) {
|
|
|
|
for (const va of varsToRender) {
|
2018-09-20 07:39:24 +00:00
|
|
|
if (va.input === document.activeElement) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-05 23:59:12 +00:00
|
|
|
const value = isDefault(va) ? va.default : va.value;
|
2017-11-27 11:51:51 +00:00
|
|
|
if (va.type === 'color') {
|
2017-12-05 23:59:12 +00:00
|
|
|
va.input.style.backgroundColor = value;
|
2017-11-27 11:51:51 +00:00
|
|
|
if (colorpicker.options.va === va) {
|
|
|
|
colorpicker.setColor(value);
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-11-27 11:51:51 +00:00
|
|
|
} else if (va.type === 'checkbox') {
|
|
|
|
va.input.checked = Number(value);
|
2018-09-05 12:38:22 +00:00
|
|
|
} else if (va.type === 'range') {
|
|
|
|
va.input.value = value;
|
2018-09-19 13:31:52 +00:00
|
|
|
updateRangeCurrentValue(va, va.input.value);
|
2018-09-05 12:38:22 +00:00
|
|
|
} else {
|
|
|
|
va.input.value = value;
|
2018-09-05 02:16:40 +00:00
|
|
|
}
|
2017-12-08 00:23:09 +00:00
|
|
|
if (!prefs.get('config.autosave')) {
|
|
|
|
renderValueState(va);
|
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-11-27 11:51:51 +00:00
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
|
2017-12-08 00:23:09 +00:00
|
|
|
function renderValueState(va) {
|
|
|
|
const el = va.input.closest('label');
|
|
|
|
el.classList.toggle('dirty', Boolean(va.dirty));
|
|
|
|
el.classList.toggle('nondefault', !isDefault(va));
|
2017-12-12 18:33:41 +00:00
|
|
|
$('.config-reset-icon', el).disabled = isDefault(va);
|
2017-12-08 00:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetOnClick(event) {
|
2017-12-12 18:33:41 +00:00
|
|
|
event.preventDefault();
|
|
|
|
this.va.value = null;
|
|
|
|
renderValues([this.va]);
|
|
|
|
onchange({target: this.va.input});
|
2017-12-08 00:23:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 18:33:41 +00:00
|
|
|
function showColorpicker(event) {
|
|
|
|
event.preventDefault();
|
2021-01-01 14:27:58 +00:00
|
|
|
window.off('keydown', messageBox.listeners.key, true);
|
2017-11-27 11:51:51 +00:00
|
|
|
const box = $('#message-box-contents');
|
|
|
|
colorpicker.show({
|
|
|
|
va: this.va,
|
|
|
|
color: this.va.value || this.va.default,
|
|
|
|
top: this.getBoundingClientRect().bottom - 5,
|
|
|
|
left: box.getBoundingClientRect().left - 360,
|
|
|
|
guessBrightness: box,
|
|
|
|
callback: onColorChanged,
|
|
|
|
});
|
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
|
2017-11-27 11:51:51 +00:00
|
|
|
function onColorChanged(newColor) {
|
|
|
|
if (newColor) {
|
|
|
|
this.va.value = newColor;
|
2017-12-05 23:59:12 +00:00
|
|
|
this.va.input.style.backgroundColor = newColor;
|
|
|
|
this.va.input.dispatchEvent(new Event('change', {bubbles: true}));
|
2017-11-27 06:56:22 +00:00
|
|
|
}
|
2017-11-27 11:51:51 +00:00
|
|
|
debounce(restoreEscInDialog);
|
|
|
|
}
|
2017-11-27 06:56:22 +00:00
|
|
|
|
2017-11-27 11:51:51 +00:00
|
|
|
function restoreEscInDialog() {
|
|
|
|
if (!$('.colorpicker-popup') && messageBox.element) {
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('keydown', messageBox.listeners.key, true);
|
2017-11-27 11:51:51 +00:00
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-12-05 23:59:12 +00:00
|
|
|
|
|
|
|
function adjustSizeForPopup(box) {
|
2017-12-06 21:11:30 +00:00
|
|
|
const contents = box.firstElementChild;
|
|
|
|
contents.style = 'max-width: none; max-height: none;'.replace(/;/g, '!important;');
|
|
|
|
let {offsetWidth: width, offsetHeight: height} = contents;
|
|
|
|
contents.style = '';
|
2017-12-05 23:59:12 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
const elPicker = document.body.appendChild(
|
2017-12-05 23:59:12 +00:00
|
|
|
$create('.colorpicker-popup', {style: 'display: none!important'}));
|
2017-12-07 22:08:25 +00:00
|
|
|
const PADDING = 50;
|
2021-01-01 14:27:58 +00:00
|
|
|
const MIN_WIDTH = parseFloat(getComputedStyle(elPicker).width) || 350;
|
2017-12-07 22:08:25 +00:00
|
|
|
const MIN_HEIGHT = 250 + PADDING;
|
2021-01-01 14:27:58 +00:00
|
|
|
elPicker.remove();
|
2017-12-05 23:59:12 +00:00
|
|
|
|
2019-06-01 09:12:52 +00:00
|
|
|
width = constrain(MIN_WIDTH, 798, width + PADDING);
|
|
|
|
height = constrain(MIN_HEIGHT, 598, height + PADDING);
|
2017-12-05 23:59:12 +00:00
|
|
|
document.body.style.setProperty('min-width', width + 'px', 'important');
|
|
|
|
document.body.style.setProperty('min-height', height + 'px', 'important');
|
|
|
|
}
|
2017-12-07 22:08:25 +00:00
|
|
|
|
|
|
|
function constrain(min, max, value) {
|
|
|
|
return value < min ? min : value > max ? max : value;
|
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|