forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
233 lines (217 loc) · 5.7 KB
/
settings.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"use strict";
var meta = require('./meta');
function expandObjBy(obj1, obj2) {
var key, val1, val2, xorValIsArray, changed = false;
for (key in obj2) {
if (obj2.hasOwnProperty(key)) {
val2 = obj2[key];
val1 = obj1[key];
xorValIsArray = Array.isArray(val1) ^ Array.isArray(val2);
if (xorValIsArray || !obj1.hasOwnProperty(key) || typeof val2 !== typeof val1) {
obj1[key] = val2;
changed = true;
} else if (typeof val2 === 'object' && !Array.isArray(val2)) {
if (expandObjBy(val1, val2)) {
changed = true;
}
}
}
}
return changed;
}
function trim(obj1, obj2) {
var key, val1;
for (key in obj1) {
if (obj1.hasOwnProperty(key)) {
val1 = obj1[key];
if (!obj2.hasOwnProperty(key)) {
delete obj1[key];
} else if (typeof val1 === 'object' && !Array.isArray(val1)) {
trim(val1, obj2[key]);
}
}
}
}
function mergeSettings(cfg, defCfg) {
if (typeof defCfg !== 'object') {
return;
}
if (typeof cfg._ !== 'object') {
cfg._ = defCfg;
} else {
expandObjBy(cfg._, defCfg);
trim(cfg._, defCfg);
}
}
/**
A class to manage Objects saved in {@link meta.settings} within property "_".
Constructor, synchronizes the settings and repairs them if version differs.
@param hash The hash to use for {@link meta.settings}.
@param version The version of the settings, used to determine whether the saved settings may be corrupt.
@param defCfg The default settings.
@param callback Gets called once the Settings-object is ready.
@param forceUpdate Whether to trigger structure-update even if the version doesn't differ from saved one.
Should be true while plugin-development to ensure structure-changes within settings persist.
@param reset Whether to reset the settings.
*/
function Settings(hash, version, defCfg, callback, forceUpdate, reset) {
this.hash = hash;
this.version = version || this.version;
this.defCfg = defCfg;
if (reset) {
this.reset(callback);
} else {
this.sync(function () {
this.checkStructure(callback, forceUpdate);
});
}
}
Settings.prototype.hash = '';
Settings.prototype.defCfg = {};
Settings.prototype.cfg = {};
Settings.prototype.version = '0.0.0';
/**
Synchronizes the local object with the saved object (reverts changes).
@param callback Gets called when done.
*/
Settings.prototype.sync = function (callback) {
var _this = this;
meta.settings.get(this.hash, function (err, settings) {
try {
if (settings._) {
settings._ = JSON.parse(settings._);
}
} catch (_error) {}
_this.cfg = settings;
if (typeof _this.cfg._ !== 'object') {
_this.cfg._ = _this.defCfg;
_this.persist(callback);
} else if (expandObjBy(_this.cfg._, _this.defCfg)) {
_this.persist(callback);
} else if (typeof callback === 'function') {
callback.apply(_this, err);
}
});
};
/**
Persists the local object.
@param callback Gets called when done.
*/
Settings.prototype.persist = function (callback) {
var conf = this.cfg._,
_this = this;
if (typeof conf === 'object') {
conf = JSON.stringify(conf);
}
meta.settings.set(this.hash, this.createWrapper(this.cfg.v, conf), function () {
if (typeof callback === 'function') {
callback.apply(_this, arguments || []);
}
});
return this;
};
/**
Returns the setting of given key or default value if not set.
@param key The key of the setting to return.
@param def The default value, if not set global default value gets used.
@returns Object The setting to be used.
*/
Settings.prototype.get = function (key, def) {
var obj = this.cfg._,
parts = (key || '').split('.'),
part;
for (var i = 0; i < parts.length; i++) {
part = parts[i];
if (part && obj != null) {
obj = obj[part];
}
}
if (obj === void 0) {
if (def === void 0) {
def = this.defCfg;
for (var j = 0; j < parts.length; j++) {
part = parts[j];
if (part && def != null) {
def = def[part];
}
}
}
return def;
}
return obj;
};
/**
Returns the settings-wrapper object.
@returns Object The settings-wrapper.
*/
Settings.prototype.getWrapper = function () {
return this.cfg;
};
/**
Creates a new wrapper for the given settings with the given version.
@returns Object The new settings-wrapper.
*/
Settings.prototype.createWrapper = function (version, settings) {
return {
v: version,
_: settings
};
};
/**
Creates a new wrapper for the default settings.
@returns Object The new settings-wrapper.
*/
Settings.prototype.createDefaultWrapper = function () {
return this.createWrapper(this.version, this.defCfg);
};
/**
Sets the setting of given key to given value.
@param key The key of the setting to set.
@param val The value to set.
*/
Settings.prototype.set = function (key, val) {
var part, obj, parts;
this.cfg.v = this.version;
if (val == null || !key) {
this.cfg._ = val || key;
} else {
obj = this.cfg._;
parts = key.split('.');
for (var i = 0, _len = parts.length - 1; i < _len; i++) {
if (part = parts[i]) {
if (!obj.hasOwnProperty(part)) {
obj[part] = {};
}
obj = obj[part];
}
}
obj[parts[parts.length - 1]] = val;
}
return this;
};
/**
Resets the saved settings to default settings.
@param callback Gets called when done.
*/
Settings.prototype.reset = function (callback) {
this.set(this.defCfg).persist(callback);
return this;
};
/**
If the version differs the settings get updated and persisted.
@param callback Gets called when done.
@param force Whether to update and persist the settings even if the versions ara equal.
*/
Settings.prototype.checkStructure = function (callback, force) {
if (!force && this.cfg.v === this.version) {
if (typeof callback === 'function') {
callback();
}
} else {
mergeSettings(this.cfg, this.defCfg);
this.cfg.v = this.version;
this.persist(callback);
}
return this;
};
module.exports = Settings;