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 !isNaN(parseFloat(v));
37 return (cbi_validators['float'](v) && (v >= 0));
42 return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v);
45 'ip4addr': function(v)
47 if( v.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?$/) )
49 return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
50 (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
51 (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
52 (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
53 (!RegExp.$5 || ((RegExp.$6 >= 0) && (RegExp.$6 <= 32)))
60 'ip6addr': function(v)
62 if( v.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
64 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
73 if( addr.indexOf('.') > 0 )
75 var off = addr.lastIndexOf(':');
77 if( !(off && cbi_validators.ip4addr(addr.substr(off+1))) )
80 addr = addr.substr(0, off) + ':0:0';
83 if( addr.indexOf('::') >= 0 )
88 for( var i = 1; i < (addr.length-1); i++ )
89 if( addr.charAt(i) == ':' )
95 for( var i = 0; i < (7 - colons); i++ )
98 if (addr.match(/^(.*?)::(.*?)$/))
99 addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
100 (RegExp.$2 ? ':' + RegExp.$2 : '');
103 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
112 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
115 'portrange': function(v)
117 if( v.match(/^(\d+)-(\d+)$/) )
122 return cbi_validators.port(p1) &&
123 cbi_validators.port(p2) &&
124 (parseInt(p1) <= parseInt(p2))
129 return cbi_validators.port(v);
133 'macaddr': function(v)
135 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
140 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
143 'hostname': function(v)
145 return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null);
148 'wpakey': function(v)
151 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
153 return (v.length >= 8) && (v.length <= 63);
156 'wepkey': function(v)
158 if( v.substr(0,2) == 's:' )
161 if( (v.length == 10) || (v.length == 26) )
162 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
164 return (v.length == 5) || (v.length == 13);
167 'uciname': function(v)
169 return (v.match(/^[a-zA-Z0-9_]+$/) != null);
172 'range': function(v, args)
174 var min = parseInt(args[0]);
175 var max = parseInt(args[1]);
176 var val = parseInt(v);
178 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
179 return ((val >= min) && (val <= max));
186 function cbi_d_add(field, dep, next) {
187 var obj = document.getElementById(field);
190 for (var i=0; i<cbi_d.length; i++) {
191 if (cbi_d[i].id == field) {
200 "parent": obj.parentNode.id,
204 cbi_d.unshift(entry);
210 function cbi_d_checkvalue(target, ref) {
211 var t = document.getElementById(target);
215 var tl = document.getElementsByName(target);
217 if( tl.length > 0 && tl[0].type == 'radio' )
218 for( var i = 0; i < tl.length; i++ )
219 if( tl[i].checked ) {
224 value = value ? value : "";
225 } else if (!t.value) {
230 if (t.type == "checkbox") {
231 value = t.checked ? value : "";
235 return (value == ref)
238 function cbi_d_check(deps) {
241 for (var i=0; i<deps.length; i++) {
244 for (var j in deps[i]) {
245 if (j == "!reverse") {
247 } else if (j == "!default") {
251 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
261 function cbi_d_update() {
263 for (var i=0; i<cbi_d.length; i++) {
264 var entry = cbi_d[i];
265 var next = document.getElementById(entry.next)
266 var node = document.getElementById(entry.id)
267 var parent = document.getElementById(entry.parent)
269 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
270 node.parentNode.removeChild(node);
273 cbi_c[entry.parent]--;
274 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
276 parent.appendChild(entry.node);
278 next.parentNode.insertBefore(entry.node, next);
282 cbi_c[entry.parent]++;
286 if (entry && entry.parent) {
295 function cbi_bind(obj, type, callback, mode) {
296 if (!obj.addEventListener) {
297 obj.attachEvent('on' + type,
299 var e = window.event;
301 if (!e.target && e.srcElement)
302 e.target = e.srcElement;
304 return !!callback(e);
308 obj.addEventListener(type, callback, !!mode);
313 function cbi_combobox(id, values, def, man) {
314 var selid = "cbi.combobox." + id;
315 if (document.getElementById(selid)) {
319 var obj = document.getElementById(id)
320 var sel = document.createElement("select");
322 sel.className = 'cbi-input-select';
324 if (obj.nextSibling) {
325 obj.parentNode.insertBefore(sel, obj.nextSibling);
327 obj.parentNode.appendChild(sel);
330 var dt = obj.getAttribute('cbi_datatype');
331 var op = obj.getAttribute('cbi_optional');
334 cbi_validate_field(sel, op == 'true', dt);
336 if (!values[obj.value]) {
337 if (obj.value == "") {
338 var optdef = document.createElement("option");
340 optdef.appendChild(document.createTextNode(def));
341 sel.appendChild(optdef);
343 var opt = document.createElement("option");
344 opt.value = obj.value;
345 opt.selected = "selected";
346 opt.appendChild(document.createTextNode(obj.value));
347 sel.appendChild(opt);
351 for (var i in values) {
352 var opt = document.createElement("option");
355 if (obj.value == i) {
356 opt.selected = "selected";
359 opt.appendChild(document.createTextNode(values[i]));
360 sel.appendChild(opt);
363 var optman = document.createElement("option");
365 optman.appendChild(document.createTextNode(man));
366 sel.appendChild(optman);
368 obj.style.display = "none";
370 cbi_bind(sel, "change", function() {
371 if (sel.selectedIndex == sel.options.length - 1) {
372 obj.style.display = "inline";
373 sel.parentNode.removeChild(sel);
376 obj.value = sel.options[sel.selectedIndex].value;
387 function cbi_combobox_init(id, values, def, man) {
388 var obj = document.getElementById(id);
389 cbi_bind(obj, "blur", function() {
390 cbi_combobox(id, values, def, man)
392 cbi_combobox(id, values, def, man);
395 function cbi_filebrowser(id, url, defpath) {
396 var field = document.getElementById(id);
397 var browser = window.open(
398 url + ( field.value || defpath || '' ) + '?field=' + id,
399 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
405 function cbi_dynlist_init(name)
407 function cbi_dynlist_renumber(e)
410 var childs = e.parentNode.childNodes;
412 for( var i = 0; i < childs.length; i++ )
413 if( childs[i].name == name )
414 childs[i].id = name + '.' + (count++);
419 function cbi_dynlist_keypress(ev)
421 ev = ev ? ev : window.event;
423 var se = ev.target ? ev.target : ev.srcElement;
425 if (se.nodeType == 3)
430 /* backspace, delete */
433 if (se.value.length == 0)
435 if (ev.preventDefault)
443 /* enter, arrow up, arrow down */
447 if (ev.preventDefault)
456 function cbi_dynlist_keydown(ev)
458 ev = ev ? ev : window.event;
460 var se = ev.target ? ev.target : ev.srcElement;
462 if (se.nodeType == 3)
465 var prev = se.previousSibling;
466 while (prev && prev.name != name)
467 prev = prev.previousSibling;
469 var next = se.nextSibling;
470 while (next && next.name != name)
471 next = next.nextSibling;
475 /* backspace, delete */
478 var jump = (ev.keyCode == 8)
479 ? (prev || next) : (next || prev);
481 if (se.value.length == 0 && jump)
483 se.parentNode.removeChild(se.nextSibling);
484 se.parentNode.removeChild(se);
486 cbi_dynlist_renumber(jump);
488 if (ev.preventDefault)
498 var n = document.createElement('input');
502 cbi_bind(n, 'keydown', cbi_dynlist_keydown);
503 cbi_bind(n, 'keypress', cbi_dynlist_keypress);
507 se.parentNode.insertBefore(n, next);
508 se.parentNode.insertBefore(document.createElement('br'), next);
512 se.parentNode.appendChild(n);
513 se.parentNode.appendChild(document.createElement('br'));
516 var dt = se.getAttribute('cbi_datatype');
517 var op = se.getAttribute('cbi_optional') == 'true';
520 cbi_validate_field(n, op, dt);
522 cbi_dynlist_renumber(n);
543 var inputs = document.getElementsByName(name);
544 for( var i = 0; i < inputs.length; i++ )
546 cbi_bind(inputs[i], 'keydown', cbi_dynlist_keydown);
547 cbi_bind(inputs[i], 'keypress', cbi_dynlist_keypress);
551 //Hijacks the CBI form to send via XHR (requires Prototype)
552 function cbi_hijack_forms(layer, win, fail, load) {
553 var forms = layer.getElementsByTagName('form');
554 for (var i=0; i<forms.length; i++) {
555 $(forms[i]).observe('submit', function(event) {
556 // Prevent the form from also submitting the regular way
560 event.element().request({
573 function cbi_t_add(section, tab) {
574 var t = document.getElementById('tab.' + section + '.' + tab);
575 var c = document.getElementById('container.' + section + '.' + tab);
578 cbi_t[section] = (cbi_t[section] || [ ]);
579 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
583 function cbi_t_switch(section, tab) {
584 if( cbi_t[section] && cbi_t[section][tab] ) {
585 var o = cbi_t[section][tab];
586 var h = document.getElementById('tab.' + section);
587 for( var tid in cbi_t[section] ) {
588 var o2 = cbi_t[section][tid];
589 if( o.tab.id != o2.tab.id ) {
590 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
591 o2.container.style.display = 'none';
595 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
596 o2.container.style.display = 'block';
603 function cbi_t_update() {
606 for( var sid in cbi_t )
607 for( var tid in cbi_t[sid] )
608 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
609 cbi_t[sid][tid].tab.style.display = 'none';
611 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
612 cbi_t[sid][tid].tab.style.display = '';
614 var t = cbi_t[sid][tid].tab;
615 t.className += ' cbi-tab-highlighted';
619 if( hl_tabs.length > 0 )
620 window.setTimeout(function() {
621 for( var i = 0; i < hl_tabs.length; i++ )
622 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
627 function cbi_validate_form(form, errmsg)
629 /* if triggered by a section removal or addition, don't validate */
630 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
633 if( form.cbi_validators )
635 for( var i = 0; i < form.cbi_validators.length; i++ )
637 var validator = form.cbi_validators[i];
638 if( !validator() && errmsg )
649 function cbi_validate_reset(form)
652 function() { cbi_validate_form(form, null) }, 100
658 function cbi_validate_field(cbid, optional, type)
660 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
663 if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
666 vargs = RegExp.$2.split(/\s*,\s*/);
669 var vldcb = cbi_validators[type];
673 var validator = function()
678 field.className = field.className.replace(/ cbi-input-invalid/g, '');
681 var value = (field.options && field.options.selectedIndex > -1)
682 ? field.options[field.options.selectedIndex].value : field.value;
684 if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
687 field.className += ' cbi-input-invalid';
695 if( ! field.form.cbi_validators )
696 field.form.cbi_validators = [ ];
698 field.form.cbi_validators.push(validator);
700 cbi_bind(field, "blur", validator);
701 cbi_bind(field, "keyup", validator);
703 if (field.nodeName == 'SELECT')
705 cbi_bind(field, "change", validator);
706 cbi_bind(field, "click", validator);
709 field.setAttribute("cbi_validate", validator);
710 field.setAttribute("cbi_datatype", type);
711 field.setAttribute("cbi_optional", (!!optional).toString());
715 var fcbox = document.getElementById('cbi.combobox.' + field.id);
717 cbi_validate_field(fcbox, optional, type);
721 if( ! String.serialize )
722 String.serialize = function(o)
738 for( var i = 0; i < o.length; i++ )
739 s += (s ? ', ' : '') + String.serialize(o[i]);
741 return '[ ' + s + ' ]';
750 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
752 return '{ ' + s + ' }';
759 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
760 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
764 return '"' + o + '"';
774 if( ! String.format )
775 String.format = function()
777 if (!arguments || arguments.length < 1 || !RegExp)
780 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
781 var quot_esc = [/"/g, '"', /'/g, '''];
784 for( var i = 0; i < r.length; i += 2 )
785 s = s.replace(r[i], r[i+1]);
789 var str = arguments[0];
791 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t))/;
792 var a = b = [], numSubstitutions = 0, numMatches = 0;
794 while( a = re.exec(str) )
797 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
798 var pPrecision = a[6], pType = a[7];
808 if (numSubstitutions++ < arguments.length)
810 var param = arguments[numSubstitutions];
813 if (pPad && pPad.substr(0,1) == "'")
814 pad = leftpart.substr(1,1);
818 var justifyRight = true;
819 if (pJustify && pJustify === "-")
820 justifyRight = false;
824 minLength = parseInt(pMinLength);
827 if (pPrecision && pType == 'f')
828 precision = parseInt(pPrecision.substring(1));
835 subst = (parseInt(param) || 0).toString(2);
839 subst = String.fromCharCode(parseInt(param) || 0);
843 subst = (parseInt(param) || 0);
847 subst = Math.abs(parseInt(param) || 0);
851 subst = (precision > -1)
852 ? ((parseFloat(param) || 0.0)).toFixed(precision)
853 : (parseFloat(param) || 0.0);
857 subst = (parseInt(param) || 0).toString(8);
865 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
869 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
873 subst = esc(param, html_esc);
877 subst = esc(param, quot_esc);
881 subst = String.serialize(param);
888 var ts = (param || 0);
891 tm = Math.floor(ts / 60);
896 th = Math.floor(tm / 60);
901 td = Math.floor(th / 24);
906 ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
907 : String.format('%dh %dm %ds', th, tm, ts);
914 out += leftpart + subst;
915 str = str.substr(m.length);