Add Zotero.Prefs.registerObserver

* Easier monitoring of preference changes
* Takes a preference name and a handler function that will be passed the new value of the preference
* Unregister observer via Zotero.Prefs.unregisterObserver with the same parameters
This commit is contained in:
Aurimas Vinckevicius 2015-01-20 22:45:28 -06:00
parent 4ecdd55717
commit 3f3666c972

View File

@ -2291,33 +2291,9 @@ Zotero.Prefs = new function(){
// TODO: parse settings XML // TODO: parse settings XML
} }
// Handlers for some Zotero preferences
// var _handlers = [
// Methods to register a preferences observer [ "statusBarIcon", function(val) {
//
function register(){
this.prefBranch.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.prefBranch.addObserver("", this, false);
}
function unregister(){
if (!this.prefBranch){
return;
}
this.prefBranch.removeObserver("", this);
}
function observe(subject, topic, data){
if(topic!="nsPref:changed"){
return;
}
try {
// subject is the nsIPrefBranch we're observing (after appropriate QI)
// data is the name of the pref that's been changed (relative to subject)
switch (data) {
case "statusBarIcon":
var doc = Components.classes["@mozilla.org/appshell/window-mediator;1"] var doc = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator) .getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser").document; .getMostRecentWindow("navigator:browser").document;
@ -2336,7 +2312,7 @@ Zotero.Prefs = new function(){
var toolbar = Zotero.getAncestorByTagName(icon, "toolbar"); var toolbar = Zotero.getAncestorByTagName(icon, "toolbar");
inAddonBar = toolbar == addonBar; inAddonBar = toolbar == addonBar;
} }
var val = this.get("statusBarIcon");
if (val == 0) { if (val == 0) {
// If showing in add-on bar, hide // If showing in add-on bar, hide
if (!icon || !inAddonBar) { if (!icon || !inAddonBar) {
@ -2377,54 +2353,54 @@ Zotero.Prefs = new function(){
icon.removeAttribute("compact"); icon.removeAttribute("compact");
} }
} }
break; }],
[ "automaticScraperUpdates", function(val) {
case "automaticScraperUpdates": if (val){
if (this.get('automaticScraperUpdates')){
Zotero.Schema.updateFromRepository(); Zotero.Schema.updateFromRepository();
} }
else { else {
Zotero.Schema.stopRepositoryTimer(); Zotero.Schema.stopRepositoryTimer();
} }
break; }],
[ "note.fontSize", function(val) {
case "note.fontSize":
var val = this.get('note.fontSize');
if (val < 6) { if (val < 6) {
this.set('note.fontSize', 11); Zotero.Prefs.set('note.fontSize', 11);
} }
break; }],
[ "zoteroDotOrgVersionHeader", function(val) {
case "zoteroDotOrgVersionHeader": if (val) {
if (this.get("zoteroDotOrgVersionHeader")) {
Zotero.VersionHeader.register(); Zotero.VersionHeader.register();
} }
else { else {
Zotero.VersionHeader.unregister(); Zotero.VersionHeader.unregister();
} }
break; }],
[ "zoteroDotOrgVersionHeader", function(val) {
case "sync.autoSync": if (val) {
if (this.get("sync.autoSync")) { Zotero.VersionHeader.register();
}
else {
Zotero.VersionHeader.unregister();
}
}],
[ "sync.autoSync", function(val) {
if (val) {
Zotero.Sync.Runner.IdleListener.register(); Zotero.Sync.Runner.IdleListener.register();
} }
else { else {
Zotero.Sync.Runner.IdleListener.unregister(); Zotero.Sync.Runner.IdleListener.unregister();
} }
break; }],
[ "sync.fulltext.enabled", function(val) {
// TEMP if (val) {
case "sync.fulltext.enabled":
if (this.get("sync.fulltext.enabled")) {
// Disable downgrades if full-text sync is enabled, since otherwise // Disable downgrades if full-text sync is enabled, since otherwise
// we could miss full-text content updates // we could miss full-text content updates
if (Zotero.DB.valueQuery("SELECT version FROM version WHERE schema='userdata'") < 77) { if (Zotero.DB.valueQuery("SELECT version FROM version WHERE schema='userdata'") < 77) {
Zotero.DB.query("UPDATE version SET version=77 WHERE schema='userdata'"); Zotero.DB.query("UPDATE version SET version=77 WHERE schema='userdata'");
} }
} }
break; }],
[ "search.quicksearch-mode", function(val) {
case "search.quicksearch-mode":
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator); .getService(Components.interfaces.nsIWindowMediator);
var enumerator = wm.getEnumerator("navigator:browser"); var enumerator = wm.getEnumerator("navigator:browser");
@ -2440,17 +2416,74 @@ Zotero.Prefs = new function(){
if (!win.Zotero) continue; if (!win.Zotero) continue;
Zotero.updateQuickSearchBox(win.document); Zotero.updateQuickSearchBox(win.document);
} }
break; }]
];
//
// Methods to register a preferences observer
//
function register(){
this.prefBranch.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.prefBranch.addObserver("", this, false);
// Register pre-set handlers
for (var i=0; i<_handlers.length; i++) {
this.registerObserver(_handlers[i][0], _handlers[i][1]);
}
} }
function unregister(){
if (!this.prefBranch){
return;
}
this.prefBranch.removeObserver("", this);
}
/**
* @param {nsIPrefBranch} subject The nsIPrefBranch we're observing (after appropriate QI)
* @param {String} topic The string defined by NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
* @param {String} data The name of the pref that's been changed (relative to subject)
*/
function observe(subject, topic, data){
if (topic != "nsPref:changed" || !_observers[data] || !_observers[data].length) {
return;
}
var obs = _observers[data];
for (var i=0; i<obs.length; i++) {
try {
obs[i](this.get(data));
} }
catch (e) { catch (e) {
Zotero.debug("Error while executing preference observer handler for " + data);
Zotero.debug(e); Zotero.debug(e);
throw (e);
} }
} }
} }
var _observers = {};
this.registerObserver = function(name, handler) {
_observers[name] = _observers[name] || [];
_observers[name].push(handler);
}
this.unregisterObserver = function(name, handler) {
var obs = _observers[name];
if (!obs) {
Zotero.debug("No preferences observer registered for " + name);
return;
}
var i = obs.indexOf(handler);
if (i == -1) {
Zotero.debug("Handler was not registered for preference " + name);
return;
}
obs.splice(i, 1);
}
}
/* /*
* Handles keyboard shortcut initialization from preferences, optionally * Handles keyboard shortcut initialization from preferences, optionally