root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_icon_view.js @ 365af933
History | View | Annotate | Download (32.7 kB)
1 |
// Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
//
|
3 |
// Redistribution and use in source and binary forms, with or
|
4 |
// without modification, are permitted provided that the following
|
5 |
// conditions are met:
|
6 |
//
|
7 |
// 1. Redistributions of source code must retain the above
|
8 |
// copyright notice, this list of conditions and the following
|
9 |
// disclaimer.
|
10 |
//
|
11 |
// 2. Redistributions in binary form must reproduce the above
|
12 |
// copyright notice, this list of conditions and the following
|
13 |
// disclaimer in the documentation and/or other materials
|
14 |
// provided with the distribution.
|
15 |
//
|
16 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
// POSSIBILITY OF SUCH DAMAGE.
|
28 |
//
|
29 |
// The views and conclusions contained in the software and
|
30 |
// documentation are those of the authors and should not be
|
31 |
// interpreted as representing official policies, either expressed
|
32 |
// or implied, of GRNET S.A.
|
33 |
//
|
34 |
|
35 |
;(function(root){
|
36 |
|
37 |
// root
|
38 |
var root = root;
|
39 |
|
40 |
// setup namepsaces
|
41 |
var snf = root.synnefo = root.synnefo || {};
|
42 |
var models = snf.models = snf.models || {}
|
43 |
var storage = snf.storage = snf.storage || {};
|
44 |
var ui = snf.ui = snf.ui || {};
|
45 |
var util = snf.util = snf.util || {};
|
46 |
|
47 |
var views = snf.views = snf.views || {}
|
48 |
|
49 |
// shortcuts
|
50 |
var bb = root.Backbone;
|
51 |
|
52 |
// handle extended info toggler
|
53 |
views.VMActionErrorView = views.View.extend({ |
54 |
|
55 |
initialize: function (vm, view) { |
56 |
this.vm = vm;
|
57 |
this.view = view;
|
58 |
this.vm_view = this.view.vm(vm); |
59 |
|
60 |
this.has_error = false; |
61 |
|
62 |
this.error = this.vm_view.find(".action-error"); |
63 |
this.close = this.vm_view.find(".close-action-error"); |
64 |
this.show_btn = this.vm_view.find(".show-action-error"); |
65 |
|
66 |
this.init_handlers();
|
67 |
this.update_layout();
|
68 |
}, |
69 |
|
70 |
init_handlers: function() { |
71 |
// action call failed notify the user
|
72 |
this.vm.bind("action:fail", _.bind(function(args){ |
73 |
if (this.vm.action_error) { |
74 |
this.has_error = true; |
75 |
var action = "undefined"; |
76 |
try {
|
77 |
action = _.last(args).error_params.extra_details['Action'];
|
78 |
} catch (err) {console.log(err)};
|
79 |
|
80 |
this.error.find(".action").text(action); |
81 |
this.error.show();
|
82 |
} |
83 |
}, this));
|
84 |
|
85 |
// show error overlay
|
86 |
this.show_btn.click(_.bind(function() { |
87 |
if (this.vm.action_error) { |
88 |
this.show_error_overlay(this.vm.action_error); |
89 |
} |
90 |
this.vm.reset_action_error();
|
91 |
}, this));
|
92 |
|
93 |
// user requests to forget about the error
|
94 |
this.close.click(_.bind(_.bind(function() { |
95 |
this.error.hide();
|
96 |
this.vm.reset_action_error();
|
97 |
}, this)));
|
98 |
|
99 |
// hide error message if action fail get reset
|
100 |
this.vm.bind("action:fail:reset", _.bind(function(){ |
101 |
this.error.hide();
|
102 |
}, this));
|
103 |
}, |
104 |
|
105 |
show_error_overlay: function(args) { |
106 |
var args = util.parse_api_error.apply(util, args);
|
107 |
|
108 |
// force logout if UNAUTHORIZED request arrives
|
109 |
if (args.code == 401) { snf.ui.logout(); return }; |
110 |
|
111 |
var error_entry = [args.ns, args.code, args.message, args.type, args.details, args];
|
112 |
ui.main.error_view.show_error.apply(ui.main.error_view, error_entry); |
113 |
}, |
114 |
|
115 |
update_layout: function() { |
116 |
if (this.vm.action_error) { |
117 |
this.error.show();
|
118 |
} |
119 |
} |
120 |
}); |
121 |
|
122 |
// handle extended info toggler
|
123 |
views.IconInfoView = views.View.extend({ |
124 |
|
125 |
initialize: function (vm, view) { |
126 |
this.vm = vm;
|
127 |
this.view = view;
|
128 |
this.vm_view = this.view.vm(vm); |
129 |
|
130 |
this.info_toggle = $(".cont-toggler-wrapper.info .toggler", this.vm_view); |
131 |
this.ips_toggle = $(".cont-toggler-wrapper.ips .toggler", this.vm_view); |
132 |
this.info_el = $("div.info-content.vm-info", this.vm_view); |
133 |
this.ips_el = $("div.info-content.ips", this.vm_view); |
134 |
this.label = $(".label", this.vm_view); |
135 |
|
136 |
this.set_handlers();
|
137 |
}, |
138 |
|
139 |
set_handlers: function() { |
140 |
this.info_toggle.click(_.bind(function(){ |
141 |
this.ips_el.slideUp();
|
142 |
this.ips_toggle.removeClass("open"); |
143 |
|
144 |
this.info_el.slideToggle();
|
145 |
this.view.vm(this.vm).toggleClass("light-background"); |
146 |
|
147 |
if (this.info_toggle.hasClass("open")) { |
148 |
this.info_toggle.removeClass("open"); |
149 |
this.vm.stop_stats_update();
|
150 |
} else {
|
151 |
this.info_toggle.addClass("open"); |
152 |
this.view.details_views[this.vm.id].update_layout(); |
153 |
this.view.tags_views[this.vm.id].update_layout(); |
154 |
this.view.stats_views[this.vm.id].update_layout(); |
155 |
} |
156 |
|
157 |
var self = this; |
158 |
window.setTimeout(function() {$(self.view).trigger("resize")}, 300); |
159 |
}, this));
|
160 |
|
161 |
this.ips_toggle.click(_.bind(function(){ |
162 |
this.info_el.slideUp();
|
163 |
this.info_toggle.removeClass("open"); |
164 |
|
165 |
this.ips_el.slideToggle();
|
166 |
this.view.vm(this.vm).toggleClass("light-background"); |
167 |
var self = this; |
168 |
if (this.ips_toggle.hasClass("open")) { |
169 |
this.ips_toggle.removeClass("open"); |
170 |
} else {
|
171 |
this.ips_toggle.addClass("open"); |
172 |
} |
173 |
window.setTimeout(function() {$(self.view).trigger("resize")}, 300); |
174 |
}, this));
|
175 |
|
176 |
|
177 |
this.$(".stats-report").click(_.bind(function(e){ |
178 |
e.preventDefault(); |
179 |
snf.ui.main.show_vm_details(this.vm);
|
180 |
}, this))
|
181 |
} |
182 |
|
183 |
}) |
184 |
|
185 |
// rename handler view
|
186 |
// only icon view contains rename capability
|
187 |
views.IconRenameView = views.View.extend({ |
188 |
|
189 |
initialize: function(vm, view) { |
190 |
this.vm = vm;
|
191 |
this.view = view;
|
192 |
// name container
|
193 |
this.el = $('div#' + this.view.id_tpl + vm.id + " div.name").get(0); |
194 |
// name inline element
|
195 |
this.name = this.$('span.name'); |
196 |
// rename button
|
197 |
this.rename = this.$('span.rename'); |
198 |
// save button
|
199 |
this.save = this.$('.save'); |
200 |
// cancel rename button
|
201 |
this.cancel = this.$('.cancel'); |
202 |
// where to place the input field
|
203 |
this.edit_cont = this.$(".namecontainer"); |
204 |
// buttons container
|
205 |
this.buttons = this.$(".editbuttons"); |
206 |
// current state
|
207 |
this.renaming = false; |
208 |
// init event handlers
|
209 |
this.set_handlers();
|
210 |
// paint
|
211 |
this.update_layout();
|
212 |
views.IconRenameView.__super__.initialize.call(this);
|
213 |
}, |
214 |
|
215 |
// update elements visibility/state
|
216 |
update_layout: function() { |
217 |
// if in renaming state
|
218 |
if (this.renaming) { |
219 |
// if name is hidden we are already in renaming state
|
220 |
// dont do nothing
|
221 |
if (this.name.is(":hidden")){return} |
222 |
|
223 |
// hide name element to make space for the
|
224 |
// text input
|
225 |
this.name.hide();
|
226 |
this.rename.hide();
|
227 |
// show confirm/cancel buttons
|
228 |
this.buttons.show();
|
229 |
// create text element
|
230 |
this.create_input();
|
231 |
} else {
|
232 |
// name is visible not in edit mode
|
233 |
if (this.name.is(":visible")){return} |
234 |
|
235 |
this.name.show();
|
236 |
this.rename.show();
|
237 |
this.buttons.hide();
|
238 |
this.remove_input();
|
239 |
} |
240 |
}, |
241 |
|
242 |
// create rename input field and set appropriate
|
243 |
// event handlers
|
244 |
create_input: function() { |
245 |
var self = this; |
246 |
this.edit_cont.append('<input class="vm-rename nametextbox" type="text" />'); |
247 |
this.$('input').val(this.vm.get('name')); |
248 |
// give edit focus
|
249 |
this.$('input').focus(); |
250 |
// handle enter press
|
251 |
this.$('input').keydown(function(ev){ |
252 |
ev.keyCode = ev.keyCode || ev.which; |
253 |
if (ev.keyCode == 13) { self.submit(); } |
254 |
if (ev.keyCode == 27) { self.renaming = false; self.update_layout(); } |
255 |
}) |
256 |
}, |
257 |
|
258 |
// remove input element
|
259 |
remove_input: function() { |
260 |
this.$('input').remove(); |
261 |
}, |
262 |
|
263 |
// initialize event handlers
|
264 |
set_handlers: function() { |
265 |
var self = this; |
266 |
// start rename when rename button is pressed
|
267 |
this.rename.click(function() { |
268 |
self.renaming = true;
|
269 |
self.update_layout(); |
270 |
}); |
271 |
|
272 |
// double click on name
|
273 |
$(this.el).dblclick(function() { |
274 |
self.renaming = true;
|
275 |
self.update_layout(); |
276 |
}); |
277 |
|
278 |
// cancel rename
|
279 |
this.cancel.click(function() { |
280 |
self.renaming = false;
|
281 |
self.update_layout(); |
282 |
}) |
283 |
|
284 |
// apply the rename
|
285 |
// TODO: check if name is equal than the previous value
|
286 |
this.save.click(function() { |
287 |
self.submit(); |
288 |
}) |
289 |
}, |
290 |
|
291 |
submit: function() { |
292 |
var value = _(self.$('input').val()).trim(); |
293 |
if (value == "") { return }; |
294 |
this.renaming = false; |
295 |
this.vm.rename(self.$('input').val()); |
296 |
this.update_layout();
|
297 |
} |
298 |
}); |
299 |
|
300 |
// VM connect interaction view
|
301 |
views.IconVMConnectView = views.View.extend({ |
302 |
|
303 |
initialize: function(vm, view) { |
304 |
// parent view (single, icon, list)
|
305 |
this.parent = view;
|
306 |
this.vm = vm;
|
307 |
this.el = view.vm(vm);
|
308 |
this.set_handlers();
|
309 |
views.IconVMConnectView.__super__.initialize.call(this);
|
310 |
}, |
311 |
|
312 |
// set the appropriate handlers
|
313 |
set_handlers: function() { |
314 |
// setup connect handler on vm icon interaction
|
315 |
var el = this.el; |
316 |
var vm = this.vm; |
317 |
|
318 |
// element that triggers the connect handler
|
319 |
var connect = el.find("div.connect-arrow, .logo"); |
320 |
// connect status handler
|
321 |
var handler = _.bind(this.connect_handler, {vm:vm, el:el, view:this.parent}); |
322 |
$(connect).bind({'mouseover': handler, 'mouseleave': handler, |
323 |
'mousedown': handler, 'mouseup': handler, |
324 |
'click': handler });
|
325 |
|
326 |
// setup connect arrow display handlers
|
327 |
// while hovering vm container
|
328 |
el.bind("mouseover", function(){ |
329 |
if (vm.is_connectable()) {
|
330 |
el.find(".connect-border").show();
|
331 |
el.find(".connect-arrow").show();
|
332 |
el.find(".logo").css({cursor:"pointer"}); |
333 |
} else {
|
334 |
el.find(".connect-border").hide();
|
335 |
el.find(".connect-arrow").hide();
|
336 |
el.find(".logo").css({cursor: "default"}); |
337 |
} |
338 |
}).bind("mouseleave", function(){ |
339 |
el.find(".connect-border").hide();
|
340 |
el.find(".connect-arrow").hide();
|
341 |
}); |
342 |
}, |
343 |
|
344 |
// connect arrow interaction handlers
|
345 |
// BEWARE, this function has different context
|
346 |
// than the View object itself, see set_vm_handlers
|
347 |
connect_handler: function(event) { |
348 |
// nothing to do if we cannot connect to the vm
|
349 |
if (!this.vm.is_connectable()) {return} |
350 |
|
351 |
var logo = this.el.find(".logo"); |
352 |
var arrow = this.el.find(".connect-arrow"); |
353 |
var border = this.el.find(".connect-border"); |
354 |
|
355 |
// clear icon states
|
356 |
logo.removeClass('single-image-state1 single-image-state2 single-image-state3 single-image-state4');
|
357 |
|
358 |
// append the appropriate state class
|
359 |
switch (event.type) {
|
360 |
case "mouseover": |
361 |
logo.addClass('single-image-state4');
|
362 |
arrow.addClass('border-hover');
|
363 |
break;
|
364 |
|
365 |
case "mouseleave": |
366 |
logo.addClass('single-image-state1');
|
367 |
arrow.removeClass('border-hover');
|
368 |
break;
|
369 |
|
370 |
case "mouseup": |
371 |
logo.addClass('single-image-state4');
|
372 |
//this.view.connect_overlay.show(this.vm);
|
373 |
break;
|
374 |
|
375 |
case "mousedown": |
376 |
logo.addClass('single-image-state2');
|
377 |
break;
|
378 |
|
379 |
case "click": |
380 |
//logo.addCLass('single-image-state4');
|
381 |
//this.view.connect_to_console(vm);
|
382 |
this.view.connect_overlay.show(this.vm); |
383 |
break;
|
384 |
|
385 |
default:
|
386 |
; |
387 |
} |
388 |
}, |
389 |
|
390 |
update_layout: function() { |
391 |
} |
392 |
|
393 |
}); |
394 |
|
395 |
// vm metadata subview for icon and single view
|
396 |
views.VMTagsView = views.View.extend({ |
397 |
view_id: 'vm_tags', |
398 |
// metadata container selector
|
399 |
el_sel: '.vm-metadata', |
400 |
// metadata row template
|
401 |
tag_tpl: '<span class="tag-item"><span class="key">{0}</span><span class="value">{1}</span></span>', |
402 |
// how many tags to show
|
403 |
tag_limit: 4, |
404 |
// truncate options (because container has different size on icon/single views)
|
405 |
tag_key_truncate: 7, |
406 |
tag_value_truncate: 15, |
407 |
|
408 |
initialize: function(vm, view, toggle, limit, tag_key_truncate, tag_value_truncate) { |
409 |
this.tag_limit = limit || this.tag_limit; |
410 |
|
411 |
this.tag_key_truncate = tag_key_truncate || this.tag_key_truncate; |
412 |
this.tag_value_truncate = tag_value_truncate || this.tag_value_truncate; |
413 |
|
414 |
// does the view toggles the metadata container (single view)
|
415 |
this.toggle = toggle || false; |
416 |
// parent view
|
417 |
this.parent = view;
|
418 |
this.vm = vm;
|
419 |
this.el = this.parent.vm(vm); |
420 |
this.view_id = this.view_id + "_" + vm.id; |
421 |
|
422 |
// link to raise the metadata manager overlay
|
423 |
this.link = this.$('a.manage-metadata'); |
424 |
|
425 |
views.VMTagsView.__super__.initialize.call(this);
|
426 |
this.set_handlers();
|
427 |
this.update_layout();
|
428 |
}, |
429 |
|
430 |
// set the appropriate handlers
|
431 |
set_handlers: function() { |
432 |
var self = this; |
433 |
// show the metadata editor overlay
|
434 |
this.link.click(_.bind(function(ev) { |
435 |
ev.preventDefault(); |
436 |
this.parent.metadata_view.show(this.vm); |
437 |
}, this));
|
438 |
|
439 |
// tags/ips have show/hide control ? bind events for them
|
440 |
var self = this; |
441 |
if (this.toggle) { |
442 |
$(this.el).find(".tags-header").click(_.bind(function(){ |
443 |
$(self.el).find(".tags-content").slideToggle(600); |
444 |
|
445 |
var details_toggler = $(this.el).find(".tags-header " + |
446 |
".cont-toggler.info");
|
447 |
var ips_toggler = $(this.el).find(".tags-header " + |
448 |
".cont-toggler.ips");
|
449 |
|
450 |
if (details_toggler.hasClass("open")) { |
451 |
details_toggler.removeClass("open");
|
452 |
} else {
|
453 |
details_toggler.addClass("open");
|
454 |
} |
455 |
|
456 |
if (ips_toggler.hasClass("open")) { |
457 |
ips_toggler.removeClass("open");
|
458 |
} else {
|
459 |
ips_toggler.addClass("open");
|
460 |
} |
461 |
}, this));
|
462 |
$(self.el).find(".tags-content").hide(); |
463 |
} |
464 |
}, |
465 |
|
466 |
// update metadata container and data
|
467 |
update_layout: function() { |
468 |
|
469 |
// api metadata object
|
470 |
var meta = this.vm.get('metadata'); |
471 |
|
472 |
var i = 0; |
473 |
var cont = $(this.el).find(".items"); |
474 |
|
475 |
// clear existing items
|
476 |
cont.find(".tag-item").remove();
|
477 |
|
478 |
// create tag elements
|
479 |
_.each(meta, function(value, key){
|
480 |
// respect the limit
|
481 |
if (i > this.tag_limit) { |
482 |
return;
|
483 |
} |
484 |
|
485 |
// create element
|
486 |
var new_el = $(this.tag_tpl.format(util.truncate(key, this.tag_key_truncate), |
487 |
util.truncate(": " + _.escape(value), this.tag_value_truncate))); |
488 |
|
489 |
// add title attributes, improve accesibility
|
490 |
// truncated values
|
491 |
new_el.find("span.key").attr("title", key); |
492 |
new_el.find("span.value").attr("title", _.escape(value)); |
493 |
|
494 |
cont.append(new_el); |
495 |
}, this);
|
496 |
} |
497 |
}); |
498 |
|
499 |
|
500 |
// stats subview for single/icon views
|
501 |
views.VMStatsView = views.View.extend({ |
502 |
|
503 |
initialize: function(vm, view, options) { |
504 |
if (!options) {options = {}};
|
505 |
this.vm = vm;
|
506 |
this.parent = view;
|
507 |
this.sel = options.el || this.el_sel || ".lower"; |
508 |
this.el = this.parent.vm(vm).find(this.sel); |
509 |
this.selected_stats_period = 'hourly'; |
510 |
|
511 |
// elements shortcuts
|
512 |
this.cpu_loading = this.el.find(".cpu-graph .stat-busy"); |
513 |
this.cpu_error = this.el.find(".cpu-graph .stat-error"); |
514 |
this.cpu_img = this.el.find(".cpu-graph .stat-img"); |
515 |
this.net_loading = this.el.find(".network-graph .stat-busy"); |
516 |
this.net_error = this.el.find(".network-graph .stat-error"); |
517 |
this.net_img = this.el.find(".network-graph .stat-img"); |
518 |
|
519 |
this.loading = this.el.find(".stat-busy"); |
520 |
this.error = this.el.find(".stat-error"); |
521 |
this.img = this.el.find(".stat-img"); |
522 |
this.stats_period_options = this.el.find(".stats-select-option"); |
523 |
|
524 |
|
525 |
// handle stats period option select
|
526 |
var self = this; |
527 |
this.stats_period_options.click(function(){ |
528 |
// skip if current selection is clicked
|
529 |
if ($(this).filter(".stats-" + self.selected_stats_period).length) { |
530 |
return
|
531 |
} else {
|
532 |
// identify class
|
533 |
var cls = $(this).attr("class"); |
534 |
regex = /.*\sstats-(\w+)/;
|
535 |
self.set_stats_period(cls.match(regex)[1]);
|
536 |
} |
537 |
}); |
538 |
|
539 |
// initial state paremeters
|
540 |
this.stats = this.vm.get("stats"); |
541 |
|
542 |
// timeseries or bar images ?
|
543 |
this.stats_type = options.stats_type || "bar"; |
544 |
|
545 |
views.VMStatsView.__super__.initialize.apply(this, arguments); |
546 |
this.set_handlers();
|
547 |
this.update_layout();
|
548 |
|
549 |
this.net_loading.show();
|
550 |
this.net_error.hide();
|
551 |
this.cpu_loading.show();
|
552 |
this.cpu_error.hide();
|
553 |
|
554 |
this.net_img.hide();
|
555 |
this.cpu_img.hide();
|
556 |
|
557 |
if (!window.t) {
|
558 |
window.t = []; |
559 |
} |
560 |
if (this.parent.menu) { |
561 |
window.t[window.t.length] = this;
|
562 |
} |
563 |
}, |
564 |
|
565 |
|
566 |
set_stats_period: function(period) { |
567 |
this.selected_stats_period = period;
|
568 |
this.update_layout();
|
569 |
}, |
570 |
|
571 |
set_handlers: function() { |
572 |
// update view state when vm stats update gets triggered
|
573 |
this.vm.bind("stats:update", _.bind(function(){ |
574 |
// update the layout
|
575 |
this.update_layout();
|
576 |
}, this));
|
577 |
}, |
578 |
|
579 |
get_images: function (type, period) { |
580 |
var period = period || 'hourly'; |
581 |
var images;
|
582 |
|
583 |
if (type == 'bar') { |
584 |
images = {'cpu': this.stats.cpuBar, 'net': this.stats.netBar }; |
585 |
} else {
|
586 |
images = {'cpu': this.stats.cpuTimeSeries, |
587 |
'net': this.stats.netTimeSeries }; |
588 |
} |
589 |
|
590 |
if (period == 'weekly' && type != 'bar') { |
591 |
if (images.cpu)
|
592 |
images.cpu = images.cpu.replace('cpu-ts', 'cpu-ts-w') |
593 |
if (images.net)
|
594 |
images.net = images.net.replace('net-ts', 'net-ts-w') |
595 |
} |
596 |
return images
|
597 |
}, |
598 |
|
599 |
update_layout: function() { |
600 |
if (!this.vm.stats_available) { |
601 |
this.loading.show();
|
602 |
this.img.hide();
|
603 |
this.error.hide();
|
604 |
} else {
|
605 |
this.loading.hide();
|
606 |
this.stats = this.vm.get("stats"); |
607 |
var images = this.get_images(this.stats_type, |
608 |
this.selected_stats_period)
|
609 |
|
610 |
if (images.cpu) {
|
611 |
this.cpu_img.attr({src:images.cpu}).show(); |
612 |
this.cpu_error.hide();
|
613 |
} else {
|
614 |
this.cpu_img.hide();
|
615 |
this.cpu_error.show();
|
616 |
} |
617 |
|
618 |
if (images.net) {
|
619 |
this.net_img.attr({src:images.net}).show(); |
620 |
this.net_error.hide();
|
621 |
} else {
|
622 |
this.net_img.hide();
|
623 |
this.net_error.show();
|
624 |
} |
625 |
} |
626 |
|
627 |
// update selected stats period
|
628 |
this.stats_period_options.removeClass("selected"); |
629 |
this.stats_period_options.filter(".stats-" + this.selected_stats_period).addClass("selected") |
630 |
|
631 |
$(window).trigger("resize"); |
632 |
} |
633 |
}); |
634 |
|
635 |
views.VMDetailsView = views.View.extend({ |
636 |
view_id: "vm_details", |
637 |
el_sel: '.vm-details', |
638 |
|
639 |
|
640 |
selectors: {
|
641 |
'cpu': '.cpu-data', |
642 |
'ram': '.ram-data', |
643 |
'disk': '.disk-data', |
644 |
'image_name': '.image-data', |
645 |
'image_size': '.image-size-data' |
646 |
}, |
647 |
|
648 |
initialize: function(vm, view) { |
649 |
this.parent = view;
|
650 |
this.vm = vm;
|
651 |
this.el = $(this.parent.vm(vm)).find(this.el_sel).get(0); |
652 |
this.view_id = "vm_{0}_details".format(vm.id); |
653 |
|
654 |
views.VMDetailsView.__super__.initialize.call(this);
|
655 |
|
656 |
this.resize_actions = this.$(".trigger-resize"); |
657 |
this.init_handlers();
|
658 |
this.update_layout();
|
659 |
}, |
660 |
|
661 |
init_handlers: function() { |
662 |
this.resize_actions.bind('click', _.bind(function(e){ |
663 |
ui.main.vm_resize_view.show(this.vm);
|
664 |
}, this));
|
665 |
}, |
666 |
|
667 |
update_layout: function() { |
668 |
if (!this.visible() && this.parent.details_hidden) { return }; |
669 |
|
670 |
var image = this.vm.get_image(_.bind(function(image){ |
671 |
this.sel('image_name').text(util.truncate(image.escape('name'), 17)).attr("title", image.escape('name')); |
672 |
this.sel('image_size').text(image.get_readable_size()).attr('title', image.get_readable_size()); |
673 |
}, this));
|
674 |
|
675 |
var flavor = this.vm.get_flavor(); |
676 |
if (!flavor || !image) {
|
677 |
return;
|
678 |
} |
679 |
|
680 |
|
681 |
this.sel('cpu').text(flavor.get('cpu')); |
682 |
this.sel('ram').text(flavor.get('ram')); |
683 |
this.sel('disk').text(flavor.get('disk')); |
684 |
|
685 |
this.parent.tags_views[this.vm.id].update_layout(); |
686 |
this.parent.stats_views[this.vm.id].update_layout(); |
687 |
|
688 |
if (this.parent.details_hidden) { |
689 |
this.vm.start_stats_update(true); |
690 |
} |
691 |
} |
692 |
}); |
693 |
|
694 |
// VMs icon view
|
695 |
views.IconView = views.VMListView.extend({ |
696 |
|
697 |
// view id (this could be used to identify
|
698 |
// the view object from global context
|
699 |
view_id: 'vm_icon', |
700 |
|
701 |
details_hidden: true, |
702 |
|
703 |
el: '#machinesview-icon', |
704 |
id_tpl: 'icon-vm-', |
705 |
|
706 |
selectors: {
|
707 |
'vms': '.machine-container', |
708 |
'vm': '#icon-vm-', |
709 |
'view': '#machinesview-icon', |
710 |
'tpl': '#machinesview-icon.standard #machine-container-template', |
711 |
'spinner': '.large-spinner', |
712 |
'vm_spinner': '#icon-vm-{0} .state .spinner', |
713 |
'vm_wave': '#icon-vm-{0} .wave', |
714 |
'vm_cont_active': '#machinesview-icon.standard .running', |
715 |
'vm_cont_terminated': '#machinesview-icon.standard .terminated' |
716 |
}, |
717 |
|
718 |
reset: function() {}, |
719 |
// overload show function
|
720 |
show_view: function() { |
721 |
$(this.el).show(); |
722 |
this.__update_layout();
|
723 |
}, |
724 |
|
725 |
post_update_vm: function(vm) { |
726 |
}, |
727 |
|
728 |
// identify vm model instance id based on DOM element
|
729 |
vm_id_for_element: function(el) { |
730 |
return el.attr('id').replace("icon-vm-",""); |
731 |
}, |
732 |
|
733 |
// set generic view handlers
|
734 |
set_handlers: function() { |
735 |
}, |
736 |
|
737 |
// stuff to do when a new vm has been created.
|
738 |
// - create vm subviews
|
739 |
post_add: function(vm) { |
740 |
// rename views index
|
741 |
this.rename_views = this.rename_views || {}; |
742 |
this.stats_views = this.stats_views || {}; |
743 |
this.connect_views = this.connect_views || {}; |
744 |
this.tags_views = this.tags_views || {}; |
745 |
this.details_views = this.details_views || {}; |
746 |
this.info_views = this.info_views || {}; |
747 |
this.action_error_views = this.action_error_views || {}; |
748 |
this.action_views = this.action_views || {}; |
749 |
this.ports_views = this.ports_views || {}; |
750 |
|
751 |
this.action_views[vm.id] = new views.VMActionsView(vm, this, this.vm(vm), this.hide_actions); |
752 |
this.rename_views[vm.id] = new views.IconRenameView(vm, this); |
753 |
this.stats_views[vm.id] = new views.VMStatsView(vm, this, {el:'.vm-stats'}); |
754 |
this.connect_views[vm.id] = new views.IconVMConnectView(vm, this); |
755 |
this.tags_views[vm.id] = new views.VMTagsView(vm, this); |
756 |
this.details_views[vm.id] = new views.VMDetailsView(vm, this); |
757 |
this.action_error_views[vm.id] = new views.VMActionErrorView(vm, this); |
758 |
|
759 |
var ports_container = this.vm(vm).find(".machine-data"); |
760 |
var ports_view = new views.VMPortListView({ |
761 |
collection: vm.ports,
|
762 |
container: ports_container,
|
763 |
parent: this |
764 |
}); |
765 |
this.ports_views[vm.id] = ports_view
|
766 |
ports_view.show(); |
767 |
ports_view.el.hide(); |
768 |
|
769 |
this.info_views[vm.id] = new views.IconInfoView(vm, this); |
770 |
}, |
771 |
|
772 |
// vm specific event handlers
|
773 |
set_vm_handlers: function(vm) { |
774 |
}, |
775 |
|
776 |
check_terminated_is_empty: function() { |
777 |
// hide/show terminated container
|
778 |
if (this.$(".terminated .machine-container").length == 0) { |
779 |
this.$(".terminated").hide() |
780 |
} else {
|
781 |
this.$(".terminated").show() |
782 |
} |
783 |
|
784 |
$(window).trigger("resize"); |
785 |
}, |
786 |
|
787 |
// generic stuff to do on each view update
|
788 |
// called once after each vm has been updated
|
789 |
update_layout: function() { |
790 |
// TODO: why do we do this ??
|
791 |
if (storage.vms.models.length > 0) { |
792 |
this.$(".running").removeClass("disabled"); |
793 |
} else {
|
794 |
this.$(".running").addClass("disabled"); |
795 |
} |
796 |
|
797 |
this.check_terminated_is_empty();
|
798 |
|
799 |
// FIXME: code from old js api
|
800 |
this.$("div.separator").show(); |
801 |
this.$("div.machine-container:last-child").find("div.separator").hide(); |
802 |
fix_v6_addresses(); |
803 |
}, |
804 |
|
805 |
update_status_message: function(vm) { |
806 |
var el = this.vm(vm); |
807 |
var message = vm.get_status_message();
|
808 |
if (message) {
|
809 |
// update bulding progress
|
810 |
el.find("div.machine-ips").hide();
|
811 |
el.find("div.build-progress").show();
|
812 |
el.find("div.build-progress .message").text(util.truncate(message, 42)); |
813 |
|
814 |
if (vm.in_error_state()) {
|
815 |
el.find("div.build-progress .btn").show();
|
816 |
} else {
|
817 |
el.find("div.build-progress .btn").hide();
|
818 |
} |
819 |
} else {
|
820 |
// hide building progress
|
821 |
el.find("div.machine-ips").show()
|
822 |
el.find("div.build-progress").hide();
|
823 |
el.find("div.build-progress .btn").hide();
|
824 |
} |
825 |
}, |
826 |
|
827 |
// update vm details
|
828 |
update_details: function(vm) { |
829 |
var el = this.vm(vm); |
830 |
// truncate name
|
831 |
el.find("span.name").text(util.truncate(vm.get("name"), 40)); |
832 |
|
833 |
el.find('.fqdn').text(vm.get('fqdn')); |
834 |
el.find("div.status").text(STATE_TEXTS[vm.state()]);
|
835 |
// set state class
|
836 |
el.find("div.state").removeClass().addClass(views.IconView.STATE_CLASSES[vm.state()].join(" ")); |
837 |
// os icon
|
838 |
el.find("div.logo").css({'background-image': "url(" + this.get_vm_icon_path(vm, "medium") + ")"}); |
839 |
|
840 |
el.removeClass("connectable");
|
841 |
if (vm.is_connectable()) {
|
842 |
el.addClass("connectable");
|
843 |
} |
844 |
|
845 |
var status = vm.get("status"); |
846 |
var state = vm.get("state"); |
847 |
|
848 |
this.update_status_message(vm);
|
849 |
|
850 |
icon_state = vm.is_active() ? "on" : "off"; |
851 |
set_machine_os_image(el, "icon", icon_state, this.get_vm_icon_os(vm)); |
852 |
|
853 |
// update subviews
|
854 |
this.rename_views[vm.id].update_layout();
|
855 |
this.connect_views[vm.id].update_layout();
|
856 |
this.details_views[vm.id].update_layout();
|
857 |
}, |
858 |
|
859 |
post_remove_vm: function(vm) { |
860 |
this.check_terminated_is_empty();
|
861 |
$(window).trigger("resize"); |
862 |
}, |
863 |
|
864 |
get_vm_icon_os: function(vm) { |
865 |
var os = vm.get_os();
|
866 |
var icons = window.os_icons || views.IconView.VM_OS_ICONS;
|
867 |
|
868 |
if (icons.indexOf(os) == -1) { |
869 |
os = "unknown";
|
870 |
} |
871 |
|
872 |
return os;
|
873 |
}, |
874 |
|
875 |
// TODO: move to views.utils (the method and the VM_OS_ICON vars)
|
876 |
get_vm_icon_path: function(vm, icon_type) { |
877 |
var os = vm.get_os();
|
878 |
var icons = window.os_icons || views.IconView.VM_OS_ICONS;
|
879 |
|
880 |
if (icons.indexOf(os) == -1) { |
881 |
os = "unknown";
|
882 |
} |
883 |
|
884 |
return views.IconView.VM_OS_ICON_TPLS()[icon_type].format(os);
|
885 |
} |
886 |
}) |
887 |
|
888 |
views.IconView.VM_OS_ICON_TPLS = function() { |
889 |
return {
|
890 |
"medium": snf.config.machines_icons_url + "medium/{0}-sprite.png" |
891 |
} |
892 |
} |
893 |
|
894 |
views.IconView.VM_OS_ICONS = window.os_icons || []; |
895 |
|
896 |
views.IconView.STATE_CLASSES = { |
897 |
'UNKNOWN': ['state', 'error-state'], |
898 |
'BUILD': ['state', 'build-state'], |
899 |
'REBOOT': ['state', 'rebooting-state'], |
900 |
'STOPPED': ['state', 'terminated-state'], |
901 |
'ACTIVE': ['state', 'running-state'], |
902 |
'ERROR': ['state', 'error-state'], |
903 |
'DELETED': ['state', 'destroying-state'], |
904 |
'DESTROY': ['state', 'destroying-state'], |
905 |
'SHUTDOWN': ['state', 'shutting-state'], |
906 |
'START': ['state', 'starting-state'], |
907 |
'CONNECT': ['state', 'connecting-state'], |
908 |
'DISCONNECT': ['state', 'disconnecting-state'], |
909 |
'RESIZE': ['state', 'rebooting-state'] |
910 |
}; |
911 |
|
912 |
})(this);
|