2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 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
20 var cbi_validators = {
22 'integer': function(v)
24 return (v.match(/^-?[0-9]+$/) != null);
27 'uinteger': function(v)
29 return (cbi_validators.integer(v) && (v >= 0));
34 return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v);
37 'ip4addr': function(v)
39 if( v.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?$/) )
41 return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
42 (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
43 (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
44 (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
45 (!RegExp.$5 || ((RegExp.$6 >= 0) && (RegExp.$6 <= 32)))
52 'ip6addr': function(v)
54 if( v.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
56 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
65 if( addr.indexOf('.') > 0 )
67 var off = addr.lastIndexOf(':');
69 if( !(off && cbi_validators.ip4addr(addr.substr(off+1))) )
72 addr = addr.substr(0, off) + ':0:0';
75 if( addr.indexOf('::') < 0 )
77 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
82 for( var i = 0, last = 0, comp = false; i <= addr.length; i++ )
84 if( (addr.charAt(i) == ':') || (i == addr.length) )
86 if( (i == last) && !comp )
92 var f = addr.substring(last, i);
93 if( !(f && f.match(/^[a-fA-F0-9]{1,4}$/)) )
102 return (fields == 8);
111 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
114 'portrange': function(v)
116 if( v.match(/^(\d+)-(\d+)$/) )
121 return cbi_validators.port(p1) &&
122 cbi_validators.port(p2) &&
123 (parseInt(p1) <= parseInt(p2))
128 return cbi_validators.port(v);
132 'macaddr': function(v)
134 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
139 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
142 'hostname': function(v)
144 return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null);
147 'wpakey': function(v)
150 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
152 return (v.length >= 8) && (v.length <= 63);
155 'wepkey': function(v)
157 if( v.substr(0,2) == 's:' )
160 if( (v.length == 10) || (v.length == 26) )
161 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
163 return (v.length == 5) || (v.length == 13);
169 function cbi_d_add(field, dep, next) {
170 var obj = document.getElementById(field);
173 for (var i=0; i<cbi_d.length; i++) {
174 if (cbi_d[i].id == field) {
183 "parent": obj.parentNode.id,
187 cbi_d.unshift(entry);
193 function cbi_d_checkvalue(target, ref) {
194 var t = document.getElementById(target);
198 var tl = document.getElementsByName(target);
200 if( tl.length > 0 && tl[0].type == 'radio' )
201 for( var i = 0; i < tl.length; i++ )
202 if( tl[i].checked ) {
207 value = value ? value : "";
208 } else if (!t.value) {
213 if (t.type == "checkbox") {
214 value = t.checked ? value : "";
218 return (value == ref)
221 function cbi_d_check(deps) {
224 for (var i=0; i<deps.length; i++) {
227 for (var j in deps[i]) {
228 if (j == "!reverse") {
230 } else if (j == "!default") {
234 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
244 function cbi_d_update() {
246 for (var i=0; i<cbi_d.length; i++) {
247 var entry = cbi_d[i];
248 var next = document.getElementById(entry.next)
249 var node = document.getElementById(entry.id)
250 var parent = document.getElementById(entry.parent)
252 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
253 node.parentNode.removeChild(node);
256 cbi_c[entry.parent]--;
257 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
259 parent.appendChild(entry.node);
261 next.parentNode.insertBefore(entry.node, next);
265 cbi_c[entry.parent]++;
278 function cbi_bind(obj, type, callback, mode) {
279 if (typeof mode == "undefined") {
282 if (!obj.addEventListener) {
283 ieCallback = function(){
284 var e = window.event;
285 if (!e.target && e.srcElement) {
286 e.target = e.srcElement;
288 e.target['_eCB' + type + callback] = callback;
289 e.target['_eCB' + type + callback](e);
290 e.target['_eCB' + type + callback] = null;
292 obj.attachEvent('on' + type, ieCallback);
294 obj.addEventListener(type, callback, mode);
299 function cbi_combobox(id, values, def, man) {
300 var selid = "cbi.combobox." + id;
301 if (document.getElementById(selid)) {
305 var obj = document.getElementById(id)
306 var sel = document.createElement("select");
308 sel.className = 'cbi-input-select';
309 if (obj.nextSibling) {
310 obj.parentNode.insertBefore(sel, obj.nextSibling);
312 obj.parentNode.appendChild(sel);
315 if (!values[obj.value]) {
316 if (obj.value == "") {
317 var optdef = document.createElement("option");
319 optdef.appendChild(document.createTextNode(def));
320 sel.appendChild(optdef);
322 var opt = document.createElement("option");
323 opt.value = obj.value;
324 opt.selected = "selected";
325 opt.appendChild(document.createTextNode(obj.value));
326 sel.appendChild(opt);
330 for (var i in values) {
331 var opt = document.createElement("option");
334 if (obj.value == i) {
335 opt.selected = "selected";
338 opt.appendChild(document.createTextNode(values[i]));
339 sel.appendChild(opt);
342 var optman = document.createElement("option");
344 optman.appendChild(document.createTextNode(man));
345 sel.appendChild(optman);
347 obj.style.display = "none";
349 cbi_bind(sel, "change", function() {
350 if (sel.selectedIndex == sel.options.length - 1) {
351 obj.style.display = "inline";
352 sel.parentNode.removeChild(sel);
355 obj.value = sel.options[sel.selectedIndex].value;
366 function cbi_combobox_init(id, values, def, man) {
367 var obj = document.getElementById(id);
368 cbi_bind(obj, "blur", function() {
369 cbi_combobox(id, values, def, man)
371 cbi_combobox(id, values, def, man);
374 function cbi_filebrowser(id, url, defpath) {
375 var field = document.getElementById(id);
376 var browser = window.open(
377 url + ( field.value || defpath || '' ) + '?field=' + id,
378 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
384 //Hijacks the CBI form to send via XHR (requires Prototype)
385 function cbi_hijack_forms(layer, win, fail, load) {
386 var forms = layer.getElementsByTagName('form');
387 for (var i=0; i<forms.length; i++) {
388 $(forms[i]).observe('submit', function(event) {
389 // Prevent the form from also submitting the regular way
393 event.element().request({
406 function cbi_t_add(section, tab) {
407 var t = document.getElementById('tab.' + section + '.' + tab);
408 var c = document.getElementById('container.' + section + '.' + tab);
411 cbi_t[section] = (cbi_t[section] || [ ]);
412 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
416 function cbi_t_switch(section, tab) {
417 if( cbi_t[section] && cbi_t[section][tab] ) {
418 var o = cbi_t[section][tab];
419 var h = document.getElementById('tab.' + section);
420 for( var tid in cbi_t[section] ) {
421 var o2 = cbi_t[section][tid];
422 if( o.tab.id != o2.tab.id ) {
423 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
424 o2.container.style.display = 'none';
428 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
429 o2.container.style.display = 'block';
436 function cbi_t_update() {
437 for( var sid in cbi_t )
438 for( var tid in cbi_t[sid] )
439 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
440 cbi_t[sid][tid].tab.style.display = 'none';
442 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
443 cbi_t[sid][tid].tab.style.display = '';
445 var t = cbi_t[sid][tid].tab;
446 window.setTimeout(function() { t.className = t.className.replace(/ cbi-tab-highlighted/g, '') }, 750);
447 cbi_t[sid][tid].tab.className += ' cbi-tab-highlighted';