pdf.js/extensions/chromium/options/options.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

211 lines
6.9 KiB
JavaScript
Raw Normal View History

2015-01-07 02:22:35 +09:00
/*
Copyright 2015 Mozilla Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
"use strict";
var storageAreaName = chrome.storage.sync ? "sync" : "local";
var storageArea = chrome.storage[storageAreaName];
2015-01-07 02:22:35 +09:00
Promise.all([
new Promise(function getManagedPrefs(resolve) {
if (!chrome.storage.managed) {
resolve({});
return;
}
2015-01-07 02:22:35 +09:00
// Get preferences as set by the system administrator.
chrome.storage.managed.get(null, function (prefs) {
2015-01-07 02:22:35 +09:00
// Managed storage may be disabled, e.g. in Opera.
resolve(prefs || {});
});
}),
new Promise(function getUserPrefs(resolve) {
storageArea.get(null, function (prefs) {
2015-01-07 02:22:35 +09:00
resolve(prefs || {});
});
}),
new Promise(function getStorageSchema(resolve) {
// Get the storage schema - a dictionary of preferences.
var x = new XMLHttpRequest();
var schema_location = chrome.runtime.getManifest().storage.managed_schema;
x.open("get", chrome.runtime.getURL(schema_location));
x.onload = function () {
2015-01-07 02:22:35 +09:00
resolve(x.response.properties);
};
x.responseType = "json";
x.send();
}),
])
.then(function (values) {
2015-01-07 02:22:35 +09:00
var managedPrefs = values[0];
var userPrefs = values[1];
var schema = values[2];
function getPrefValue(prefName) {
if (prefName in userPrefs) {
return userPrefs[prefName];
} else if (prefName in managedPrefs) {
return managedPrefs[prefName];
2015-01-07 02:22:35 +09:00
}
return schema[prefName].default;
}
var prefNames = Object.keys(schema);
var renderPreferenceFunctions = {};
// Render options
prefNames.forEach(function (prefName) {
2015-01-07 02:22:35 +09:00
var prefSchema = schema[prefName];
if (!prefSchema.title) {
// Don't show preferences if the title is missing.
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
return;
}
2015-01-07 02:22:35 +09:00
// A DOM element with a method renderPreference.
var renderPreference;
if (prefSchema.type === "boolean") {
// Most prefs are booleans, render them in a generic way.
renderPreference = renderBooleanPref(
prefSchema.title,
prefSchema.description,
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
prefName
);
} else if (prefSchema.type === "integer" && prefSchema.enum) {
// Most other prefs are integer-valued enumerations, render them in a
// generic way too.
// Unlike the renderBooleanPref branch, each preference handled by this
// branch still needs its own template in options.html with
// id="$prefName-template".
renderPreference = renderEnumPref(prefSchema.title, prefName);
2015-01-07 02:22:35 +09:00
} else if (prefName === "defaultZoomValue") {
renderPreference = renderDefaultZoomValue(prefSchema.title);
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
} else {
2015-01-07 02:22:35 +09:00
// Should NEVER be reached. Only happens if a new type of preference is
// added to the storage manifest.
console.error("Don't know how to handle " + prefName + "!");
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
return;
}
2015-01-07 02:22:35 +09:00
renderPreference(getPrefValue(prefName));
renderPreferenceFunctions[prefName] = renderPreference;
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
});
2015-01-07 02:22:35 +09:00
// Names of preferences that are displayed in the UI.
var renderedPrefNames = Object.keys(renderPreferenceFunctions);
// Reset button to restore default settings.
document.getElementById("reset-button").onclick = function () {
2015-01-07 02:22:35 +09:00
userPrefs = {};
storageArea.remove(prefNames, function () {
renderedPrefNames.forEach(function (prefName) {
2015-01-07 02:22:35 +09:00
renderPreferenceFunctions[prefName](getPrefValue(prefName));
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
});
2015-01-07 02:22:35 +09:00
});
};
// Automatically update the UI when the preferences were changed elsewhere.
chrome.storage.onChanged.addListener(function (changes, areaName) {
var prefs = null;
if (areaName === storageAreaName) {
prefs = userPrefs;
} else if (areaName === "managed") {
prefs = managedPrefs;
}
2015-01-07 02:22:35 +09:00
if (prefs) {
renderedPrefNames.forEach(function (prefName) {
2015-01-07 02:22:35 +09:00
var prefChanges = changes[prefName];
if (prefChanges) {
if ("newValue" in prefChanges) {
userPrefs[prefName] = prefChanges.newValue;
} else {
// Otherwise the pref was deleted
delete userPrefs[prefName];
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
}
2015-01-07 02:22:35 +09:00
renderPreferenceFunctions[prefName](getPrefValue(prefName));
}
});
}
});
})
.then(null, console.error.bind(console));
function importTemplate(id) {
return document.importNode(document.getElementById(id).content, true);
}
// Helpers to create UI elements that display the preference, and return a
// function which updates the UI with the preference.
function renderBooleanPref(shortDescription, description, prefName) {
var wrapper = importTemplate("checkbox-template");
wrapper.title = description;
var checkbox = wrapper.querySelector('input[type="checkbox"]');
checkbox.onchange = function () {
2015-01-07 02:22:35 +09:00
var pref = {};
pref[prefName] = this.checked;
storageArea.set(pref);
2015-01-07 02:22:35 +09:00
};
wrapper.querySelector("span").textContent = shortDescription;
document.getElementById("settings-boxes").append(wrapper);
2015-01-07 02:22:35 +09:00
function renderPreference(value) {
checkbox.checked = value;
}
return renderPreference;
}
function renderEnumPref(shortDescription, prefName) {
var wrapper = importTemplate(prefName + "-template");
var select = wrapper.querySelector("select");
select.onchange = function () {
var pref = {};
pref[prefName] = parseInt(this.value);
storageArea.set(pref);
};
wrapper.querySelector("span").textContent = shortDescription;
document.getElementById("settings-boxes").append(wrapper);
function renderPreference(value) {
select.value = value;
}
return renderPreference;
}
2015-01-07 02:22:35 +09:00
function renderDefaultZoomValue(shortDescription) {
var wrapper = importTemplate("defaultZoomValue-template");
var select = wrapper.querySelector("select");
select.onchange = function () {
storageArea.set({
defaultZoomValue: this.value,
2015-01-07 02:22:35 +09:00
});
};
wrapper.querySelector("span").textContent = shortDescription;
document.getElementById("settings-boxes").append(wrapper);
2015-01-07 02:22:35 +09:00
function renderPreference(value) {
value = value || "auto";
select.value = value;
var customOption = select.querySelector("option.custom-zoom");
if (select.selectedIndex === -1 && value) {
// Custom zoom percentage, e.g. set via managed preferences.
// [zoom] or [zoom],[left],[top]
customOption.text = value.indexOf(",") > 0 ? value : value + "%";
customOption.value = value;
customOption.hidden = false;
customOption.selected = true;
} else {
customOption.hidden = true;
}
}
return renderPreference;
}