2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2010 Jo-Philipp Wich <xm@subsignal.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
18 var cbi_validators = {
20 'integer': function(v)
22 return (v.match(/^-?[0-9]+$/) != null);
25 'uinteger': function(v)
27 return (cbi_validators.integer(v) && (v >= 0));
32 return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v);
35 'ip4addr': function(v)
37 if( v.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?$/) )
39 return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
40 (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
41 (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
42 (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
43 (!RegExp.$5 || ((RegExp.$6 >= 0) && (RegExp.$6 <= 32)))
50 'ip6addr': function(v)
52 if( v.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
54 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
63 if( addr.indexOf('.') > 0 )
65 var off = addr.lastIndexOf(':');
67 if( !(off && cbi_validators.ip4addr(addr.substr(off+1))) )
70 addr = addr.substr(0, off) + ':0:0';
73 if( addr.indexOf('::') >= 0 )
78 for( var i = 1; i < (addr.length-1); i++ )
79 if( addr.charAt(i) == ':' )
85 for( var i = 0; i < (7 - colons); i++ )
88 if (addr.match(/^(.*?)::(.*?)$/))
89 addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
90 (RegExp.$2 ? ':' + RegExp.$2 : '');
94 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
103 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
106 'portrange': function(v)
108 if( v.match(/^(\d+)-(\d+)$/) )
113 return cbi_validators.port(p1) &&
114 cbi_validators.port(p2) &&
115 (parseInt(p1) <= parseInt(p2))
120 return cbi_validators.port(v);
124 'macaddr': function(v)
126 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
131 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
134 'hostname': function(v)
136 return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null);
139 'wpakey': function(v)
142 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
144 return (v.length >= 8) && (v.length <= 63);
147 'wepkey': function(v)
149 if( v.substr(0,2) == 's:' )
152 if( (v.length == 10) || (v.length == 26) )
153 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
155 return (v.length == 5) || (v.length == 13);
158 'uciname': function(v)
160 return (v.match(/^[a-zA-Z0-9_]+$/) != null);
163 'range': function(v, args)
165 var min = parseInt(args[0]);
166 var max = parseInt(args[1]);
167 var val = parseInt(v);
169 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
170 return ((val >= min) && (val <= max));
177 function cbi_d_add(field, dep, next) {
178 var obj = document.getElementById(field);
181 for (var i=0; i<cbi_d.length; i++) {
182 if (cbi_d[i].id == field) {
191 "parent": obj.parentNode.id,
195 cbi_d.unshift(entry);
201 function cbi_d_checkvalue(target, ref) {
202 var t = document.getElementById(target);
206 var tl = document.getElementsByName(target);
208 if( tl.length > 0 && tl[0].type == 'radio' )
209 for( var i = 0; i < tl.length; i++ )
210 if( tl[i].checked ) {
215 value = value ? value : "";
216 } else if (!t.value) {
221 if (t.type == "checkbox") {
222 value = t.checked ? value : "";
226 return (value == ref)
229 function cbi_d_check(deps) {
232 for (var i=0; i<deps.length; i++) {
235 for (var j in deps[i]) {
236 if (j == "!reverse") {
238 } else if (j == "!default") {
242 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
252 function cbi_d_update() {
254 for (var i=0; i<cbi_d.length; i++) {
255 var entry = cbi_d[i];
256 var next = document.getElementById(entry.next)
257 var node = document.getElementById(entry.id)
258 var parent = document.getElementById(entry.parent)
260 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
261 node.parentNode.removeChild(node);
264 cbi_c[entry.parent]--;
265 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
267 parent.appendChild(entry.node);
269 next.parentNode.insertBefore(entry.node, next);
273 cbi_c[entry.parent]++;
277 if (entry && entry.parent) {
286 function cbi_bind(obj, type, callback, mode) {
287 if (!obj.addEventListener) {
288 obj.attachEvent('on' + type,
290 var e = window.event;
292 if (!e.target && e.srcElement)
293 e.target = e.srcElement;
295 return !!callback(e);
299 obj.addEventListener(type, callback, !!mode);
304 function cbi_combobox(id, values, def, man) {
305 var selid = "cbi.combobox." + id;
306 if (document.getElementById(selid)) {
310 var obj = document.getElementById(id)
311 var sel = document.createElement("select");
313 sel.className = 'cbi-input-select';
315 if (obj.nextSibling) {
316 obj.parentNode.insertBefore(sel, obj.nextSibling);
318 obj.parentNode.appendChild(sel);
321 var dt = obj.getAttribute('cbi_datatype');
322 var op = obj.getAttribute('cbi_optional');
325 cbi_validate_field(sel, op == 'true', dt);
327 if (!values[obj.value]) {
328 if (obj.value == "") {
329 var optdef = document.createElement("option");
331 optdef.appendChild(document.createTextNode(def));
332 sel.appendChild(optdef);
334 var opt = document.createElement("option");
335 opt.value = obj.value;
336 opt.selected = "selected";
337 opt.appendChild(document.createTextNode(obj.value));
338 sel.appendChild(opt);
342 for (var i in values) {
343 var opt = document.createElement("option");
346 if (obj.value == i) {
347 opt.selected = "selected";
350 opt.appendChild(document.createTextNode(values[i]));
351 sel.appendChild(opt);
354 var optman = document.createElement("option");
356 optman.appendChild(document.createTextNode(man));
357 sel.appendChild(optman);
359 obj.style.display = "none";
361 cbi_bind(sel, "change", function() {
362 if (sel.selectedIndex == sel.options.length - 1) {
363 obj.style.display = "inline";
364 sel.parentNode.removeChild(sel);
367 obj.value = sel.options[sel.selectedIndex].value;
378 function cbi_combobox_init(id, values, def, man) {
379 var obj = document.getElementById(id);
380 cbi_bind(obj, "blur", function() {
381 cbi_combobox(id, values, def, man)
383 cbi_combobox(id, values, def, man);
386 function cbi_filebrowser(id, url, defpath) {
387 var field = document.getElementById(id);
388 var browser = window.open(
389 url + ( field.value || defpath || '' ) + '?field=' + id,
390 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
396 function cbi_dynlist_init(name)
398 function cbi_dynlist_renumber(e)
401 var childs = e.parentNode.childNodes;
403 for( var i = 0; i < childs.length; i++ )
404 if( childs[i].name == name )
405 childs[i].id = name + '.' + (count++);
410 function cbi_dynlist_keypress(ev)
412 ev = ev ? ev : window.event;
414 var se = ev.target ? ev.target : ev.srcElement;
416 if (se.nodeType == 3)
421 /* backspace, delete */
424 if (se.value.length == 0)
426 if (ev.preventDefault)
434 /* enter, arrow up, arrow down */
438 if (ev.preventDefault)
447 function cbi_dynlist_keydown(ev)
449 ev = ev ? ev : window.event;
451 var se = ev.target ? ev.target : ev.srcElement;
453 if (se.nodeType == 3)
456 var prev = se.previousSibling;
457 while (prev && prev.name != name)
458 prev = prev.previousSibling;
460 var next = se.nextSibling;
461 while (next && next.name != name)
462 next = next.nextSibling;
466 /* backspace, delete */
469 var jump = (ev.keyCode == 8)
470 ? (prev || next) : (next || prev);
472 if (se.value.length == 0 && jump)
474 se.parentNode.removeChild(se.nextSibling);
475 se.parentNode.removeChild(se);
477 cbi_dynlist_renumber(jump);
479 if (ev.preventDefault)
489 var n = document.createElement('input');
493 cbi_bind(n, 'keydown', cbi_dynlist_keydown);
494 cbi_bind(n, 'keypress', cbi_dynlist_keypress);
498 se.parentNode.insertBefore(n, next);
499 se.parentNode.insertBefore(document.createElement('br'), next);
503 se.parentNode.appendChild(n);
504 se.parentNode.appendChild(document.createElement('br'));
507 var dt = se.getAttribute('cbi_datatype');
508 var op = se.getAttribute('cbi_optional') == 'true';
511 cbi_validate_field(n, op, dt);
513 cbi_dynlist_renumber(n);
534 var inputs = document.getElementsByName(name);
535 for( var i = 0; i < inputs.length; i++ )
537 cbi_bind(inputs[i], 'keydown', cbi_dynlist_keydown);
538 cbi_bind(inputs[i], 'keypress', cbi_dynlist_keypress);
542 //Hijacks the CBI form to send via XHR (requires Prototype)
543 function cbi_hijack_forms(layer, win, fail, load) {
544 var forms = layer.getElementsByTagName('form');
545 for (var i=0; i<forms.length; i++) {
546 $(forms[i]).observe('submit', function(event) {
547 // Prevent the form from also submitting the regular way
551 event.element().request({
564 function cbi_t_add(section, tab) {
565 var t = document.getElementById('tab.' + section + '.' + tab);
566 var c = document.getElementById('container.' + section + '.' + tab);
569 cbi_t[section] = (cbi_t[section] || [ ]);
570 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
574 function cbi_t_switch(section, tab) {
575 if( cbi_t[section] && cbi_t[section][tab] ) {
576 var o = cbi_t[section][tab];
577 var h = document.getElementById('tab.' + section);
578 for( var tid in cbi_t[section] ) {
579 var o2 = cbi_t[section][tid];
580 if( o.tab.id != o2.tab.id ) {
581 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
582 o2.container.style.display = 'none';
586 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
587 o2.container.style.display = 'block';
594 function cbi_t_update() {
597 for( var sid in cbi_t )
598 for( var tid in cbi_t[sid] )
599 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
600 cbi_t[sid][tid].tab.style.display = 'none';
602 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
603 cbi_t[sid][tid].tab.style.display = '';
605 var t = cbi_t[sid][tid].tab;
606 t.className += ' cbi-tab-highlighted';
610 if( hl_tabs.length > 0 )
611 window.setTimeout(function() {
612 for( var i = 0; i < hl_tabs.length; i++ )
613 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
618 function cbi_validate_form(form, errmsg)
620 /* if triggered by a section removal or addition, don't validate */
621 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
624 if( form.cbi_validators )
626 for( var i = 0; i < form.cbi_validators.length; i++ )
628 var validator = form.cbi_validators[i];
629 if( !validator() && errmsg )
640 function cbi_validate_reset(form)
643 function() { cbi_validate_form(form, null) }, 100
649 function cbi_validate_field(cbid, optional, type)
651 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
654 if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
657 vargs = RegExp.$2.split(/\s*,\s*/);
660 var vldcb = cbi_validators[type];
664 var validator = function()
669 field.className = field.className.replace(/ cbi-input-invalid/g, '');
672 var value = (field.options && field.options.selectedIndex > -1)
673 ? field.options[field.options.selectedIndex].value : field.value;
675 if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
678 field.className += ' cbi-input-invalid';
686 if( ! field.form.cbi_validators )
687 field.form.cbi_validators = [ ];
689 field.form.cbi_validators.push(validator);
691 cbi_bind(field, "blur", validator);
692 cbi_bind(field, "keyup", validator);
694 if (field.nodeName == 'SELECT')
696 cbi_bind(field, "change", validator);
697 cbi_bind(field, "click", validator);
700 field.setAttribute("cbi_validate", validator);
701 field.setAttribute("cbi_datatype", type);
702 field.setAttribute("cbi_optional", (!!optional).toString());
706 var fcbox = document.getElementById('cbi.combobox.' + field.id);
708 cbi_validate_field(fcbox, optional, type);
712 if( ! String.serialize )
713 String.serialize = function(o)
729 for( var i = 0; i < o.length; i++ )
730 s += (s ? ', ' : '') + String.serialize(o[i]);
732 return '[ ' + s + ' ]';
741 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
743 return '{ ' + s + ' }';
750 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
751 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
755 return '"' + o + '"';
765 if( ! String.format )
766 String.format = function()
768 if (!arguments || arguments.length < 1 || !RegExp)
771 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
772 var quot_esc = [/"/g, '"', /'/g, '''];
775 for( var i = 0; i < r.length; i += 2 )
776 s = s.replace(r[i], r[i+1]);
780 var str = arguments[0];
782 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t))/;
783 var a = b = [], numSubstitutions = 0, numMatches = 0;
785 while( a = re.exec(str) )
788 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
789 var pPrecision = a[6], pType = a[7];
799 if (numSubstitutions++ < arguments.length)
801 var param = arguments[numSubstitutions];
804 if (pPad && pPad.substr(0,1) == "'")
805 pad = leftpart.substr(1,1);
809 var justifyRight = true;
810 if (pJustify && pJustify === "-")
811 justifyRight = false;
815 minLength = parseInt(pMinLength);
818 if (pPrecision && pType == 'f')
819 precision = parseInt(pPrecision.substring(1));
826 subst = (parseInt(param) || 0).toString(2);
830 subst = String.fromCharCode(parseInt(param) || 0);
834 subst = (parseInt(param) || 0);
838 subst = Math.abs(parseInt(param) || 0);
842 subst = (precision > -1)
843 ? ((parseFloat(param) || 0.0)).toFixed(precision)
844 : (parseFloat(param) || 0.0);
848 subst = (parseInt(param) || 0).toString(8);
856 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
860 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
864 subst = esc(param, html_esc);
868 subst = esc(param, quot_esc);
872 subst = String.serialize(param);
879 var ts = (param || 0);
882 tm = Math.floor(ts / 60);
887 th = Math.floor(tm / 60);
892 td = Math.floor(th / 24);
897 ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
898 : String.format('%dh %dm %ds', th, tm, ts);
905 out += leftpart + subst;
906 str = str.substr(m.length);