forked from gbtami/pychess-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.ts
51 lines (43 loc) · 1.37 KB
/
settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { VNode } from 'snabbdom';
import { getDocumentData } from './document';
export abstract class Settings<T> {
readonly name: string;
protected _value: T;
constructor(name: string) {
this.name = name;
}
get value(): T {
return this._value;
}
set value(value: T) {
// TODO some mechanism to save settings to server
localStorage[this.name] = value;
this._value = value;
this.update();
}
abstract update(): void;
abstract view(): VNode;
}
export abstract class StringSettings extends Settings<string> {
constructor(name: string, defaultValue: string) {
super(name);
this._value = getDocumentData(name) ?? localStorage[name] ?? defaultValue;
}
}
export abstract class NumberSettings extends Settings<number> {
constructor(name: string, defaultValue: number) {
super(name);
this._value = Number(getDocumentData(name) ?? (localStorage[name] ?? defaultValue));
}
}
export abstract class BooleanSettings extends Settings<boolean> {
constructor(name: string, defaultValue: boolean) {
super(name);
if (getDocumentData(name))
this._value = getDocumentData(name) === 'True';
else if (localStorage[name])
this._value = localStorage[name] === 'true';
else
this._value = defaultValue;
}
}