Merge pull request #62 from hideheader/preferences

Preferences fixes & improvements
This commit is contained in:
Jason Barnabe 2015-03-10 13:18:08 -05:00
commit 0edc0dbd56
3 changed files with 17 additions and 12 deletions

View File

@ -15,7 +15,7 @@ function showStyles(styles) {
styles.map(createStyleElement).forEach(function(e) { styles.map(createStyleElement).forEach(function(e) {
installed.appendChild(e); installed.appendChild(e);
}); });
loadPrefs({"show-badge": "true"}); loadPrefs({"show-badge": true});
} }
function createStyleElement(style) { function createStyleElement(style) {

View File

@ -27,7 +27,7 @@ chrome.tabs.getSelected(null, function(tab) {
var urlLink = writeStyleTemplate.cloneNode(true); var urlLink = writeStyleTemplate.cloneNode(true);
urlLink.href = "edit.html?url-prefix=" + encodeURIComponent(tab.url); urlLink.href = "edit.html?url-prefix=" + encodeURIComponent(tab.url);
urlLink.appendChild(document.createTextNode( // switchable; default="this URL" urlLink.appendChild(document.createTextNode( // switchable; default="this URL"
prefs.getPref("popup.breadcrumbs.usePath") !prefs.getPref("popup.breadcrumbs.usePath")
? t("writeStyleForURL").replace(/ /g, "\u00a0") ? t("writeStyleForURL").replace(/ /g, "\u00a0")
: /\/\/[^/]+\/(.*)/.exec(tab.url)[1] : /\/\/[^/]+\/(.*)/.exec(tab.url)[1]
)); ));

View File

@ -144,13 +144,14 @@ function changePref(event) {
// Accepts a hash of pref name to default value // Accepts a hash of pref name to default value
function loadPrefs(prefs) { function loadPrefs(prefs) {
for (var id in prefs) { for (var id in prefs) {
var value = this.prefs.getPref(id); var value = this.prefs.getPref(id, prefs[id]);
var el = document.getElementById(id); var el = document.getElementById(id);
if (isCheckbox(el)) { if (isCheckbox(el)) {
el.checked = value; el.checked = value;
} else { } else {
el.value = value; el.value = value;
} }
el.dispatchEvent(new Event("change", {bubbles: true, cancelable: true}));
el.addEventListener("change", changePref); el.addEventListener("change", changePref);
} }
} }
@ -165,13 +166,7 @@ var prefs = {
"popup.breadcrumbs": true, // display "New style" links as URL breadcrumbs "popup.breadcrumbs": true, // display "New style" links as URL breadcrumbs
"popup.breadcrumbs.usePath": false, // use URL path for "this URL" "popup.breadcrumbs.usePath": false, // use URL path for "this URL"
"popup.enabledFirst": true, // display enabled styles before disabled styles "popup.enabledFirst": true, // display enabled styles before disabled styles
"manage.enabledFirst": true, // display enabled styles before disabled styles
"observer.observeFrameContent": false, // [hh] add MutationObserver inside IFRAMEs
"observer.observeFrameLoad": false, // [hh] add onLoad listener to IFRAMEs
// https://github.com/JasonBarnabe/stylish-chrome/pull/39#issuecomment-76681235
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?",
@ -197,10 +192,20 @@ var prefs = {
}, },
setPref: function(key, value) { setPref: function(key, value) {
if (!(key in this)) console.warn(this.NO_DEFAULT_PREFERENCE, key); if (!(key in this)) console.warn(this.NO_DEFAULT_PREFERENCE, key);
if (value === undefined) localStorage.removeItem(key); var oldValue = this.getPref(key);
else localStorage.setItem(key, JSON.stringify(value));
if (undefined === value || this[key] === value) {
localStorage.removeItem(key); // (deleted || default)
} else {
var strValue = ("string" === typeof value ||
undefined === value) ? value : JSON.stringify(value);
localStorage.setItem(key, strValue);
}
var newValue = this.getPref(key);
if (newValue !== oldValue) {
notifyAllTabs({method: "prefChanged", prefName: key, value: value}); notifyAllTabs({method: "prefChanged", prefName: key, value: value});
}
}, },
removePref: function(key) { setPref(key, undefined) } removePref: function(key) { setPref(key, undefined) }
}; };