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
19 function cbi_d_add(field
, dep
, next
) {
20 var obj
= document
.getElementById(field
);
23 for (var i
=0; i
<cbi_d
.length
; i
++) {
24 if (cbi_d
[i
].id
== field
) {
33 "parent": obj
.parentNode
.id
,
43 function cbi_d_checkvalue(target
, ref
) {
44 var t
= document
.getElementById(target
);
52 if (t
.type
== "checkbox") {
53 value
= t
.checked
? value
: "";
60 function cbi_d_check(deps
) {
61 for (var i
=0; i
<deps
.length
; i
++) {
63 for (var j
in deps
[i
]) {
64 istat
= (istat
&& cbi_d_checkvalue(j
, deps
[i
][j
]))
72 function cbi_d_update() {
74 for (var i
=0; i
<cbi_d
.length
; i
++) {
76 var next
= document
.getElementById(entry
.next
)
77 var node
= document
.getElementById(entry
.id
)
78 var parent
= document
.getElementById(entry
.parent
)
80 if (node
&& node
.parentNode
&& !cbi_d_check(entry
.deps
)) {
81 node
.parentNode
.removeChild(node
);
82 state
= (state
|| !node
.parentNode
)
83 } else if ((!node
|| !node
.parentNode
) && cbi_d_check(entry
.deps
)) {
85 parent
.appendChild(entry
.node
);
87 next
.parentNode
.insertBefore(entry
.node
, next
);
89 state
= (state
|| (node
&& node
.parentNode
))
97 function cbi_bind(obj
, type
, callback
, mode
) {
98 if (typeof mode
== "undefined") {
101 if (!obj
.addEventListener
) {
102 ieCallback = function(){
103 var e
= window
.event
;
104 if (!e
.target
&& e
.srcElement
) {
105 e
.target
= e
.srcElement
;
107 e
.target
['_eCB' + type
+ callback
] = callback
;
108 e
.target
['_eCB' + type
+ callback
](e
);
109 e
.target
['_eCB' + type
+ callback
] = null;
111 obj
.attachEvent('on' + type
, ieCallback
);
113 obj
.addEventListener(type
, callback
, mode
);
118 function cbi_combobox(id
, values
, def
, man
) {
119 var selid
= "cbi.combobox." + id
;
120 if (document
.getElementById(selid
)) {
124 var obj
= document
.getElementById(id
)
125 var sel
= document
.createElement("select");
127 sel
.className
= 'cbi-input-select';
128 if (obj
.nextSibling
) {
129 obj
.parentNode
.insertBefore(sel
, obj
.nextSibling
);
131 obj
.parentNode
.appendChild(sel
);
134 if (!values
[obj
.value
]) {
135 if (obj
.value
== "") {
136 var optdef
= document
.createElement("option");
138 optdef
.appendChild(document
.createTextNode(def
));
139 sel
.appendChild(optdef
);
141 var opt
= document
.createElement("option");
142 opt
.value
= obj
.value
;
143 opt
.selected
= "selected";
144 opt
.appendChild(document
.createTextNode(obj
.value
));
145 sel
.appendChild(opt
);
149 for (var i
in values
) {
150 var opt
= document
.createElement("option");
153 if (obj
.value
== i
) {
154 opt
.selected
= "selected";
157 opt
.appendChild(document
.createTextNode(values
[i
]));
158 sel
.appendChild(opt
);
161 var optman
= document
.createElement("option");
163 optman
.appendChild(document
.createTextNode(man
));
164 sel
.appendChild(optman
);
166 obj
.style
.display
= "none";
168 cbi_bind(sel
, "change", function() {
169 if (sel
.selectedIndex
== sel
.options
.length
- 1) {
170 obj
.style
.display
= "inline";
171 sel
.parentNode
.removeChild(sel
);
174 obj
.value
= sel
.options
[sel
.selectedIndex
].value
;
185 function cbi_combobox_init(id
, values
, def
, man
) {
186 var obj
= document
.getElementById(id
);
187 cbi_bind(obj
, "blur", function() {
188 cbi_combobox(id
, values
, def
, man
)
190 cbi_combobox(id
, values
, def
, man
);
193 function cbi_filebrowser(id
, url
, defpath
) {
194 var field
= document
.getElementById(id
);
195 var browser
= window
.open(
196 url
+ ( field
.value
|| defpath
|| '' ) + '?field=' + id
,
197 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
203 //Hijacks the CBI form to send via XHR (requires Prototype)
204 function cbi_hijack_forms(layer
, win
, fail
, load
) {
205 var forms
= layer
.getElementsByTagName('form');
206 for (var i
=0; i
<forms
.length
; i
++) {
207 $(forms
[i
]).observe('submit', function(event
) {
208 // Prevent the form from also submitting the regular way
212 event
.element().request({
225 function cbi_t_add(section
, tab
) {
226 var t
= document
.getElementById('tab.' + section
+ '.' + tab
);
227 var c
= document
.getElementById('container.' + section
+ '.' + tab
);
230 cbi_t
[section
] = (cbi_t
[section
] || [ ]);
231 cbi_t
[section
][tab
] = { 'tab': t
, 'container': c
};
235 function cbi_t_switch(section
, tab
) {
236 if( cbi_t
[section
] && cbi_t
[section
][tab
] ) {
237 var o
= cbi_t
[section
][tab
];
238 var h
= document
.getElementById('tab.' + section
);
239 for( var tid
in cbi_t
[section
] ) {
240 var o2
= cbi_t
[section
][tid
];
241 if( o
.tab
.id
!= o2
.tab
.id
) {
242 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
243 o2
.container
.style
.display
= 'none';
247 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
248 o2
.container
.style
.display
= 'block';