From 54b268fe012bbcbb5e5205ec9633f1cf74e14e1f Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 20 Sep 2019 03:50:42 -0400 Subject: [PATCH] Add `global` parameter to Zotero.Prefs.registerObserver() This allows Zotero.Prefs to be used instead of Services.prefs for pref observing in plugins. Zotero.Prefs.prefBranch was replaced by Zotero.Prefs.rootBranch. --- chrome/content/zotero/xpcom/prefs.js | 58 ++++++++++++--------------- chrome/content/zotero/xpcom/zotero.js | 2 +- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/chrome/content/zotero/xpcom/prefs.js b/chrome/content/zotero/xpcom/prefs.js index 5662e1145a..1b732ac08c 100644 --- a/chrome/content/zotero/xpcom/prefs.js +++ b/chrome/content/zotero/xpcom/prefs.js @@ -31,12 +31,9 @@ Zotero.Prefs = new function(){ this.unregister = unregister; this.observe = observe; - // Public properties - this.prefBranch; + this.rootBranch = Services.prefs.getBranch(""); this.init = async function init() { - this.prefBranch = Services.prefs.getBranch(ZOTERO_CONFIG.PREF_BRANCH); - await loadExtensionDefaults(); // Register observer to handle pref changes @@ -88,12 +85,8 @@ Zotero.Prefs = new function(){ **/ function get(pref, global){ try { - if (global) { - var branch = Services.prefs.getBranch(""); - } - else { - var branch = this.prefBranch; - } + pref = global ? pref : ZOTERO_CONFIG.PREF_BRANCH + pref; + let branch = this.rootBranch; let value; switch (branch.getPrefType(pref)){ @@ -134,12 +127,8 @@ Zotero.Prefs = new function(){ **/ function set(pref, value, global) { try { - if (global) { - var branch = Services.prefs.getBranch(""); - } - else { - var branch = this.prefBranch; - } + pref = global ? pref : ZOTERO_CONFIG.PREF_BRANCH + pref; + let branch = this.rootBranch; switch (branch.getPrefType(pref)) { case branch.PREF_BOOL: @@ -192,25 +181,25 @@ Zotero.Prefs = new function(){ this.clear = function (pref, global) { - if (global) { - var branch = Services.prefs.getBranch(""); - } - else { - var branch = this.prefBranch; - } - branch.clearUserPref(pref); + pref = global ? pref : ZOTERO_CONFIG.PREF_BRANCH + pref; + this.rootBranch.clearUserPref(pref); } - this.resetBranch = function (exclude = []) { - var keys = this.prefBranch.getChildList("", {}); + /** + * @param {String[]} [exclude] + * @param {String} [branch] - Name of pref branch, ending with a period + */ + this.resetBranch = function (exclude = [], branch) { + var branch = Services.prefs.getBranch(branch || ZOTERO_CONFIG.PREF_BRANCH); + var keys = branch.getChildList("", {}); for (let key of keys) { - if (this.prefBranch.prefHasUserValue(key)) { + if (branch.prefHasUserValue(key)) { if (exclude.includes(key)) { continue; } Zotero.debug("Clearing " + key); - this.prefBranch.clearUserPref(key); + branch.clearUserPref(key); } } }; @@ -271,7 +260,7 @@ Zotero.Prefs = new function(){ // Methods to register a preferences observer // function register(){ - this.prefBranch.addObserver("", this, false); + this.rootBranch.addObserver("", this, false); // Register pre-set handlers for (var i=0; i<_handlers.length; i++) { @@ -280,10 +269,10 @@ Zotero.Prefs = new function(){ } function unregister(){ - if (!this.prefBranch){ + if (!this.rootBranch){ return; } - this.prefBranch.removeObserver("", this); + this.rootBranch.removeObserver("", this); } /** @@ -299,7 +288,7 @@ Zotero.Prefs = new function(){ var obs = _observers[data]; for (var i=0; icommand mappings from the prefs for (let cmd of cmds) {