audio-vis/raw/javascript/config.js

48 lines
1.0 KiB
JavaScript

class Config {
static allConfigs = {};
constructor(type) {
this.config = {};
this.name = ''
this.type = type;
Config.allConfigs[type] = this;
}
loadConfigByName(name) {
this.save();
this.name = 'config-' + name;
let item = localStorage.getItem(this.name);
if (item) {
this.config = JSON.parse(item);
}
}
save() {
if (this.name !== '') {
localStorage.setItem(this.name, JSON.stringify(this.config));
}
}
set(name, value) {
this.config[name] = value;
}
remove(name) {
delete this.config[name];
}
get(name, def) {
let value = this.config[name];
if (value === undefined || value === null) {
this.config[name] = def;
value = def;
}
return value;
}
reset() {
NotificationHandler.createNotification(`CONFIG REQUEST SUCCESS FOR ${this.type}`, "success", 2000);
this.config = {};
this.save();
}
}