Change: drop setupRadioButtons

This commit is contained in:
eight 2019-06-26 11:13:25 +08:00
parent c5ba22ffed
commit f99ac66d53
2 changed files with 13 additions and 28 deletions

View File

@ -404,10 +404,10 @@ function moveFocus(rootElement, step) {
// and establishes a two-way connection between the document elements and the actual prefs // and establishes a two-way connection between the document elements and the actual prefs
function setupLivePrefs( function setupLivePrefs(
IDs = Object.getOwnPropertyNames(prefs.defaults) IDs = Object.getOwnPropertyNames(prefs.defaults)
.filter(id => $('#' + id)) .filter(id => $(`#${CSS.escape(id)}, [name=${CSS.escape(id)}]`))
) { ) {
for (const id of IDs) { for (const id of IDs) {
const elements = [$('#' + id), ...$$(`[name=${id}]`)]; const elements = $$(`#${CSS.escape(id)}, [name=${CSS.escape(id)}]`);
for (const element of elements) { for (const element of elements) {
updateElement({id, elements: [element], force: true}); updateElement({id, elements: [element], force: true});
element.addEventListener('change', onChange); element.addEventListener('change', onChange);
@ -416,6 +416,9 @@ function setupLivePrefs(
prefs.subscribe(IDs, (id, value) => updateElement({id, value})); prefs.subscribe(IDs, (id, value) => updateElement({id, value}));
function onChange() { function onChange() {
if (!this.checkValidity()) {
return;
}
const key = this.id || this.name; const key = this.id || this.name;
const value = getInputValue(this); const value = getInputValue(this);
if (value !== undefined && prefs.get(key) !== value) { if (value !== undefined && prefs.get(key) !== value) {
@ -425,7 +428,7 @@ function setupLivePrefs(
function updateElement({ function updateElement({
id, id,
value = prefs.get(id), value = prefs.get(id),
elements = [$('#' + id), ...$$(`[name=${id}]`)], elements = $$(`#${CSS.escape(id)}, [name=${CSS.escape(id)}]`),
force, force,
}) { }) {
// FIXME: this has no effect. `updateElement` is not a listener // FIXME: this has no effect. `updateElement` is not a listener
@ -447,13 +450,19 @@ function setupLivePrefs(
if (input.type === 'radio' && !input.checked) { if (input.type === 'radio' && !input.checked) {
return undefined; return undefined;
} }
// FIXME: use a string value for iconset
if (input.name === 'iconset') {
return Number(input.value);
}
return input.value; return input.value;
} }
function setInputValue(input, value, force = false) { function setInputValue(input, value, force = false) {
let oldValue, newValue; let oldValue, newValue;
if (input.type === 'radio') { if (input.type === 'radio') {
oldValue = input.checked; oldValue = input.checked;
newValue = input.checked = value === input.value; // FIXME: use == because we use number value in iconset
newValue = input.checked = value == input.value;
console.log(value, input.value);
} else if (input.type === 'checkbox') { } else if (input.type === 'checkbox') {
oldValue = input.checked; oldValue = input.checked;
newValue = input.checked = value; newValue = input.checked = value;

View File

@ -5,7 +5,6 @@
'use strict'; 'use strict';
setupLivePrefs(); setupLivePrefs();
setupRadioButtons();
enforceInputRange($('#popupWidth')); enforceInputRange($('#popupWidth'));
setTimeout(splitLongTooltips); setTimeout(splitLongTooltips);
@ -113,29 +112,6 @@ function checkUpdates() {
} }
} }
function setupRadioButtons() {
const sets = {};
const onChange = function () {
const newValue = sets[this.name].indexOf(this);
if (newValue >= 0 && prefs.get(this.name) !== newValue) {
prefs.set(this.name, newValue);
}
};
// group all radio-inputs by name="prefName" attribute
for (const el of $$('input[type="radio"][name]')) {
(sets[el.name] = sets[el.name] || []).push(el);
el.addEventListener('change', onChange);
}
// select the input corresponding to the actual pref value
for (const name in sets) {
sets[name][prefs.get(name)].checked = true;
}
// listen to pref changes and update the values
prefs.subscribe(Object.keys(sets), (key, value) => {
sets[key][value].checked = true;
});
}
function splitLongTooltips() { function splitLongTooltips() {
for (const el of $$('[title]')) { for (const el of $$('[title]')) {
if (el.title.length < 50) { if (el.title.length < 50) {