1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-11 23:58:52 +01:00
CanvasBlocker/options/options.js

203 lines
5.9 KiB
JavaScript
Raw Normal View History

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
2017-10-03 15:35:31 +02:00
(function(){
"use strict";
const logging = require("./logging");
2017-11-07 00:36:44 +01:00
logging.setPrefix("options page");
const optionsGui = require("./optionsGui");
const settings = require("./settings");
const settingsDisplay = require("./settingsDisplay");
2017-10-03 15:35:31 +02:00
2017-11-07 00:36:44 +01:00
var callbacks = {
showReleaseNotes: function(){
logging.verbose("open release notes");
window.open("../releaseNotes.txt", "_blank");
},
clearPersistentRnd: function(){
logging.message("clear persistent rnd storage");
logging.notice("empty storage");
settings.persistentRndStorage = "";
logging.notice("send message to main script");
browser.runtime.sendMessage({"canvasBlocker-clear-domain-rnd": true});
2017-11-14 01:07:27 +01:00
},
exportSettings: function(){
logging.verbose("open settings export");
window.open("export.html", "_blank");
},
resetSettings: function(){
if (window.confirm(browser.i18n.getMessage("resetSettings_confirm"))){
browser.storage.local.clear();
}
2017-06-25 22:11:28 +02:00
}
2017-11-07 00:36:44 +01:00
};
if (window === window.top){
let head = document.createElement("header");
let heading = document.createElement("h1");
heading.textContent = browser.i18n.getMessage("options");
head.appendChild(heading);
let introduction = document.createElement("div");
introduction.textContent = browser.i18n.getMessage("optionsIntroduction");
head.appendChild(introduction);
if (window.location.search){
let noticeName = window.location.search.substr(1).trim() + "Notice";
let notice = browser.i18n.getMessage(noticeName);
if (notice){
let bookmarkingNotice = document.createElement("div");
bookmarkingNotice.className = noticeName + " bookmarkNotice";
bookmarkingNotice.textContent = notice;
head.appendChild(bookmarkingNotice);
}
}
document.body.appendChild(head);
document.body.classList.add("standalone");
}
2017-11-07 00:36:44 +01:00
var table = document.createElement("table");
2017-11-11 23:34:59 +01:00
table.className = "settings " + (settings.displayDescriptions? "display": "hide") + "Descriptions";
settings.on("displayDescriptions", function(){
table.className = "settings " + (settings.displayDescriptions? "display": "hide") + "Descriptions";
});
2017-11-07 00:36:44 +01:00
document.body.appendChild(table);
2017-12-24 21:52:44 +01:00
2018-07-02 00:29:41 +02:00
const displayHidden = settings.getDefinition(settingsDisplay.displayHidden);
table.appendChild(optionsGui.createThead(displayHidden));
2017-12-24 21:52:44 +01:00
let lastSection = null;
let addSection = function addSection(name){
let body = document.createElement("tbody");
if (name){
let row = document.createElement("tr");
row.className = "section";
let cell = document.createElement("td");
2018-07-02 00:29:41 +02:00
cell.colSpan = 3;
2017-12-24 21:52:44 +01:00
row.appendChild(cell);
let heading = document.createElement("h2");
heading.textContent = browser.i18n.getMessage("section_" + name);
cell.appendChild(heading);
body.appendChild(row);
}
table.appendChild(body);
let rows = [];
let section = {
addRow: function(row){
rows.push(row);
body.appendChild(row);
},
updateDisplay: function(){
2018-01-08 17:08:36 +01:00
var anyVisible = false;
rows.forEach(function(row){
var isHidden = row.classList.contains("hidden");
if (!isHidden){
if (anyVisible){
row.classList.remove("firstVisible");
}
else {
anyVisible = true;
row.classList.add("firstVisible");
}
}
});
body.classList[anyVisible? "remove": "add"]("hidden");
2017-10-03 15:35:31 +02:00
}
2017-12-24 21:52:44 +01:00
};
lastSection = section;
};
addSection();
2018-07-02 00:29:41 +02:00
const {hide: hideContainer} = settings.getContainers();
2017-12-24 21:52:44 +01:00
settingsDisplay.forEach(function(display){
if (typeof display === "string"){
addSection(display);
2017-11-07 00:36:44 +01:00
}
2017-12-24 21:52:44 +01:00
else {
var setting = settings.getDefinition(display.name);
if (!setting){
if (display.inputs){
setting = {
name: display.name,
inputs: display.inputs.map(settings.getDefinition)
};
}
else if (callbacks[display.name]){
setting = {
name: display.name,
action: callbacks[display.name]
};
}
}
if (setting){
2018-07-02 00:29:41 +02:00
let hideChangeListeners = [];
setting.setHide = function setHide(value){
if (hideContainer){
hideContainer.setHideByName(display.name, value);
if (computeDependencies){
computeDependencies();
}
}
};
setting.onHideChange = function(listener){
hideChangeListeners.push(listener);
};
setting.getHide = function getHide(){
if (hideContainer){
return hideContainer.getHideByName(display.name);
}
else {
return false;
}
};
if (hideContainer){
hideContainer.onHideChange(display.name, function(value){
if (computeDependencies){
computeDependencies();
}
hideChangeListeners.forEach(function(listener){
listener(value);
});
});
}
2017-12-24 21:52:44 +01:00
var row = optionsGui.createSettingRow(setting);
let section = lastSection;
section.addRow(row);
2018-07-02 00:29:41 +02:00
if (!display.displayDependencies){
display.displayDependencies = {};
2017-12-24 21:52:44 +01:00
}
2018-07-02 00:29:41 +02:00
var displayDependencies = display.displayDependencies;
displayDependencies = Array.isArray(displayDependencies)?
displayDependencies:
[displayDependencies];
var computeDependencies = function(){
logging.verbose("evaluate display dependencies for", setting);
row.classList[(
(
displayHidden.get() ||
!setting.getHide()
) &&
displayDependencies.some(function(displayDependency){
return Object.keys(displayDependency).every(function(key){
return displayDependency[key].indexOf(settings[key]) !== -1;
});
})
)? "remove": "add"]("hidden");
section.updateDisplay();
};
computeDependencies();
displayDependencies.forEach(function(displayDependency){
Object.keys(displayDependency).forEach(function(name){
settings.on(name, computeDependencies);
});
});
displayHidden.on(computeDependencies);
2017-10-03 15:35:31 +02:00
}
2017-11-07 00:36:44 +01:00
}
2017-06-25 22:11:28 +02:00
});
2017-10-03 19:51:03 +02:00
}());