audio-vis/raw/javascript/config.js

48 lines
1 KiB
JavaScript
Raw Normal View History

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