Verified Commit 74e6c6fa authored by Thomas Rientjes's avatar Thomas Rientjes
Browse files

Refactor options page logic

parent 440def81
......@@ -28,7 +28,7 @@ options._renderContents = function () {
document.body.setAttribute('dir', options._scriptDirection);
helpers.insertI18nContentIntoDocument(document);
options._determineOptionDetails()
options._determineOptionValues()
.then(options._renderOptionsPanel);
};
......@@ -37,17 +37,10 @@ options._renderOptionsPanel = function () {
let whitelistedDomains, domainWhitelist, elements;
whitelistedDomains = options._optionValues.whitelistedDomains;
domainWhitelist = '';
domainWhitelist = options._serializeWhitelistedDomains(whitelistedDomains);
elements = options._optionElements;
Object.keys(whitelistedDomains).forEach(function (domain) {
domainWhitelist = `${domainWhitelist}${domain};`;
});
domainWhitelist = domainWhitelist.slice(0, -1);
domainWhitelist = domainWhitelist.replace(Whitelist.TRIM_EXPRESSION, '');
elements.showIconBadge.checked = options._optionValues.showIconBadge;
elements.blockMissing.checked = options._optionValues.blockMissing;
elements.disablePrefetch.checked = options._optionValues.disablePrefetch;
......@@ -76,10 +69,26 @@ options._registerOptionChangedEventListeners = function (elements) {
elements.whitelistedDomains.addEventListener('keyup', options._onOptionChanged);
};
options._determineOptionDetails = function () {
options._determineOptionValues = function () {
return new Promise((resolve) => {
let optionKeys = Object.keys(options._optionElements);
chrome.storage.local.get(optionKeys, function (items) {
options._optionValues = items;
resolve();
});
});
};
options._getOptionElement = function (optionKey) {
return document.querySelector(`[data-option=${optionKey}]`);
};
options._getOptionElements = function () {
let optionElements = {
'showIconBadge': options._getOptionElement(Setting.SHOW_ICON_BADGE),
'blockMissing': options._getOptionElement(Setting.BLOCK_MISSING),
......@@ -88,18 +97,50 @@ options._determineOptionDetails = function () {
'whitelistedDomains': options._getOptionElement(Setting.WHITELISTED_DOMAINS)
};
chrome.storage.local.get(Object.keys(optionElements), function (items) {
return optionElements;
};
options._configureLinkPrefetching = function (value) {
options._optionElements = optionElements;
options._optionValues = items;
if (value === false) {
resolve();
// Restore default values of related preference values.
chrome.privacy.network.networkPredictionEnabled.clear({});
} else {
chrome.privacy.network.networkPredictionEnabled.set({
'value': false
});
}
};
options._serializeWhitelistedDomains = function (whitelistedDomains) {
let domainWhitelist, whitelistedDomainKeys;
whitelistedDomainKeys = Object.keys(whitelistedDomains);
domainWhitelist = '';
whitelistedDomainKeys.forEach(function (domain) {
domainWhitelist = `${domainWhitelist}${domain};`;
});
domainWhitelist = domainWhitelist.slice(0, -1);
domainWhitelist = domainWhitelist.replace(Whitelist.TRIM_EXPRESSION, '');
return domainWhitelist;
};
options._getOptionElement = function (optionKey) {
return document.querySelector(`[data-option=${optionKey}]`);
options._parseDomainWhitelist = function (domainWhitelist) {
let whitelistedDomains = {};
domainWhitelist.split(Whitelist.VALUE_SEPARATOR).forEach(function (domain) {
whitelistedDomains[helpers.normalizeDomain(domain)] = true;
});
return whitelistedDomains;
};
/**
......@@ -110,6 +151,7 @@ options._onDocumentLoaded = function () {
let language = navigator.language;
options._optionElements = options._getOptionElements();
options._languageSupported = helpers.languageIsFullySupported(language);
options._scriptDirection = helpers.determineScriptDirection(language);
......@@ -132,29 +174,11 @@ options._onOptionChanged = function ({target}) {
}
if (optionKey === Setting.DISABLE_PREFETCH) {
if (optionValue === false) {
// Restore default values of related preference values.
chrome.privacy.network.networkPredictionEnabled.clear({});
} else {
chrome.privacy.network.networkPredictionEnabled.set({
'value': false
});
}
options._configureLinkPrefetching(optionValue);
}
if (optionKey === Setting.WHITELISTED_DOMAINS) {
let domainWhitelist = optionValue;
optionValue = {};
domainWhitelist.split(Whitelist.VALUE_SEPARATOR).forEach(function (domain) {
optionValue[helpers.normalizeDomain(domain)] = true;
});
optionValue = options._parseDomainWhitelist(optionValue);
}
chrome.storage.local.set({
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment