forked from dvmarinoff/Auuki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues.js
112 lines (99 loc) · 2.88 KB
/
values.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { xf } from './xf.js';
import { memberOf } from './functions.js';
import { LocalStorageItem } from './storage/local-storage.js';
class Value {
constructor(args) {
this.name = args.name || undefined;
this.init();
this.postInit();
}
init() {
const self = this;
xf.sub('app:start', _ => { self.localStorage(); });
}
postInit() { return; }
localStorage() {
const self = this;
self.local = new LocalStorageItem({key: self.name, default: self.defaultValue(), validate: self.isValid.bind(self)});
xf.sub(`db:${self.name}`, value => {
self.local.set(value);
});
}
defaultValue() { return undefined; }
isValid(value) { return false; }
}
class Ftp extends Value {
defaultValue() { return 200; }
isValid(value) {
value = parseInt(value);
if(isNaN(value) || value > 600 || value < 30) { return false; }
return true;
}
}
class Weight extends Value {
defaultValue() { return 75; }
isValid(value) {
value = parseInt(value);
if(isNaN(value) || value > 400 || value < 20) { return false; }
return true;
}
}
class Theme extends Value {
defaultValue() { return 'dark'; }
collection() { return ['dark', 'white']; }
isValid(value) {
const self = this;
value = String(value);
if(memberOf(self.collection(), value)) { return true; }
return false;
}
switch(value) {
const self = this;
if(value === 'dark') { return 'white'; }
if(value === 'white') { return 'dark'; }
return self.defaultValue();
}
}
class Measurement extends Value {
defaultValue() { return 'metric'; }
collection() { return ['metric', 'imperial']; }
isValid(value) {
const self = this;
value = String(value);
if(memberOf(self.collection(), value)) { return true; }
return false;
}
switch(value) {
const self = this;
if(value === 'metric') { return 'imperial';}
if(value === 'imperial') { return 'metric'; }
return self.defaultValue();
}
}
class Page extends Value {
defaultValue() { return 'home'; }
collection() { return ['settings', 'home', 'workouts']; }
isValid(value) {
const self = this;
value = String(value);
if(memberOf(self.collection(), value)) { return true; }
return false;
}
switch(value) {
const self = this;
return self.defaultValue();
}
}
const ftp = new Ftp({name: 'ftp'});
const weight = new Weight({name: 'weight'});
const theme = new Theme({name: 'theme'});
const measurement = new Measurement({name: 'measurement'});
const page = new Page({name: 'page'});
const values = {
ftp: ftp,
weight: weight,
theme: theme,
measurement: measurement,
page: page
};
export { values };