prefs: handle object type
This commit is contained in:
parent
b5df8802a0
commit
fa6b0f4477
60
storage.js
60
storage.js
|
@ -173,7 +173,7 @@ var prefs = {
|
||||||
"manage.onlyEnabled": false, // display only enabled styles
|
"manage.onlyEnabled": false, // display only enabled styles
|
||||||
"manage.onlyEdited": false, // display only styles created locally
|
"manage.onlyEdited": false, // display only styles created locally
|
||||||
|
|
||||||
"editor.options": null, // CodeMirror.defaults.*
|
"editor.options": {}, // CodeMirror.defaults.*
|
||||||
"editor.lineWrapping": true, // word wrap
|
"editor.lineWrapping": true, // word wrap
|
||||||
"editor.smartIndent": true, // "smart" indent
|
"editor.smartIndent": true, // "smart" indent
|
||||||
"editor.indentWithTabs": false,// smart indent with tabs
|
"editor.indentWithTabs": false,// smart indent with tabs
|
||||||
|
@ -193,39 +193,31 @@ var prefs = {
|
||||||
NO_DEFAULT_PREFERENCE: "No default preference for '%s'",
|
NO_DEFAULT_PREFERENCE: "No default preference for '%s'",
|
||||||
UNHANDLED_DATA_TYPE: "Default '%s' is of type '%s' - what should be done with it?",
|
UNHANDLED_DATA_TYPE: "Default '%s' is of type '%s' - what should be done with it?",
|
||||||
|
|
||||||
getPref: function(key, ifUndefined) {
|
getPref: function(key, defaultValue) {
|
||||||
// Returns localStorage[key], ifUndefined, this[key], or undefined
|
// Returns localStorage[key], defaultValue, this[key], or undefined
|
||||||
// as type of ifUndefined, this[key], or localStorage[key]
|
// as type of defaultValue, this[key], or localStorage[key]
|
||||||
if (ifUndefined === undefined) ifUndefined = this[key]; // default value
|
|
||||||
var value = localStorage[key];
|
var value = localStorage[key];
|
||||||
if (undefined === value) { // no user preference
|
if (value === undefined) {
|
||||||
if (ifUndefined === undefined) console.error(this.NO_DEFAULT_PREFERENCE, key);
|
return defaultValue === undefined ? shallowCopy(this[key]) : defaultValue;
|
||||||
return ifUndefined;
|
|
||||||
}
|
}
|
||||||
switch (typeof ifUndefined) {
|
switch (typeof (defaultValue === undefined ? this[key] : defaultValue)) {
|
||||||
case "boolean": return value.toLowerCase() === "true";
|
case "boolean": return value.toLowerCase() === "true";
|
||||||
case "number": return Number(value);
|
case "number": return Number(value);
|
||||||
case "object": return JSON.parse(value);
|
case "object": return JSON.parse(value);
|
||||||
case "string": break;
|
case "string": break;
|
||||||
case "undefined": console.warn(this.NO_DEFAULT_PREFERENCE, key); break;
|
case "undefined": console.warn(this.NO_DEFAULT_PREFERENCE, key); break;
|
||||||
default: console.error(UNHANDLED_DATA_TYPE, key, typeof ifUndefined);
|
default: console.error(UNHANDLED_DATA_TYPE, key, typeof defaultValue);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
setPref: function(key, value) {
|
setPref: function(key, value) {
|
||||||
if (!(key in this)) console.warn(this.NO_DEFAULT_PREFERENCE, key);
|
var oldValue = localStorage[key];
|
||||||
var oldValue = this.getPref(key);
|
if (value === undefined || equal(value, this[key])) {
|
||||||
|
delete localStorage[key];
|
||||||
if (undefined === value || this[key] === value) {
|
|
||||||
localStorage.removeItem(key); // (deleted || default)
|
|
||||||
} else {
|
} else {
|
||||||
var strValue = ("string" === typeof value ||
|
localStorage[key] = typeof value == "string" ? value : JSON.stringify(value);
|
||||||
undefined === value) ? value : JSON.stringify(value);
|
|
||||||
localStorage.setItem(key, strValue);
|
|
||||||
}
|
}
|
||||||
|
if (!equal(value, oldValue === undefined ? this[key] : oldValue)) {
|
||||||
var newValue = this.getPref(key);
|
|
||||||
if (newValue !== oldValue) {
|
|
||||||
var message = {method: "prefChanged", prefName: key, value: value};
|
var message = {method: "prefChanged", prefName: key, value: value};
|
||||||
notifyAllTabs(message);
|
notifyAllTabs(message);
|
||||||
chrome.extension.sendMessage(message);
|
chrome.extension.sendMessage(message);
|
||||||
|
@ -266,3 +258,29 @@ function sessionStorageHash(name) {
|
||||||
Object.defineProperty(hash, "name", {value: name});
|
Object.defineProperty(hash, "name", {value: name});
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shallowCopy(obj) {
|
||||||
|
if (typeof obj != "object") {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
var copy = {};
|
||||||
|
for (var k in obj) {
|
||||||
|
copy[k] = obj[k];
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
function equal(a, b) {
|
||||||
|
if (!a || !b || typeof a != "object") {
|
||||||
|
return a === b;
|
||||||
|
}
|
||||||
|
if (Object.keys(a).length != Object.keys(b).length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (var k in a) {
|
||||||
|
if (a[k] !== b[k]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user