-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfig Util.js
More file actions
211 lines (173 loc) · 4.53 KB
/
Config Util.js
File metadata and controls
211 lines (173 loc) · 4.53 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: cogs;
/**
* Enumeration for color mode.
*
* @class ColorMode
*/
class ColorMode {
static Light = "light";
static Dark = "dark";
/**
* Used to get color mode based on
* the provided string.
*
* @static
* @param {String} colorModeString color mode as string
* @return {ColorMode} color mode
* @memberof ColorMode
*/
static of(colorModeString) {
const colorModes = [
this.Light,
this.Dark
];
const index = colorModes.indexOf(colorModeString.toLowerCase());
if (index === -1) {
throw new Error("Color mode doesn't exist.");
}
return colorModes[index];
}
}
/**
* Property with two values.
* One is used for white mode and
* another for dark mode.
*
* @class ThemedProperty
*/
class ThemedProperty {
#darkModeProperty;
#lightModeProperty;
/**
* Creates an instance of ThemedProperty.
*
* @param {Object} darkModeProperty dark mode property
* @param {Object} lightModeProperty light mode property
* @memberof ThemedProperty
*/
constructor(darkModeProperty, lightModeProperty) {
this.#darkModeProperty = darkModeProperty;
this.#lightModeProperty = lightModeProperty;
}
/**
* Used to get value based on
* the provided color mode.
*
* @param {ColorMode} colorMode color mode
* @return {Object} value corresponding provided color mode
* @memberof ThemedProperty
*/
get(colorMode) {
switch (colorMode) {
case ColorMode.Dark:
return this.#darkModeProperty;
case ColorMode.Light:
return this.#lightModeProperty;
}
}
}
/**
* Main component.
* Used to work with configurations.
*
* @class ConfigStore
*/
class ConfigStore {
#config = {};
#userConfig = {};
#colorMode = ColorMode.Dark;
/**
* Used to set default config.
*
* @param {Object} config default config
* @memberof ConfigStore
*/
setConfig(config) {
this.#config = config;
}
/**
* Used to set user-provided config.
*
* @param {Object} config user-provided config
* @memberof ConfigStore
*/
overrideConfig(config) {
if (config) {
this.#userConfig = config;
}
}
/**
* Used to set color mode.
*
* @param {ColorMode} colorMode color mode
* @memberof ConfigStore
*/
setColorMode(colorMode) {
this.#colorMode = colorMode;
}
/**
* Used to return configuration value.
* If value is present in config
* provided by user, it would be used
* otherwise default one will be returned.
*
* @param {String} composedProperty composed proeprty
* @return {Object} user-provided or default configuration value
*/
get(composedProperty) {
const propertyChain = composedProperty.split(".");
const configValue = this.#getField(this.#config, propertyChain);
const userConfigValue = this.#getField(this.#userConfig, propertyChain);
if (userConfigValue) {
return userConfigValue;
}
return configValue;
}
/**
* Used to get value from provided
* configuration by iterating through
* property chain.
*
* @param {Object} config configuration
* @param {List<String>} propertyChain list of configuration recursive properties
* @return {Object} field from config
*/
#getField(config, propertyChain) {
let configValue = config;
for (const property of propertyChain) {
const nextValue = configValue[property];
if (nextValue == undefined) {
configValue = null;
break;
}
configValue = nextValue;
}
if (configValue instanceof ThemedProperty) {
return configValue.get(this.#colorMode);
}
return configValue;
}
}
/**
* Used to return value based on current
* device theme
*
* @param dark - parameter that would
* be used when dark theme is enabled
*
* @param light - parameter that would
* be used when light theme is enabled
*
*
* @return theme specific value
*/
function themed(dark, light) {
return new ThemedProperty(dark, light);
}
module.exports = {
ConfigStore,
ColorMode,
themed
};