web-api-manager/add-on/config/js/state.js

74 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-10-12 19:00:04 +00:00
(function () {
2017-10-14 20:24:18 +00:00
"use strict";
2017-10-12 19:00:04 +00:00
const defaultDomain = "(default)";
2017-10-12 19:00:04 +00:00
const generateStateObject = function (initialDomain, standards) {
2017-10-12 19:00:04 +00:00
const state = {
selectedDomain: initialDomain,
standards: standards,
domainRules: {},
domainNames: [],
shouldLog: false,
2017-10-12 19:00:04 +00:00
selectedStandards: [],
2017-10-14 20:24:18 +00:00
toStorage: function () {
return {
domainRules: this.domainRules,
shouldLog: this.shouldLog
};
},
populateFromStorage: function (storedValues) {
this.setDomainRules(storedValues.domainRules);
this.setShouldLog(storedValues.shouldLog);
},
2017-10-12 19:00:04 +00:00
setDomainRules: function (newDomainRules) {
this.domainRules = newDomainRules;
this.domainNames = Object.keys(newDomainRules);
this.selectedStandards = this.domainRules[this.selectedDomain];
},
2017-10-14 20:24:18 +00:00
2017-10-12 19:00:04 +00:00
setSelectedDomain: function (newDomain) {
this.selectedDomain = newDomain;
this.selectedStandards = this.domainRules[newDomain];
},
2017-10-14 20:24:18 +00:00
2017-10-12 19:00:04 +00:00
setSelectedStandards: function (selectedStandards) {
this.selectedStandards = selectedStandards;
this.domainRules[this.selectedDomain] = selectedStandards;
},
2017-10-14 20:24:18 +00:00
2017-10-12 19:00:04 +00:00
deleteDomainRule: function (domainToDelete) {
2017-10-14 20:24:18 +00:00
2017-10-12 19:00:04 +00:00
// If we're deleted the domain thats currently selected, then
// select the default domain.
if (this.selectedDomain === domainToDelete) {
this.setSelectedDomain(defaultDomain);
2017-10-12 19:00:04 +00:00
}
delete this.domainRules[domainToDelete];
this.domainNames = Object.keys(this.domainRules);
2017-10-12 19:00:04 +00:00
},
addDomainRule: function (newDomainRule) {
this.domainRules[newDomainRule] = [];
this.domainNames = Object.keys(this.domainRules);
this.selectedDomain = newDomainRule;
this.selectedStandards = this.domainRules[newDomainRule];
},
setShouldLog: function (shouldLog) {
this.shouldLog = shouldLog;
2017-10-12 19:00:04 +00:00
}
};
return state;
};
window.WEB_API_MANAGER.stateLib = {
generateStateObject
};
2017-10-12 19:00:04 +00:00
}());