1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01:00

Separated setting containers

For #166
This commit is contained in:
kkapsner 2018-09-16 01:22:40 +02:00
parent 57ef8a4166
commit ee7f9b4dae
8 changed files with 170 additions and 126 deletions

View File

@ -10,6 +10,7 @@
<script src="../lib/require.js"></script> <script src="../lib/require.js"></script>
<script src="../lib/logging.js"></script> <script src="../lib/logging.js"></script>
<script src="../lib/settingDefinitions.js"></script> <script src="../lib/settingDefinitions.js"></script>
<script src="../lib/settingContainers.js"></script>
<script src="../lib/settings.js"></script> <script src="../lib/settings.js"></script>
<script src="browserAction.js"></script> <script src="browserAction.js"></script>
</body> </body>

138
lib/settingContainers.js Normal file
View File

@ -0,0 +1,138 @@
/* 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/. */
(function(){
"use strict";
var scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
scope = {};
window.scope.settingContainers = scope;
}
const logging = require("./logging");
scope.urlContainer = null;
scope.hideContainer = null;
scope.expandContainer = null;
scope.getUrlValueContainer = function(name, url){
var matching = scope.urlContainer.get().filter(function(urlSetting){
return urlSetting.hasOwnProperty(name);
}).filter(function(urlSetting){
return urlSetting.match(url);
});
if (matching.length){
return matching[0];
}
else {
return null;
}
};
scope.setUrlValue = function(name, value, url){
var urlContainerValue = scope.urlContainer.get();
var matching = urlContainerValue.filter(function(urlSetting){
return urlSetting.match(url);
});
if (!matching.length){
let newEntry = {url};
newEntry[name] = value;
urlContainerValue.push(newEntry);
matching = [newEntry];
}
matching[0][name] = value;
return scope.urlContainer.set(urlContainerValue);
};
scope.resetUrlValue = function(name, url){
var urlContainerValue = scope.urlContainer.get();
var matching = urlContainerValue.filter(function(urlSetting){
return urlSetting.match(url);
});
if (matching.length){
delete matching[0][name];
if (Object.keys(matching[0]).every(function(key){return key === "url";})){
urlContainerValue = urlContainerValue.filter(function(urlSetting){
return urlSetting !== matching[0];
});
}
scope.urlContainer.set(urlContainerValue);
}
};
scope.check = function(settingDefinition){
if (settingDefinition.isUrlContainer){
scope.urlContainer = settingDefinition;
settingDefinition.refresh = function(){
settingDefinition.set(settingDefinition.get());
};
}
if (settingDefinition.isHideContainer){
scope.hideContainer = settingDefinition;
let changeListeners = {};
settingDefinition.setHideByName = function(name, value){
logging.verbose("set hide of", name, "to", value);
const hideStore = settingDefinition.get();
hideStore[name] = value;
settingDefinition.set(hideStore);
(changeListeners[name] || []).forEach(function(listener){
listener(value);
});
};
settingDefinition.getHideByName = function(name){
const hideStore = settingDefinition.get();
return hideStore[name] || false;
};
settingDefinition.onHideChange = function(name, listener){
if (!changeListeners[name]){
changeListeners[name] = [];
}
changeListeners[name].push(listener);
};
settingDefinition.on(function(event){
const value = event.newValue;
Object.keys(value).forEach(function(name){
(changeListeners[name] || []).forEach(function(listener){
listener(value[name]);
});
});
});
settingDefinition.hideAble = false;
}
if (settingDefinition.isExpandContainer){
scope.expandContainer = settingDefinition;
let changeListeners = {};
settingDefinition.setExpandByName = function(name, value){
logging.verbose("set expand of", name, "to", value);
const expandStore = settingDefinition.get();
expandStore[name] = value;
settingDefinition.set(expandStore);
(changeListeners[name] || []).forEach(function(listener){
listener(value);
});
};
settingDefinition.getExpandByName = function(name){
const expandStore = settingDefinition.get();
return expandStore[name] || false;
};
settingDefinition.onExpandChange = function(name, listener){
if (!changeListeners[name]){
changeListeners[name] = [];
}
changeListeners[name].push(listener);
};
settingDefinition.on(function(event){
const value = event.newValue;
Object.keys(value).forEach(function(name){
(changeListeners[name] || []).forEach(function(listener){
listener(value[name]);
});
});
});
}
};
}());

View File

