Newer
Older
/*
Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008-2012 Jo-Philipp Wich <jow@openwrt.org>
Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
*/
/*
Build using:
uglifyjs javascript/gluon-web-model.js -o files/lib/gluon/web/www/static/gluon-web-model.js -c -m --support-ie8
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
*/
(function() {
var dep_entries = {};
function Int(x) {
return (/^-?\d+$/.test(x) ? +x : NaN);
}
function Dec(x) {
return (/^-?\d*\.?\d+?$/.test(x) ? +x : NaN);
}
var validators = {
'integer': function() {
return !isNaN(Int(this));
},
'uinteger': function() {
return (Int(this) >= 0);
},
'float': function() {
return !isNaN(Dec(this));
},
'ufloat': function() {
return (Dec(this) >= 0);
},
'ipaddr': function() {
return validators.ip4addr.apply(this) ||
validators.ip6addr.apply(this);
},
'ip4addr': function() {

Matthias Schiffer
committed
var match;
if ((match = this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))) {
return (match[1] >= 0) && (match[1] <= 255) &&
(match[2] >= 0) && (match[2] <= 255) &&
(match[3] >= 0) && (match[3] <= 255) &&
(match[4] >= 0) && (match[4] <= 255);
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
}
return false;
},
'ip6addr': function() {
if (this.indexOf('::') < 0)
return (this.match(/^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i) != null);
if (
(this.indexOf(':::') >= 0) || this.match(/::.+::/) ||
this.match(/^:[^:]/) || this.match(/[^:]:$/)
)
return false;
if (this.match(/^(?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}$/i))
return true;
if (this.match(/^(?:[a-f0-9]{1,4}:){7}:$/i))
return true;
if (this.match(/^:(?::[a-f0-9]{1,4}){7}$/i))
return true;
return false;
},
'wpakey': function() {
var v = this;
if (v.length == 64)
return (v.match(/^[a-f0-9]{64}$/i) != null);
else
return (v.length >= 8) && (v.length <= 63);
},
'range': function(min, max) {
var val = Dec(this);
return (val >= +min && val <= +max);
},
'min': function(min) {
return (Dec(this) >= +min);
},
'max': function(max) {
return (Dec(this) <= +max);
},
'irange': function(min, max) {
var val = Int(this);
return (val >= +min && val <= +max);
},
'imin': function(min) {
return (Int(this) >= +min);
},
'imax': function(max) {
return (Int(this) <= +max);
},
'minlength': function(min) {
return ((''+this).length >= +min);
},
'maxlength': function(max) {
return ((''+this).length <= +max);
},
};
function compile(type) {

Matthias Schiffer
committed
var v, match;
if ((match = type.match(/^([^\(]+)\(([^,]+),([^\)]+)\)$/)) && (v = validators[match[1]]) !== undefined) {

Matthias Schiffer
committed
return v.apply(this, [match[2], match[3]]);

Matthias Schiffer
committed
} else if ((match = type.match(/^([^\(]+)\(([^,\)]+)\)$/)) && (v = validators[match[1]]) !== undefined) {

Matthias Schiffer
committed
return v.apply(this, [match[2]]);
}
} else {
return validators[type];
}
}
function checkvalue(target, ref) {
var t = document.getElementById(target);
var value;
if (t) {
if (t.type == "checkbox") {
value = t.checked;
} else if (t.value) {
value = t.value;
} else {
value = "";
}
return (value == ref);
} else {
t = document.getElementById(target + '.' + ref);
if (t)
return (t.type == "radio" && t.checked);
}
function check(deps) {
for (var i=0; i < deps.length; i++) {
var stat = true;
for (var j in deps[i]) {
stat = (stat && checkvalue(j, deps[i][j]));
}
if (stat)
return true;
}
return false;
}
function update() {
window.dispatchEvent(new Event('gluon-update'));
var state = false;
for (var id in dep_entries) {
var entry = dep_entries[id];
var node = document.getElementById(id);
var parent = document.getElementById(entry.parent);
if (node && node.parentNode && !check(entry.deps)) {
node.parentNode.removeChild(node);
node.dispatchEvent(new Event('gluon-hide'));
state = true;
} else if (parent && (!node || !node.parentNode) && check(entry.deps)) {
var next = undefined;
for (next = parent.firstChild; next; next = next.nextSibling) {
if (next.getAttribute && parseInt(next.getAttribute('data-index'), 10) > entry.index) {
break;
}
}
if (!next) {
parent.appendChild(entry.node);
} else {
parent.insertBefore(entry.node, next);
}
entry.node.dispatchEvent(new Event('gluon-show'));
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
state = true;
}
// hide optionals widget if no choices remaining
if (parent && parent.parentNode && parent.getAttribute('data-optionals'))
parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
}
if (state) {
update();
}
}
function bind(obj, type, callback, mode) {
if (!obj.addEventListener) {
obj.attachEvent('on' + type,
function() {
var e = window.event;
if (!e.target && e.srcElement)
e.target = e.srcElement;
return !!callback(e);
}
);
} else {
obj.addEventListener(type, callback, !!mode);
}
return obj;
}
function init_dynlist(parent, attr) {
var prefix = attr.prefix;
function dynlist_redraw(focus, add, del) {
var values = [];
while (parent.firstChild) {
var n = parent.firstChild;
var i = +n.index;
if (i != del) {
if (n.nodeName.toLowerCase() == 'input')
values.push(n.value || '');
else if (n.nodeName.toLowerCase() == 'select')
values[values.length-1] = n.options[n.selectedIndex].value;
}
parent.removeChild(n);
}
if (add >= 0) {
focus = add + 1;
values.splice(add, 0, '');
} else if (!attr.optional && values.length == 0) {
values.push('');
}
for (var i = 1; i <= values.length; i++) {
var t = document.createElement('input');
t.id = prefix + '.' + i;
t.name = prefix;
t.value = values[i-1];
t.type = 'text';
t.index = i;
t.className = 'gluon-input-text';
if (attr.size)
t.size = attr.size;
if (attr.placeholder)
t.placeholder = attr.placeholder;
if (attr.type)
validate_field(t, false, attr.type);
bind(t, 'keydown', dynlist_keydown);
bind(t, 'keypress', dynlist_keypress);
if (i == focus) {
t.focus();
} else if (-i == focus) {
t.focus();
/* force cursor to end */
var v = t.value;
t.value = ' '
t.value = v;
}
if (attr.optional || values.length > 1) {
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
var b = document.createElement('span');
b.className = 'gluon-remove';
parent.appendChild(b);
bind(b, 'click', dynlist_btnclick(false));
parent.appendChild(document.createElement('br'));
}
}
var b = document.createElement('span');
b.className = 'gluon-add';
parent.appendChild(b);
bind(b, 'click', dynlist_btnclick(true));
}
function dynlist_keypress(ev) {
ev = ev ? ev : window.event;
var se = ev.target ? ev.target : ev.srcElement;
if (se.nodeType == 3)
se = se.parentNode;
switch (ev.keyCode) {
/* backspace, delete */
case 8:
case 46:
if (se.value.length == 0) {
if (ev.preventDefault)
ev.preventDefault();
return false;
}
return true;
/* enter, arrow up, arrow down */
case 13:
case 38:
case 40:
if (ev.preventDefault)
ev.preventDefault();
return false;
}
return true;
}
function dynlist_keydown(ev) {
ev = ev ? ev : window.event;
var se = ev.target ? ev.target : ev.srcElement;
var index = 0;
var prev, next;
if (se) {
if (se.nodeType == 3)
se = se.parentNode;
index = se.index;
prev = se.previousSibling;
while (prev && prev.name != prefix)
prev = prev.previousSibling;
next = se.nextSibling;
while (next && next.name != prefix)
next = next.nextSibling;
}
switch (ev.keyCode) {
/* backspace, delete */
case 8:
case 46:
var del = (se.nodeName.toLowerCase() == 'select')
? true : (se.value.length == 0);
if (del) {
if (ev.preventDefault)
ev.preventDefault();
var focus = se.index;
if (ev.keyCode == 8)
focus = -focus+1;
dynlist_redraw(focus, -1, index);
return false;
}
break;
/* enter */
case 13:
dynlist_redraw(-1, index, -1);
break;
/* arrow up */
case 38:
if (prev)
prev.focus();
break;
/* arrow down */
case 40:
if (next)
next.focus();
break;
}
return true;
}
function dynlist_btnclick(add) {
return function(ev) {
ev = ev ? ev : window.event;
var se = ev.target ? ev.target : ev.srcElement;
var input = se.previousSibling;
while (input && input.name != prefix) {
input = input.previousSibling;
}
if (add) {
dynlist_keydown({
target: input,
keyCode: 13
});
} else {
input.value = '';
dynlist_keydown({
target: input,
keyCode: 8
});
}
return false;
}
}
dynlist_redraw(NaN, -1, -1);
}
function validate_field(field, optional, type) {
var check = compile(type);
if (!check)
return;
var validator = function() {
if (!field.form)
return;
field.className = field.className.replace(/ gluon-input-invalid/g, '');
var value = (field.options && field.options.selectedIndex > -1)
? field.options[field.options.selectedIndex].value : field.value;
if (!(((value.length == 0) && optional) || check.apply(value)))
field.className += ' gluon-input-invalid';
};
bind(field, "blur", validator);
bind(field, "keyup", validator);
bind(field, "gluon-revalidate", validator);
if (field.nodeName.toLowerCase() == 'select') {
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
bind(field, "change", validator);
bind(field, "click", validator);
}
validator();
}
function add(obj, dep, index) {
var entry = dep_entries[obj.id];
if (!entry) {
entry = {
"node": obj,
"parent": obj.parentNode.id,
"deps": [],
"index": index
};
dep_entries[obj.id] = entry;
}
entry.deps.push(dep)
}
(function() {
var nodes;
nodes = document.querySelectorAll('[data-depends]');
for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
var index = parseInt(node.getAttribute('data-index'), 10);
var depends = JSON.parse(node.getAttribute('data-depends'));
if (!isNaN(index) && depends.length > 0) {
for (var alt = 0; alt < depends.length; alt++) {
add(node, depends[alt], index);
}
}
}
nodes = document.querySelectorAll('[data-update]');
for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
var events = node.getAttribute('data-update').split(' ');
for (var j = 0, event; (event = events[j]) !== undefined; j++) {
bind(node, event, function () {setTimeout(update, 0);});
}
}
nodes = document.querySelectorAll('[data-type]');
for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
validate_field(node, node.getAttribute('data-optional') === 'true',
node.getAttribute('data-type'));
}
nodes = document.querySelectorAll('[data-dynlist]');
for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
var attr = JSON.parse(node.getAttribute('data-dynlist'));
init_dynlist(node, attr);