2017-12-03 21:12:09 +00:00
|
|
|
/* global messageBox */
|
2017-09-05 20:26:01 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function configDialog(style) {
|
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-09-05 20:26:01 +00:00
|
|
|
return messageBox({
|
2017-12-03 21:12:09 +00:00
|
|
|
title: `${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'))),
|
|
|
|
$create('.config-body', elements)
|
2017-10-14 19:03:01 +00:00
|
|
|
],
|
2017-09-05 20:26:01 +00:00
|
|
|
buttons: [
|
2017-12-05 23:59:12 +00:00
|
|
|
{textContent: t('confirmSave'), dataset: {cmd: 'save'}, disabled: true, onclick: save},
|
|
|
|
{textContent: t('confirmDefault'), dataset: {cmd: 'default'}, onclick: useDefault},
|
|
|
|
{textContent: t('confirmClose'), dataset: {cmd: 'close'}},
|
|
|
|
],
|
|
|
|
onshow,
|
|
|
|
}).then(() => {
|
|
|
|
document.body.style.minWidth = '';
|
|
|
|
document.body.style.minHeight = '';
|
|
|
|
colorpicker.hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
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 00:16:53 +00:00
|
|
|
box.style.animationDuration = '0s';
|
2017-12-05 23:59:12 +00:00
|
|
|
}
|
2017-12-06 20:33:48 +00:00
|
|
|
|
2017-12-05 23:59:12 +00:00
|
|
|
box.addEventListener('change', onchange);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function onchange({target}) {
|
|
|
|
// 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-06 20:33:48 +00:00
|
|
|
if (prefs.get('config.autosave')) {
|
|
|
|
debounce(save, 100);
|
|
|
|
} else {
|
|
|
|
target.closest('label').classList.toggle('dirty', va.dirty);
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
function save() {
|
|
|
|
if (!vars.length || !vars.some(va => va.dirty)) {
|
2017-11-27 11:51:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-05 23:59:12 +00:00
|
|
|
style.enabled = true;
|
2017-11-27 11:51:51 +00:00
|
|
|
style.reason = 'config';
|
|
|
|
const styleVars = style.usercssData.vars;
|
|
|
|
const bgStyle = BG.cachedStyles.byId.get(style.id);
|
|
|
|
const bgVars = bgStyle && (bgStyle.usercssData || {}).vars || {};
|
|
|
|
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];
|
2017-11-27 11:51:51 +00:00
|
|
|
} else
|
|
|
|
if (bgva.type !== va.type) {
|
2017-11-27 12:57:32 +00:00
|
|
|
error = ['type ', '*' + va.type, ' != ', '*' + bgva.type];
|
2017-11-27 11:51:51 +00:00
|
|
|
} else
|
|
|
|
if ((va.type === 'select' || va.type === 'dropdown') &&
|
2017-12-05 23:59:12 +00:00
|
|
|
!isDefault(va) &&
|
2017-11-27 11:51:51 +00:00
|
|
|
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-11-27 11:51:51 +00:00
|
|
|
} else if (!va.dirty) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
styleVars[va.name].value = va.value;
|
|
|
|
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) {
|
|
|
|
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}))),
|
2017-11-27 11:51:51 +00:00
|
|
|
]);
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-12-05 23:59:12 +00:00
|
|
|
return numValid && BG.usercssHelper.save(style).then(saved => {
|
|
|
|
varsInitial = getInitialValues(deepCopy(saved.usercssData.vars));
|
|
|
|
vars.forEach(va => onchange({target: va.input}));
|
|
|
|
updateButtons();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function useDefault() {
|
|
|
|
for (const va of vars) {
|
|
|
|
va.value = null;
|
|
|
|
onchange({target: va.input});
|
|
|
|
}
|
|
|
|
renderValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDefault(va) {
|
|
|
|
return va.value === null || va.value === undefined || va.value === va.default;
|
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
|
|
|
|
function buildConfigForm() {
|
2017-11-27 11:51:51 +00:00
|
|
|
for (const va of vars) {
|
2017-12-03 21:12:09 +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 = [
|
|
|
|
$create('.cm-colorview', [
|
2017-12-05 23:59:12 +00:00
|
|
|
va.input = $create('.color-swatch', {
|
2017-12-05 22:17:22 +00:00
|
|
|
va,
|
|
|
|
onclick: showColorpicker
|
|
|
|
}),
|
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 = [
|
|
|
|
$create('span.onoffswitch', [
|
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',
|
|
|
|
onchange() {
|
2017-12-05 23:59:12 +00:00
|
|
|
va.value = va.input.checked ? '1' : '0';
|
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 = [
|
|
|
|
$create('.select-resizer', [
|
|
|
|
va.input = $create('select', {
|
2017-12-05 23:59:12 +00:00
|
|
|
va,
|
2017-12-05 22:17:22 +00:00
|
|
|
onchange() {
|
|
|
|
va.value = this.value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
|
|
|
|
default:
|
2017-12-05 22:17:22 +00:00
|
|
|
children = [
|
|
|
|
va.input = $create('input', {
|
2017-12-05 23:59:12 +00:00
|
|
|
va,
|
2017-12-05 22:17:22 +00:00
|
|
|
type: 'text',
|
|
|
|
oninput() {
|
2017-12-05 23:59:12 +00:00
|
|
|
va.value = this.value;
|
|
|
|
this.dispatchEvent(new Event('change', {bubbles: true}));
|
2017-12-05 22:17:22 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
2017-11-09 01:00:46 +00:00
|
|
|
break;
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-12-03 21:12:09 +00:00
|
|
|
elements.push(
|
|
|
|
$create(`label.config-${va.type}`, [
|
|
|
|
$create('span', va.label),
|
|
|
|
...children,
|
|
|
|
]));
|
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-11-27 11:51:51 +00:00
|
|
|
function renderValues() {
|
|
|
|
for (const va of vars) {
|
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);
|
|
|
|
} else {
|
|
|
|
va.input.value = 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
|
|
|
|
2017-11-27 11:51:51 +00:00
|
|
|
function showColorpicker() {
|
|
|
|
window.removeEventListener('keydown', messageBox.listeners.key, true);
|
|
|
|
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,
|
|
|
|
hideDelay: 1e6,
|
|
|
|
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) {
|
|
|
|
window.addEventListener('keydown', messageBox.listeners.key, true);
|
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|
2017-12-05 23:59:12 +00:00
|
|
|
|
|
|
|
function adjustSizeForPopup(box) {
|
|
|
|
box.style = 'white-space: nowrap !important';
|
|
|
|
box.firstElementChild.style = 'max-width: none; max-height: none;'.replace(/;/g, '!important;');
|
|
|
|
const {offsetWidth, offsetHeight} = box.firstElementChild;
|
|
|
|
box.style = box.firstElementChild.style = '';
|
|
|
|
|
|
|
|
const colorpicker = document.body.appendChild(
|
|
|
|
$create('.colorpicker-popup', {style: 'display: none!important'}));
|
|
|
|
const MIN_WIDTH = parseFloat(getComputedStyle(colorpicker).width) || 350;
|
|
|
|
const MIN_HEIGHT = 250;
|
|
|
|
colorpicker.remove();
|
|
|
|
|
|
|
|
const width = Math.max(Math.min(offsetWidth / 0.9 + 2, 800), MIN_WIDTH);
|
|
|
|
const height = Math.max(Math.min(offsetHeight / 0.9 + 2, 600), MIN_HEIGHT);
|
|
|
|
document.body.style.setProperty('min-width', width + 'px', 'important');
|
|
|
|
document.body.style.setProperty('min-height', height + 'px', 'important');
|
|
|
|
}
|
2017-09-05 20:26:01 +00:00
|
|
|
}
|