Remove unneeded "Pref" word from prefs methods

This commit is contained in:
tophf 2015-10-05 14:27:17 +03:00
parent 1f961b0993
commit d971bbda8a
6 changed files with 54 additions and 53 deletions

View File

@ -71,7 +71,7 @@ chrome.commands.onCommand.addListener(function(command) {
break; break;
case "styleDisableAll": case "styleDisableAll":
disableAllStylesToggle(); disableAllStylesToggle();
chrome.contextMenus.update("disableAll", {checked: prefs.getPref("disableAll")}); chrome.contextMenus.update("disableAll", {checked: prefs.get("disableAll")});
break; break;
} }
}); });
@ -81,11 +81,11 @@ chrome.commands.onCommand.addListener(function(command) {
runTryCatch(function() { runTryCatch(function() {
chrome.contextMenus.create({ chrome.contextMenus.create({
id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"), id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"),
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("show-badge") type: "checkbox", contexts: ["browser_action"], checked: prefs.get("show-badge")
}, function() { var clearError = chrome.runtime.lastError }); }, function() { var clearError = chrome.runtime.lastError });
chrome.contextMenus.create({ chrome.contextMenus.create({
id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"), id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"),
type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("disableAll") type: "checkbox", contexts: ["browser_action"], checked: prefs.get("disableAll")
}, function() { var clearError = chrome.runtime.lastError }); }, function() { var clearError = chrome.runtime.lastError });
}); });
@ -93,15 +93,15 @@ chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "disableAll") { if (info.menuItemId == "disableAll") {
disableAllStylesToggle(info.checked); disableAllStylesToggle(info.checked);
} else { } else {
prefs.setPref(info.menuItemId, info.checked); prefs.set(info.menuItemId, info.checked);
} }
}); });
function disableAllStylesToggle(newState) { function disableAllStylesToggle(newState) {
if (newState === undefined || newState === null) { if (newState === undefined || newState === null) {
newState = !prefs.getPref("disableAll"); newState = !prefs.get("disableAll");
} }
prefs.setPref("disableAll", newState); prefs.set("disableAll", newState);
} }
function getStyles(options, callback) { function getStyles(options, callback) {
@ -114,7 +114,7 @@ function getStyles(options, callback) {
var asHash = "asHash" in options ? options.asHash : false; var asHash = "asHash" in options ? options.asHash : false;
var callCallback = function() { var callCallback = function() {
var styles = asHash ? {disableAll: prefs.getPref("disableAll", false)} : []; var styles = asHash ? {disableAll: prefs.get("disableAll", false)} : [];
cachedStyles.forEach(function(style) { cachedStyles.forEach(function(style) {
if (enabled != null && fixBoolean(style.enabled) != enabled) { if (enabled != null && fixBoolean(style.enabled) != enabled) {
return; return;
@ -387,7 +387,7 @@ chrome.tabs.onAttached.addListener(function(tabId, data) {
if (tabData.url.indexOf(editFullUrl) == 0) { if (tabData.url.indexOf(editFullUrl) == 0) {
chrome.windows.get(tabData.windowId, {populate: true}, function(win) { chrome.windows.get(tabData.windowId, {populate: true}, function(win) {
// If there's only one tab in this window, it's been dragged to new window // If there's only one tab in this window, it's been dragged to new window
prefs.setPref('openEditInWindow', win.tabs.length == 1); prefs.set("openEditInWindow", win.tabs.length == 1);
}); });
} }
}); });

40
edit.js
View File

@ -132,16 +132,16 @@ function initCodeMirror() {
foldGutter: true, foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"], gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
matchBrackets: true, matchBrackets: true,
lint: {getAnnotations: CodeMirror.lint.css, delay: prefs.getPref("editor.lintDelay")}, lint: {getAnnotations: CodeMirror.lint.css, delay: prefs.get("editor.lintDelay")},
lintReportDelay: prefs.getPref("editor.lintReportDelay"), lintReportDelay: prefs.get("editor.lintReportDelay"),
styleActiveLine: true, styleActiveLine: true,
theme: "default", theme: "default",
keyMap: prefs.getPref("editor.keyMap"), keyMap: prefs.get("editor.keyMap"),
extraKeys: { // independent of current keyMap extraKeys: { // independent of current keyMap
"Alt-PageDown": "nextEditor", "Alt-PageDown": "nextEditor",
"Alt-PageUp": "prevEditor" "Alt-PageUp": "prevEditor"
} }
}, prefs.getPref("editor.options")); }, prefs.get("editor.options"));
// additional commands // additional commands
CM.commands.jumpToLine = jumpToLine; CM.commands.jumpToLine = jumpToLine;
@ -219,8 +219,8 @@ function initCodeMirror() {
}); });
} }
// preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->loadPrefs() // preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs()
var theme = prefs.getPref("editor.theme"); var theme = prefs.get("editor.theme");
document.getElementById("cm-theme").href = theme == "default" ? "" : "codemirror/theme/" + theme + ".css"; document.getElementById("cm-theme").href = theme == "default" ? "" : "codemirror/theme/" + theme + ".css";
// initialize global editor controls // initialize global editor controls
@ -242,7 +242,7 @@ function initCodeMirror() {
} }
document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort()); document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort());
document.getElementById("options").addEventListener("change", acmeEventListener, false); document.getElementById("options").addEventListener("change", acmeEventListener, false);
loadPrefs( setupLivePrefs(
document.querySelectorAll("#options *[data-option][id^='editor.']") document.querySelectorAll("#options *[data-option][id^='editor.']")
.map(function(option) { return option.id }) .map(function(option) { return option.id })
); );
@ -270,8 +270,8 @@ function acmeEventListener(event) {
// use non-localized "default" internally // use non-localized "default" internally
if (!value || value == "default" || value == t("defaultTheme")) { if (!value || value == "default" || value == t("defaultTheme")) {
value = "default"; value = "default";
if (prefs.getPref(el.id) != value) { if (prefs.get(el.id) != value) {
prefs.setPref(el.id, value); prefs.set(el.id, value);
} }
themeLink.href = ""; themeLink.href = "";
el.selectedIndex = 0; el.selectedIndex = 0;
@ -386,7 +386,7 @@ document.addEventListener("wheel", function(event) {
chrome.tabs.query({currentWindow: true}, function(tabs) { chrome.tabs.query({currentWindow: true}, function(tabs) {
var windowId = tabs[0].windowId; var windowId = tabs[0].windowId;
if (prefs.getPref("openEditInWindow")) { if (prefs.get("openEditInWindow")) {
if (tabs.length == 1 && window.history.length == 1) { if (tabs.length == 1 && window.history.length == 1) {
chrome.windows.getAll(function(windows) { chrome.windows.getAll(function(windows) {
if (windows.length > 1) { if (windows.length > 1) {
@ -420,7 +420,7 @@ function goBackToManage(event) {
window.onbeforeunload = function() { window.onbeforeunload = function() {
if (saveSizeOnClose) { if (saveSizeOnClose) {
prefs.setPref("windowPosition", { prefs.set("windowPosition", {
left: screenLeft, left: screenLeft,
top: screenTop, top: screenTop,
width: outerWidth, width: outerWidth,
@ -964,9 +964,9 @@ function beautify(event) {
script.onload = doBeautify; script.onload = doBeautify;
} }
function doBeautify() { function doBeautify() {
var tabs = prefs.getPref("editor.indentWithTabs"); var tabs = prefs.get("editor.indentWithTabs");
var options = prefs.getPref("editor.beautify"); var options = prefs.get("editor.beautify");
options.indent_size = tabs ? 1 : prefs.getPref("editor.tabSize"); options.indent_size = tabs ? 1 : prefs.get("editor.tabSize");
options.indent_char = tabs ? "\t" : " "; options.indent_char = tabs ? "\t" : " ";
var section = getSectionForChild(event.target); var section = getSectionForChild(event.target);
@ -1015,7 +1015,7 @@ function beautify(event) {
document.querySelector(".beautify-options").addEventListener("change", function(event) { document.querySelector(".beautify-options").addEventListener("change", function(event) {
var value = event.target.selectedIndex > 0; var value = event.target.selectedIndex > 0;
options[event.target.dataset.option] = value; options[event.target.dataset.option] = value;
prefs.setPref("editor.beautify", options); prefs.set("editor.beautify", options);
event.target.parentNode.setAttribute("newline", value.toString()); event.target.parentNode.setAttribute("newline", value.toString());
doBeautify(); doBeautify();
}); });
@ -1090,7 +1090,7 @@ function initWithStyle(style) {
function add() { function add() {
var sectionDiv = addSection(null, queue.shift()); var sectionDiv = addSection(null, queue.shift());
maximizeCodeHeight(sectionDiv, !queue.length); maximizeCodeHeight(sectionDiv, !queue.length);
updateLintReport(getCodeMirrorForSection(sectionDiv), prefs.getPref("editor.lintDelay")); updateLintReport(getCodeMirrorForSection(sectionDiv), prefs.get("editor.lintDelay"));
} }
} }
@ -1442,12 +1442,12 @@ function showToMozillaHelp() {
} }
function showKeyMapHelp() { function showKeyMapHelp() {
var keyMap = mergeKeyMaps({}, prefs.getPref("editor.keyMap"), CodeMirror.defaults.extraKeys); var keyMap = mergeKeyMaps({}, prefs.get("editor.keyMap"), CodeMirror.defaults.extraKeys);
var keyMapSorted = Object.keys(keyMap) var keyMapSorted = Object.keys(keyMap)
.map(function(key) { return {key: key, cmd: keyMap[key]} }) .map(function(key) { return {key: key, cmd: keyMap[key]} })
.concat([{key: "Shift-Ctrl-Wheel", cmd: "scrollWindow"}]) .concat([{key: "Shift-Ctrl-Wheel", cmd: "scrollWindow"}])
.sort(function(a, b) { return a.cmd < b.cmd || (a.cmd == b.cmd && a.key < b.key) ? -1 : 1 }); .sort(function(a, b) { return a.cmd < b.cmd || (a.cmd == b.cmd && a.key < b.key) ? -1 : 1 });
showHelp(t("cm_keyMap") + ": " + prefs.getPref("editor.keyMap"), showHelp(t("cm_keyMap") + ": " + prefs.get("editor.keyMap"),
'<table class="keymap-list">' + '<table class="keymap-list">' +
'<thead><tr><th><input placeholder="' + t("helpKeyMapHotkey") + '" type="search"></th>' + '<thead><tr><th><input placeholder="' + t("helpKeyMapHotkey") + '" type="search"></th>' +
'<th><input placeholder="' + t("helpKeyMapCommand") + '" type="search"></th></tr></thead>' + '<th><input placeholder="' + t("helpKeyMapCommand") + '" type="search"></th></tr></thead>' +
@ -1564,8 +1564,8 @@ function showCodeMirrorPopup(title, html, options) {
matchBrackets: true, matchBrackets: true,
lint: {getAnnotations: CodeMirror.lint.css, delay: 0}, lint: {getAnnotations: CodeMirror.lint.css, delay: 0},
styleActiveLine: true, styleActiveLine: true,
theme: prefs.getPref("editor.theme"), theme: prefs.get("editor.theme"),
keyMap: prefs.getPref("editor.keyMap") keyMap: prefs.get("editor.keyMap")
}, options)); }, options));
popup.codebox.focus(); popup.codebox.focus();
popup.codebox.on("focus", function() { hotkeyRerouter.setState(false) }); popup.codebox.on("focus", function() { hotkeyRerouter.setState(false) });

View File

@ -110,7 +110,7 @@ function createStyleElement(style) {
event.stopPropagation(); event.stopPropagation();
if (openWindow || openBackgroundTab || openForegroundTab) { if (openWindow || openBackgroundTab || openForegroundTab) {
if (openWindow) { if (openWindow) {
var options = prefs.getPref("windowPosition"); var options = prefs.get("windowPosition");
options.url = url; options.url = url;
chrome.windows.create(options); chrome.windows.create(options);
} else { } else {
@ -475,7 +475,7 @@ document.addEventListener("DOMContentLoaded", function() {
document.getElementById("search").addEventListener("input", searchStyles); document.getElementById("search").addEventListener("input", searchStyles);
searchStyles(true); // re-apply filtering on history Back searchStyles(true); // re-apply filtering on history Back
loadPrefs([ setupLivePrefs([
"manage.onlyEnabled", "manage.onlyEnabled",
"manage.onlyEdited", "manage.onlyEdited",
"show-badge", "show-badge",

View File

@ -44,13 +44,13 @@ function updateIcon(tab, styles) {
}); });
function stylesReceived(styles) { function stylesReceived(styles) {
var disableAll = "disableAll" in styles ? styles.disableAll : prefs.getPref("disableAll"); var disableAll = "disableAll" in styles ? styles.disableAll : prefs.get("disableAll");
var postfix = styles.length == 0 || disableAll ? "w" : ""; var postfix = styles.length == 0 || disableAll ? "w" : "";
chrome.browserAction.setIcon({ chrome.browserAction.setIcon({
path: {19: "19" + postfix + ".png", 38: "38" + postfix + ".png"}, path: {19: "19" + postfix + ".png", 38: "38" + postfix + ".png"},
tabId: tab.id tabId: tab.id
}); });
var t = prefs.getPref("show-badge") && styles.length ? ("" + styles.length) : ""; var t = prefs.get("show-badge") && styles.length ? ("" + styles.length) : "";
chrome.browserAction.setBadgeText({text: t, tabId: tab.id}); chrome.browserAction.setBadgeText({text: t, tabId: tab.id});
chrome.browserAction.setBadgeBackgroundColor({color: disableAll ? "#aaa" : [0, 0, 0, 0]}); chrome.browserAction.setBadgeBackgroundColor({color: disableAll ? "#aaa" : [0, 0, 0, 0]});
//console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'."); //console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");

View File

@ -3,7 +3,7 @@ writeStyleTemplate.className = "write-style-link";
var installed = document.getElementById("installed"); var installed = document.getElementById("installed");
if (!prefs.getPref("popup.stylesFirst")) { if (!prefs.get("popup.stylesFirst")) {
document.body.insertBefore(document.querySelector("body > .actions"), installed); document.body.insertBefore(document.querySelector("body > .actions"), installed);
} }
@ -29,14 +29,14 @@ function updatePopUp(url) {
var urlLink = writeStyleTemplate.cloneNode(true); var urlLink = writeStyleTemplate.cloneNode(true);
urlLink.href = "edit.html?url-prefix=" + encodeURIComponent(url); urlLink.href = "edit.html?url-prefix=" + encodeURIComponent(url);
urlLink.appendChild(document.createTextNode( // switchable; default="this&nbsp;URL" urlLink.appendChild(document.createTextNode( // switchable; default="this&nbsp;URL"
!prefs.getPref("popup.breadcrumbs.usePath") !prefs.get("popup.breadcrumbs.usePath")
? t("writeStyleForURL").replace(/ /g, "\u00a0") ? t("writeStyleForURL").replace(/ /g, "\u00a0")
: /\/\/[^/]+\/(.*)/.exec(url)[1] : /\/\/[^/]+\/(.*)/.exec(url)[1]
)); ));
urlLink.title = "url-prefix(\"$\")".replace("$", url); urlLink.title = "url-prefix(\"$\")".replace("$", url);
writeStyleLinks.push(urlLink); writeStyleLinks.push(urlLink);
document.querySelector("#write-style").appendChild(urlLink) document.querySelector("#write-style").appendChild(urlLink)
if (prefs.getPref("popup.breadcrumbs")) { // switchable; default=enabled if (prefs.get("popup.breadcrumbs")) { // switchable; default=enabled
urlLink.addEventListener("mouseenter", function(event) { this.parentNode.classList.add("url()") }, false); urlLink.addEventListener("mouseenter", function(event) { this.parentNode.classList.add("url()") }, false);
urlLink.addEventListener("focus", function(event) { this.parentNode.classList.add("url()") }, false); urlLink.addEventListener("focus", function(event) { this.parentNode.classList.add("url()") }, false);
urlLink.addEventListener("mouseleave", function(event) { this.parentNode.classList.remove("url()") }, false); urlLink.addEventListener("mouseleave", function(event) { this.parentNode.classList.remove("url()") }, false);
@ -63,7 +63,7 @@ function updatePopUp(url) {
link.addEventListener("click", openLinkInTabOrWindow, false); link.addEventListener("click", openLinkInTabOrWindow, false);
container.appendChild(link); container.appendChild(link);
}); });
if (prefs.getPref("popup.breadcrumbs")) { if (prefs.get("popup.breadcrumbs")) {
container.classList.add("breadcrumbs"); container.classList.add("breadcrumbs");
container.appendChild(container.removeChild(container.firstChild)); container.appendChild(container.removeChild(container.firstChild));
} }
@ -71,7 +71,7 @@ function updatePopUp(url) {
} }
function showStyles(styles) { function showStyles(styles) {
var enabledFirst = prefs.getPref("popup.enabledFirst"); var enabledFirst = prefs.get("popup.enabledFirst");
styles.sort(function(a, b) { styles.sort(function(a, b) {
if (enabledFirst && a.enabled !== b.enabled) return !(a.enabled < b.enabled) ? -1 : 1; if (enabledFirst && a.enabled !== b.enabled) return !(a.enabled < b.enabled) ? -1 : 1;
return a.name.localeCompare(b.name); return a.name.localeCompare(b.name);
@ -146,9 +146,9 @@ function getId(event) {
function openLinkInTabOrWindow(event) { function openLinkInTabOrWindow(event) {
event.preventDefault(); event.preventDefault();
if (prefs.getPref('openEditInWindow', false)) { if (prefs.get("openEditInWindow", false)) {
var options = {url: event.target.href} var options = {url: event.target.href}
var wp = prefs.getPref('windowPosition', {}); var wp = prefs.get("windowPosition", {});
for (var k in wp) options[k] = wp[k]; for (var k in wp) options[k] = wp[k];
chrome.windows.create(options); chrome.windows.create(options);
} else { } else {
@ -204,6 +204,6 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
}); });
document.getElementById("disableAll").addEventListener("change", function(event) { document.getElementById("disableAll").addEventListener("change", function(event) {
installed.classList.toggle("disabled", prefs.getPref("disableAll")); installed.classList.toggle("disabled", prefs.get("disableAll"));
}); });
loadPrefs(["disableAll"]); setupLivePrefs(["disableAll"]);

View File

@ -136,13 +136,14 @@ function isCheckbox(el) {
return el.nodeName.toLowerCase() == "input" && "checkbox" == el.type.toLowerCase(); return el.nodeName.toLowerCase() == "input" && "checkbox" == el.type.toLowerCase();
} }
// Accepts an array of pref names (values are fetched via prefs.getPref) // Accepts an array of pref names (values are fetched via prefs.get)
function loadPrefs(IDs) { // and establishes a two-way connection between the document elements and the actual prefs
function setupLivePrefs(IDs) {
var localIDs = {}; var localIDs = {};
IDs.forEach(function(id) { IDs.forEach(function(id) {
localIDs[id] = true; localIDs[id] = true;
updateElement(id).addEventListener("change", function() { updateElement(id).addEventListener("change", function() {
prefs.setPref(this.id, isCheckbox(this) ? this.checked : this.value); prefs.set(this.id, isCheckbox(this) ? this.checked : this.value);
}); });
}); });
chrome.extension.onMessage.addListener(function(request) { chrome.extension.onMessage.addListener(function(request) {
@ -152,7 +153,7 @@ function loadPrefs(IDs) {
}); });
function updateElement(id) { function updateElement(id) {
var el = document.getElementById(id); var el = document.getElementById(id);
el[isCheckbox(el) ? "checked" : "value"] = prefs.getPref(id); el[isCheckbox(el) ? "checked" : "value"] = prefs.get(id);
el.dispatchEvent(new Event("change", {bubbles: true, cancelable: true})); el.dispatchEvent(new Event("change", {bubbles: true, cancelable: true}));
return el; return el;
} }
@ -200,7 +201,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
Object.defineProperty(this, "readOnlyValues", {value: {}}); Object.defineProperty(this, "readOnlyValues", {value: {}});
Prefs.prototype.getPref = function(key, defaultValue) { Prefs.prototype.get = function(key, defaultValue) {
if (key in values) { if (key in values) {
return values[key]; return values[key];
} }
@ -213,11 +214,11 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
console.warn("No default preference for '%s'", key); console.warn("No default preference for '%s'", key);
}; };
Prefs.prototype.getAllPrefs = function(key) { Prefs.prototype.getAll = function(key) {
return deepCopy(values); return deepCopy(values);
}; };
Prefs.prototype.setPref = function(key, value, options) { Prefs.prototype.set = function(key, value, options) {
var oldValue = deepCopy(values[key]); var oldValue = deepCopy(values[key]);
values[key] = value; values[key] = value;
defineReadonlyProperty(this.readOnlyValues, key, value); defineReadonlyProperty(this.readOnlyValues, key, value);
@ -226,7 +227,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
} }
}; };
Prefs.prototype.removePref = function(key) { me.setPref(key, undefined) }; Prefs.prototype.remove = function(key) { me.set(key, undefined) };
Prefs.prototype.broadcast = function(key, value, options) { Prefs.prototype.broadcast = function(key, value, options) {
var message = {method: "prefChanged", prefName: key, value: value}; var message = {method: "prefChanged", prefName: key, value: value};
@ -244,18 +245,18 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
}; };
Object.keys(defaults).forEach(function(key) { Object.keys(defaults).forEach(function(key) {
me.setPref(key, defaults[key], {noBroadcast: true}); me.set(key, defaults[key], {noBroadcast: true});
}); });
chrome.storage.sync.get("settings", function(result) { chrome.storage.sync.get("settings", function(result) {
var synced = result.settings; var synced = result.settings;
for (var key in defaults) { for (var key in defaults) {
if (synced && (key in synced)) { if (synced && (key in synced)) {
me.setPref(key, synced[key], {noSync: true}); me.set(key, synced[key], {noSync: true});
} else { } else {
var value = tryMigrating(key); var value = tryMigrating(key);
if (value !== undefined) { if (value !== undefined) {
me.setPref(key, value); me.set(key, value);
} }
} }
} }
@ -267,7 +268,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
if (synced) { if (synced) {
for (key in defaults) { for (key in defaults) {
if (key in synced) { if (key in synced) {
me.setPref(key, synced[key], {noSync: true}); me.set(key, synced[key], {noSync: true});
} }
} }
} else { } else {