TeaWeb/js/settings.ts

97 lines
3 KiB
TypeScript
Raw Normal View History

2018-02-27 17:20:49 +01:00
/// <reference path="client.ts" />
2018-03-24 23:38:01 +01:00
class X_Properties extends HTMLElement {}
class X_Property extends HTMLElement {}
customElements.define('x-properties', X_Properties, { extends: 'div' });
customElements.define('x-property', X_Property, { extends: 'div' });
2018-02-27 17:20:49 +01:00
class Settings {
handle: TSClient;
private static readonly UPDATE_DIRECT: boolean = true;
private cacheGlobal = {};
private cacheServer = {};
private saveWorker: NodeJS.Timer;
private updated: boolean = false;
2018-03-24 23:38:01 +01:00
private _staticPropsTag: JQuery;
2018-02-27 17:20:49 +01:00
constructor(handle: TSClient) {
this.handle = handle;
2018-03-24 23:38:01 +01:00
this._staticPropsTag = $("#properties");
2018-02-27 17:20:49 +01:00
this.cacheGlobal = JSON.parse(localStorage.getItem("settings.global"));
if(!this.cacheGlobal) this.cacheGlobal = {};
const _this = this;
this.saveWorker = setInterval(() => {
if(_this.updated)
_this.save();
}, 5 * 1000);
}
2018-03-07 19:06:52 +01:00
global?(key: string, _default?: string) : string {
let result = this.cacheGlobal[key];
return result ? result : _default;
2018-02-27 17:20:49 +01:00
}
2018-04-11 17:56:09 +02:00
server?(key: string, _default?: string) : string {
let result = this.cacheServer[key];
return result ? result : _default;
2018-02-27 17:20:49 +01:00
}
changeGlobal(key: string, value?: string){
2018-03-07 19:06:52 +01:00
if(this.cacheGlobal[key] == value) return;
2018-02-27 17:20:49 +01:00
this.updated = true;
this.cacheGlobal[key] = value;
if(Settings.UPDATE_DIRECT)
this.save();
}
changeServer(key: string, value?: string) {
2018-03-07 19:06:52 +01:00
if(this.cacheServer[key] == value) return;
2018-02-27 17:20:49 +01:00
this.updated = true;
this.cacheServer[key] = value;
if(Settings.UPDATE_DIRECT)
this.save();
}
loadServer() {
2018-04-11 17:56:09 +02:00
if(!this.handle.channelTree.server) {
2018-03-07 19:06:52 +01:00
this.cacheServer = {};
console.warn("[Settings] tried to load settings for unknown server");
return;
}
2018-02-27 17:20:49 +01:00
let serverId = this.handle.channelTree.server.properties.virtualserver_unique_identifier;
this.cacheServer = JSON.parse(localStorage.getItem("settings.server_" + serverId));
if(!this.cacheServer)
this.cacheServer = {};
}
save() {
this.updated = false;
2018-03-07 19:06:52 +01:00
if(this.handle.channelTree.server) {
let serverId = this.handle.channelTree.server.properties.virtualserver_unique_identifier;
let server = JSON.stringify(this.cacheServer);
localStorage.setItem("settings.server_" + serverId, server);
}
let global = JSON.stringify(this.cacheGlobal);
2018-02-27 17:20:49 +01:00
localStorage.setItem("settings.global", global);
}
2018-03-24 23:38:01 +01:00
static?(key: string, _default: string = undefined) : string {
let result = this._staticPropsTag.find("[key='" + key + "']");
if(result.length == 0) return _default;
2018-04-11 17:56:09 +02:00
return decodeURIComponent(result.attr("value"));
}
deleteStatic(key: string) {
let result = this._staticPropsTag.find("[key='" + key + "']");
if(result.length != 0) result.detach();
2018-03-24 23:38:01 +01:00
}
2018-02-27 17:20:49 +01:00
}