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 = 0; i < addr.length; i++ )
79 if( addr.charAt(i) == ':' )
85 for( var i = 0; i < (7 - colons); i++ )
88 addr = addr.replace(/::/, ':' + fill + ':');
91 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
100 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
103 'portrange': function(v)
105 if( v.match(/^(\d+)-(\d+)$/) )
110 return cbi_validators.port(p1) &&
111 cbi_validators.port(p2) &&
112 (parseInt(p1) <= parseInt(p2))
117 return cbi_validators.port(v);
121 'macaddr': function(v)
123 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
128 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
131 'hostname': function(v)
133 return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null);
136 'wpakey': function(v)
139 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
141 return (v.length >= 8) && (v.length <= 63);
144 'wepkey': function(v)
146 if( v.substr(0,2) == 's:' )
149 if( (v.length == 10) || (v.length == 26) )
150 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
152 return (v.length == 5) || (v.length == 13);
155 'uciname': function(v)
157 return (v.match(/^[a-zA-Z0-9_]+$/) != null);
162 function cbi_d_add(field, dep, next) {
163 var obj = document.getElementById(field);
166 for (var i=0; i<cbi_d.length; i++) {
167 if (cbi_d[i].id == field) {
176 "parent": obj.parentNode.id,
180 cbi_d.unshift(entry);
186 function cbi_d_checkvalue(target, ref) {
187 var t = document.getElementById(target);
191 var tl = document.getElementsByName(target);
193 if( tl.length > 0 && tl[0].type == 'radio' )
194 for( var i = 0; i < tl.length; i++ )
195 if( tl[i].checked ) {
200 value = value ? value : "";
201 } else if (!t.value) {
206 if (t.type == "checkbox") {
207 value = t.checked ? value : "";
211 return (value == ref)
214 function cbi_d_check(deps) {
217 for (var i=0; i<deps.length; i++) {
220 for (var j in deps[i]) {
221 if (j == "!reverse") {
223 } else if (j == "!default") {
227 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
237 function cbi_d_update() {
239 for (var i=0; i<cbi_d.length; i++) {
240 var entry = cbi_d[i];
241 var next = document.getElementById(entry.next)
242 var node = document.getElementById(entry.id)
243 var parent = document.getElementById(entry.parent)
245 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
246 node.parentNode.removeChild(node);
249 cbi_c[entry.parent]--;
250 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
252 parent.appendChild(entry.node);
254 next.parentNode.insertBefore(entry.node, next);
258 cbi_c[entry.parent]++;
262 if (entry && entry.parent) {
271 function cbi_bind(obj, type, callback, mode) {
272 if (!obj.addEventListener) {
273 obj.attachEvent('on' + type,
275 var e = window.event;
277 if (!e.target && e.srcElement)
278 e.target = e.srcElement;
280 return !!callback(e);
284 obj.addEventListener(type, callback, !!mode);
289 function cbi_combobox(id, values, def, man) {
290 var selid = "cbi.combobox." + id;
291 if (document.getElementById(selid)) {
295 var obj = document.getElementById(id)
296 var sel = document.createElement("select");
298 sel.className = 'cbi-input-select';
300 if (obj.nextSibling) {
301 obj.parentNode.insertBefore(sel, obj.nextSibling);
303 obj.parentNode.appendChild(sel);
306 var dt = obj.getAttribute('cbi_datatype');
307 var op = obj.getAttribute('cbi_optional');
310 cbi_validate_field(sel, op == 'true', dt);
312 if (!values[obj.value]) {
313 if (obj.value == "") {
314 var optdef = document.createElement("option");
316 optdef.appendChild(document.createTextNode(def));
317 sel.appendChild(optdef);
319 var opt = document.createElement("option");
320 opt.value = obj.value;
321 opt.selected = "selected";
322 opt.appendChild(document.createTextNode(obj.value));
323 sel.appendChild(opt);
327 for (var i in values) {
328 var opt = document.createElement("option");
331 if (obj.value == i) {
332 opt.selected = "selected";
335 opt.appendChild(document.createTextNode(values[i]));
336 sel.appendChild(opt);
339 var optman = document.createElement("option");
341 optman.appendChild(document.createTextNode(man));
342 sel.appendChild(optman);
344 obj.style.display = "none";
346 cbi_bind(sel, "change", function() {
347 if (sel.selectedIndex == sel.options.length - 1) {
348 obj.style.display = "inline";
349 sel.parentNode.removeChild(sel);
352 obj.value = sel.options[sel.selectedIndex].value;
363 function cbi_combobox_init(id, values, def, man) {
364 var obj = document.getElementById(id);
365 cbi_bind(obj, "blur", function() {
366 cbi_combobox(id, values, def, man)
368 cbi_combobox(id, values, def, man);
371 function cbi_filebrowser(id, url, defpath) {
372 var field = document.getElementById(id);
373 var browser = window.open(
374 url + ( field.value || defpath || '' ) + '?field=' + id,
375 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
381 function cbi_dynlist_init(name)
383 function cbi_dynlist_renumber(e)
386 var childs = e.parentNode.childNodes;
388 for( var i = 0; i < childs.length; i++ )
389 if( childs[i].name == name )
390 childs[i].id = name + '.' + (count++);
395 function cbi_dynlist_keypress(ev)
397 ev = ev ? ev : window.event;
399 var se = ev.target ? ev.target : ev.srcElement;
401 if (se.nodeType == 3)
406 /* backspace, delete */
409 if (se.value.length == 0)
411 if (ev.preventDefault)
419 /* enter, arrow up, arrow down */
423 if (ev.preventDefault)
432 function cbi_dynlist_keydown(ev)
434 ev = ev ? ev : window.event;
436 var se = ev.target ? ev.target : ev.srcElement;
438 if (se.nodeType == 3)
441 var prev = se.previousSibling;
442 while (prev && prev.name != name)
443 prev = prev.previousSibling;
445 var next = se.nextSibling;
446 while (next && next.name != name)
447 next = next.nextSibling;
451 /* backspace, delete */
454 var jump = (ev.keyCode == 8)
455 ? (prev || next) : (next || prev);
457 if (se.value.length == 0 && jump)
459 se.parentNode.removeChild(se.nextSibling);
460 se.parentNode.removeChild(se);
462 cbi_dynlist_renumber(jump);
464 if (ev.preventDefault)
474 var n = document.createElement('input');
478 cbi_bind(n, 'keydown', cbi_dynlist_keydown);
479 cbi_bind(n, 'keypress', cbi_dynlist_keypress);
483 se.parentNode.insertBefore(n, next);
484 se.parentNode.insertBefore(document.createElement('br'), next);
488 se.parentNode.appendChild(n);
489 se.parentNode.appendChild(document.createElement('br'));
492 var dt = se.getAttribute('cbi_datatype');
493 var op = se.getAttribute('cbi_optional') == 'true';
496 cbi_validate_field(n, op, dt);
498 cbi_dynlist_renumber(n);
519 var inputs = document.getElementsByName(name);
520 for( var i = 0; i < inputs.length; i++ )
522 cbi_bind(inputs[i], 'keydown', cbi_dynlist_keydown);
523 cbi_bind(inputs[i], 'keypress', cbi_dynlist_keypress);
527 //Hijacks the CBI form to send via XHR (requires Prototype)
528 function cbi_hijack_forms(layer, win, fail, load) {
529 var forms = layer.getElementsByTagName('form');
530 for (var i=0; i<forms.length; i++) {
531 $(forms[i]).observe('submit', function(event) {
532 // Prevent the form from also submitting the regular way
536 event.element().request({
549 function cbi_t_add(section, tab) {
550 var t = document.getElementById('tab.' + section + '.' + tab);
551 var c = document.getElementById('container.' + section + '.' + tab);
554 cbi_t[section] = (cbi_t[section] || [ ]);
555 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
559 function cbi_t_switch(section, tab) {
560 if( cbi_t[section] && cbi_t[section][tab] ) {
561 var o = cbi_t[section][tab];
562 var h = document.getElementById('tab.' + section);
563 for( var tid in cbi_t[section] ) {
564 var o2 = cbi_t[section][tid];
565 if( o.tab.id != o2.tab.id ) {
566 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
567 o2.container.style.display = 'none';
571 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
572 o2.container.style.display = 'block';
579 function cbi_t_update() {
582 for( var sid in cbi_t )
583 for( var tid in cbi_t[sid] )
584 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
585 cbi_t[sid][tid].tab.style.display = 'none';
587 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
588 cbi_t[sid][tid].tab.style.display = '';
590 var t = cbi_t[sid][tid].tab;
591 t.className += ' cbi-tab-highlighted';
595 if( hl_tabs.length > 0 )
596 window.setTimeout(function() {
597 for( var i = 0; i < hl_tabs.length; i++ )
598 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
603 function cbi_validate_form(form, errmsg)
605 /* if triggered by a section removal or addition, don't validate */
606 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
609 if( form.cbi_validators )
611 for( var i = 0; i < form.cbi_validators.length; i++ )
613 var validator = form.cbi_validators[i];
614 if( !validator() && errmsg )
625 function cbi_validate_reset(form)
628 function() { cbi_validate_form(form, null) }, 100
634 function cbi_validate_field(cbid, optional, type)
636 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
637 var vldcb = cbi_validators[type];
641 var validator = function()
646 field.className = field.className.replace(/ cbi-input-invalid/g, '');
649 var value = (field.options && field.options.selectedIndex > -1)
650 ? field.options[field.options.selectedIndex].value : field.value;
652 if( !(((value.length == 0) && optional) || vldcb(value)) )
655 field.className += ' cbi-input-invalid';
663 if( ! field.form.cbi_validators )
664 field.form.cbi_validators = [ ];
666 field.form.cbi_validators.push(validator);
668 cbi_bind(field, "blur", validator);
669 cbi_bind(field, "keyup", validator);
671 if (field.nodeName == 'SELECT')
673 cbi_bind(field, "change", validator);
674 cbi_bind(field, "click", validator);
677 field.setAttribute("cbi_validate", validator);
678 field.setAttribute("cbi_datatype", type);
679 field.setAttribute("cbi_optional", (!!optional).toString());
683 var fcbox = document.getElementById('cbi.combobox.' + field.id);
685 cbi_validate_field(fcbox, optional, type);
689 if( ! String.serialize )
690 String.serialize = function(o)
706 for( var i = 0; i < o.length; i++ )
707 s += (s ? ', ' : '') + String.serialize(o[i]);
709 return '[ ' + s + ' ]';
718 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
720 return '{ ' + s + ' }';
727 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
728 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
732 return '"' + o + '"';
742 if( ! String.format )
743 String.format = function()
745 if (!arguments || arguments.length < 1 || !RegExp)
748 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
749 var quot_esc = [/"/g, '"', /'/g, '''];
752 for( var i = 0; i < r.length; i += 2 )
753 s = s.replace(r[i], r[i+1]);
757 var str = arguments[0];
759 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j))/;
760 var a = b = [], numSubstitutions = 0, numMatches = 0;
762 while( a = re.exec(str) )
765 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
766 var pPrecision = a[6], pType = a[7];
776 if (numSubstitutions++ < arguments.length)
778 var param = arguments[numSubstitutions];
781 if (pPad && pPad.substr(0,1) == "'")
782 pad = leftpart.substr(1,1);
786 var justifyRight = true;
787 if (pJustify && pJustify === "-")
788 justifyRight = false;
792 minLength = parseInt(pMinLength);
795 if (pPrecision && pType == 'f')
796 precision = parseInt(pPrecision.substring(1));
803 subst = (parseInt(param) || 0).toString(2);
807 subst = String.fromCharCode(parseInt(param) || 0);
811 subst = (parseInt(param) || 0);
815 subst = Math.abs(parseInt(param) || 0);
819 subst = (precision > -1)
820 ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision)
821 : (parseFloat(param) || 0.0);
825 subst = (parseInt(param) || 0).toString(8);
833 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
837 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
841 subst = esc(param, html_esc);
845 subst = esc(param, quot_esc);
849 subst = String.serialize(param);
855 out += leftpart + subst;
856 str = str.substr(m.length);