stylus/manage/config-dialog.js

157 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-11-01 02:54:14 +00:00
/* global colorParser messageBox makeLink */
2017-09-05 20:26:01 +00:00
'use strict';
function configDialog(style) {
const form = buildConfigForm();
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(result => {
if (result.button !== 0 && !result.enter) {
return;
}
return form.getVars();
});
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':
va.inputColor = $element({tag: 'input', type: 'color'});
va.inputAlpha = $element({
tag: 'input',
type: 'range',
min: 0,
max: 1,
title: chrome.i18n.getMessage('alphaChannel'),
step: 'any'
});
va.inputColor.onchange = va.inputAlpha.oninput = () => {
va.dirty = true;
const color = colorParser.parse(va.inputColor.value);
color.a = Number(va.inputAlpha.value);
va.value = colorParser.format(color);
va.inputColor.style.opacity = color.a;
};
appendChild = [
$element({appendChild: [va.inputColor, va.inputAlpha]})
];
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];
const value = va.value === null || va.value === undefined ?
va.default : va.value;
if (va.type === 'color') {
const color = colorParser.parse(value);
va.inputAlpha.value = color.a;
va.inputColor.style.opacity = color.a;
delete color.a;
va.inputColor.value = colorParser.formatHex(color);
} 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;
}
return {
2017-11-09 01:03:00 +00:00
elements: labels,
2017-09-05 20:26:01 +00:00
useDefault,
getVars
};
}
}