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);
160 'range': function(v, args)
162 var min = parseInt(args[0]);
163 var max = parseInt(args[1]);
164 var val = parseInt(v);
166 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
167 return ((val >= min) && (val <= max));
174 function cbi_d_add(field, dep, next) {
175 var obj = document.getElementById(field);
178 for (var i=0; i<cbi_d.length; i++) {
179 if (cbi_d[i].id == field) {
188 "parent": obj.parentNode.id,
192 cbi_d.unshift(entry);
198 function cbi_d_checkvalue(target, ref) {
199 var t = document.getElementById(target);
203 var tl = document.getElementsByName(target);
205 if( tl.length > 0 && tl[0].type == 'radio' )
206 for( var i = 0; i < tl.length; i++ )
207 if( tl[i].checked ) {
212 value = value ? value : "";
213 } else if (!t.value) {
218 if (t.type == "checkbox") {
219 value = t.checked ? value : "";
223 return (value == ref)
226 function cbi_d_check(deps) {
229 for (var i=0; i<deps.length; i++) {
232 for (var j in deps[i]) {
233 if (j == "!reverse") {
235 } else if (j == "!default") {
239 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
249 function cbi_d_update() {
251 for (var i=0; i<cbi_d.length; i++) {
252 var entry = cbi_d[i];
253 var next = document.getElementById(entry.next)
254 var node = document.getElementById(entry.id)
255 var parent = document.getElementById(entry.parent)
257 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
258 node.parentNode.removeChild(node);
261 cbi_c[entry.parent]--;
262 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
264 parent.appendChild(entry.node);
266 next.parentNode.insertBefore(entry.node, next);
270 cbi_c[entry.parent]++;
274 if (entry && entry.parent) {
283 function cbi_bind(obj, type, callback, mode) {
284 if (!obj.addEventListener) {
285 obj.attachEvent('on' + type,
287 var e = window.event;
289 if (!e.target && e.srcElement)
290 e.target = e.srcElement;
292 return !!callback(e);
296 obj.addEventListener(type, callback, !!mode);
301 function cbi_combobox(id, values, def, man) {
302 var selid = "cbi.combobox." + id;
303 if (document.getElementById(selid)) {
307 var obj = document.getElementById(id)
308 var sel = document.createElement("select");
310 sel.className = 'cbi-input-select';
312 if (obj.nextSibling) {
313 obj.parentNode.insertBefore(sel, obj.nextSibling);
315 obj.parentNode.appendChild(sel);
318 var dt = obj.getAttribute('cbi_datatype');
319 var op = obj.getAttribute('cbi_optional');
322 cbi_validate_field(sel, op == 'true', dt);
324 if (!values[obj.value]) {
325 if (obj.value == "") {
326 var optdef = document.createElement("option");
328 optdef.appendChild(document.createTextNode(def));
329 sel.appendChild(optdef);
331 var opt = document.createElement("option");
332 opt.value = obj.value;
333 opt.selected = "selected";
334 opt.appendChild(document.createTextNode(obj.value));
335 sel.appendChild(opt);
339 for (var i in values) {
340 var opt = document.createElement("option");
343 if (obj.value == i) {
344 opt.selected = "selected";
347 opt.appendChild(document.createTextNode(values[i]));
348 sel.appendChild(opt);
351 var optman = document.createElement("option");
353 optman.appendChild(document.createTextNode(man));
354 sel.appendChild(optman);
356 obj.style.display = "none";
358 cbi_bind(sel, "change", function() {
359 if (sel.selectedIndex == sel.options.length - 1) {
360 obj.style.display = "inline";
361 sel.parentNode.removeChild(sel);
364 obj.value = sel.options[sel.selectedIndex].value;
375 function cbi_combobox_init(id, values, def, man) {
376 var obj = document.getElementById(id);
377 cbi_bind(obj, "blur", function() {
378 cbi_combobox(id, values, def, man)
380 cbi_combobox(id, values, def, man);
383 function cbi_filebrowser(id, url, defpath) {
384 var field = document.getElementById(id);
385 var browser = window.open(
386 url + ( field.value || defpath || '' ) + '?field=' + id,
387 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
393 function cbi_dynlist_init(name)
395 function cbi_dynlist_renumber(e)
398 var childs = e.parentNode.childNodes;
400 for( var i = 0; i < childs.length; i++ )
401 if( childs[i].name == name )
402 childs[i].id = name + '.' + (count++);
407 function cbi_dynlist_keypress(ev)
409 ev = ev ? ev : window.event;
411 var se = ev.target ? ev.target : ev.srcElement;
413 if (se.nodeType == 3)
418 /* backspace, delete */
421 if (se.value.length == 0)
423 if (ev.preventDefault)
431 /* enter, arrow up, arrow down */
435 if (ev.preventDefault)
444 function cbi_dynlist_keydown(ev)
446 ev = ev ? ev : window.event;
448 var se = ev.target ? ev.target : ev.srcElement;
450 if (se.nodeType == 3)
453 var prev = se.previousSibling;
454 while (prev && prev.name != name)
455 prev = prev.previousSibling;
457 var next = se.nextSibling;
458 while (next && next.name != name)
459 next = next.nextSibling;
463 /* backspace, delete */
466 var jump = (ev.keyCode == 8)
467 ? (prev || next) : (next || prev);
469 if (se.value.length == 0 && jump)
471 se.parentNode.removeChild(se.nextSibling);
472 se.parentNode.removeChild(se);
474 cbi_dynlist_renumber(jump);
476 if (ev.preventDefault)
486 var n = document.createElement('input');
490 cbi_bind(n, 'keydown', cbi_dynlist_keydown);
491 cbi_bind(n, 'keypress', cbi_dynlist_keypress);
495 se.parentNode.insertBefore(n, next);
496 se.parentNode.insertBefore(document.createElement('br'), next);
500 se.parentNode.appendChild(n);
501 se.parentNode.appendChild(document.createElement('br'));
504 var dt = se.getAttribute('cbi_datatype');
505 var op = se.getAttribute('cbi_optional') == 'true';
508 cbi_validate_field(n, op, dt);
510 cbi_dynlist_renumber(n);
531 var inputs = document.getElementsByName(name);
532 for( var i = 0; i < inputs.length; i++ )
534 cbi_bind(inputs[i], 'keydown', cbi_dynlist_keydown);
535 cbi_bind(inputs[i], 'keypress', cbi_dynlist_keypress);
539 //Hijacks the CBI form to send via XHR (requires Prototype)
540 function cbi_hijack_forms(layer, win, fail, load) {
541 var forms = layer.getElementsByTagName('form');
542 for (var i=0; i<forms.length; i++) {
543 $(forms[i]).observe('submit', function(event) {
544 // Prevent the form from also submitting the regular way
548 event.element().request({
561 function cbi_t_add(section, tab) {
562 var t = document.getElementById('tab.' + section + '.' + tab);
563 var c = document.getElementById('container.' + section + '.' + tab);
566 cbi_t[section] = (cbi_t[section] || [ ]);
567 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
571 function cbi_t_switch(section, tab) {
572 if( cbi_t[section] && cbi_t[section][tab] ) {
573 var o = cbi_t[section][tab];
574 var h = document.getElementById('tab.' + section);
575 for( var tid in cbi_t[section] ) {
576 var o2 = cbi_t[section][tid];
577 if( o.tab.id != o2.tab.id ) {
578 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
579 o2.container.style.display = 'none';
583 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
584 o2.container.style.display = 'block';
591 function cbi_t_update() {
594 for( var sid in cbi_t )
595 for( var tid in cbi_t[sid] )
596 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
597 cbi_t[sid][tid].tab.style.display = 'none';
599 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
600 cbi_t[sid][tid].tab.style.display = '';
602 var t = cbi_t[sid][tid].tab;
603 t.className += ' cbi-tab-highlighted';
607 if( hl_tabs.length > 0 )
608 window.setTimeout(function() {
609 for( var i = 0; i < hl_tabs.length; i++ )
610 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
615 function cbi_validate_form(form, errmsg)
617 /* if triggered by a section removal or addition, don't validate */
618 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
621 if( form.cbi_validators )
623 for( var i = 0; i < form.cbi_validators.length; i++ )
625 var validator = form.cbi_validators[i];
626 if( !validator() && errmsg )
637 function cbi_validate_reset(form)
640 function() { cbi_validate_form(form, null) }, 100
646 function cbi_validate_field(cbid, optional, type)
648 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
651 if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
654 vargs = RegExp.$2.split(/\s*,\s*/);
657 var vldcb = cbi_validators[type];
661 var validator = function()
666 field.className = field.className.replace(/ cbi-input-invalid/g, '');
669 var value = (field.options && field.options.selectedIndex > -1)
670 ? field.options[field.options.selectedIndex].value : field.value;
672 if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
675 field.className += ' cbi-input-invalid';
683 if( ! field.form.cbi_validators )
684 field.form.cbi_validators = [ ];
686 field.form.cbi_validators.push(validator);
688 cbi_bind(field, "blur", validator);
689 cbi_bind(field, "keyup", validator);
691 if (field.nodeName == 'SELECT')
693 cbi_bind(field, "change", validator);
694 cbi_bind(field, "click", validator);
697 field.setAttribute("cbi_validate", validator);
698 field.setAttribute("cbi_datatype", type);
699 field.setAttribute("cbi_optional", (!!optional).toString());
703 var fcbox = document.getElementById('cbi.combobox.' + field.id);
705 cbi_validate_field(fcbox, optional, type);
709 if( ! String.serialize )
710 String.serialize = function(o)
726 for( var i = 0; i < o.length; i++ )
727 s += (s ? ', ' : '') + String.serialize(o[i]);
729 return '[ ' + s + ' ]';
738 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
740 return '{ ' + s + ' }';
747 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
748 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
752 return '"' + o + '"';
762 if( ! String.format )
763 String.format = function()
765 if (!arguments || arguments.length < 1 || !RegExp)
768 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
769 var quot_esc = [/"/g, '"', /'/g, '''];
772 for( var i = 0; i < r.length; i += 2 )
773 s = s.replace(r[i], r[i+1]);
777 var str = arguments[0];
779 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j))/;
780 var a = b = [], numSubstitutions = 0, numMatches = 0;
782 while( a = re.exec(str) )
785 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
786 var pPrecision = a[6], pType = a[7];
796 if (numSubstitutions++ < arguments.length)
798 var param = arguments[numSubstitutions];
801 if (pPad && pPad.substr(0,1) == "'")
802 pad = leftpart.substr(1,1);
806 var justifyRight = true;
807 if (pJustify && pJustify === "-")
808 justifyRight = false;
812 minLength = parseInt(pMinLength);
815 if (pPrecision && pType == 'f')
816 precision = parseInt(pPrecision.substring(1));
823 subst = (parseInt(param) || 0).toString(2);
827 subst = String.fromCharCode(parseInt(param) || 0);
831 subst = (parseInt(param) || 0);
835 subst = Math.abs(parseInt(param) || 0);
839 subst = (precision > -1)
840 ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision)
841 : (parseFloat(param) || 0.0);
845 subst = (parseInt(param) || 0).toString(8);
853 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
857 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
861 subst = esc(param, html_esc);
865 subst = esc(param, quot_esc);
869 subst = String.serialize(param);
875 out += leftpart + subst;
876 str = str.substr(m.length);