stylus/manage/config-dialog.js

168 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-11-27 06:56:22 +00:00
/* global messageBox makeLink */
2017-09-05 20:26:01 +00:00
'use strict';
function configDialog(style) {
const form = buildConfigForm();
2017-11-27 06:56:22 +00:00
const colorpicker = window.colorpicker();
2017-09-05 20:26:01 +00:00
return messageBox({
title: `${style.name} v${style.usercssData.version}`,
2017-09-05 20:26:01 +00:00
className: 'config-dialog',
contents: [
$element({
className: 'config-heading',
appendChild: style.usercssData.supportURL && makeLink({
className: 'external-support',
href: style.usercssData.supportURL,
textContent: t('externalFeedback')
})
}),
$element({
className: 'config-body',
2017-11-09 01:03:00 +00:00
appendChild: form.elements
})
],
2017-09-05 20:26:01 +00:00
buttons: [
t('confirmSave'),
{
textContent: t('confirmDefault'),
onclick: form.useDefault
},
t('confirmCancel')
]
}).then(({button, enter}) => {
if (button !== 1) {
colorpicker.hide();
}
if (button === 0 || enter) {
2017-11-27 06:56:22 +00:00
return form.getVars();
2017-09-05 20:26:01 +00:00
}
});
function buildConfigForm() {
const labels = [];
2017-09-16 01:24:50 +00:00
const vars = deepCopy(style.usercssData.vars);
2017-09-05 20:26:01 +00:00
for (const key of Object.keys(vars)) {
const va = vars[key];
let appendChild;
2017-11-09 01:00:46 +00:00
switch (va.type) {
case 'color':
2017-11-27 06:56:22 +00:00
appendChild = [$element({
className: 'cm-colorview',
appendChild: va.inputColor = $element({
va,
className: 'color-swatch',
onclick: onColorClicked,
})
})];
2017-11-09 01:00:46 +00:00
break;
case 'checkbox':
va.input = $element({tag: 'input', type: 'checkbox'});
va.input.onchange = () => {
va.dirty = true;
va.value = String(Number(va.input.checked));
};
appendChild = [
$element({tag: 'span', className: 'onoffswitch', appendChild: [
va.input,
$element({tag: 'span'})
]})
];
break;
case 'select':
case 'dropdown':
case 'image':
// TODO: a image picker input?
va.input = $element({
tag: 'select',
appendChild: va.options.map(o => $element({
tag: 'option', value: o.name, textContent: o.label
}))
});
va.input.onchange = () => {
va.dirty = true;
va.value = va.input.value;
};
appendChild = [va.input];
break;
default:
va.input = $element({tag: 'input', type: 'text'});
va.input.oninput = () => {
va.dirty = true;
va.value = va.input.value;
};
appendChild = [va.input];
break;
2017-09-05 20:26:01 +00:00
}
2017-09-05 21:33:08 +00:00
appendChild.unshift($element({tag: 'span', appendChild: va.label}));
2017-09-05 20:26:01 +00:00
labels.push($element({
tag: 'label',
className: `config-${va.type}`,
appendChild
}));
}
drawValues();
function drawValues() {
for (const key of Object.keys(vars)) {
const va = vars[key];
2017-11-27 06:56:22 +00:00
const useDefault = va.value === null || va.value === undefined;
const value = useDefault ? va.default : va.value;
2017-09-05 20:26:01 +00:00
if (va.type === 'color') {
2017-11-27 06:56:22 +00:00
va.inputColor.style.backgroundColor = value;
2017-09-05 20:26:01 +00:00
} else if (va.type === 'checkbox') {
va.input.checked = Number(value);
} else {
va.input.value = value;
}
}
}
function useDefault() {
for (const key of Object.keys(vars)) {
const va = vars[key];
2017-11-09 01:02:07 +00:00
va.dirty = va.value !== null && va.value !== undefined && va.value !== va.default;
2017-09-05 20:26:01 +00:00
va.value = null;
}
drawValues();
}
function getVars() {
return vars;
}
2017-11-27 06:56:22 +00:00
function onColorClicked() {
window.removeEventListener('keydown', messageBox.listeners.key, true);
const box = $('#message-box-contents');
colorpicker.show({
color: this.va.value || this.va.default,
top: this.getBoundingClientRect().bottom - 5,
left: box.getBoundingClientRect().left - 360,
hideDelay: 1e6,
guessBrightness: box,
callback: newColor => {
if (newColor) {
this.va.dirty = true;
this.va.value = newColor;
this.style.backgroundColor = newColor;
}
setTimeout(() => {
if (!$('.colorpicker-popup')) {
window.addEventListener('keydown', messageBox.listeners.key, true);
}
});
},
});
}
2017-09-05 20:26:01 +00:00
return {
2017-11-09 01:03:00 +00:00
elements: labels,
2017-09-05 20:26:01 +00:00
useDefault,
getVars
};
}
}