9af2b0def2e6839f24ce267cb73ea51ee86ebe2d
[project/luci.git] / libs / cbi / htdocs / luci-static / resources / cbi.js
1 /*
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 */
15
16 var cbi_d = [];
17 var cbi_t = [];
18 var cbi_c = [];
19
20 var cbi_validators = {
21
22 'integer': function(v)
23 {
24 return (v.match(/^-?[0-9]+$/) != null);
25 },
26
27 'uinteger': function(v)
28 {
29 return (cbi_validators.integer(v) && (v >= 0));
30 },
31
32 'ipaddr': function(v)
33 {
34 return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v);
35 },
36
37 'ip4addr': function(v)
38 {
39 if( v.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?$/) )
40 {
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)))
46 ;
47 }
48
49 return false;
50 },
51
52 'ip6addr': function(v)
53 {
54 if( v.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
55 {
56 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
57 {
58 var addr = RegExp.$1;
59
60 if( addr == '::' )
61 {
62 return true;
63 }
64
65 if( addr.indexOf('.') > 0 )
66 {
67 var off = addr.lastIndexOf(':');
68
69 if( !(off && cbi_validators.ip4addr(addr.substr(off+1))) )
70 return false;
71
72 addr = addr.substr(0, off) + ':0:0';
73 }
74
75 if( addr.indexOf('::') < 0 )
76 {
77 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
78 }
79
80 var fields = 0;
81
82 for( var i = 0, last = 0, comp = false; i <= addr.length; i++ )
83 {
84 if( (addr.charAt(i) == ':') || (i == addr.length) )
85 {
86 if( (i == last) && !comp )
87 {
88 comp = true;
89 }
90 else
91 {
92 var f = addr.substring(last, i);
93 if( !(f && f.match(/^[a-fA-F0-9]{1,4}$/)) )
94 return false;
95 }
96
97 fields++;
98 last = i + 1;
99 }
100 }
101
102 return (fields == 8);
103 }
104 }
105
106 return false;
107 },
108
109 'port': function(v)
110 {
111 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
112 },
113
114 'portrange': function(v)
115 {
116 if( v.match(/^(\d+)-(\d+)$/) )
117 {
118 var p1 = RegExp.$1;
119 var p2 = RegExp.$2;
120
121 return cbi_validators.port(p1) &&
122 cbi_validators.port(p2) &&
123 (parseInt(p1) <= parseInt(p2))
124 ;
125 }
126 else
127 {
128 return cbi_validators.port(v);
129 }
130 },
131
132 'macaddr': function(v)
133 {
134 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
135 },
136
137 'host': function(v)
138 {
139 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
140 },
141
142 'hostname': function(v)
143 {
144 return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null);
145 },
146
147 'wpakey': function(v)
148 {
149 if( v.length == 64 )
150 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
151 else
152 return (v.length >= 8) && (v.length <= 63);
153 },
154
155 'wepkey': function(v)
156 {
157 if( v.substr(0,2) == 's:' )
158 v = v.substr(2);
159
160 if( (v.length == 10) || (v.length == 26) )
161 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
162 else
163 return (v.length == 5) || (v.length == 13);
164 },
165
166 };
167
168
169 function cbi_d_add(field, dep, next) {
170 var obj = document.getElementById(field);
171 if (obj) {
172 var entry
173 for (var i=0; i<cbi_d.length; i++) {
174 if (cbi_d[i].id == field) {
175 entry = cbi_d[i];
176 break;
177 }
178 }
179 if (!entry) {
180 entry = {
181 "node": obj,
182 "id": field,
183 "parent": obj.parentNode.id,
184 "next": next,
185 "deps": []
186 };
187 cbi_d.unshift(entry);
188 }
189 entry.deps.push(dep)
190 }
191 }
192
193 function cbi_d_checkvalue(target, ref) {
194 var t = document.getElementById(target);
195 var value;
196
197 if (!t) {
198 var tl = document.getElementsByName(target);
199
200 if( tl.length > 0 && tl[0].type == 'radio' )
201 for( var i = 0; i < tl.length; i++ )
202 if( tl[i].checked ) {
203 value = tl[i].value;
204 break;
205 }
206
207 value = value ? value : "";
208 } else if (!t.value) {
209 value = "";
210 } else {
211 value = t.value;
212
213 if (t.type == "checkbox") {
214 value = t.checked ? value : "";
215 }
216 }
217
218 return (value == ref)
219 }
220
221 function cbi_d_check(deps) {
222 var reverse;
223 var def = false;
224 for (var i=0; i<deps.length; i++) {
225 var istat = true;
226 reverse = false;
227 for (var j in deps[i]) {
228 if (j == "!reverse") {
229 reverse = true;
230 } else if (j == "!default") {
231 def = true;
232 istat = false;
233 } else {
234 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
235 }
236 }
237 if (istat) {
238 return !reverse;
239 }
240 }
241 return def;
242 }
243
244 function cbi_d_update() {
245 var state = false;
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)
251
252 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
253 node.parentNode.removeChild(node);
254 state = true;
255 if( entry.parent )
256 cbi_c[entry.parent]--;
257 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
258 if (!next) {
259 parent.appendChild(entry.node);
260 } else {
261 next.parentNode.insertBefore(entry.node, next);
262 }
263 state = true;
264 if( entry.parent )
265 cbi_c[entry.parent]++;
266 }
267 }
268
269 if (entry.parent) {
270 cbi_t_update();
271 }
272
273 if (state) {
274 cbi_d_update();
275 }
276 }
277
278 function cbi_bind(obj, type, callback, mode) {
279 if (typeof mode == "undefined") {
280 mode = false;
281 }
282 if (!obj.addEventListener) {
283 ieCallback = function(){
284 var e = window.event;
285 if (!e.target && e.srcElement) {
286 e.target = e.srcElement;
287 };
288 e.target['_eCB' + type + callback] = callback;
289 e.target['_eCB' + type + callback](e);
290 e.target['_eCB' + type + callback] = null;
291 };
292 obj.attachEvent('on' + type, ieCallback);
293 } else {
294 obj.addEventListener(type, callback, mode);
295 }
296 return obj;
297 }
298
299 function cbi_combobox(id, values, def, man) {
300 var selid = "cbi.combobox." + id;
301 if (document.getElementById(selid)) {
302 return
303 }
304
305 var obj = document.getElementById(id)
306 var sel = document.createElement("select");
307 sel.id = selid;
308 sel.className = 'cbi-input-select';
309 if (obj.nextSibling) {
310 obj.parentNode.insertBefore(sel, obj.nextSibling);
311 } else {
312 obj.parentNode.appendChild(sel);
313 }
314
315 if (!values[obj.value]) {
316 if (obj.value == "") {
317 var optdef = document.createElement("option");
318 optdef.value = "";
319 optdef.appendChild(document.createTextNode(def));
320 sel.appendChild(optdef);
321 } else {
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);
327 }
328 }
329
330 for (var i in values) {
331 var opt = document.createElement("option");
332 opt.value = i;
333
334 if (obj.value == i) {
335 opt.selected = "selected";
336 }
337
338 opt.appendChild(document.createTextNode(values[i]));
339 sel.appendChild(opt);
340 }
341
342 var optman = document.createElement("option");
343 optman.value = "";
344 optman.appendChild(document.createTextNode(man));
345 sel.appendChild(optman);
346
347 obj.style.display = "none";
348
349 cbi_bind(sel, "change", function() {
350 if (sel.selectedIndex == sel.options.length - 1) {
351 obj.style.display = "inline";
352 sel.parentNode.removeChild(sel);
353 obj.focus();
354 } else {
355 obj.value = sel.options[sel.selectedIndex].value;
356 }
357
358 try {
359 cbi_d_update();
360 } catch (e) {
361 //Do nothing
362 }
363 })
364 }
365
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)
370 });
371 cbi_combobox(id, values, def, man);
372 }
373
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"
379 );
380
381 browser.focus();
382 }
383
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
390 event.stop();
391
392 // Submit via XHR
393 event.element().request({
394 onSuccess: win,
395 onFailure: fail
396 });
397
398 if (load) {
399 load();
400 }
401 });
402 }
403 }
404
405
406 function cbi_t_add(section, tab) {
407 var t = document.getElementById('tab.' + section + '.' + tab);
408 var c = document.getElementById('container.' + section + '.' + tab);
409
410 if( t && c ) {
411 cbi_t[section] = (cbi_t[section] || [ ]);
412 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
413 }
414 }
415
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';
425 }
426 else {
427 if(h) h.value = tab;
428 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
429 o2.container.style.display = 'block';
430 }
431 }
432 }
433 return false
434 }
435
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';
441 }
442 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
443 cbi_t[sid][tid].tab.style.display = '';
444
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';
448 }
449 }
450