@ -13,19 +13,19 @@
{ {
name: "urlSettings", name: "urlSettings",
defaultValue: [], defaultValue: [],
urlContainer: true, isUrlContainer: true,
entries: [ entries: [
{name: "url", defaultValue: ""} {name: "url", defaultValue: ""}
] ]
}, },
{ {
name: "hiddenSettings", name: "hiddenSettings",
hideContainer: true, isHideContainer: true,
defaultValue: {} defaultValue: {}
}, },
{ {
name: "expandStatus", name: "expandStatus",
expandContainer: true, isExpandContainer: true,
defaultValue: {} defaultValue: {}
}, },
{ {

View File

@ -15,6 +15,7 @@
const logging = require("./logging"); const logging = require("./logging");
const settingDefinitions = require("./settingDefinitions.js"); const settingDefinitions = require("./settingDefinitions.js");
const settingContainers = require("./settingContainers");
const definitionsByName = {}; const definitionsByName = {};
const defaultSymbol = ""; const defaultSymbol = "";
@ -22,9 +23,6 @@
eventHandler.any[defaultSymbol] = []; eventHandler.any[defaultSymbol] = [];
eventHandler.all = eventHandler.any; eventHandler.all = eventHandler.any;
const settings = {}; const settings = {};
let urlContainer;
let hideContainer;
let expandContainer;
function isDefinitionInvalid(settingDefinition, newValue){ function isDefinitionInvalid(settingDefinition, newValue){
if (newValue === undefined && settingDefinition.optional){ if (newValue === undefined && settingDefinition.optional){
@ -77,13 +75,9 @@
else if (settingDefinition.urlSpecific){ else if (settingDefinition.urlSpecific){
return function getValue(url){ return function getValue(url){
if (url){ if (url){
var matching = urlContainer.get().filter(function(urlSetting){ var match = settingContainers.getUrlValueContainer(settingDefinition.name, url);
return urlSetting.hasOwnProperty(settingDefinition.name); if (match){
}).filter(function(urlSetting){ return match[settingDefinition.name];
return urlSetting.match(url);
});
if (matching.length){
return matching[0][settingDefinition.name];
} }
} }
return settings[settingDefinition.name]; return settings[settingDefinition.name];
@ -147,18 +141,7 @@
logging.verbose("New value for %s (%s):", name, url, newValue); logging.verbose("New value for %s (%s):", name, url, newValue);
if (isValid(newValue)){ if (isValid(newValue)){
if (url){ if (url){
var urlContainerValue = urlContainer.get(); return settingContainers.setUrlValue(name, newValue, url);
var matching = urlContainerValue.filter(function(urlSetting){
return urlSetting.match(url);
});
if (!matching.length){
let newEntry = {url};
newEntry[settingDefinition.name] = newValue;
urlContainerValue.push(newEntry);
matching = [newEntry];
}
matching[0][settingDefinition.name] = newValue;
return urlContainer.set(urlContainerValue);
} }
else { else {
return storeValue(newValue); return storeValue(newValue);
@ -196,19 +179,7 @@
if (settingDefinition.urlSpecific){ if (settingDefinition.urlSpecific){
return function(url){ return function(url){
if (url){ if (url){
var urlContainerValue = urlContainer.get(); settingContainers.resetUrlValue(name, url);
var matching = urlContainerValue.filter(function(urlSetting){
return urlSetting.match(url);
});
if (matching.length){
delete matching[0][name];
if (Object.keys(matching[0]).every(function(key){return key === "url";})){
urlContainerValue = urlContainerValue.filter(function(urlSetting){
return urlSetting !== matching[0];
});
}
urlContainer.set(urlContainerValue);
}
} }
else { else {
reset(); reset();
@ -244,13 +215,6 @@
}; };
settingDefinitions.forEach(function(settingDefinition){ settingDefinitions.forEach(function(settingDefinition){
if (settingDefinition.urlContainer){
urlContainer = settingDefinition;
settingDefinition.refresh = function(){
settingDefinition.set(settingDefinition.get());
};
}
var name = settingDefinition.name; var name = settingDefinition.name;
definitionsByName[name] = settingDefinition; definitionsByName[name] = settingDefinition;
if (typeof settingDefinition.defaultValue === "function"){ if (typeof settingDefinition.defaultValue === "function"){
@ -281,14 +245,14 @@
settingDefinition.reset = createResetter(settingDefinition); settingDefinition.reset = createResetter(settingDefinition);
if (settingDefinition.urlSpecific){ if (settingDefinition.urlSpecific){
if (!urlContainer){ if (!settingContainers.urlContainer){
logging.error("Unable to use url specific settings without url-container"); logging.error("Unable to use url specific settings without url-container");
} }
else { else {
settingDefinition.urlContainer = urlContainer; settingDefinition.urlContainer = settingContainers.urlContainer;
let entry = Object.create(settingDefinition); let entry = Object.create(settingDefinition);
entry.optional = true; entry.optional = true;
urlContainer.entries.push(entry); settingContainers.urlContainer.entries.push(entry);
} }
} }
@ -302,74 +266,7 @@
} }
); );
if (settingDefinition.hideContainer){ settingContainers.check(settingDefinition);
hideContainer = settingDefinition;
let changeListeners = {};
settingDefinition.setHideByName = function(name, value){
logging.verbose("set hide of", name, "to", value);
const hideStore = settingDefinition.get();
hideStore[name] = value;
settingDefinition.set(hideStore);
(changeListeners[name] || []).forEach(function(listener){
listener(value);
});
};
settingDefinition.getHideByName = function(name){
const hideStore = settingDefinition.get();
return hideStore[name] || false;
};
settingDefinition.onHideChange = function(name, listener){
if (!changeListeners[name]){
changeListeners[name] = [];
}
changeListeners[name].push(listener);
};
settingDefinition.on(function(event){
const value = event.newValue;
Object.keys(value).forEach(function(name){
if (value[name]){
(changeListeners[name] || []).forEach(function(listener){
listener(true);
});
}
});
});
settingDefinition.hideAble = false;
}
if (settingDefinition.expandContainer){
expandContainer = settingDefinition;
let changeListeners = {};
settingDefinition.setExpandByName = function(name, value){
logging.verbose("set expand of", name, "to", value);
const expandStore = settingDefinition.get();
expandStore[name] = value;
settingDefinition.set(expandStore);
(changeListeners[name] || []).forEach(function(listener){
listener(value);
});
};
settingDefinition.getExpandByName = function(name){
const expandStore = settingDefinition.get();
return expandStore[name] || false;
};
settingDefinition.onExpandChange = function(name, listener){
if (!changeListeners[name]){
changeListeners[name] = [];
}
changeListeners[name].push(listener);
};
settingDefinition.on(function(event){
const value = event.newValue;
Object.keys(value).forEach(function(name){
if (value[name]){
(changeListeners[name] || []).forEach(function(listener){
listener(true);
});
}
});
});
}
}); });
scope.getDefinition = function(name){ scope.getDefinition = function(name){
@ -384,9 +281,9 @@
scope.getContainers = function(){ scope.getContainers = function(){
return { return {
url: Object.create(urlContainer), url: Object.create(settingContainers.urlContainer),
hide: Object.create(hideContainer), hide: Object.create(settingContainers.hideContainer),
expand: Object.create(expandContainer) expand: Object.create(settingContainers.expandContainer)
}; };
}; };
@ -431,7 +328,7 @@
}); });
if (settingDefinition.urlSpecific){ if (settingDefinition.urlSpecific){
urlContainer.get().forEach(function(entry){ settingContainers.urlContainer.get().forEach(function(entry){
if (!entry.hasOwnProperty(name)){ if (!entry.hasOwnProperty(name)){
((eventHandler[name] || {})[entry.url] || []).forEach(function(callback){ ((eventHandler[name] || {})[entry.url] || []).forEach(function(callback){
callback({name, newValue, oldValue, url: entry.url}); callback({name, newValue, oldValue, url: entry.url});
@ -449,7 +346,7 @@
var delayedChange = []; var delayedChange = [];
Object.entries(changes).forEach(function(entry){ Object.entries(changes).forEach(function(entry){
const [name, change] = entry; const [name, change] = entry;
if (urlContainer && name === urlContainer.name){ if (settingContainers.urlContainer && name === settingContainers.urlContainer.name){
// changes in the url container have to trigger after the other changes // changes in the url container have to trigger after the other changes
delayedChange.push(entry); delayedChange.push(entry);
} }
@ -477,8 +374,8 @@
} }
}); });
if (urlContainer){ if (settingContainers.urlContainer){
urlContainer.on(function({newValue, oldValue}){ settingContainers.urlContainer.on(function({newValue, oldValue}){
newValue.forEach(function(urlSetting){ newValue.forEach(function(urlSetting){
var regExp; var regExp;
var domain = !!urlSetting.url.match(/^[A-Za-z0-9_.-]+$/); var domain = !!urlSetting.url.match(/^[A-Za-z0-9_.-]+$/);
@ -534,7 +431,7 @@
Object.keys(matching).forEach(function(url){ Object.keys(matching).forEach(function(url){
var oldEntry = oldValue[matching[url].old] || {}; var oldEntry = oldValue[matching[url].old] || {};
var newEntry = newValue[matching[url].new] || {}; var newEntry = newValue[matching[url].new] || {};
urlContainer.entries.forEach(function(settingDefinition){ settingContainers.urlContainer.entries.forEach(function(settingDefinition){
var name = settingDefinition.name; var name = settingDefinition.name;
var oldValue = oldEntry[name]; var oldValue = oldEntry[name];
var newValue = newEntry[name]; var newValue = newEntry[name];
@ -564,12 +461,15 @@
logging.message("settings loaded"); logging.message("settings loaded");
if (require.exists("./settingsMigration")){ if (require.exists("./settingsMigration")){
const settingsMigration = require("./settingsMigration"); const settingsMigration = require("./settingsMigration");
settingsMigration.check(storage, {settings, logging, changeValue, urlContainer}); settingsMigration.check(
storage,
{settings, logging, changeValue, urlContainer: settingContainers.urlContainer}
);
} }
var delayedChange = []; var delayedChange = [];
Object.entries(storage).forEach(function(entry){ Object.entries(storage).forEach(function(entry){
const [name, value] = entry; const [name, value] = entry;
if (urlContainer && name === urlContainer.name){ if (settingContainers.urlContainer && name === settingContainers.urlContainer.name){
// changes in the url container have to trigger after the other changes // changes in the url container have to trigger after the other changes
delayedChange.push(entry); delayedChange.push(entry);
} }

View File

@ -12,6 +12,7 @@
"lib/require.js", "lib/require.js",
"lib/logging.js", "lib/logging.js",
"lib/settingDefinitions.js", "lib/settingDefinitions.js",
"lib/settingContainers.js",
"lib/settingsMigration.js", "lib/settingsMigration.js",
"lib/settings.js", "lib/settings.js",
"lib/lists.js", "lib/lists.js",
@ -31,6 +32,7 @@
"lib/logging.js", "lib/logging.js",
"lib/settingDefinitions.js", "lib/settingDefinitions.js",
"lib/settingContainers.js",
"lib/settings.js", "lib/settings.js",
"lib/colorStatistics.js", "lib/colorStatistics.js",

View File

@ -10,6 +10,7 @@
<script src="../lib/require.js"></script> <script src="../lib/require.js"></script>
<script src="../lib/logging.js"></script> <script src="../lib/logging.js"></script>
<script src="../lib/settingDefinitions.js"></script> <script src="../lib/settingDefinitions.js"></script>
<script src="../lib/settingContainers.js"></script>
<script src="../lib/settings.js"></script> <script src="../lib/settings.js"></script>
<script src="export.js"></script> <script src="export.js"></script>
</body> </body>

View File

@ -9,6 +9,7 @@
<script src="../lib/require.js"></script> <script src="../lib/require.js"></script>
<script src="../lib/logging.js"></script> <script src="../lib/logging.js"></script>
<script src="../lib/settingDefinitions.js"></script> <script src="../lib/settingDefinitions.js"></script>
<script src="../lib/settingContainers.js"></script>
<script src="../lib/settings.js"></script> <script src="../lib/settings.js"></script>
<script src="optionsGui.js"></script> <script src="optionsGui.js"></script>
<script src="settingsDisplay.js"></script> <script src="settingsDisplay.js"></script>

View File

@ -13,6 +13,7 @@
<script src="../lib/require.js"></script> <script src="../lib/require.js"></script>
<script src="../lib/logging.js"></script> <script src="../lib/logging.js"></script>
<script src="../lib/settingDefinitions.js"></script> <script src="../lib/settingDefinitions.js"></script>
<script src="../lib/settingContainers.js"></script>
<script src="../lib/settings.js"></script> <script src="../lib/settings.js"></script>
<script src="../lib/lists.js"></script> <script src="../lib/lists.js"></script>
<script src="../lib/callingStack.js"></script> <script src="../lib/callingStack.js"></script>