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