From 282bdf77067a75f2959082152e913a84414a0625 Mon Sep 17 00:00:00 2001 From: eight Date: Wed, 3 Oct 2018 20:24:06 +0800 Subject: [PATCH] Fix: numbers are not compared correctly --- js/prefs.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/js/prefs.js b/js/prefs.js index 638b79eb..f3862b89 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -392,17 +392,15 @@ function setupLivePrefs( IDs = Object.getOwnPropertyNames(prefs.defaults) .filter(id => $('#' + id)) ) { - const checkedProps = {}; for (const id of IDs) { const element = $('#' + id); - checkedProps[id] = element.type === 'checkbox' ? 'checked' : 'value'; updateElement({id, element, force: true}); element.addEventListener('change', onChange); } prefs.subscribe(IDs, (id, value) => updateElement({id, value})); function onChange() { - const value = this[checkedProps[this.id]]; + const value = getInputValue(this); if (prefs.get(this.id) !== value) { prefs.set(this.id, value); } @@ -417,10 +415,25 @@ function setupLivePrefs( prefs.unsubscribe(IDs, updateElement); return; } - const prop = checkedProps[id]; - if (force || element[prop] !== value) { - element[prop] = value; - element.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); + setInputValue(element, value, force); + } + function getInputValue(input) { + if (input.type === 'checkbox') { + return input.checked; + } + if (input.type === 'number') { + return Number(input.value); + } + return input.value; + } + function setInputValue(input, value, force = false) { + if (force || getInputValue(input) !== value) { + if (input.type === 'checkbox') { + input.checked = value; + } else { + input.value = value; + } + input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); } } }