1 | dbSelectionInfoTimeout = null; |
---|
2 | currentlySelected = { |
---|
3 | titles: [], |
---|
4 | uids: [], |
---|
5 | type: null |
---|
6 | } |
---|
7 | |
---|
8 | shoppingCart = { |
---|
9 | contents: { |
---|
10 | titles: [], |
---|
11 | uids: [], |
---|
12 | types: [] |
---|
13 | } |
---|
14 | } |
---|
15 | |
---|
16 | function infoPanelTimeout(event) { |
---|
17 | // If no event, crash. |
---|
18 | if (event == undefined || event == null) return; |
---|
19 | // Cancel previous timeout if present. |
---|
20 | if (dbSelectionInfoTimeout) { |
---|
21 | window.clearTimeout(dbSelectionInfoTimeout); |
---|
22 | } |
---|
23 | // Gather parameters about event. |
---|
24 | var dbSelectionInfoTime = 500; |
---|
25 | var selectBox = event.target; |
---|
26 | var values = getMultipleListValue(selectBox).values; |
---|
27 | var titles = getMultipleListValue(selectBox).titles; |
---|
28 | var uid = values[values.length-1]; |
---|
29 | var type; |
---|
30 | switch (selectBox.id) { |
---|
31 | case "dbSessionInstancesList": |
---|
32 | // This should be session instance, not session. Testing purpose only. |
---|
33 | type = "Session"; |
---|
34 | break; |
---|
35 | case "dbGamedataList": |
---|
36 | // This is obviously not the real type, change before committing |
---|
37 | type = "Application"; |
---|
38 | break; |
---|
39 | default: |
---|
40 | alert("No valid type passed!"); |
---|
41 | break; |
---|
42 | } |
---|
43 | currentlySelected.uids = values; |
---|
44 | currentlySelected.titles = titles; |
---|
45 | currentlySelected.type = type; |
---|
46 | // Set timeout of 500ms, then execute ajaxInfoRequest with previously gathered parameters. |
---|
47 | dbSelectionInfoTimeout = window.setTimeout(ajaxInfoRequest, dbSelectionInfoTime, uid, ge("dbInfoPanelContent"), type); |
---|
48 | |
---|
49 | } |
---|
50 | |
---|
51 | function getMultipleListValue(element) { |
---|
52 | // Get multiple values from multiple select box |
---|
53 | //TODO: |
---|
54 | var result = { |
---|
55 | values: [], |
---|
56 | titles: [] |
---|
57 | } |
---|
58 | |
---|
59 | for (var i = 0; i < element.options.length; i++) { |
---|
60 | if (element.options[i].selected == true) { |
---|
61 | result.values.push(element.options[i].value); |
---|
62 | result.titles.push(element.options[i].text); |
---|
63 | } |
---|
64 | } |
---|
65 | return result; |
---|
66 | } |
---|
67 | |
---|
68 | function addDataToList() { |
---|
69 | // Take selected objects and add to shopping cart |
---|
70 | |
---|
71 | // set element |
---|
72 | var element; |
---|
73 | switch (currentlySelected.type){ |
---|
74 | case "Session": |
---|
75 | element = ge("dbSessionInstancesList"); |
---|
76 | break; |
---|
77 | case "Application": |
---|
78 | element = ge("dbGamedataList"); |
---|
79 | break; |
---|
80 | default: |
---|
81 | // ??? |
---|
82 | break; |
---|
83 | } |
---|
84 | var uid; |
---|
85 | while (uid = currentlySelected.uids.pop()) { |
---|
86 | // Add to shopping cart |
---|
87 | shoppingCart.contents.uids.push(uid); |
---|
88 | shoppingCart.contents.titles.push(currentlySelected.titles.pop()); |
---|
89 | shoppingCart.contents.types.push(currentlySelected.type); |
---|
90 | |
---|
91 | // Remove option from list |
---|
92 | for (var i = 0; i < element.options.length; i++) { |
---|
93 | if (element.options[i].value == uid) { |
---|
94 | element.remove(i); |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | // Then add display of shopping cart contents |
---|
101 | drawShoppingCart(); |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | function removeFromList(event) { |
---|
106 | if (event == null || event == undefined) return; |
---|
107 | |
---|
108 | var id = event.target.parentNode.id; |
---|
109 | var uid = id.split("shoppingCartItem")[1]; |
---|
110 | // find index, remove from object |
---|
111 | var indexInCart = shoppingCart.contents.uids.indexOf(uid); |
---|
112 | var tempTitle = shoppingCart.contents.titles[indexInCart]; |
---|
113 | var cn = confirm("Are you sure you want to remove: "+tempTitle+"?"); |
---|
114 | if (cn == false) return; |
---|
115 | |
---|
116 | |
---|
117 | shoppingCart.contents.uids.splice(indexInCart, 1); |
---|
118 | var title = shoppingCart.contents.titles.splice(indexInCart, 1)[0]; |
---|
119 | var type = shoppingCart.contents.types.splice(indexInCart, 1)[0]; |
---|
120 | //Then replace element back into relevant list |
---|
121 | // First set element |
---|
122 | |
---|
123 | var element; |
---|
124 | switch (type){ |
---|
125 | case "Session": |
---|
126 | element = ge("dbSessionInstancesList"); |
---|
127 | break; |
---|
128 | case "Application": |
---|
129 | element = ge("dbGamedataList"); |
---|
130 | break; |
---|
131 | default: |
---|
132 | // ??? |
---|
133 | break; |
---|
134 | } |
---|
135 | |
---|
136 | var option = ce("option"); |
---|
137 | option.value = uid; |
---|
138 | option.text = title; |
---|
139 | element.add(option,null); |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | // Lastly, refresh |
---|
144 | drawShoppingCart(); |
---|
145 | } |
---|
146 | |
---|
147 | function drawShoppingCart() { |
---|
148 | var scElement = ge("shoppingCartContent"); |
---|
149 | scElement.innerHTML = ""; |
---|
150 | for (var i = 0; i < shoppingCart.contents.uids.length; i++) { |
---|
151 | var newLine = ce("div"); |
---|
152 | newLine.className = "shoppingCartItem"; |
---|
153 | newLine.id = "shoppingCartItem"+shoppingCart.contents.uids[i]; |
---|
154 | var icon = ce("image"); |
---|
155 | icon.src = "images/ui/DeleteIcon.gif"; |
---|
156 | icon.addEventListener("click", function(event){ |
---|
157 | // TODO: Actually write this function |
---|
158 | removeFromList(event); |
---|
159 | }, false); |
---|
160 | var titleField = ce("text"); |
---|
161 | titleField.innerHTML = shoppingCart.contents.titles[i]; |
---|
162 | newLine.appendChild(icon); |
---|
163 | newLine.appendChild(titleField); |
---|
164 | scElement.appendChild(newLine); |
---|
165 | } |
---|
166 | |
---|
167 | // reset type after empty |
---|
168 | currentlySelected.type = null; |
---|
169 | } |
---|
170 | |
---|
171 | function configureDashboard() { |
---|
172 | var data = JSON.stringify(shoppingCart); |
---|
173 | var form = ce("form"); |
---|
174 | form.action = "DProbeersel.php"; |
---|
175 | form.method = "POST"; |
---|
176 | var input = ce("input"); |
---|
177 | input.type = "hidden"; |
---|
178 | input.name = "selectedData"; |
---|
179 | input.value = data; |
---|
180 | form.appendChild(input); |
---|
181 | form.submit(); |
---|
182 | } |
---|