From 135423860de55cf20fce85c37baf6ad005a0584d Mon Sep 17 00:00:00 2001 From: tophf Date: Fri, 21 Apr 2017 19:39:34 +0300 Subject: [PATCH] setupLivePrefs() now automatically finds the elements To make an element a live pref discoverable by setupLivePrefs() just use the corresponding pref's id as the element's HTML id attribute. --- edit.js | 5 +---- manage.js | 2 +- options/index.js | 9 +-------- popup.js | 2 +- prefs.js | 5 ++++- 5 files changed, 8 insertions(+), 15 deletions(-) diff --git a/edit.js b/edit.js index 15f58376..451e45a0 100644 --- a/edit.js +++ b/edit.js @@ -271,10 +271,7 @@ function initCodeMirror() { } document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort()); document.getElementById("options").addEventListener("change", acmeEventListener, false); - setupLivePrefs( - document.querySelectorAll("#options *[data-option][id^='editor.']") - .map(function(option) { return option.id }) - ); + setupLivePrefs(); hotkeyRerouter.setState(true); } diff --git a/manage.js b/manage.js index 90fed779..b9f4bfc5 100644 --- a/manage.js +++ b/manage.js @@ -80,7 +80,7 @@ function initGlobalEvents() { enforceInputRange($('#manage.newUI.targets')); // N.B. triggers existing onchange listeners - setupLivePrefs($$('input[id^="manage."]').map(el => el.id)); + setupLivePrefs(); $$('[data-filter]').forEach(el => { el.onchange = handleEvent.filterOnChange; diff --git a/options/index.js b/options/index.js index 2d0b5808..030edcf5 100644 --- a/options/index.js +++ b/options/index.js @@ -1,13 +1,6 @@ 'use strict'; -setupLivePrefs([ - 'show-badge', - 'popup.stylesFirst', - 'badgeNormal', - 'badgeDisabled', - 'popupWidth', - 'updateInterval', -]); +setupLivePrefs(); enforceInputRange($('#popupWidth')); // overwrite the default URL if browser is Opera diff --git a/popup.js b/popup.js index 28a2d3ac..6a9eabf8 100644 --- a/popup.js +++ b/popup.js @@ -62,7 +62,7 @@ function initPopup(url) { $('#disableAll').onchange = function() { installed.classList.toggle('disabled', this.checked); }; - setupLivePrefs(['disableAll']); + setupLivePrefs(); $('#find-styles-link').onclick = handleEvent.openURLandHide; $('#popup-manage-button').onclick = handleEvent.openURLandHide; diff --git a/prefs.js b/prefs.js index c838cf7e..6f407c35 100644 --- a/prefs.js +++ b/prefs.js @@ -302,7 +302,10 @@ var prefs = new function Prefs() { // Accepts an array of pref names (values are fetched via prefs.get) // and establishes a two-way connection between the document elements and the actual prefs -function setupLivePrefs(IDs) { +function setupLivePrefs( + IDs = Object.getOwnPropertyNames(prefs.readOnlyValues) + .filter(id => document.getElementById(id)) +) { const checkedProps = {}; for (const id of IDs) { const element = document.getElementById(id);