root / ui / static / snf / js / ui / web / ui_list_view.js @ 643de8c0
History | View | Annotate | Download (16.4 kB)
1 |
;(function(root){
|
---|---|
2 |
|
3 |
// root
|
4 |
var root = root;
|
5 |
|
6 |
// setup namepsaces
|
7 |
var snf = root.synnefo = root.synnefo || {};
|
8 |
var models = snf.models = snf.models || {}
|
9 |
var storage = snf.storage = snf.storage || {};
|
10 |
var ui = snf.ui = snf.ui || {};
|
11 |
var util = snf.util = snf.util || {};
|
12 |
|
13 |
var views = snf.views = snf.views || {}
|
14 |
|
15 |
// shortcuts
|
16 |
var bb = root.Backbone;
|
17 |
|
18 |
views.ListMultipleActions = views.View.extend({ |
19 |
|
20 |
view_id: "list_actions", |
21 |
|
22 |
initialize: function(view) { |
23 |
this.parent = view;
|
24 |
this.el = this.parent.el; |
25 |
|
26 |
views.ListMultipleActions.__super__.initialize.call(this);
|
27 |
this.set_handlers();
|
28 |
this.update_layout();
|
29 |
|
30 |
this.selected_action = undefined; |
31 |
this.available_actions = [];
|
32 |
this.multi_view = synnefo.ui.main.multiple_actions_view;
|
33 |
this.hovered = false; |
34 |
}, |
35 |
|
36 |
set_handlers: function() { |
37 |
var self = this; |
38 |
storage.vms.bind("change:pending_action", function() { |
39 |
if (!storage.vms.has_pending_actions()) {
|
40 |
self.parent.$(".actions a").removeClass("selected"); |
41 |
self.parent.clear_indicators(); |
42 |
} |
43 |
}); |
44 |
|
45 |
var self = this; |
46 |
this.parent.$(".actions a.enabled").live('click', function() { |
47 |
self.parent.$(".actions a").removeClass("selected"); |
48 |
$(this).addClass("selected"); |
49 |
self.parent.select_action($(this).attr("id").replace("action-","")); |
50 |
}); |
51 |
|
52 |
this.parent.$(".actions a.enabled").live({ |
53 |
'mouseenter': function() { |
54 |
self.hovered = true;
|
55 |
self.parent.set_indicator_for($(this).attr("id").replace("action-","")) |
56 |
}, |
57 |
'mouseleave': function() { |
58 |
self.hovered = false;
|
59 |
self.parent.clear_indicators(); |
60 |
} |
61 |
}); |
62 |
}, |
63 |
|
64 |
update_actions: function() { |
65 |
actions = undefined;
|
66 |
this.available_actions = [];
|
67 |
_.each(this.parent.get_selected_vms(), function(vm) { |
68 |
if (!actions) {
|
69 |
actions = vm.get_available_actions(); |
70 |
return;
|
71 |
} |
72 |
actions = _.intersection(actions, vm.get_available_actions()); |
73 |
}); |
74 |
|
75 |
this.available_actions = actions;
|
76 |
|
77 |
this.$(".actions a").removeClass("enabled"); |
78 |
_.each(this.available_actions, _.bind(function(name){ |
79 |
this.$("#action-" + name).addClass("enabled"); |
80 |
}, this))
|
81 |
}, |
82 |
|
83 |
update_selected: function() { |
84 |
this.$("tr").removeClass("checked"); |
85 |
this.$("tr input:checked").parent().parent().addClass("checked"); |
86 |
}, |
87 |
|
88 |
update_layout: function() { |
89 |
this.update_actions();
|
90 |
this.update_selected();
|
91 |
} |
92 |
}); |
93 |
|
94 |
// VMs list view
|
95 |
views.ListView = views.VMListView.extend({ |
96 |
|
97 |
// view id (this could be used to identify
|
98 |
// the view object from global context
|
99 |
view_id: 'vm_list', |
100 |
|
101 |
el: '#machinesview-list', |
102 |
id_tpl: 'list-vm-', |
103 |
link_id_tpl: 'list-vm-at-', |
104 |
|
105 |
hide_actions: false, |
106 |
|
107 |
selectors: {
|
108 |
'vms': '.list-container', |
109 |
'vm': '#list-vm-{0}', |
110 |
'view': '#machinesview-list', |
111 |
'tpl': '.list-container#machine-container-template', |
112 |
'spinner': '.large-spinner', |
113 |
'vm_spinner': '#list-vm-{0} .spinner', |
114 |
'vm_wave': '#list-vm-{0} .wave', |
115 |
'os_icon': '#list-vm-{0} .os_icon', |
116 |
'vm_cont_active': '#machinesview-list', |
117 |
'vm_cont_terminated': '#machinesview-list' |
118 |
}, |
119 |
|
120 |
initialize: function() { |
121 |
this.current_vm = 0; |
122 |
|
123 |
// button selectors
|
124 |
this.prev_button = this.$(".controls .previous"); |
125 |
this.next_button = this.$(".controls .next"); |
126 |
|
127 |
this.actions = this.$(".actions").show(); |
128 |
this.datatable_cont = this.$(".dataTables_wrapper").show(); |
129 |
this.content = this.$("#machinesview_content").show(); |
130 |
this.filter = this.$(".dataTables_filter").show().css({'display':'block'}); |
131 |
this.table_el = this.$(".list-machines").show(); |
132 |
this.select_all = $("#list-view-select-all"); |
133 |
|
134 |
this.actions = new views.ListMultipleActions(this); |
135 |
|
136 |
this.table = $("div.list table.list-machines").dataTable({ |
137 |
"bInfo": false, |
138 |
"bRetrieve": true, |
139 |
"bPaginate": false, |
140 |
"bAutoWidth": false, |
141 |
"bSort": true, |
142 |
"bStateSave": true, |
143 |
"sScrollXInner": "500px", |
144 |
"aoColumnDefs": [
|
145 |
{ "bSortable": false, "aTargets": [ 0 ] } |
146 |
] |
147 |
}); |
148 |
|
149 |
this.table_data = {};
|
150 |
views.ListView.__super__.initialize.apply(this, arguments); |
151 |
}, |
152 |
|
153 |
reset: function() { |
154 |
}, |
155 |
|
156 |
hide_actions: function() { |
157 |
this.$(".actions a").removeClass("selected"); |
158 |
}, |
159 |
|
160 |
// overload show function
|
161 |
show_view: function() { |
162 |
this.log.debug("showing"); |
163 |
this.sel('spinner').hide(); |
164 |
this.__update_layout();
|
165 |
}, |
166 |
|
167 |
check_vm_container: function() { |
168 |
}, |
169 |
|
170 |
// identify vm model instance id based on DOM element
|
171 |
vm_id_for_element: function(el) { |
172 |
return el.attr('id').replace("list-vm-", ""); |
173 |
}, |
174 |
|
175 |
reset_actions: function() { |
176 |
this.$(".actions a").removeClass("selected"); |
177 |
storage.vms.reset_pending_actions(); |
178 |
}, |
179 |
|
180 |
// set generic view handlers
|
181 |
set_handlers: function() { |
182 |
this.$(".list-vm-checkbox").live('change', _.bind(function(){ |
183 |
this.reset_actions();
|
184 |
this.actions.update_layout();
|
185 |
if (this.$("tbody input:checked").length > 0) { |
186 |
this.select_all.attr("checked", true); |
187 |
} else {
|
188 |
this.select_all.attr("checked", false); |
189 |
} |
190 |
self.actions.update_layout(); |
191 |
}, this))
|
192 |
|
193 |
var self = this; |
194 |
this.select_all.click(function(){ |
195 |
if ($(this).is(":checked")) { |
196 |
self.$("tbody input").attr("checked", true); |
197 |
} else {
|
198 |
self.$("tbody input").attr("checked", false); |
199 |
} |
200 |
self.actions.update_layout(); |
201 |
}); |
202 |
}, |
203 |
|
204 |
get_selected_vms: function() { |
205 |
var selected = $(this.el).find(".list-vm-checkbox:checked"); |
206 |
var vms = []
|
207 |
_.each(selected, function(el){
|
208 |
var id = parseInt($(el).attr("id").replace("checkbox-list-vm-", "")); |
209 |
vm = storage.vms.get(id); |
210 |
if (!vm) { return }; |
211 |
vms.push(vm); |
212 |
}); |
213 |
|
214 |
return vms;
|
215 |
}, |
216 |
|
217 |
select_action: function(action) { |
218 |
this.reset_actions();
|
219 |
this.$(".actions a#action-" + action).addClass("selected"); |
220 |
var vms = this.get_selected_vms(); |
221 |
_.each(vms, function(vm){
|
222 |
vm.update_pending_action(action); |
223 |
}) |
224 |
}, |
225 |
|
226 |
reset: function() { |
227 |
this.reset_actions();
|
228 |
}, |
229 |
|
230 |
create_vm: function(vm) { |
231 |
params = this.get_vm_table_data(vm);
|
232 |
var index = this.table.fnAddData.call(this.table, params); |
233 |
this.table_data["vm_" + vm.id] = {index: index[0], params: params}; |
234 |
|
235 |
// append row id
|
236 |
$(this.table.fnGetNodes(index)).attr("id", this.id_tpl + vm.id); |
237 |
|
238 |
// hide indicators on creation
|
239 |
this.vm(vm).find(".spinner").hide(); |
240 |
this.vm(vm).find(".wave").hide(); |
241 |
this.vm(vm).find(".os_icon").show(); |
242 |
this.vm(vm).find(".action-indicator").hide(); |
243 |
|
244 |
// ancestor method
|
245 |
this.__set_vm_handlers(vm);
|
246 |
this.post_add(vm);
|
247 |
}, |
248 |
|
249 |
// remove vm
|
250 |
remove_vm: function(vm) { |
251 |
this.vm(vm).find("input[type=checkbox]").removeAttr("checked"); |
252 |
var index = this.table_data["vm_" + vm.id].index; |
253 |
this.table.fnDeleteRow(index);
|
254 |
delete this.table_data["vm_" + vm.id]; |
255 |
this.update_data();
|
256 |
}, |
257 |
|
258 |
update_data: function() { |
259 |
var new_data = this.table.fnGetData(); |
260 |
_.each(new_data, _.bind(function(row, i){
|
261 |
this.table_data["vm_" + row[5]].index = i; |
262 |
this.table_data["vm_" + row[5]].params = row; |
263 |
}, this));
|
264 |
}, |
265 |
|
266 |
set_indicator_for: function(action) { |
267 |
var vms = this.get_selected_vms(); |
268 |
_.each(vms, _.bind(function(vm){
|
269 |
var vmel = this.vm(vm); |
270 |
vmel.find("img.spinner, img.wave, img.os_icon").hide();
|
271 |
vmel.find("span.action-indicator").show().removeClass().addClass(action + " action-indicator"); |
272 |
}, this));
|
273 |
}, |
274 |
|
275 |
clear_indicators: function() { |
276 |
var vms = storage.vms.models;
|
277 |
_.each(vms, _.bind(function(vm){
|
278 |
var vmel = this.vm(vm); |
279 |
|
280 |
vmel.find("img.wave").hide();
|
281 |
|
282 |
if (vm.pending_action) {
|
283 |
vmel.find("img.os_icon").hide();
|
284 |
vmel.find("span.action-indicator").show().removeClass().addClass(vm.pending_action + " action-indicator"); |
285 |
return;
|
286 |
} |
287 |
|
288 |
if (vm.in_transition()) {
|
289 |
vmel.find("span.action-indicator").hide();
|
290 |
vmel.find("img.spinner").show();
|
291 |
return;
|
292 |
} |
293 |
|
294 |
if (!this.actions.hovered) { |
295 |
vmel.find("img.os_icon").show();
|
296 |
vmel.find("span.action-indicator").hide();
|
297 |
vmel.find("img.spinner").hide();
|
298 |
} |
299 |
|
300 |
|
301 |
}, this));
|
302 |
}, |
303 |
|
304 |
get_vm_table_data: function(vm) { |
305 |
var checkbox = '<input type="checkbox" class="' + |
306 |
views.ListView.STATE_CLASSES[vm.state()].join(" ") +
|
307 |
' list-vm-checkbox" id="checkbox-' + this.id_tpl + vm.id + '"/>'; |
308 |
|
309 |
var img = '<img class="os_icon" src="'+ this.get_vm_icon_path(vm, "small") +'" />'; |
310 |
img = img + '<img src="static/icons/indicators/small/progress.gif" class="spinner" />';
|
311 |
img = img + '<img src="static/icons/indicators/medium/wave.gif" class="wave" />';
|
312 |
img = img + '<span class="action-indicator" />';
|
313 |
|
314 |
var name = util.truncate(vm.get('name'), 25); |
315 |
var flavor = vm.get_flavor().details_string();
|
316 |
var status = STATE_TEXTS[vm.state()];
|
317 |
|
318 |
return [checkbox, img, name, flavor, status, vm.id];
|
319 |
}, |
320 |
|
321 |
post_add: function(vm) { |
322 |
}, |
323 |
|
324 |
// is vm in transition ??? show the progress spinner
|
325 |
update_transition_state: function(vm) { |
326 |
if ((this.actions.hovered && this.vm(vm).find("input").is(":checked")) || vm.pending_action) { |
327 |
this.sel('vm_spinner', vm.id).hide(); |
328 |
this.sel('vm_wave', vm.id).hide(); |
329 |
this.sel('os_icon', vm.id).hide(); |
330 |
this.vm(vm).find(".action-indicator").show(); |
331 |
return;
|
332 |
} |
333 |
|
334 |
if (vm.in_transition()){
|
335 |
this.sel('vm_spinner', vm.id).show(); |
336 |
this.sel('vm_wave', vm.id).hide(); |
337 |
this.sel('os_icon', vm.id).hide(); |
338 |
this.$(".action-indicator").hide(); |
339 |
} else {
|
340 |
this.sel('vm_spinner', vm.id).hide(); |
341 |
this.$(".action-indicator").hide(); |
342 |
} |
343 |
}, |
344 |
|
345 |
// display transition animations
|
346 |
show_transition: function(vm) { |
347 |
if (!this.visible()) { return }; |
348 |
var wave = this.sel('vm_wave', vm.id); |
349 |
if (!wave.length) {
|
350 |
return
|
351 |
} |
352 |
|
353 |
this.sel('vm_spinner', vm.id).hide(); |
354 |
this.sel('os_icon', vm.id).hide(); |
355 |
|
356 |
var src = wave.attr('src'); |
357 |
var self = this; |
358 |
|
359 |
// change src to force gif play from the first frame
|
360 |
// animate for 500 ms then hide
|
361 |
wave.attr('src', "").show().attr('src', src).fadeIn(500).delay(700).fadeOut(300, function() { |
362 |
if (vm.in_transition()) {
|
363 |
self.sel("vm_spinner", vm.id).fadeIn(200); |
364 |
} else {
|
365 |
if (vm.pending_action || (self.actions.hovered && this.vm(vm).find("input").is(":checked"))) { |
366 |
self.vm(vm).find(".action-indicator").show();
|
367 |
} else {
|
368 |
self.sel("os_icon", vm.id).fadeIn(200); |
369 |
} |
370 |
} |
371 |
}); |
372 |
}, |
373 |
|
374 |
update_actions_layout: function(vm) { |
375 |
}, |
376 |
|
377 |
post_update_vm: function(vm) { |
378 |
var index = this.table_data["vm_" + vm.id].index; |
379 |
params = this.get_vm_table_data(vm);
|
380 |
this.table_data["vm_" + vm.id].params = params; |
381 |
data = this.table.fnGetData()[index];
|
382 |
|
383 |
// do not recreate checkboxes and images to avoid messing
|
384 |
// with user interaction
|
385 |
this.table.fnUpdate(params[2], parseInt(index), 2); |
386 |
this.table.fnUpdate(params[3], parseInt(index), 3); |
387 |
this.table.fnUpdate(params[4], parseInt(index), 4); |
388 |
|
389 |
var active_class = vm.is_active() ? "active" : "inactive"; |
390 |
this.vm(vm).removeClass("active").removeClass("inactive").addClass(active_class); |
391 |
$(this.vm(vm).find("td").get(4)).addClass("status"); |
392 |
$(this.vm(vm).find("td").get(3)).addClass("flavor"); |
393 |
$(this.vm(vm).find("td").get(2)).addClass("name"); |
394 |
|
395 |
if (vm.status() == "ERROR") { |
396 |
this.vm(vm).removeClass("active").removeClass("inactive").addClass("error"); |
397 |
} |
398 |
|
399 |
this.update_os_icon(vm);
|
400 |
this.update_transition_state(vm);
|
401 |
}, |
402 |
|
403 |
update_os_icon: function(vm) { |
404 |
this.sel('os_icon', vm.id).attr('src', this.get_vm_icon_path(vm, "small")); |
405 |
}, |
406 |
|
407 |
// vm specific event handlers
|
408 |
set_vm_handlers: function(vm) { |
409 |
}, |
410 |
|
411 |
// generic stuff to do on each view update
|
412 |
// called once after each vm has been updated
|
413 |
update_layout: function() { |
414 |
this.actions.update_layout();
|
415 |
}, |
416 |
|
417 |
// update vm details
|
418 |
update_details: function(vm) { |
419 |
}, |
420 |
|
421 |
get_vm_icon_os: function(vm) { |
422 |
var os = vm.get_os();
|
423 |
var icons = window.os_icons || views.ListView.VM_OS_ICONS;
|
424 |
if (icons.indexOf(os) == -1) { |
425 |
os = "unknown";
|
426 |
} |
427 |
return os;
|
428 |
}, |
429 |
|
430 |
// TODO: move to views.utils (the method and the VM_OS_ICON vars)
|
431 |
get_vm_icon_path: function(vm, icon_type) { |
432 |
var os = vm.get_os();
|
433 |
var icons = window.os_icons || views.ListView.VM_OS_ICONS;
|
434 |
|
435 |
if (icons.indexOf(os) == -1) { |
436 |
os = "unknown";
|
437 |
} |
438 |
|
439 |
var st = "off"; |
440 |
if (vm.is_active()) {
|
441 |
st = "on"
|
442 |
} |
443 |
|
444 |
return views.ListView.VM_OS_ICON_TPLS[icon_type].format(os, st);
|
445 |
} |
446 |
}) |
447 |
|
448 |
views.ListView.VM_OS_ICON_TPLS = { |
449 |
"small": "/static/icons/machines/small/{0}-{1}.png" |
450 |
} |
451 |
|
452 |
views.ListView.VM_OS_ICONS = window.os_icons || []; |
453 |
|
454 |
views.ListView.STATE_CLASSES = { |
455 |
'UNKNOWN': ['error-state'], |
456 |
'BUILD': ['build-state'], |
457 |
'REBOOT': ['rebooting-state'], |
458 |
'STOPPED': ['terminated-state'], |
459 |
'ACTIVE': ['running-state'], |
460 |
'ERROR': ['error-state'], |
461 |
'DELETED': ['destroying-state'], |
462 |
'DESTROY': ['destroying-state'], |
463 |
'BUILD_INIT': ['build-state'], |
464 |
'BUILD_COPY': ['build-state'], |
465 |
'BUILD_FINAL': ['build-state'], |
466 |
'SHUTDOWN': ['shutting-state'], |
467 |
'START': ['starting-state'], |
468 |
'CONNECT': ['connecting-state'], |
469 |
'DISCONNECT': ['disconnecting-state'] |
470 |
}; |
471 |
|
472 |
})(this);
|