2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2011 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 !isNaN(parseFloat(v));
37 return (cbi_validators['float'](v) && (v >= 0));
42 return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v);
45 'neg_ipaddr': function(v)
47 return cbi_validators.ip4addr(v.replace(/^\s*!/, "")) || cbi_validators.ip6addr(v.replace(/^\s*!/, ""));
50 'ip4addr': function(v)
52 if (v.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/))
54 return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
55 (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
56 (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
57 (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
58 ((RegExp.$6.indexOf('.') < 0)
59 ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32))
60 : (cbi_validators.ip4addr(RegExp.$6)))
67 'neg_ip4addr': function(v)
69 return cbi_validators.ip4addr(v.replace(/^\s*!/, ""));
72 'ip6addr': function(v)
74 if( v.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
76 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
85 if( addr.indexOf('.') > 0 )
87 var off = addr.lastIndexOf(':');
89 if( !(off && cbi_validators.ip4addr(addr.substr(off+1))) )
92 addr = addr.substr(0, off) + ':0:0';
95 if( addr.indexOf('::') >= 0 )
100 for( var i = 1; i < (addr.length-1); i++ )
101 if( addr.charAt(i) == ':' )
107 for( var i = 0; i < (7 - colons); i++ )
110 if (addr.match(/^(.*?)::(.*?)$/))
111 addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
112 (RegExp.$2 ? ':' + RegExp.$2 : '');
115 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
124 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
127 'portrange': function(v)
129 if( v.match(/^(\d+)-(\d+)$/) )
134 return cbi_validators.port(p1) &&
135 cbi_validators.port(p2) &&
136 (parseInt(p1) <= parseInt(p2))
141 return cbi_validators.port(v);
145 'macaddr': function(v)
147 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
152 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
155 'hostname': function(v)
158 return (v.match(/^[a-zA-Z]+$/) != null ||
159 (v.match(/^[a-zA-Z0-9][a-zA-Z0-9\-.]*[a-zA-Z0-9]$/) &&
160 v.match(/[^0-9.]/)));
165 'network': function(v)
167 return cbi_validators.uciname(v) || cbi_validators.host(v);
170 'wpakey': function(v)
173 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
175 return (v.length >= 8) && (v.length <= 63);
178 'wepkey': function(v)
180 if( v.substr(0,2) == 's:' )
183 if( (v.length == 10) || (v.length == 26) )
184 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
186 return (v.length == 5) || (v.length == 13);
189 'uciname': function(v)
191 return (v.match(/^[a-zA-Z0-9_]+$/) != null);
194 'neg_network_ip4addr': function(v)
196 v = v.replace(/^\s*!/, "");
197 return cbi_validators.uciname(v) || cbi_validators.ip4addr(v);
200 'range': function(v, args)
202 var min = parseInt(args[0]);
203 var max = parseInt(args[1]);
204 var val = parseInt(v);
206 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
207 return ((val >= min) && (val <= max));
212 'min': function(v, args)
214 var min = parseInt(args[0]);
215 var val = parseInt(v);
217 if (!isNaN(min) && !isNaN(val))
223 'max': function(v, args)
225 var max = parseInt(args[0]);
226 var val = parseInt(v);
228 if (!isNaN(max) && !isNaN(val))
234 'neg': function(v, args)
236 if (args[0] && typeof cbi_validators[args[0]] == "function")
237 return cbi_validators[args[0]](v.replace(/^\s*!\s*/, ''));
242 'list': function(v, args)
244 var cb = cbi_validators[args[0] || 'string'];
245 if (typeof cb == "function")
247 var cbargs = args.slice(1);
248 var values = v.match(/[^\s]+/g);
250 for (var i = 0; i < values.length; i++)
251 if (!cb(values[i], cbargs))
262 function cbi_d_add(field, dep, next) {
263 var obj = document.getElementById(field);
266 for (var i=0; i<cbi_d.length; i++) {
267 if (cbi_d[i].id == field) {
276 "parent": obj.parentNode.id,
280 cbi_d.unshift(entry);
286 function cbi_d_checkvalue(target, ref) {
287 var t = document.getElementById(target);
291 var tl = document.getElementsByName(target);
293 if( tl.length > 0 && tl[0].type == 'radio' )
294 for( var i = 0; i < tl.length; i++ )
295 if( tl[i].checked ) {
300 value = value ? value : "";
301 } else if (!t.value) {
306 if (t.type == "checkbox") {
307 value = t.checked ? value : "";
311 return (value == ref)
314 function cbi_d_check(deps) {
317 for (var i=0; i<deps.length; i++) {
320 for (var j in deps[i]) {
321 if (j == "!reverse") {
323 } else if (j == "!default") {
327 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
337 function cbi_d_update() {
339 for (var i=0; i<cbi_d.length; i++) {
340 var entry = cbi_d[i];
341 var next = document.getElementById(entry.next)
342 var node = document.getElementById(entry.id)
343 var parent = document.getElementById(entry.parent)
345 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
346 node.parentNode.removeChild(node);
349 cbi_c[entry.parent]--;
350 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
352 parent.appendChild(entry.node);
354 next.parentNode.insertBefore(entry.node, next);
358 cbi_c[entry.parent]++;
362 if (entry && entry.parent) {
364 cbi_tag_last(parent);
372 function cbi_bind(obj, type, callback, mode) {
373 if (!obj.addEventListener) {
374 obj.attachEvent('on' + type,
376 var e = window.event;
378 if (!e.target && e.srcElement)
379 e.target = e.srcElement;
381 return !!callback(e);
385 obj.addEventListener(type, callback, !!mode);
390 function cbi_combobox(id, values, def, man) {
391 var selid = "cbi.combobox." + id;
392 if (document.getElementById(selid)) {
396 var obj = document.getElementById(id)
397 var sel = document.createElement("select");
399 sel.className = 'cbi-input-select';
401 if (obj.nextSibling) {
402 obj.parentNode.insertBefore(sel, obj.nextSibling);
404 obj.parentNode.appendChild(sel);
407 var dt = obj.getAttribute('cbi_datatype');
408 var op = obj.getAttribute('cbi_optional');
411 cbi_validate_field(sel, op == 'true', dt);
413 if (!values[obj.value]) {
414 if (obj.value == "") {
415 var optdef = document.createElement("option");
417 optdef.appendChild(document.createTextNode(def));
418 sel.appendChild(optdef);
420 var opt = document.createElement("option");
421 opt.value = obj.value;
422 opt.selected = "selected";
423 opt.appendChild(document.createTextNode(obj.value));
424 sel.appendChild(opt);
428 for (var i in values) {
429 var opt = document.createElement("option");
432 if (obj.value == i) {
433 opt.selected = "selected";
436 opt.appendChild(document.createTextNode(values[i]));
437 sel.appendChild(opt);
440 var optman = document.createElement("option");
442 optman.appendChild(document.createTextNode(man));
443 sel.appendChild(optman);
445 obj.style.display = "none";
447 cbi_bind(sel, "change", function() {
448 if (sel.selectedIndex == sel.options.length - 1) {
449 obj.style.display = "inline";
450 sel.parentNode.removeChild(sel);
453 obj.value = sel.options[sel.selectedIndex].value;
464 function cbi_combobox_init(id, values, def, man) {
465 var obj = document.getElementById(id);
466 cbi_bind(obj, "blur", function() {
467 cbi_combobox(id, values, def, man)
469 cbi_combobox(id, values, def, man);
472 function cbi_filebrowser(id, url, defpath) {
473 var field = document.getElementById(id);
474 var browser = window.open(
475 url + ( field.value || defpath || '' ) + '?field=' + id,
476 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
482 function cbi_browser_init(id, respath, url, defpath)
484 function cbi_browser_btnclick(e) {
485 cbi_filebrowser(id, url, defpath);
489 var field = document.getElementById(id);
491 var btn = document.createElement('img');
492 btn.className = 'cbi-image-button';
493 btn.src = respath + '/cbi/folder.gif';
494 field.parentNode.insertBefore(btn, field.nextSibling);
496 cbi_bind(btn, 'click', cbi_browser_btnclick);
499 function cbi_dynlist_init(name, respath, datatype, optional, choices)
501 var input0 = document.getElementsByName(name)[0];
502 var prefix = input0.name;
503 var parent = input0.parentNode;
507 function cbi_dynlist_redraw(focus, add, del)
511 while (parent.firstChild)
513 var n = parent.firstChild;
514 var i = parseInt(n.index);
518 if (n.nodeName.toLowerCase() == 'input')
519 values.push(n.value || '');
520 else if (n.nodeName.toLowerCase() == 'select')
521 values[values.length-1] = n.options[n.selectedIndex].value;
524 parent.removeChild(n);
530 values.splice(focus, 0, '');
532 else if (values.length == 0)
538 for (var i = 0; i < values.length; i++)
540 var t = document.createElement('input');
541 t.id = prefix + '.' + (i+1);
546 t.className = 'cbi-input-text';
548 var b = document.createElement('img');
549 b.src = respath + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif');
550 b.className = 'cbi-image-button';
552 parent.appendChild(t);
553 parent.appendChild(b);
554 parent.appendChild(document.createElement('br'));
558 cbi_validate_field(t.id, ((i+1) == values.length) || optional, datatype);
563 cbi_combobox_init(t.id, choices[0], '', choices[1]);
564 t.nextSibling.index = i;
566 cbi_bind(t.nextSibling, 'keydown', cbi_dynlist_keydown);
567 cbi_bind(t.nextSibling, 'keypress', cbi_dynlist_keypress);
569 if (i == focus || -i == focus)
570 t.nextSibling.focus();
574 cbi_bind(t, 'keydown', cbi_dynlist_keydown);
575 cbi_bind(t, 'keypress', cbi_dynlist_keypress);
581 else if (-i == focus)
585 /* force cursor to end */
592 cbi_bind(b, 'click', cbi_dynlist_btnclick);
596 function cbi_dynlist_keypress(ev)
598 ev = ev ? ev : window.event;
600 var se = ev.target ? ev.target : ev.srcElement;
602 if (se.nodeType == 3)
607 /* backspace, delete */
610 if (se.value.length == 0)
612 if (ev.preventDefault)
620 /* enter, arrow up, arrow down */
624 if (ev.preventDefault)
633 function cbi_dynlist_keydown(ev)
635 ev = ev ? ev : window.event;
637 var se = ev.target ? ev.target : ev.srcElement;
639 if (se.nodeType == 3)
642 var prev = se.previousSibling;
643 while (prev && prev.name != name)
644 prev = prev.previousSibling;
646 var next = se.nextSibling;
647 while (next && next.name != name)
648 next = next.nextSibling;
650 /* advance one further in combobox case */
651 if (next && next.nextSibling.name == name)
652 next = next.nextSibling;
656 /* backspace, delete */
659 var del = (se.nodeName.toLowerCase() == 'select')
660 ? true : (se.value.length == 0);
664 if (ev.preventDefault)
667 var focus = se.index;
671 cbi_dynlist_redraw(focus, -1, se.index);
680 cbi_dynlist_redraw(-1, se.index, -1);
701 function cbi_dynlist_btnclick(ev)
703 ev = ev ? ev : window.event;
705 var se = ev.target ? ev.target : ev.srcElement;
707 if (se.src.indexOf('remove') > -1)
709 se.previousSibling.value = '';
711 cbi_dynlist_keydown({
712 target: se.previousSibling,
718 cbi_dynlist_keydown({
719 target: se.previousSibling,
727 cbi_dynlist_redraw(-1, -1, -1);
730 //Hijacks the CBI form to send via XHR (requires Prototype)
731 function cbi_hijack_forms(layer, win, fail, load) {
732 var forms = layer.getElementsByTagName('form');
733 for (var i=0; i<forms.length; i++) {
734 $(forms[i]).observe('submit', function(event) {
735 // Prevent the form from also submitting the regular way
739 event.element().request({
752 function cbi_t_add(section, tab) {
753 var t = document.getElementById('tab.' + section + '.' + tab);
754 var c = document.getElementById('container.' + section + '.' + tab);
757 cbi_t[section] = (cbi_t[section] || [ ]);
758 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
762 function cbi_t_switch(section, tab) {
763 if( cbi_t[section] && cbi_t[section][tab] ) {
764 var o = cbi_t[section][tab];
765 var h = document.getElementById('tab.' + section);
766 for( var tid in cbi_t[section] ) {
767 var o2 = cbi_t[section][tid];
768 if( o.tab.id != o2.tab.id ) {
769 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
770 o2.container.style.display = 'none';
774 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
775 o2.container.style.display = 'block';
782 function cbi_t_update() {
786 for( var sid in cbi_t )
787 for( var tid in cbi_t[sid] )
789 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
790 cbi_t[sid][tid].tab.style.display = 'none';
792 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
793 cbi_t[sid][tid].tab.style.display = '';
795 var t = cbi_t[sid][tid].tab;
796 t.className += ' cbi-tab-highlighted';
800 cbi_tag_last(cbi_t[sid][tid].container);
804 if( hl_tabs.length > 0 )
805 window.setTimeout(function() {
806 for( var i = 0; i < hl_tabs.length; i++ )
807 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
814 function cbi_validate_form(form, errmsg)
816 /* if triggered by a section removal or addition, don't validate */
817 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
820 if( form.cbi_validators )
822 for( var i = 0; i < form.cbi_validators.length; i++ )
824 var validator = form.cbi_validators[i];
825 if( !validator() && errmsg )
836 function cbi_validate_reset(form)
839 function() { cbi_validate_form(form, null) }, 100
845 function cbi_validate_field(cbid, optional, type)
847 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
850 if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
853 vargs = RegExp.$2.split(/\s*,\s*/);
856 var vldcb = cbi_validators[type];
860 var validator = function()
865 field.className = field.className.replace(/ cbi-input-invalid/g, '');
868 var value = (field.options && field.options.selectedIndex > -1)
869 ? field.options[field.options.selectedIndex].value : field.value;
871 if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
874 field.className += ' cbi-input-invalid';
882 if( ! field.form.cbi_validators )
883 field.form.cbi_validators = [ ];
885 field.form.cbi_validators.push(validator);
887 cbi_bind(field, "blur", validator);
888 cbi_bind(field, "keyup", validator);
890 if (field.nodeName == 'SELECT')
892 cbi_bind(field, "change", validator);
893 cbi_bind(field, "click", validator);
896 field.setAttribute("cbi_validate", validator);
897 field.setAttribute("cbi_datatype", type);
898 field.setAttribute("cbi_optional", (!!optional).toString());
902 var fcbox = document.getElementById('cbi.combobox.' + field.id);
904 cbi_validate_field(fcbox, optional, type);
908 function cbi_row_swap(elem, up, store)
910 var tr = elem.parentNode;
911 while (tr && tr.nodeName.toLowerCase() != 'tr')
917 var table = tr.parentNode;
918 while (table && table.nodeName.toLowerCase() != 'table')
919 table = table.parentNode;
925 var e = up ? table.rows.length : table.rows.length - 1;
927 for (var idx = s; idx < e; idx++)
929 if (table.rows[idx] == tr)
932 tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]);
934 tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]);
941 for (idx = 2; idx < table.rows.length; idx++)
943 table.rows[idx].className = table.rows[idx].className.replace(
944 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2))
947 if (table.rows[idx].id && table.rows[idx].id.match(/-([^\-]+)$/) )
951 var input = document.getElementById(store);
953 input.value = ids.join(' ');
958 function cbi_tag_last(container)
962 for (var i = 0; i < container.childNodes.length; i++)
964 var c = container.childNodes[i];
965 if (c.nodeType == 1 && c.nodeName.toLowerCase() == 'div')
967 c.className = c.className.replace(/ cbi-value-last$/, '');
974 last.className += ' cbi-value-last';
978 if( ! String.serialize )
979 String.serialize = function(o)
995 for( var i = 0; i < o.length; i++ )
996 s += (s ? ', ' : '') + String.serialize(o[i]);
998 return '[ ' + s + ' ]';
1007 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
1009 return '{ ' + s + ' }';
1016 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
1017 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
1021 return '"' + o + '"';
1026 return o.toString();
1031 if( ! String.format )
1032 String.format = function()
1034 if (!arguments || arguments.length < 1 || !RegExp)
1037 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1038 var quot_esc = [/"/g, '"', /'/g, '''];
1040 function esc(s, r) {
1041 for( var i = 0; i < r.length; i += 2 )
1042 s = s.replace(r[i], r[i+1]);
1046 var str = arguments[0];
1048 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
1049 var a = b = [], numSubstitutions = 0, numMatches = 0;
1051 while( a = re.exec(str) )
1054 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
1055 var pPrecision = a[6], pType = a[7];
1065 if (numSubstitutions++ < arguments.length)
1067 var param = arguments[numSubstitutions];
1070 if (pPad && pPad.substr(0,1) == "'")
1071 pad = leftpart.substr(1,1);
1075 var justifyRight = true;
1076 if (pJustify && pJustify === "-")
1077 justifyRight = false;
1081 minLength = parseInt(pMinLength);
1084 if (pPrecision && pType == 'f')
1085 precision = parseInt(pPrecision.substring(1));
1092 subst = (parseInt(param) || 0).toString(2);
1096 subst = String.fromCharCode(parseInt(param) || 0);
1100 subst = (parseInt(param) || 0);
1104 subst = Math.abs(parseInt(param) || 0);
1108 subst = (precision > -1)
1109 ? ((parseFloat(param) || 0.0)).toFixed(precision)
1110 : (parseFloat(param) || 0.0);
1114 subst = (parseInt(param) || 0).toString(8);
1122 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
1126 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
1130 subst = esc(param, html_esc);
1134 subst = esc(param, quot_esc);
1138 subst = String.serialize(param);
1145 var ts = (param || 0);
1148 tm = Math.floor(ts / 60);
1153 th = Math.floor(tm / 60);
1158 td = Math.floor(th / 24);
1163 ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
1164 : String.format('%dh %dm %ds', th, tm, ts);
1169 var mf = pMinLength ? parseInt(pMinLength) : 1000;
1170 var pr = pPrecision ? Math.floor(10*parseFloat('0'+pPrecision)) : 2;
1173 var val = parseFloat(param || 0);
1174 var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ];
1176 for (i = 0; (i < units.length) && (val > mf); i++)
1179 subst = val.toFixed(pr) + ' ' + units[i];
1185 out += leftpart + subst;
1186 str = str.substr(m.length);