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.
This commit is contained in:
Dan Stillman 2019-09-20 03:50:42 -04:00
parent 82ce5d9742
commit 54b268fe01
2 changed files with 26 additions and 34 deletions

View file

@ -31,12 +31,9 @@ Zotero.Prefs = new function(){
this.unregister = unregister; this.unregister = unregister;
this.observe = observe; this.observe = observe;
// Public properties this.rootBranch = Services.prefs.getBranch("");
this.prefBranch;
this.init = async function init() { this.init = async function init() {
this.prefBranch = Services.prefs.getBranch(ZOTERO_CONFIG.PREF_BRANCH);
await loadExtensionDefaults(); await loadExtensionDefaults();
// Register observer to handle pref changes // Register observer to handle pref changes
@ -88,12 +85,8 @@ Zotero.Prefs = new function(){
**/ **/
function get(pref, global){ function get(pref, global){
try { try {
if (global) { pref = global ? pref : ZOTERO_CONFIG.PREF_BRANCH + pref;
var branch = Services.prefs.getBranch(""); let branch = this.rootBranch;
}
else {
var branch = this.prefBranch;
}
let value; let value;
switch (branch.getPrefType(pref)){ switch (branch.getPrefType(pref)){
@ -134,12 +127,8 @@ Zotero.Prefs = new function(){
**/ **/
function set(pref, value, global) { function set(pref, value, global) {
try { try {
if (global) { pref = global ? pref : ZOTERO_CONFIG.PREF_BRANCH + pref;
var branch = Services.prefs.getBranch(""); let branch = this.rootBranch;
}
else {
var branch = this.prefBranch;
}
switch (branch.getPrefType(pref)) { switch (branch.getPrefType(pref)) {
case branch.PREF_BOOL: case branch.PREF_BOOL:
@ -192,25 +181,25 @@ Zotero.Prefs = new function(){
this.clear = function (pref, global) { this.clear = function (pref, global) {
if (global) { pref = global ? pref : ZOTERO_CONFIG.PREF_BRANCH + pref;
var branch = Services.prefs.getBranch(""); this.rootBranch.clearUserPref(pref);
}
else {
var branch = this.prefBranch;
}
branch.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) { for (let key of keys) {
if (this.prefBranch.prefHasUserValue(key)) { if (branch.prefHasUserValue(key)) {
if (exclude.includes(key)) { if (exclude.includes(key)) {
continue; continue;
} }
Zotero.debug("Clearing " + key); 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 // Methods to register a preferences observer
// //
function register(){ function register(){
this.prefBranch.addObserver("", this, false); this.rootBranch.addObserver("", this, false);
// Register pre-set handlers // Register pre-set handlers
for (var i=0; i<_handlers.length; i++) { for (var i=0; i<_handlers.length; i++) {
@ -280,10 +269,10 @@ Zotero.Prefs = new function(){
} }
function unregister(){ function unregister(){
if (!this.prefBranch){ if (!this.rootBranch){
return; return;
} }
this.prefBranch.removeObserver("", this); this.rootBranch.removeObserver("", this);
} }
/** /**
@ -299,7 +288,7 @@ Zotero.Prefs = new function(){
var obs = _observers[data]; var obs = _observers[data];
for (var i=0; i<obs.length; i++) { for (var i=0; i<obs.length; i++) {
try { try {
obs[i](this.get(data)); obs[i](this.get(data, true));
} }
catch (e) { catch (e) {
Zotero.debug("Error while executing preference observer handler for " + data); Zotero.debug("Error while executing preference observer handler for " + data);
@ -312,11 +301,14 @@ Zotero.Prefs = new function(){
var _observersBySymbol = {}; var _observersBySymbol = {};
/** /**
* @param {String} name - Preference name on extensions.zotero branch * @param {String} name - Preference name; if not global, this is on the extensions.zotero branch
* @param {Function} handler * @param {Function} handler
* @param {Boolean} [global]
* @return {Symbol} - Symbol to pass to unregisterObserver() * @return {Symbol} - Symbol to pass to unregisterObserver()
*/ */
this.registerObserver = function (name, handler) { this.registerObserver = function (name, handler, global) {
name = global ? name : ZOTERO_CONFIG.PREF_BRANCH + name;
var symbol = Symbol(); var symbol = Symbol();
_observers[name] = _observers[name] || []; _observers[name] = _observers[name] || [];
_observers[name].push(handler); _observers[name].push(handler);

View file

@ -1784,7 +1784,7 @@ Zotero.Keys = new function() {
* Called by Zotero.init() * Called by Zotero.init()
*/ */
function init() { function init() {
var cmds = Zotero.Prefs.prefBranch.getChildList('keys', {}, {}); var cmds = Zotero.Prefs.rootBranch.getChildList(ZOTERO_CONFIG.PREF_BRANCH + 'keys', {}, {});
// Get the key=>command mappings from the prefs // Get the key=>command mappings from the prefs
for (let cmd of cmds) { for (let cmd of cmds) {