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 : '');
93 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
102 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
105 'portrange': function(v)
107 if( v.match(/^(\d+)-(\d+)$/) )
112 return cbi_validators.port(p1) &&
113 cbi_validators.port(p2) &&
114 (parseInt(p1) <= parseInt(p2))
119 return cbi_validators.port(v);
123 'macaddr': function(v)
125 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
130 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
133 'hostname': function(v)
135 return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null);
138 'wpakey': function(v)
141 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
143 return (v.length >= 8) && (v.length <= 63);
146 'wepkey': function(v)
148 if( v.substr(0,2) == 's:' )
151 if( (v.length == 10) || (v.length == 26) )
152 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
154 return (v.length == 5) || (v.length == 13);
157 'uciname': function(v)
159 return (v.match(/^[a-zA-Z0-9_]+$/) != null);
162 'range': function(v, args)
164 var min = parseInt(args[0]);
165 var max = parseInt(args[1]);
166 var val = parseInt(v);
168 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
169 return ((val >= min) && (val <= max));
176 function cbi_d_add(field, dep, next) {
177 var obj = document.getElementById(field);
180 for (var i=0; i<cbi_d.length; i++) {
181 if (cbi_d[i].id == field) {
190 "parent": obj.parentNode.id,
194 cbi_d.unshift(entry);
200 function cbi_d_checkvalue(target, ref) {
201 var t = document.getElementById(target);
205 var tl = document.getElementsByName(target);
207 if( tl.length > 0 && tl[0].type == 'radio' )
208 for( var i = 0; i < tl.length; i++ )
209 if( tl[i].checked ) {
214 value = value ? value : "";
215 } else if (!t.value) {
220 if (t.type == "checkbox") {
221 value = t.checked ? value : "";
225 return (value == ref)
228 function cbi_d_check(deps) {
231 for (var i=0; i<deps.length; i++) {
234 for (var j in deps[i]) {
235 if (j == "!reverse") {
237 } else if (j == "!default") {
241 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
251 function cbi_d_update() {
253 for (var i=0; i<cbi_d.length; i++) {
254 var entry = cbi_d[i];
255 var next = document.getElementById(entry.next)
256 var node = document.getElementById(entry.id)
257 var parent = document.getElementById(entry.parent)
259 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
260 node.parentNode.removeChild(node);
263 cbi_c[entry.parent]--;
264 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
266 parent.appendChild(entry.node);
268 next.parentNode.insertBefore(entry.node, next);
272 cbi_c[entry.parent]++;
276 if (entry && entry.parent) {
285 function cbi_bind(obj, type, callback, mode) {
286 if (!obj.addEventListener) {
287 obj.attachEvent('on' + type,
289 var e = window.event;
291 if (!e.target && e.srcElement)
292 e.target = e.srcElement;
294 return !!callback(e);
298 obj.addEventListener(type, callback, !!mode);
303 function cbi_combobox(id, values, def, man) {
304 var selid = "cbi.combobox." + id;
305 if (document.getElementById(selid)) {
309 var obj = document.getElementById(id)
310 var sel = document.createElement("select");
312 sel.className = 'cbi-input-select';
314 if (obj.nextSibling) {
315 obj.parentNode.insertBefore(sel, obj.nextSibling);
317 obj.parentNode.appendChild(sel);
320 var dt = obj.getAttribute('cbi_datatype');
321 var op = obj.getAttribute('cbi_optional');
324 cbi_validate_field(sel, op == 'true', dt);
326 if (!values[obj.value]) {
327 if (obj.value == "") {
328 var optdef = document.createElement("option");
330 optdef.appendChild(document.createTextNode(def));
331 sel.appendChild(optdef);
333 var opt = document.createElement("option");
334 opt.value = obj.value;
335 opt.selected = "selected";
336 opt.appendChild(document.createTextNode(obj.value));
337 sel.appendChild(opt);
341 for (var i in values) {
342 var opt = document.createElement("option");
345 if (obj.value == i) {
346 opt.selected = "selected";
349 opt.appendChild(document.createTextNode(values[i]));
350 sel.appendChild(opt);
353 var optman = document.createElement("option");
355 optman.appendChild(document.createTextNode(man));
356 sel.appendChild(optman);
358 obj.style.display = "none";
360 cbi_bind(sel, "change", function() {
361 if (sel.selectedIndex == sel.options.length - 1) {
362 obj.style.display = "inline";
363 sel.parentNode.removeChild(sel);
366 obj.value = sel.options[sel.selectedIndex].value;
377 function cbi_combobox_init(id, values, def, man) {
378 var obj = document.getElementById(id);
379 cbi_bind(obj, "blur", function() {
380 cbi_combobox(id, values, def, man)
382 cbi_combobox(id, values, def, man);
385 function cbi_filebrowser(id, url, defpath) {
386 var field = document.getElementById(id);
387 var browser = window.open(
388 url + ( field.value || defpath || '' ) + '?field=' + id,
389 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
395 function cbi_dynlist_init(name)
397 function cbi_dynlist_renumber(e)
400 var childs = e.parentNode.childNodes;
402 for( var i = 0; i < childs.length; i++ )
403 if( childs[i].name == name )
404 childs[i].id = name + '.' + (count++);
409 function cbi_dynlist_keypress(ev)
411 ev = ev ? ev : window.event;
413 var se = ev.target ? ev.target : ev.srcElement;
415 if (se.nodeType == 3)
420 /* backspace, delete */
423 if (se.value.length == 0)
425 if (ev.preventDefault)
433 /* enter, arrow up, arrow down */
437 if (ev.preventDefault)
446 function cbi_dynlist_keydown(ev)
448 ev = ev ? ev : window.event;
450 var se = ev.target ? ev.target : ev.srcElement;
452 if (se.nodeType == 3)
455 var prev = se.previousSibling;
456 while (prev && prev.name != name)
457 prev = prev.previousSibling;
459 var next = se.nextSibling;
460 while (next && next.name != name)
461 next = next.nextSibling;
465 /* backspace, delete */
468 var jump = (ev.keyCode == 8)
469 ? (prev || next) : (next || prev);
471 if (se.value.length == 0 && jump)
473 se.parentNode.removeChild(se.nextSibling);
474 se.parentNode.removeChild(se);
476 cbi_dynlist_renumber(jump);
478 if (ev.preventDefault)
488 var n = document.createElement('input');
492 cbi_bind(n, 'keydown', cbi_dynlist_keydown);
493 cbi_bind(n, 'keypress', cbi_dynlist_keypress);
497 se.parentNode.insertBefore(n, next);
498 se.parentNode.insertBefore(document.createElement('br'), next);
502 se.parentNode.appendChild(n);
503 se.parentNode.appendChild(document.createElement('br'));
506 var dt = se.getAttribute('cbi_datatype');
507 var op = se.getAttribute('cbi_optional') == 'true';
510 cbi_validate_field(n, op, dt);
512 cbi_dynlist_renumber(n);
533 var inputs = document.getElementsByName(name);
534 for( var i = 0; i < inputs.length; i++ )
536 cbi_bind(inputs[i], 'keydown', cbi_dynlist_keydown);
537 cbi_bind(inputs[i], 'keypress', cbi_dynlist_keypress);
541 //Hijacks the CBI form to send via XHR (requires Prototype)
542 function cbi_hijack_forms(layer, win, fail, load) {
543 var forms = layer.getElementsByTagName('form');
544 for (var i=0; i<forms.length; i++) {
545 $(forms[i]).observe('submit', function(event) {
546 // Prevent the form from also submitting the regular way
550 event.element().request({
563 function cbi_t_add(section, tab) {
564 var t = document.getElementById('tab.' + section + '.' + tab);
565 var c = document.getElementById('container.' + section + '.' + tab);
568 cbi_t[section] = (cbi_t[section] || [ ]);
569 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
573 function cbi_t_switch(section, tab) {
574 if( cbi_t[section] && cbi_t[section][tab] ) {
575 var o = cbi_t[section][tab];
576 var h = document.getElementById('tab.' + section);
577 for( var tid in cbi_t[section] ) {
578 var o2 = cbi_t[section][tid];
579 if( o.tab.id != o2.tab.id ) {
580 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
581 o2.container.style.display = 'none';
585 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
586 o2.container.style.display = 'block';
593 function cbi_t_update() {
596 for( var sid in cbi_t )
597 for( var tid in cbi_t[sid] )
598 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
599 cbi_t[sid][tid].tab.style.display = 'none';
601 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
602 cbi_t[sid][tid].tab.style.display = '';
604 var t = cbi_t[sid][tid].tab;
605 t.className += ' cbi-tab-highlighted';
609 if( hl_tabs.length > 0 )
610 window.setTimeout(function() {
611 for( var i = 0; i < hl_tabs.length; i++ )
612 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
617 function cbi_validate_form(form, errmsg)
619 /* if triggered by a section removal or addition, don't validate */
620 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
623 if( form.cbi_validators )
625 for( var i = 0; i < form.cbi_validators.length; i++ )
627 var validator = form.cbi_validators[i];
628 if( !validator() && errmsg )
639 function cbi_validate_reset(form)
642 function() { cbi_validate_form(form, null) }, 100
648 function cbi_validate_field(cbid, optional, type)
650 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
653 if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
656 vargs = RegExp.$2.split(/\s*,\s*/);
659 var vldcb = cbi_validators[type];
663 var validator = function()
668 field.className = field.className.replace(/ cbi-input-invalid/g, '');
671 var value = (field.options && field.options.selectedIndex > -1)
672 ? field.options[field.options.selectedIndex].value : field.value;
674 if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
677 field.className += ' cbi-input-invalid';
685 if( ! field.form.cbi_validators )
686 field.form.cbi_validators = [ ];
688 field.form.cbi_validators.push(validator);
690 cbi_bind(field, "blur", validator);
691 cbi_bind(field, "keyup", validator);
693 if (field.nodeName == 'SELECT')
695 cbi_bind(field, "change", validator);
696 cbi_bind(field, "click", validator);
699 field.setAttribute("cbi_validate", validator);
700 field.setAttribute("cbi_datatype", type);
701 field.setAttribute("cbi_optional", (!!optional).toString());
705 var fcbox = document.getElementById('cbi.combobox.' + field.id);
707 cbi_validate_field(fcbox, optional, type);
711 if( ! String.serialize )
712 String.serialize = function(o)
728 for( var i = 0; i < o.length; i++ )
729 s += (s ? ', ' : '') + String.serialize(o[i]);
731 return '[ ' + s + ' ]';
740 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
742 return '{ ' + s + ' }';
749 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
750 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
754 return '"' + o + '"';
764 if( ! String.format )
765 String.format = function()
767 if (!arguments || arguments.length < 1 || !RegExp)
770 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
771 var quot_esc = [/"/g, '"', /'/g, '''];
774 for( var i = 0; i < r.length; i += 2 )
775 s = s.replace(r[i], r[i+1]);
779 var str = arguments[0];
781 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j))/;
782 var a = b = [], numSubstitutions = 0, numMatches = 0;
784 while( a = re.exec(str) )
787 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
788 var pPrecision = a[6], pType = a[7];
798 if (numSubstitutions++ < arguments.length)
800 var param = arguments[numSubstitutions];
803 if (pPad && pPad.substr(0,1) == "'")
804 pad = leftpart.substr(1,1);
808 var justifyRight = true;
809 if (pJustify && pJustify === "-")
810 justifyRight = false;
814 minLength = parseInt(pMinLength);
817 if (pPrecision && pType == 'f')
818 precision = parseInt(pPrecision.substring(1));
825 subst = (parseInt(param) || 0).toString(2);
829 subst = String.fromCharCode(parseInt(param) || 0);
833 subst = (parseInt(param) || 0);
837 subst = Math.abs(parseInt(param) || 0);
841 subst = (precision > -1)
842 ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision)
843 : (parseFloat(param) || 0.0);
847 subst = (parseInt(param) || 0).toString(8);
855 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
859 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
863 subst = esc(param, html_esc);
867 subst = esc(param, quot_esc);
871 subst = String.serialize(param);
877 out += leftpart + subst;
878 str = str.substr(m.length);