User interface improvements part #3
[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
11 var obj = document.getElementById(field);
12 if (obj) {
13 var entry = {
14 "node": obj,
15 "parent": obj.parentNode,
16 "next": obj.nextSibling
17 }
18 cbi_d[target][value].unshift(entry);
19 }
20 }
21
22 function cbi_d_update(target) {
23 if (!cbi_d[target]) {
24 return;
25 }
26
27 for (var x in cbi_d[target]) {
28 for (var i=0; i<cbi_d[target][x].length; i++) {
29 var entry = cbi_d[target][x][i];
30 if (entry.node.parentNode) {
31 entry.parent.removeChild(entry.node)
32 }
33 }
34 }
35
36 var t = document.getElementById(target);
37 var value
38
39 if (!t || !t.value) {
40 value = "";
41 } else {
42 value = t.value;
43
44 if (t.type == "checkbox") {
45 value = t.checked ? value : "";
46 }
47 }
48
49 if (cbi_d[target][value]) {
50 for (var i=0; i<cbi_d[target][value].length; i++) {
51 var entry = cbi_d[target][value][i];
52 if (!entry.next) {
53 entry.parent.appendChild(entry.node);
54 } else {
55 entry.parent.insertBefore(entry.node, entry.next);
56 }
57 }
58 }
59 }
60
61 function cbi_d_init() {
62 for (var x in cbi_d) {
63 cbi_d_update(x);
64 }
65 }
66
67 function cbi_bind(obj, type, callback, mode) {
68 if (typeof mode == "undefined") {
69 mode = false;
70 }
71 if (!obj.addEventListener) {
72 ieCallback = function(){
73 var e = window.event;
74 if (!e.target && e.srcElement) {
75 e.target = e.srcElement;
76 };
77 e.target['_eCB' + type + callback] = callback;
78 e.target['_eCB' + type + callback](e);
79 e.target['_eCB' + type + callback] = null;
80 };
81 obj.attachEvent('on' + type, ieCallback);
82 } else {
83 obj.addEventListener(type, callback, mode);
84 }
85 return obj;
86 }
87
88 function cbi_combobox(id, values, def, man) {
89 var obj = document.getElementById(id)
90 var sel = document.createElement("select");
91 obj.parentNode.appendChild(sel);
92
93 if (!values[obj.value]) {
94 if (obj.value == "") {
95 var optdef = document.createElement("option");
96 optdef.value = "";
97 optdef.appendChild(document.createTextNode(def));
98 sel.appendChild(optdef);
99 } else {
100 var opt = document.createElement("option");
101 opt.value = obj.value;
102 opt.selected = "selected";
103 opt.appendChild(document.createTextNode(obj.value));
104 sel.appendChild(opt);
105 }
106 }
107
108 for (var i in values) {
109 var opt = document.createElement("option");
110 opt.value = i;
111
112 if (obj.value == i) {
113 opt.selected = "selected";
114 }
115
116 opt.appendChild(document.createTextNode(values[i]));
117 sel.appendChild(opt);
118 }
119
120 var optman = document.createElement("option");
121 optman.value = "";
122 optman.appendChild(document.createTextNode(man));
123 sel.appendChild(optman);
124
125 obj.style.display = "none";
126
127 cbi_bind(sel, "change", function() {
128 if (sel.selectedIndex == sel.options.length - 1) {
129 obj.style.display = "inline";
130 sel.parentNode.removeChild(sel);
131 obj.focus();
132 } else {
133 obj.value = sel.options[sel.selectedIndex].value;
134 }
135 })
136 }
137
138 function cbi_combobox_init(id, values, def, man) {
139 var obj = document.getElementById(id);
140 cbi_bind(obj, "blur", function() {
141 cbi_combobox(id, values, def, man)
142 });
143 cbi_combobox(id, values, def, man);
144 }