forked from Happy-Ferret/weh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweh-prefs.js
More file actions
274 lines (236 loc) · 6.21 KB
/
weh-prefs.js
File metadata and controls
274 lines (236 loc) · 6.21 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* weh - WebExtensions Helper
*
* @summary workflow and base code for developing WebExtensions browser add-ons
* @author Michel Gutierrez
* @link https://github.com/mi-g/weh
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
var _ = require('weh-i18n').getMessage;
function Prefs() {
this.$specs = {};
this.$values = null;
if(!this.$values)
this.$values = {};
this.$listeners = {};
}
Prefs.prototype = {
notify: function(p,val,oldVal,spec) {
var self = this;
var terms = p.split(".");
var keys = [];
for(var i=terms.length;i>=0;i--)
keys.push(terms.slice(0,i).join("."));
keys.forEach(function(key) {
var listeners = self.$listeners[key];
if(listeners)
listeners.forEach(function(listener) {
if(listener.specs!=spec)
return;
if(!listener.pack)
try {
listener.callback(p,val,oldVal);
} catch(e) {}
else {
listener.pack[p] = val;
if(typeof listener.old[p]=="undefined")
listener.old[p] = oldVal;
if(listener.timer)
clearTimeout(listener.timer);
listener.timer = setTimeout(function() {
delete listener.timer;
var pack = listener.pack;
var old = listener.old;
listener.pack = {};
listener.old = {};
try {
listener.callback(pack,old);
} catch(e) {}
},0);
}
});
});
},
forceNotify: function(spec) {
if(typeof spec==="undefined")
spec = false;
var self = this;
Object.keys(self.$specs).forEach((key)=>{
self.notify(key, self.$values[key], self.$values[key], spec);
});
},
declare: function(specs) {
var self = this;
if(!Array.isArray(specs))
specs = Object.keys(specs).map(function(prefName) {
var spec = specs[prefName];
spec.name = prefName;
return spec;
});
specs.forEach(function(spec) {
if(forbiddenKeys[spec.name])
throw new Error("Forbidden prefs key "+spec.name);
if(spec.hidden) {
spec.label = spec.name;
spec.description = "";
} else {
var localeName = spec.name.replace(/[^0-9a-zA-Z_]/g,'_');
spec.label = spec.label || _("weh_prefs_label_"+localeName) || spec.name;
spec.description = spec.description || _("weh_prefs_description_"+localeName) || "";
}
if(spec.type == "choice")
spec.choices = (spec.choices || []).map(function(choice) {
if(typeof choice=="object")
return choice;
if(spec.hidden)
return {
value: choice,
name: choice
}
else {
var localeChoice = choice.replace(/[^0-9a-zA-Z_]/g,'_');
var localeChoiceTag = "weh_prefs_"+localeName+"_option_"+localeChoice;
return {
value: choice,
name: _("weh_prefs_"+localeName+"_option_"+localeChoice) || choice
}
}
});
var prevValue = null;
if(!self.$specs[spec.name])
(function(p) {
if(typeof self[spec.name]!=="undefined")
prevValue = self[spec.name];
Object.defineProperty(self, p, {
set: function(val) {
var oldVal = self.$values[p];
if(oldVal===val)
return;
self.$values[p] = val;
self.notify(p,val,oldVal,false);
},
get: function() {
return self.$values[p]!==undefined ? self.$values[p] :
(self.$specs[p] && self.$specs[p].defaultValue || undefined);
}
});
})(spec.name);
var oldSpecs = self.$specs[spec.name];
self.$specs[spec.name] = spec;
if(prevValue!==null)
self.$values[spec.name] = prevValue;
else if(typeof self.$values[spec.name]=="undefined")
self.$values[spec.name] = spec.defaultValue;
self.notify(spec.name,spec,oldSpecs,true);
});
},
on: function() {
var pref = "", options = {}, argIndex = 0;
if(typeof arguments[argIndex]=="string")
pref = arguments[argIndex++];
if(typeof arguments[argIndex]=="object")
options = arguments[argIndex++];
var callback = arguments[argIndex];
var pack = !!options.pack;
if(!this.$listeners[pref])
this.$listeners[pref] = [];
var handler = {
callback: callback,
specs: !!options.specs
}
if(pack) {
handler.pack = {};
handler.old = {};
}
this.$listeners[pref].push(handler);
},
off: function() {
var pref = "", argIndex = 0;
if(typeof arguments[argIndex]=="string")
pref = arguments[argIndex++];
var callback = arguments[argIndex];
var listeners = this.$listeners[pref];
if(!listeners)
return;
for(var i=listeners.length-1;i>=0;i--)
if(!callback || listeners[i]==callback)
listeners.splice(i,1);
},
getAll: function() {
return Object.assign({},this.$values);
},
getSpecs: function() {
return Object.assign({},this.$specs);
},
assign: function(prefs) {
for(var k in prefs)
if(prefs.hasOwnProperty(k))
this[k] = prefs[k];
},
isValid: function(prefName,value) {
var spec = this.$specs[prefName];
if(!spec)
return undefined;
switch(spec.type) {
case "string":
if(spec.regexp && !new RegExp(spec.regexp).test(value))
return false;
break;
case "integer":
if(!/^-?[0-9]+$/.test(value))
return false;
if(isNaN(parseInt(value)))
return false;
/* falls through */
case "float":
if(spec.type=="float") {
if(!/^-?[0-9]+(\.[0-9]+)?|(\.[0-9]+)$/.test(value))
return false;
if(isNaN(parseFloat(value)))
return false;
}
if(typeof spec.minimum!="undefined" && value<spec.minimum)
return false;
if(typeof spec.maximum!="undefined" && value>spec.maximum)
return false;
break;
case "choice":
var ok = false;
(spec.choices || []).forEach((choice) => {
if(value==choice.value)
ok = true;
});
if(!ok)
return false;
break;
}
return true;
},
reducer: function(state={}, action) {
switch(action.type) {
case "weh.SET_PREFS":
state = Object.assign({},state,action.payload);
break;
}
return state;
},
reduxDispatch(store) {
this.on("", {
pack:true
},(changedPrefs)=>{
store.dispatch({
type: "weh.SET_PREFS",
payload: changedPrefs
});
});
}
}
var prefs = new Prefs();
var forbiddenKeys = {};
for(var k in prefs)
if(prefs.hasOwnProperty(k))
forbiddenKeys[k] = true;
module.exports = prefs;