/
types.uncompressed.js
444 lines (387 loc) · 13.5 KB
/
types.uncompressed.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*global Class _id_ify */
// Handling for data value types in configure
var Types = {};
(function($) {
"use strict";
Types.BaseType = Class.extend({
// Set to a function that returns true if the current value
// is to be null
null_if: null,
init: function(spec) {
this.spec = spec;
},
createUI: function(change_handler) {
var val = this.spec.current_value, m, cols, rows, value, size;
if (typeof(val) === "undefined") {
val = '';//eval(this.spec['default']);
}
// columns x rows
if (this.spec.SIZE &&
(m = this.spec.SIZE.match(/^\s*(\d+)x(\d+)(\s|$)/))) {
cols = m[1];
rows = m[2];
value = typeof(val) === "undefined" ? '' : val;
value = value.replace(/&/g, "&");
this.$ui = $('<textarea id="' + _id_ify(this.spec.keys) +
'" rows="' + rows +
'" cols="' + cols +
'" class="foswikiTextArea">' + value + '</textarea>');
} else {
// simple size
size = 80;
if (this.spec.SIZE &&
(m = this.spec.SIZE.match(/^\s*(\d+)(\s|$)/))) {
size = m[1];
}
this.$ui = $('<input type="text" id="' + _id_ify(this.spec.keys) +
'" size="' + size + '"/>');
this.useVal(val);
}
if (this.spec.SPELLCHECK) {
this.$ui.prop('spellcheck', true);
}
if (typeof(change_handler) !== "undefined") {
this.$ui.change(change_handler);
}
return this.$ui;
},
useVal: function(val) {
this.$ui.val(val);
},
currentValue: function() {
if (this.null_if !== null && this.null_if()) {
return null;
}
return this.$ui.val();
},
commitVal: function() {
this.spec.current_value = this.currentValue();
},
restoreCurrentValue: function() {
this.useVal(this.spec.current_value);
},
restoreDefaultValue: function() {
this.useVal(this.spec['default']);
},
isModified: function() {
var cv = this.spec.current_value;
if (typeof(cv) === 'undefined') {
cv = (this.null_if === null) ? '' : null;
}
return this.currentValue() != cv;
},
isDefault: function() {
// Implementation appropriate for number and string types
// which can be compared as their base type in JS. More
// complex types may need conversion to string first.
return this.currentValue() == this.spec['default'];
}
});
Types.STRING = Types.BaseType.extend({
restoreDefaultValue: function() {
var val = this.spec['default'];
if (val === 'undef')
val = null;
else
val = val.replace(/^\s*(["'])(.*)\1\s*$/, "$2");
this.useVal(val);
},
isDefault: function() {
// trim ' from the default
var val = this.spec['default'];
if (typeof(val) === 'string') {
if (/^\s*'.*'\s*$/.test(val)) {
// We can't use eval because JS eval behaves differently
// to perl eval of a single-quoted string. The currentValue
// comes from a perl eval.
val = val.replace(/^\s*'(.*)'\s*$/, "$1");
val = val.replace(/\'/g, "'");
}
}
return this.currentValue() === val;
}
});
Types.BOOLEAN = Types.BaseType.extend({
createUI: function(change_handler) {
this.$ui = $('<input type="checkbox" id="' + _id_ify(this.spec.keys) + '" />');
if (typeof(change_handler) !== "undefined") {
this.$ui.change(change_handler);
}
if (typeof(this.spec.current_value) == 'undefined') {
this.spec.current_value = 0;
} else if (this.spec.current_value == '0') {
this.spec.current_value = 0;
} else if (this.spec.current_value == '1') {
this.spec.current_value = 1;
}
if (this.spec.current_value !== 0) {
this.$ui.prop('checked', true);
}
if (this.spec.extraClass) {
this.$ui.addClass(this.spec.extraClass);
}
return this.$ui;
},
currentValue: function() {
return this.$ui[0].checked ? 1 : 0;
},
isModified: function() {
var a = this.currentValue(),
b = this.spec.current_value;
return a != b;
},
isDefault: function() {
var a = this.currentValue(),
b = eval(this.spec['default']);
return a == b;
},
useVal: function(val) {
if (typeof(val) == 'undefined') {
val = false;
} else if (val == '0' || val == '$FALSE') {
val = false;
} else if ( val == '1') {
val = true;
}
this.$ui.prop('checked', val );
}
});
Types.PASSWORD = Types.STRING.extend({
createUI: function(change_handler) {
this._super(change_handler);
this.$ui.attr('type', 'password');
this.$ui.prop('autocomplete', false);
return this.$ui;
}
});
Types.REGEX = Types.STRING.extend({
restoreDefaultValue: function() {
var val = this.spec['default'];
if (val === 'undef')
val = null;
else
val = val.replace(/^\s*(["'])(.*)\1\s*$/, "$2");
val = val.replace(/\\\\\\/, "\\");
this.useVal(val);
},
isDefault: function() {
// trim ' from the default
var val = this.spec['default'];
if (typeof(val) === 'string') {
if (/^\s*'.*'\s*$/.test(val)) {
// We can't use eval because JS eval behaves differently
// to perl eval of a single-quoted string. The currentValue
// comes from a perl eval.
val = val.replace(/^\s*'(.*)'\s*$/, "$1");
val = val.replace(/\'/g, "'");
val = val.replace(/\\\\\\/, "\\");
}
}
return this.currentValue() === val;
}
});
// Deep compare of simple object, used for perl simple structures.
// Note: no support for built-in types other than the basic types, arrays
// and hashes; but then parsing a perl value use eval will never succeed with
// anything else.
Types.deep_equals = function(x, y) {
if (x === y)
return true;
if (x === null
|| x === undefined
|| y === null
|| y === undefined)
return false; // x===y would have succeeded otherwise
if (x.constructor !== y.constructor)
return false;
if (x.valueOf() === y.valueOf())
return true;
if (Array.isArray(x) && x.length !== y.length)
return false;
// if they are strictly equal, they both need to be object at least
if (!(x instanceof Object && y instanceof Object))
return false;
// recursive equality check
var p = Object.keys(x);
return Object.keys(y).every(function (i) {
return p.indexOf(i) !== -1;
}) &&
p.every(function (i) {
return Types.deep_equals(x[i], y[i]);
});
};
Types.PERL = Types.BaseType.extend({
createUI: function(change_handler) {
if (!(this.spec.SIZE && this.spec.SIZE.match(/\b(\d+)x(\d+)\b/))) {
this.spec.SIZE = "80x20";
}
return this._super(change_handler);
},
isDefault: function() {
// To do this comparison requires parsing and rewriting the perl to
// javascript. Not impossible, but tricky.
var a = this.currentValue(),
b = this.spec['default'], av, bv;
a = (a == null) ? null : a.trim();
b = (b == null) ? null : b.trim();
try {
// See if they parse as JS - they probably will! If they don't,
// parse, fall back to a string comparison :-(
av = eval(a);
bv = eval(b);
} catch (err) {
av = null; bv = null;
}
if (av !== null && bv !== null) {
return Types.deep_equals(av, bv);
}
// String comparison of the serialised perl value. This is unlikely
// to work, but there's no other option if one or both of the values
// fails to parse using JS eval.
return a === b;
}
});
Types.NUMBER = Types.BaseType.extend({
// Local first-line validator
createUI: function(change_handler) {
return this._super(function(evt) {
var val = $(this).val();
if (/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/.test(val)) {
$(this).css('background-color', '');
change_handler.call(this, evt);
} else {
$(this).css('background-color', 'yellow');
alert('"' + val + '" is not a valid number');
}
});
}
});
Types.OCTAL = Types.NUMBER.extend({
// Local first-line validator
createUI: function(change_handler) {
return this._super(function(evt) {
var val = $(this).val();
if (/^[0-7]+$/.test(val)) {
$(this).css('background-color', '');
change_handler.call(this, evt);
} else {
$(this).css('background-color', 'yellow');
alert('"' + val + '" is not a valid octal number');
}
});
}
});
Types.PATHINFO = Types.STRING.extend({
createUI: function(change_handler) {
this._super(change_handler);
this.$ui.prop('readonly', true);
return this.$ui;
}
});
Types.PATH = Types.STRING.extend({
});
Types.URL = Types.STRING.extend({
});
Types.URILIST = Types.STRING.extend({
});
Types.URLPATH = Types.STRING.extend({
});
Types.DATE = Types.STRING.extend({
});
Types.COMMAND = Types.STRING.extend({
});
Types.EMAILADDRESS = Types.STRING.extend({
});
// This field is invisible, as it only exists to provide a hook
// for a provideFeedback button. It is disabled as there is no
// point in POSTing it.
Types.NULL = Types.BaseType.extend({
createUI: function(change_handler) {
this._super(change_handler);
this.$ui.prop('readonly', true);
this.$ui.prop('disabled', true);
this.$ui.attr('size', '1');
return this.$ui;
}
});
Types.BUTTON = Types.BaseType.extend({
createUI: function() {
this.$ui = $('<a href="' + this.spec.uri + '">' + this.spec.title + '</a>');
this.$ui.button();
return this.$ui;
},
useVal: function() {
// NOP
}
});
Types.SELECT = Types.BaseType.extend({
// Get an array of items that need to be selected given the value
// 'val'
_getSel: function(val, mult) {
var sel = {}, a, i;
if (typeof(val) !== "undefined") {
if (mult) {
a = val.split(',');
for (i = 0; i < a.length; i++) {
sel[a[i]] = true;
}
} else {
sel[val] = true;
}
}
return sel;
},
createUI: function(change_handler) {
var size = 1, m, sel, i, opt, options, $option;
if (this.spec.SIZE && (m = this.spec.SIZE.match(/\b(\d+)\b/))) {
size = m[0];
}
this.$ui = $('<select id="' + _id_ify(this.spec.keys) + '" size="' + size +
'" class="foswikiSelect" />');
if (typeof(change_handler) !== "undefined") {
this.$ui.change(change_handler);
}
if (this.spec.MULTIPLE) {
this.$ui.prop('multiple', true);
}
if (typeof(this.spec.select_from) !== "undefined") {
sel = this._getSel(this.spec.current_value, this.spec.MULTIPLE);
for (i = 0; i < this.spec.select_from.length; i++) {
opt = this.spec.select_from[i];
$option = $('<option>' + opt + '</option>');
if (sel[opt]) {
$option.prop('selected', true);
}
this.$ui.append($option);
}
}
return this.$ui;
},
useVal: function(val) {
val = val.replace(/^\s*(["'])(.*?)\1\s*/, "$2");
var sel = this._getSel(val),
sf = this.spec.select_from,
i;
if (typeof(sf) !== "undefined") {
i = 0;
this.$ui.find('option').each(function() {
var opt = sf[i++];
if (sel[opt]) {
$(this).prop('selected', true);
} else {
$(this).prop('selected', false);
}
});
}
},
isDefault: function() {
var a = this.currentValue(),
b = this.spec['default'];
a = (a == null) ? null : a.trim();
b = (b == null) ? null : b.trim();
b = b.replace(/^\s*(["'])(.*?)\1\s*/, "$2");
return a === b;
}
});
Types.SELECTCLASS = Types.SELECT.extend({});
})(jQuery);