7cb2f7c1dc41bd752080fa74ba263d625477ea61
[project/luci.git] / libs / cbi / htdocs / luci-static / resources / cbi.js
1 var cbi_d = {};
2
3 function cbi_d_add(field, target, value) {
4         if (!cbi_d[target]) {
5                 cbi_d[target] = {};
6         }
7         if (!cbi_d[target][value]) {
8                 cbi_d[target][value] = [];
9         }
10         cbi_d[target][value].push(field);
11 }
12
13 function cbi_d_update(target) {
14         if (!cbi_d[target]) {
15                 return;
16         }
17         
18         for (var x in cbi_d[target]) {
19                 for (var i=0; i<cbi_d[target][x].length; i++) { 
20                         var y = document.getElementById(cbi_d[target][x][i])    
21                         y.style.display = "none";
22                 }
23         }
24         
25         var t = document.getElementById(target);
26         if (t && t.value && cbi_d[target][t.value]) {
27                 for (var i=0; i<cbi_d[target][t.value].length; i++) {                   
28                         var y = document.getElementById(cbi_d[target][t.value][i])
29                         y.style.display = "block";
30                 }
31         }
32 }
33
34 function cbi_d_init() {
35         for (var x in cbi_d) {
36                 cbi_d_update(x);
37         }
38 }
39
40 function cbi_bind(obj, type, callback, mode) {
41         if (typeof mode == "undefined") {
42                 mode = false;
43         }
44         if (!obj.addEventListener) {
45                 ieCallback = function(){
46                         var e = window.event;
47                         if (!e.target && e.srcElement) {
48                                 e.target = e.srcElement;
49                         };
50                         e.target['_eCB' + type + callback] = callback;
51                         e.target['_eCB' + type + callback](e);
52                         e.target['_eCB' + type + callback] = null;
53                 };
54                 obj.attachEvent('on' + type, ieCallback);
55         } else {
56                 obj.addEventListener(type, callback, mode);
57         }
58         return obj;
59 }
60
61 function cbi_combobox(id, values, def, man) {
62         var obj = document.getElementById(id)
63         var sel = document.createElement("select");
64         obj.parentNode.appendChild(sel);
65
66         if (obj.value == "") {
67                 var optdef = document.createElement("option");
68                 optdef.value = "";
69                 optdef.appendChild(document.createTextNode(def));
70                 sel.appendChild(optdef);
71         } else if (!values[obj.value]) {
72                 var opt = document.createElement("option");
73                 opt.value = obj.value;
74                 opt.selected = "selected";
75                 opt.appendChild(document.createTextNode(obj.value));
76                 sel.appendChild(opt);   
77         }
78
79         for (var i in values) {
80                 var opt = document.createElement("option");
81                 opt.value = i;
82
83                 if (obj.value == i) {
84                         opt.selected = "selected";
85                 }
86
87                 opt.appendChild(document.createTextNode(values[i]));
88                 sel.appendChild(opt);
89         }
90
91         var optman = document.createElement("option");
92         optman.value = "";
93         optman.appendChild(document.createTextNode(man));
94         sel.appendChild(optman);
95
96         obj.style.display = "none";
97
98         cbi_bind(sel, "change", function() {
99                 obj.value = sel.options[sel.selectedIndex].value;
100
101                 if (sel.selectedIndex == sel.options.length - 1) {
102                         obj.style.display = "inline";
103                         sel.parentNode.removeChild(sel);
104                         obj.focus();
105                 }
106         })
107 }
108
109 function cbi_combobox_init(id, values, def, man) {
110         var obj = document.getElementById(id)
111         cbi_bind(obj, "change", function() {
112                 cbi_combobox(id, values, def, man)
113         })
114         cbi_combobox(id, values, def, man)
115 }