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);
158 function cbi_d_add(field, dep, next) {
159 var obj = document.getElementById(field);
162 for (var i=0; i<cbi_d.length; i++) {
163 if (cbi_d[i].id == field) {
172 "parent": obj.parentNode.id,
176 cbi_d.unshift(entry);
182 function cbi_d_checkvalue(target, ref) {
183 var t = document.getElementById(target);
187 var tl = document.getElementsByName(target);
189 if( tl.length > 0 && tl[0].type == 'radio' )
190 for( var i = 0; i < tl.length; i++ )
191 if( tl[i].checked ) {
196 value = value ? value : "";
197 } else if (!t.value) {
202 if (t.type == "checkbox") {
203 value = t.checked ? value : "";
207 return (value == ref)
210 function cbi_d_check(deps) {
213 for (var i=0; i<deps.length; i++) {
216 for (var j in deps[i]) {
217 if (j == "!reverse") {
219 } else if (j == "!default") {
223 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
233 function cbi_d_update() {
235 for (var i=0; i<cbi_d.length; i++) {
236 var entry = cbi_d[i];
237 var next = document.getElementById(entry.next)
238 var node = document.getElementById(entry.id)
239 var parent = document.getElementById(entry.parent)
241 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
242 node.parentNode.removeChild(node);
245 cbi_c[entry.parent]--;
246 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
248 parent.appendChild(entry.node);
250 next.parentNode.insertBefore(entry.node, next);
254 cbi_c[entry.parent]++;
258 if (entry && entry.parent) {
267 function cbi_bind(obj, type, callback, mode) {
268 if (typeof mode == "undefined") {
271 if (!obj.addEventListener) {
272 ieCallback = function(){
273 var e = window.event;
274 if (!e.target && e.srcElement) {
275 e.target = e.srcElement;
277 e.target['_eCB' + type + callback] = callback;
278 e.target['_eCB' + type + callback](e);
279 e.target['_eCB' + type + callback] = null;
281 obj.attachEvent('on' + type, ieCallback);
283 obj.addEventListener(type, callback, mode);
288 function cbi_combobox(id, values, def, man) {
289 var selid = "cbi.combobox." + id;
290 if (document.getElementById(selid)) {
294 var obj = document.getElementById(id)
295 var sel = document.createElement("select");
297 sel.className = 'cbi-input-select';
298 if (obj.className && obj.className.match(/cbi-input-invalid/)) {
299 sel.className += ' cbi-input-invalid';
301 if (obj.nextSibling) {
302 obj.parentNode.insertBefore(sel, obj.nextSibling);
304 obj.parentNode.appendChild(sel);
307 if (!values[obj.value]) {
308 if (obj.value == "") {
309 var optdef = document.createElement("option");
311 optdef.appendChild(document.createTextNode(def));
312 sel.appendChild(optdef);
314 var opt = document.createElement("option");
315 opt.value = obj.value;
316 opt.selected = "selected";
317 opt.appendChild(document.createTextNode(obj.value));
318 sel.appendChild(opt);
322 for (var i in values) {
323 var opt = document.createElement("option");
326 if (obj.value == i) {
327 opt.selected = "selected";
330 opt.appendChild(document.createTextNode(values[i]));
331 sel.appendChild(opt);
334 var optman = document.createElement("option");
336 optman.appendChild(document.createTextNode(man));
337 sel.appendChild(optman);
339 obj.style.display = "none";
341 cbi_bind(sel, "change", function() {
342 if (sel.selectedIndex == sel.options.length - 1) {
343 obj.style.display = "inline";
344 sel.parentNode.removeChild(sel);
347 obj.value = sel.options[sel.selectedIndex].value;
348 sel.className = (!obj.validate || obj.validate())
349 ? 'cbi-input-select' : 'cbi-input-select cbi-input-invalid';
360 function cbi_combobox_init(id, values, def, man) {
361 var obj = document.getElementById(id);
362 cbi_bind(obj, "blur", function() {
363 cbi_combobox(id, values, def, man)
365 cbi_combobox(id, values, def, man);
368 function cbi_filebrowser(id, url, defpath) {
369 var field = document.getElementById(id);
370 var browser = window.open(
371 url + ( field.value || defpath || '' ) + '?field=' + id,
372 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
378 //Hijacks the CBI form to send via XHR (requires Prototype)
379 function cbi_hijack_forms(layer, win, fail, load) {
380 var forms = layer.getElementsByTagName('form');
381 for (var i=0; i<forms.length; i++) {
382 $(forms[i]).observe('submit', function(event) {
383 // Prevent the form from also submitting the regular way
387 event.element().request({
400 function cbi_t_add(section, tab) {
401 var t = document.getElementById('tab.' + section + '.' + tab);
402 var c = document.getElementById('container.' + section + '.' + tab);
405 cbi_t[section] = (cbi_t[section] || [ ]);
406 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
410 function cbi_t_switch(section, tab) {
411 if( cbi_t[section] && cbi_t[section][tab] ) {
412 var o = cbi_t[section][tab];
413 var h = document.getElementById('tab.' + section);
414 for( var tid in cbi_t[section] ) {
415 var o2 = cbi_t[section][tid];
416 if( o.tab.id != o2.tab.id ) {
417 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
418 o2.container.style.display = 'none';
422 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
423 o2.container.style.display = 'block';
430 function cbi_t_update() {
433 for( var sid in cbi_t )
434 for( var tid in cbi_t[sid] )
435 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
436 cbi_t[sid][tid].tab.style.display = 'none';
438 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
439 cbi_t[sid][tid].tab.style.display = '';
441 var t = cbi_t[sid][tid].tab;
442 t.className += ' cbi-tab-highlighted';
446 if( hl_tabs.length > 0 )
447 window.setTimeout(function() {
448 for( var i = 0; i < hl_tabs.length; i++ )
449 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
454 function cbi_validate_form(form, errmsg)
456 /* if triggered by a section removal or addition, don't validate */
457 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
460 if( form.cbi_validators )
462 for( var i = 0; i < form.cbi_validators.length; i++ )
464 var validator = form.cbi_validators[i];
465 if( !validator() && errmsg )
476 function cbi_validate_reset(form)
479 function() { cbi_validate_form(form, null) }, 100
485 function cbi_validate_field(cbid, optional, type)
487 var field = document.getElementById(cbid);
488 var vldcb = cbi_validators[type];
492 var validator = function()
497 field.className = field.className.replace(/ cbi-input-invalid/g, '');
500 var value = (field.options) ? field.options[field.options.selectedIndex].value : field.value;
501 if( !(((value.length == 0) && optional) || vldcb(value)) )
504 field.className += ' cbi-input-invalid';
512 if( ! field.form.cbi_validators )
513 field.form.cbi_validators = [ ];
515 field.form.cbi_validators.push(validator);
516 field.onblur = field.onkeyup = field.validate = validator;
522 if( ! String.serialize )
523 String.serialize = function(o)
539 for( var i = 0; i < o.length; i++ )
540 s += (s ? ', ' : '') + String.serialize(o[i]);
542 return '[ ' + s + ' ]';
551 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
553 return '{ ' + s + ' }';
560 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
561 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
565 return '"' + o + '"';
575 if( ! String.format )
576 String.format = function()
578 if (!arguments || arguments.length < 1 || !RegExp)
581 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
582 var quot_esc = [/"/g, '"', /'/g, '''];
585 for( var i = 0; i < r.length; i += 2 )
586 s = s.replace(r[i], r[i+1]);
590 var str = arguments[0];
592 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j))/;
593 var a = b = [], numSubstitutions = 0, numMatches = 0;
595 while( a = re.exec(str) )
598 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
599 var pPrecision = a[6], pType = a[7];
609 if (numSubstitutions++ < arguments.length)
611 var param = arguments[numSubstitutions];
614 if (pPad && pPad.substr(0,1) == "'")
615 pad = leftpart.substr(1,1);
619 var justifyRight = true;
620 if (pJustify && pJustify === "-")
621 justifyRight = false;
625 minLength = parseInt(pMinLength);
628 if (pPrecision && pType == 'f')
629 precision = parseInt(pPrecision.substring(1));
636 subst = (parseInt(param) || 0).toString(2);
640 subst = String.fromCharCode(parseInt(param) || 0);
644 subst = (parseInt(param) || 0);
648 subst = Math.abs(parseInt(param) || 0);
652 subst = (precision > -1)
653 ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision)
654 : (parseFloat(param) || 0.0);
658 subst = (parseInt(param) || 0).toString(8);
666 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
670 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
674 subst = esc(param, html_esc);
678 subst = esc(param, quot_esc);
682 subst = String.serialize(param);
688 out += leftpart + subst;
689 str = str.substr(m.length);