38e27d8f437612d6df1b5bd341cbc016b10b45c6
[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 if (obj.value == "" || values[obj.value]) {
64 var sel = document.createElement("select")
65 obj.parentNode.appendChild(sel)
66
67 if (obj.value == "") {
68 var optdef = document.createElement("option")
69 optdef.value = ""
70 optdef.appendChild(document.createTextNode(def))
71 sel.appendChild(optdef)
72 }
73
74 for (var i in values) {
75 var opt = document.createElement("option")
76 opt.value = i
77
78 if (obj.value == i) {
79 opt.selected = "selected"
80 }
81
82 opt.appendChild(document.createTextNode(values[i]))
83 sel.appendChild(opt)
84 }
85
86 var optman = document.createElement("option")
87 optman.value = ""
88 optman.appendChild(document.createTextNode(man))
89 sel.appendChild(optman)
90
91 obj.style.display = "none"
92
93 cbi_bind(sel, "change", function() {
94 obj.value = sel.options[sel.selectedIndex].value
95
96 if (sel.selectedIndex == sel.options.length - 1) {
97 obj.style.display = "inline"
98 sel.parentNode.removeChild(sel)
99 obj.focus()
100 }
101 })
102 }
103 }
104
105 function cbi_combobox_init(id, values, def, man) {
106 var obj = document.getElementById(id)
107 cbi_bind(obj, "change", function() {
108 cbi_combobox(id, values, def, man)
109 })
110 cbi_combobox(id, values, def, man)
111 }