2018-02-27 16:20:49 +00:00
|
|
|
/// <reference path="client.ts" />
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
if(typeof(customElements) !== "undefined") {
|
2019-01-19 12:03:51 +00:00
|
|
|
try {
|
|
|
|
class X_Properties extends HTMLElement {}
|
|
|
|
class X_Property extends HTMLElement {}
|
2018-03-24 22:38:01 +00:00
|
|
|
|
2019-01-19 12:03:51 +00:00
|
|
|
customElements.define('x-properties', X_Properties, { extends: 'div' });
|
|
|
|
customElements.define('x-property', X_Property, { extends: 'div' });
|
|
|
|
} catch(error) {
|
|
|
|
console.warn("failed to define costum elements");
|
|
|
|
}
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
2018-03-24 22:38:01 +00:00
|
|
|
|
2018-04-19 17:46:47 +00:00
|
|
|
class StaticSettings {
|
|
|
|
private static _instance: StaticSettings;
|
|
|
|
static get instance() : StaticSettings {
|
|
|
|
if(!this._instance)
|
|
|
|
this._instance = new StaticSettings(true);
|
|
|
|
return this._instance;
|
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
|
2018-04-19 17:46:47 +00:00
|
|
|
protected static transformStO?<T>(input?: string, _default?: T) : T {
|
|
|
|
if (typeof input === "undefined") return _default;
|
|
|
|
if (typeof _default === "string") return input as any;
|
|
|
|
else if (typeof _default === "number") return parseInt(input) as any;
|
|
|
|
else if (typeof _default === "boolean") return (input == "1" || input == "true") as any;
|
|
|
|
else if (typeof _default === "undefined") return input as any;
|
|
|
|
return JSON.parse(input) as any;
|
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
|
2018-04-19 17:46:47 +00:00
|
|
|
protected static transformOtS?<T>(input: T) : string {
|
|
|
|
if (typeof input === "string") return input as string;
|
|
|
|
else if (typeof input === "number") return input.toString();
|
|
|
|
else if (typeof input === "boolean") return input ? "1" : "0";
|
2018-11-17 15:25:44 +00:00
|
|
|
else if (typeof input === "undefined") return undefined;
|
2018-04-19 17:46:47 +00:00
|
|
|
return JSON.stringify(input);
|
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
|
2018-04-19 17:46:47 +00:00
|
|
|
protected _handle: StaticSettings;
|
|
|
|
protected _staticPropsTag: JQuery;
|
2018-04-16 18:38:35 +00:00
|
|
|
|
2018-04-19 17:46:47 +00:00
|
|
|
protected constructor(_reserved = undefined) {
|
|
|
|
if(_reserved && !StaticSettings._instance) {
|
|
|
|
this._staticPropsTag = $("#properties");
|
|
|
|
this.initializeStatic();
|
|
|
|
} else {
|
|
|
|
this._handle = StaticSettings.instance;
|
|
|
|
}
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private initializeStatic() {
|
|
|
|
location.search.substr(1).split("&").forEach(part => {
|
|
|
|
let item = part.split("=");
|
2018-04-18 14:25:10 +00:00
|
|
|
$("<x-property></x-property>")
|
2018-04-16 18:38:35 +00:00
|
|
|
.attr("key", item[0])
|
|
|
|
.attr("value", item[1])
|
|
|
|
.appendTo(this._staticPropsTag);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:46:47 +00:00
|
|
|
static?<T>(key: string, _default?: T) : T {
|
|
|
|
if(this._handle) return this._handle.static<T>(key, _default);
|
|
|
|
let result = this._staticPropsTag.find("[key='" + key + "']");
|
|
|
|
return StaticSettings.transformStO(result.length > 0 ? decodeURIComponent(result.last().attr("value")) : undefined, _default);
|
2018-02-27 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 17:46:47 +00:00
|
|
|
deleteStatic(key: string) {
|
|
|
|
if(this._handle) {
|
|
|
|
this._handle.deleteStatic(key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let result = this._staticPropsTag.find("[key='" + key + "']");
|
|
|
|
if(result.length != 0) result.detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Settings extends StaticSettings {
|
|
|
|
static readonly KEY_DISABLE_CONTEXT_MENU = "disableContextMenu";
|
|
|
|
static readonly KEY_DISABLE_UNLOAD_DIALOG = "disableUnloadDialog";
|
2019-03-07 14:30:53 +00:00
|
|
|
static readonly KEY_DISABLE_VOICE = "disableVoice";
|
2018-04-19 17:46:47 +00:00
|
|
|
|
|
|
|
private static readonly UPDATE_DIRECT: boolean = true;
|
|
|
|
private cacheGlobal = {};
|
|
|
|
private cacheServer = {};
|
|
|
|
private currentServer: ServerEntry;
|
|
|
|
private saveWorker: NodeJS.Timer;
|
|
|
|
private updated: boolean = false;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.cacheGlobal = JSON.parse(localStorage.getItem("settings.global"));
|
|
|
|
if(!this.cacheGlobal) this.cacheGlobal = {};
|
|
|
|
this.saveWorker = setInterval(() => {
|
|
|
|
if(this.updated)
|
|
|
|
this.save();
|
|
|
|
}, 5 * 1000);
|
2018-04-16 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 14:52:26 +00:00
|
|
|
static_global?<T>(key: string, _default?: T) : T {
|
|
|
|
let _static = this.static<string>(key);
|
2018-09-23 13:24:07 +00:00
|
|
|
if(_static) return StaticSettings.transformStO(_static, _default);
|
2018-09-22 14:52:26 +00:00
|
|
|
return this.global<T>(key, _default);
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
global?<T>(key: string, _default?: T) : T {
|
2018-03-07 18:06:52 +00:00
|
|
|
let result = this.cacheGlobal[key];
|
2018-04-19 17:46:47 +00:00
|
|
|
return StaticSettings.transformStO(result, _default);
|
2018-02-27 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
server?<T>(key: string, _default?: T) : T {
|
2018-04-11 15:56:09 +00:00
|
|
|
let result = this.cacheServer[key];
|
2018-04-19 17:46:47 +00:00
|
|
|
return StaticSettings.transformStO(result, _default);
|
2018-02-27 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
changeGlobal<T>(key: string, value?: T){
|
2018-03-07 18:06:52 +00:00
|
|
|
if(this.cacheGlobal[key] == value) return;
|
|
|
|
|
2018-02-27 16:20:49 +00:00
|
|
|
this.updated = true;
|
2018-04-19 17:46:47 +00:00
|
|
|
this.cacheGlobal[key] = StaticSettings.transformOtS(value);
|
2018-02-27 16:20:49 +00:00
|
|
|
|
|
|
|
if(Settings.UPDATE_DIRECT)
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
changeServer<T>(key: string, value?: T) {
|
2018-03-07 18:06:52 +00:00
|
|
|
if(this.cacheServer[key] == value) return;
|
|
|
|
|
2018-02-27 16:20:49 +00:00
|
|
|
this.updated = true;
|
2018-04-19 17:46:47 +00:00
|
|
|
this.cacheServer[key] = StaticSettings.transformOtS(value);
|
2018-02-27 16:20:49 +00:00
|
|
|
|
|
|
|
if(Settings.UPDATE_DIRECT)
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
setServer(server: ServerEntry) {
|
|
|
|
if(this.currentServer) {
|
|
|
|
this.save();
|
2018-03-07 18:06:52 +00:00
|
|
|
this.cacheServer = {};
|
2018-04-16 18:38:35 +00:00
|
|
|
this.currentServer = undefined;
|
|
|
|
}
|
|
|
|
this.currentServer = server;
|
|
|
|
|
|
|
|
if(this.currentServer) {
|
|
|
|
let serverId = this.currentServer.properties.virtualserver_unique_identifier;
|
|
|
|
this.cacheServer = JSON.parse(localStorage.getItem("settings.server_" + serverId));
|
|
|
|
if(!this.cacheServer)
|
|
|
|
this.cacheServer = {};
|
2018-03-07 18:06:52 +00:00
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
this.updated = false;
|
|
|
|
|
2018-04-16 18:38:35 +00:00
|
|
|
if(this.currentServer) {
|
|
|
|
let serverId = this.currentServer.properties.virtualserver_unique_identifier;
|
2018-03-07 18:06:52 +00:00
|
|
|
let server = JSON.stringify(this.cacheServer);
|
|
|
|
localStorage.setItem("settings.server_" + serverId, server);
|
|
|
|
}
|
|
|
|
|
|
|
|
let global = JSON.stringify(this.cacheGlobal);
|
2018-02-27 16:20:49 +00:00
|
|
|
localStorage.setItem("settings.global", global);
|
|
|
|
}
|
|
|
|
}
|