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 function cbi_d_add(field, dep, next) {
21 var obj = document.getElementById(field);
24 for (var i=0; i<cbi_d.length; i++) {
25 if (cbi_d[i].id == field) {
34 "parent": obj.parentNode.id,
41 cbi_c[entry.parent] = (cbi_c[entry.parent] || 0) + 1;
47 function cbi_d_checkvalue(target, ref) {
48 var t = document.getElementById(target);
56 if (t.type == "checkbox") {
57 value = t.checked ? value : "";
64 function cbi_d_check(deps) {
65 for (var i=0; i<deps.length; i++) {
67 for (var j in deps[i]) {
68 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
76 function cbi_d_update() {
78 for (var i=0; i<cbi_d.length; i++) {
80 var next = document.getElementById(entry.next)
81 var node = document.getElementById(entry.id)
82 var parent = document.getElementById(entry.parent)
84 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
85 node.parentNode.removeChild(node);
86 state = (state || !node.parentNode);
88 cbi_c[entry.parent]--;
89 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
91 parent.appendChild(entry.node);
93 next.parentNode.insertBefore(entry.node, next);
95 state = (state || (node && node.parentNode))
97 cbi_c[entry.parent]++;
110 function cbi_bind(obj, type, callback, mode) {
111 if (typeof mode == "undefined") {
114 if (!obj.addEventListener) {
115 ieCallback = function(){
116 var e = window.event;
117 if (!e.target && e.srcElement) {
118 e.target = e.srcElement;
120 e.target['_eCB' + type + callback] = callback;
121 e.target['_eCB' + type + callback](e);
122 e.target['_eCB' + type + callback] = null;
124 obj.attachEvent('on' + type, ieCallback);
126 obj.addEventListener(type, callback, mode);
131 function cbi_combobox(id, values, def, man) {
132 var selid = "cbi.combobox." + id;
133 if (document.getElementById(selid)) {
137 var obj = document.getElementById(id)
138 var sel = document.createElement("select");
140 sel.className = 'cbi-input-select';
141 if (obj.nextSibling) {
142 obj.parentNode.insertBefore(sel, obj.nextSibling);
144 obj.parentNode.appendChild(sel);
147 if (!values[obj.value]) {
148 if (obj.value == "") {
149 var optdef = document.createElement("option");
151 optdef.appendChild(document.createTextNode(def));
152 sel.appendChild(optdef);
154 var opt = document.createElement("option");
155 opt.value = obj.value;
156 opt.selected = "selected";
157 opt.appendChild(document.createTextNode(obj.value));
158 sel.appendChild(opt);
162 for (var i in values) {
163 var opt = document.createElement("option");
166 if (obj.value == i) {
167 opt.selected = "selected";
170 opt.appendChild(document.createTextNode(values[i]));
171 sel.appendChild(opt);
174 var optman = document.createElement("option");
176 optman.appendChild(document.createTextNode(man));
177 sel.appendChild(optman);
179 obj.style.display = "none";
181 cbi_bind(sel, "change", function() {
182 if (sel.selectedIndex == sel.options.length - 1) {
183 obj.style.display = "inline";
184 sel.parentNode.removeChild(sel);
187 obj.value = sel.options[sel.selectedIndex].value;
198 function cbi_combobox_init(id, values, def, man) {
199 var obj = document.getElementById(id);
200 cbi_bind(obj, "blur", function() {
201 cbi_combobox(id, values, def, man)
203 cbi_combobox(id, values, def, man);
206 function cbi_filebrowser(id, url, defpath) {
207 var field = document.getElementById(id);
208 var browser = window.open(
209 url + ( field.value || defpath || '' ) + '?field=' + id,
210 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
216 //Hijacks the CBI form to send via XHR (requires Prototype)
217 function cbi_hijack_forms(layer, win, fail, load) {
218 var forms = layer.getElementsByTagName('form');
219 for (var i=0; i<forms.length; i++) {
220 $(forms[i]).observe('submit', function(event) {
221 // Prevent the form from also submitting the regular way
225 event.element().request({
238 function cbi_t_add(section, tab) {
239 var t = document.getElementById('tab.' + section + '.' + tab);
240 var c = document.getElementById('container.' + section + '.' + tab);
243 cbi_t[section] = (cbi_t[section] || [ ]);
244 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
248 function cbi_t_switch(section, tab) {
249 if( cbi_t[section] && cbi_t[section][tab] ) {
250 var o = cbi_t[section][tab];
251 var h = document.getElementById('tab.' + section);
252 for( var tid in cbi_t[section] ) {
253 var o2 = cbi_t[section][tid];
254 if( o.tab.id != o2.tab.id ) {
255 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
256 o2.container.style.display = 'none';
260 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
261 o2.container.style.display = 'block';
268 function cbi_t_update() {
269 for( var sid in cbi_t )
270 for( var tid in cbi_t[sid] )
271 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
272 cbi_t[sid][tid].tab.style.display = 'none';
274 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
275 cbi_t[sid][tid].tab.style.display = '';
277 var t = cbi_t[sid][tid].tab;
278 window.setTimeout(function() { t.className = t.className.replace(/ cbi-tab-highlighted/g, '') }, 750);
279 cbi_t[sid][tid].tab.className += ' cbi-tab-highlighted';