root / ui / static / snf / js / ui / web / ui_networks_view.js @ 5939efeb
History | View | Annotate | Download (31.9 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 || {};
|
12 |
var views = snf.views = snf.views || {}
|
13 |
|
14 |
// shortcuts
|
15 |
var bb = root.Backbone;
|
16 |
|
17 |
// logging
|
18 |
var logger = new snf.logging.logger("SNF-VIEWS"); |
19 |
var debug = _.bind(logger.debug, logger);
|
20 |
|
21 |
|
22 |
views.NetworkConnectVMsOverlay = views.Overlay.extend({ |
23 |
title: "Connect machine", |
24 |
overlay_id: "overlay-select-vms", |
25 |
content_selector: "#network-vms-select-content", |
26 |
css_class: "overlay-info", |
27 |
|
28 |
initialize: function() { |
29 |
views.NetworkConnectVMsOverlay.__super__.initialize.apply(this);
|
30 |
this.list = this.$(".vms-list ul"); |
31 |
this.empty_message = this.$(".empty-message"); |
32 |
}, |
33 |
|
34 |
init_handlers: function() { |
35 |
var self = this; |
36 |
this.list.find("li").click(function(){ |
37 |
$(this).toggleClass("selected"); |
38 |
}); |
39 |
|
40 |
this.el.find(".create").click(_.bind(function() { |
41 |
this.submit();
|
42 |
}, this));
|
43 |
}, |
44 |
|
45 |
reset: function() { |
46 |
this.list.find("li").remove(); |
47 |
}, |
48 |
|
49 |
beforeOpen: function() { |
50 |
this.reset();
|
51 |
this.update_layout();
|
52 |
}, |
53 |
|
54 |
vm: function(vm) { |
55 |
if (vm.id) { var id = vm.id } else {var id = vm} |
56 |
return this.list.find(".vm-" + id); |
57 |
}, |
58 |
|
59 |
get_selected: function() { |
60 |
return this.list.find(".selected").map(function() {return $(this).data('vm')}) |
61 |
}, |
62 |
|
63 |
update_layout: function() { |
64 |
if (this.vms.length == 0) { |
65 |
this.empty_message.show();
|
66 |
} else {
|
67 |
this.empty_message.hide();
|
68 |
} |
69 |
|
70 |
_.each(this.vms, _.bind(function(vm){ |
71 |
var html = '<li class="vm option options-object vm-{0}">' + |
72 |
'<div class="options-object-cont">' +
|
73 |
'{2}' +
|
74 |
'<span class="title">{1}</span>' +
|
75 |
'<span class="value">{3}</span></div>' +
|
76 |
'</li>';
|
77 |
var el = $(html.format(vm.id, |
78 |
util.truncate(vm.get("name"), 23), |
79 |
snf.ui.helpers.vm_icon_tag(vm, "small", {'class':'os'}), |
80 |
vm.get_os() |
81 |
)) |
82 |
el.data({vm:vm,vm_id:vm.id}) |
83 |
this.list.append(el);
|
84 |
|
85 |
vm.bind("remove", function(){ el.remove()}) |
86 |
vm.bind("change:name", function(i,v){el.find(".title").text(v)}) |
87 |
}, this));
|
88 |
|
89 |
this.init_handlers();
|
90 |
this.set_selected();
|
91 |
}, |
92 |
|
93 |
set_selected: function() { |
94 |
_.each(this.selected, _.bind(function(el){ |
95 |
this.vm(el).addClass("selected"); |
96 |
}, this));
|
97 |
}, |
98 |
|
99 |
show_vms: function(network, vms, selected, callback) { |
100 |
this.network = network;
|
101 |
this.reset();
|
102 |
this.set_subtitle(network.get("name")); |
103 |
this.vms = vms;
|
104 |
this.selected = selected;
|
105 |
this.cb = callback;
|
106 |
this.show();
|
107 |
}, |
108 |
|
109 |
submit: function() { |
110 |
this.cb(this.get_selected()); |
111 |
} |
112 |
}) |
113 |
|
114 |
views.NetworkActionsView = views.View.extend({ |
115 |
|
116 |
initialize: function(view, net, el, opts) { |
117 |
this.parent = view
|
118 |
this.network = net;
|
119 |
this.el = el;
|
120 |
|
121 |
this.actions = this.$(".actions"); |
122 |
this.selected = undefined; |
123 |
|
124 |
this.destroy = this.$(".actions .destroy a"); |
125 |
this.connect = this.$(".actions .add"); |
126 |
|
127 |
this.init_handlers();
|
128 |
this.update_layout();
|
129 |
}, |
130 |
|
131 |
init_handlers: function() { |
132 |
this.connect.click(_.bind(function(e){ |
133 |
e.preventDefault(); |
134 |
})) |
135 |
}, |
136 |
|
137 |
update_layout: function() { |
138 |
} |
139 |
}); |
140 |
|
141 |
views.NetworkCreateView = views.Overlay.extend({ |
142 |
view_id: "network_create_view", |
143 |
content_selector: "#networks-create-content", |
144 |
css_class: 'overlay-networks-create overlay-info', |
145 |
overlay_id: "network-create-overlay", |
146 |
|
147 |
title: "Create new private network", |
148 |
subtitle: "Networks", |
149 |
|
150 |
initialize: function(options) { |
151 |
views.NetworkCreateView.__super__.initialize.apply(this);
|
152 |
|
153 |
this.create_button = this.$("form .form-action.create"); |
154 |
this.text = this.$(".network-create-name"); |
155 |
this.form = this.$("form"); |
156 |
this.init_handlers();
|
157 |
}, |
158 |
|
159 |
init_handlers: function() { |
160 |
this.create_button.click(_.bind(function(e){ |
161 |
this.submit();
|
162 |
}, this));
|
163 |
|
164 |
this.form.submit(_.bind(function(e){ |
165 |
e.preventDefault(); |
166 |
this.submit;
|
167 |
return false; |
168 |
}, this))
|
169 |
|
170 |
this.text.keypress(_.bind(function(e){ |
171 |
if (e.which == 13) {this.submit()}; |
172 |
},this))
|
173 |
}, |
174 |
|
175 |
submit: function() { |
176 |
if (this.validate()) { |
177 |
this.create();
|
178 |
}; |
179 |
}, |
180 |
|
181 |
validate: function() { |
182 |
// sanitazie
|
183 |
var t = this.text.val(); |
184 |
t = t.replace(/^\s+|\s+$/g,""); |
185 |
this.text.val(t);
|
186 |
|
187 |
if (this.text.val() == "") { |
188 |
this.text.closest(".form-field").addClass("error"); |
189 |
this.text.focus();
|
190 |
return false; |
191 |
} else {
|
192 |
return true; |
193 |
} |
194 |
}, |
195 |
|
196 |
create: function() { |
197 |
this.create_button.addClass("in-progress"); |
198 |
snf.storage.networks.create(this.text.val(), _.bind(function(){ |
199 |
this.hide();
|
200 |
}, this));
|
201 |
}, |
202 |
|
203 |
beforeOpen: function() { |
204 |
this.create_button.removeClass("in-progress") |
205 |
this.text.closest(".form-field").removeClass("error"); |
206 |
this.text.val(""); |
207 |
this.text.show();
|
208 |
this.text.focus();
|
209 |
}, |
210 |
|
211 |
onOpen: function() { |
212 |
this.text.focus();
|
213 |
} |
214 |
}); |
215 |
|
216 |
views.NetworkVMView = views.View.extend({ |
217 |
|
218 |
initialize: function(vm, parent, firewall_controls, el) { |
219 |
this.firewall_controls = firewall_controls || false; |
220 |
this.vm = vm;
|
221 |
// parent view di
|
222 |
this.parent = parent;
|
223 |
// TODO make it better
|
224 |
this.el = el || this.parent.vm(vm); |
225 |
|
226 |
this.init_layout();
|
227 |
this.update_layout();
|
228 |
|
229 |
this.disconnect = this.$(".action-disconnect"); |
230 |
this.confirm_el = this.$(".confirm_single"); |
231 |
this.cancel = this.$("button.no"); |
232 |
this.confirm = this.$("button.yes"); |
233 |
this.details = this.$(".action-details"); |
234 |
this.vm_connect = this.$(".machine-connect"); |
235 |
|
236 |
this.init_handlers();
|
237 |
this.connect_overlay = new views.VMConnectView(); |
238 |
|
239 |
this.firewall_view = undefined; |
240 |
if (this.firewall_controls) { |
241 |
this.firewall_view = new views.FirewallEditView(this.vm, this.parent.network, this); |
242 |
} |
243 |
|
244 |
}, |
245 |
|
246 |
init_handlers: function() { |
247 |
if (!this.parent.network.is_public()) { |
248 |
this.disconnect.click(_.bind(function(e){ |
249 |
e.preventDefault(); |
250 |
this.confirm_disconnect();
|
251 |
}, this));
|
252 |
this.cancel.click(_.bind(function(e){ |
253 |
e.preventDefault() |
254 |
this.confirm_el.hide();
|
255 |
this.disconnect.removeClass("selected"); |
256 |
}, this));
|
257 |
this.confirm.click(_.bind(function(e){ |
258 |
e.preventDefault() |
259 |
this.disconnect_vm();
|
260 |
this.confirm_el.hide();
|
261 |
this.disconnect.removeClass("selected"); |
262 |
}, this));
|
263 |
|
264 |
snf.ui.main.bind("view:change", _.bind(function(v) { |
265 |
if (v == "networks" ){ return } |
266 |
this.confirm_el.hide();
|
267 |
this.disconnect.removeClass("selected"); |
268 |
}, this));
|
269 |
|
270 |
this.$(".remove-icon").click(_.bind(function(){ |
271 |
this.confirm_disconnect();
|
272 |
}, this));
|
273 |
|
274 |
this.vm_connect.click(_.bind(function() { |
275 |
this.connect_overlay.show(this.vm); |
276 |
}, this));
|
277 |
} |
278 |
|
279 |
var vm = this.vm; |
280 |
this.details.click(function(){ |
281 |
snf.ui.main.show_vm_details(vm); |
282 |
}) |
283 |
|
284 |
}, |
285 |
|
286 |
confirm_disconnect: function() { |
287 |
this.confirm_el.show();
|
288 |
this.disconnect.addClass("selected"); |
289 |
}, |
290 |
|
291 |
init_layout: function() { |
292 |
if (!this.firewall_controls) { return }; |
293 |
}, |
294 |
|
295 |
update_layout: function() { |
296 |
this.$(".vm-name").text(snf.util.truncate(this.vm.get("name"), 40)); |
297 |
this.$("img.logo").attr("src", ui.helpers.vm_icon_path(this.vm, "medium")); |
298 |
|
299 |
if (this.firewall_view) { |
300 |
this.$(".ipv4-text").text(this.vm.get_addresses().ip4); |
301 |
this.$(".ipv6-text").text(this.vm.get_addresses().ip6); |
302 |
} |
303 |
|
304 |
if (this.firewall_view) { |
305 |
this.firewall_view.update_layout();
|
306 |
} |
307 |
}, |
308 |
|
309 |
disconnect_vm: function() { |
310 |
this.$("a.selected").removeClass("selected"); |
311 |
this.parent.network.remove_vm(this.vm); |
312 |
}, |
313 |
|
314 |
update_firewall_layout: function() { |
315 |
} |
316 |
|
317 |
|
318 |
}) |
319 |
|
320 |
views.NetworkModelRenameView = views.View.extend({ |
321 |
initialize: function(parent, network) { |
322 |
this.parent = parent;
|
323 |
this.network = network;
|
324 |
this.el = this.parent.el.find(".name-div"); |
325 |
|
326 |
this.icon = this.$(".rename-network"); |
327 |
this.save = this.$("span.save"); |
328 |
this.cancel = this.$("span.cancel"); |
329 |
this.buttons = this.$(".editbuttons"); |
330 |
this.name = this.$("span.name"); |
331 |
this.editing = false; |
332 |
this.init_handlers();
|
333 |
this.update_layout();
|
334 |
}, |
335 |
|
336 |
init_handlers: function() { |
337 |
this.icon.click(_.bind(function(){ |
338 |
this.editing = true; |
339 |
this.update_layout();
|
340 |
}, this));
|
341 |
this.cancel.click(_.bind(function(){ |
342 |
this.editing = false; |
343 |
this.update_layout();
|
344 |
}, this));
|
345 |
this.save.click(_.bind(function(){ |
346 |
this.submit();
|
347 |
}, this))
|
348 |
}, |
349 |
|
350 |
submit: function() { |
351 |
var value = _(this.input.val()).trim(); |
352 |
if (value == "") { return } |
353 |
|
354 |
this.network.rename(value, _.bind(function(){ |
355 |
this.editing = false; |
356 |
this.update_layout();
|
357 |
}, this));
|
358 |
}, |
359 |
|
360 |
create_input: function() { |
361 |
this.input = $('<input type="text" class="network-rename-input" />'); |
362 |
this.input.val(this.network.get("name")); |
363 |
this.el.append(this.input); |
364 |
this.input.focus();
|
365 |
this.input.bind("keydown", _.bind(function(ev){ |
366 |
ev.keyCode = ev.keyCode || ev.which; |
367 |
if (ev.keyCode == 13) { this.submit(); }; |
368 |
if (ev.keyCode == 27) {this.editing = false; this.update_layout()}; |
369 |
}, this));
|
370 |
}, |
371 |
|
372 |
remove_input: function() { |
373 |
if (!this.input) { return } |
374 |
this.input.remove();
|
375 |
}, |
376 |
|
377 |
update_layout: function() { |
378 |
if (this.editing) { |
379 |
if (this.buttons.is(":visible")) { return } |
380 |
this.icon.hide();
|
381 |
this.buttons.show();
|
382 |
this.create_input();
|
383 |
this.name.hide();
|
384 |
} else {
|
385 |
this.buttons.hide();
|
386 |
this.remove_input();
|
387 |
this.name.show();
|
388 |
this.icon.show();
|
389 |
} |
390 |
} |
391 |
}) |
392 |
|
393 |
views.FirewallEditView = views.View.extend({ |
394 |
initialize: function(vm, network, parent) { |
395 |
this.parent = parent;
|
396 |
this.vm = vm;
|
397 |
this.network = network;
|
398 |
this.el = this.parent.el; |
399 |
|
400 |
views.FirewallEditView.__super__.initialize.apply(this);
|
401 |
|
402 |
// elements
|
403 |
this.toggler = this.$(".firewall-toggle"); |
404 |
this.indicator = this.$(".machines-label span"); |
405 |
this.progress = this.$(".network-progress-indicator"); |
406 |
this.content = this.$(".firewall-content"); |
407 |
this.inputs = this.$("input[type=radio]"); |
408 |
this.labels = this.$("span.checkbox-legends, label.checkbox-legends"); |
409 |
this.apply = this.$(".firewall-apply"); |
410 |
|
411 |
this.$(".firewall-content").hide(); |
412 |
this.$(".firewall-content input[type=radio]").attr("name", "firewall-opt-for-{0}".format(this.vm.id)) |
413 |
var mode = this.vm.firewall_profile(this.network.id); |
414 |
this.$(".firewall-content input[value={0}]".format(mode)).attr("checked", true); |
415 |
|
416 |
this.init_handlers();
|
417 |
this.update_layout();
|
418 |
}, |
419 |
|
420 |
_get_selected: function() { |
421 |
return this.inputs.filter(":checked"); |
422 |
}, |
423 |
|
424 |
reset_selected: function() { |
425 |
}, |
426 |
|
427 |
submit: function() { |
428 |
}, |
429 |
|
430 |
reset_value: function() { |
431 |
this.inputs.filter("[value={0}]".format(this.vm.firewall_profile(this.network.id))).attr("checked"); |
432 |
}, |
433 |
|
434 |
init_handlers: function() { |
435 |
this.toggler.click(_.bind(function(){ |
436 |
cont = this.content;
|
437 |
if (cont.is(":visible")) { |
438 |
this.hide_firewall();
|
439 |
this.reset_value();
|
440 |
} else {
|
441 |
this.show_firewall();
|
442 |
} |
443 |
|
444 |
$(window).trigger("resize"); |
445 |
}, this))
|
446 |
|
447 |
this.apply.click(_.bind(function(){ |
448 |
this.apply.addClass("in-progress"); |
449 |
|
450 |
// make the api call
|
451 |
this.vm.set_firewall(this.network.id, this.value(), |
452 |
// complete
|
453 |
_.bind(function() {
|
454 |
// complete callback
|
455 |
this.apply.removeClass("in-progress"); |
456 |
}, this),
|
457 |
// error
|
458 |
_.bind(function(){
|
459 |
this.vm.remove_pending_firewall(this.network.id, this.value()); |
460 |
}, this));
|
461 |
this.hide_firewall();
|
462 |
}, this))
|
463 |
|
464 |
this.inputs.change(_.bind(function(){ |
465 |
this.update_selected();
|
466 |
}, this))
|
467 |
|
468 |
var self = this; |
469 |
this.$(".checkbox-legends").click(function(el) { |
470 |
var el = $(this); |
471 |
el.prev().click(); |
472 |
self.update_selected(); |
473 |
}) |
474 |
}, |
475 |
|
476 |
update_selected: function() { |
477 |
this.update_layout();
|
478 |
}, |
479 |
|
480 |
show_firewall: function() { |
481 |
this.content.slideDown(100, function(){$(window).trigger("resize")}); |
482 |
this.toggler.addClass("open"); |
483 |
}, |
484 |
|
485 |
hide_firewall: function() { |
486 |
this.content.slideUp(100, function(){$(window).trigger("resize")}); |
487 |
this.toggler.removeClass("open"); |
488 |
}, |
489 |
|
490 |
value: function() { |
491 |
return this._get_selected().val(); |
492 |
}, |
493 |
|
494 |
update_layout: function() { |
495 |
if (this.value() == this.vm.firewall_profile(this.network.id)) { |
496 |
this.apply.hide();
|
497 |
} else {
|
498 |
this.apply.show();
|
499 |
} |
500 |
|
501 |
profile = this.vm.firewall_profile(this.network.id); |
502 |
if (this.vm.has_firewall(this.network.id)) { |
503 |
this.$(".firewall-toggle .label span").text("On"); |
504 |
this.$(".firewall-toggle .label span").removeClass("firewall-off"); |
505 |
this.$(".firewall-toggle .label span").addClass("firewall-on"); |
506 |
} else {
|
507 |
this.$(".firewall-toggle .label span").text("Off"); |
508 |
this.$(".firewall-toggle .label span").removeClass("firewall-on"); |
509 |
this.$(".firewall-toggle .label span").addClass("firewall-off"); |
510 |
} |
511 |
|
512 |
this.$("span.checkbox-legends").removeClass("current"); |
513 |
this.inputs.filter("[value={0}]".format(this.vm.firewall_profile(this.network.id))).next().addClass("current"); |
514 |
|
515 |
var firewalling = this.vm.firewall_pending(this.network.id); |
516 |
var el = this.el; |
517 |
|
518 |
if (firewalling) {
|
519 |
el.find("button").addClass("in-progress").show(); |
520 |
el.find(".network-progress-indicator").show();
|
521 |
} else {
|
522 |
el.find("button").removeClass("in-progress"); |
523 |
el.find(".network-progress-indicator").hide();
|
524 |
} |
525 |
} |
526 |
}) |
527 |
|
528 |
views.NetworkModelView = views.View.extend({ |
529 |
|
530 |
firewall: false, |
531 |
|
532 |
initialize: function(network, view) { |
533 |
this.parent_view = view;
|
534 |
this.network = network;
|
535 |
this.is_public = network.is_public();
|
536 |
|
537 |
this.init_vm_handlers();
|
538 |
|
539 |
this.view_id = "networks_view_" + network.id; |
540 |
views.NetworkModelView.__super__.initialize.call(this);
|
541 |
|
542 |
this.vm_views = {};
|
543 |
|
544 |
this.el = this.create_el(); |
545 |
|
546 |
// element helpers
|
547 |
this.vms_list = this.$(".machines-list"); |
548 |
this.vms_list_toggler = this.$(".list-toggle"); |
549 |
|
550 |
this.init_handlers();
|
551 |
this.update_vms();
|
552 |
this.update_layout();
|
553 |
|
554 |
this.hide_vm_list();
|
555 |
this.vms_list.hide();
|
556 |
|
557 |
this.rename_view = undefined; |
558 |
if (!this.network.is_public()) { |
559 |
this.rename_view = new views.NetworkModelRenameView(this, network); |
560 |
} |
561 |
}, |
562 |
|
563 |
show_vm_list: function() { |
564 |
if (this.vms_empty()) { return } |
565 |
this.vms_list_toggler.addClass("open"); |
566 |
this.vms_list.slideDown(function(){ |
567 |
$(window).trigger("resize"); |
568 |
}).closest(".network").addClass("expand"); |
569 |
this.$(".empty-network-slot").show(); |
570 |
this.vms_visible = true; |
571 |
}, |
572 |
|
573 |
hide_vm_list: function() { |
574 |
this.vms_list_toggler.removeClass("open"); |
575 |
this.vms_list.slideUp(function(){ |
576 |
$(window).trigger("resize"); |
577 |
}).closest(".network").removeClass("expand"); |
578 |
this.$(".empty-network-slot").hide(); |
579 |
this.vms_visible = false; |
580 |
}, |
581 |
|
582 |
// fix left border position
|
583 |
fix_left_border: function() { |
584 |
if (!this.vms_visible) { return }; |
585 |
|
586 |
var imgheight = 2783; |
587 |
var opened_vm_height = 133 + 20; |
588 |
var closed_vm_height = 61 + 20; |
589 |
var additional_height = 25; |
590 |
|
591 |
if (!this.is_public) { |
592 |
imgheight = 2700;
|
593 |
additional_height = 65;
|
594 |
}; |
595 |
|
596 |
var contents = this.$(".network-contents"); |
597 |
var last_vm = this.$(".network-machine:last .cont-toggler.open").length; |
598 |
var last_vm_height = closed_vm_height;
|
599 |
if (last_vm > 0){ |
600 |
last_vm_height = opened_vm_height; |
601 |
} |
602 |
|
603 |
var vms_opened = this.$(".network-machine .cont-toggler.open").length; |
604 |
var vms_closed = this.$(".network-machine").length - vms_opened; |
605 |
|
606 |
var calc_height = (vms_opened * opened_vm_height) + (vms_closed * closed_vm_height) + additional_height;
|
607 |
var bgpos = imgheight - calc_height + last_vm_height - 30; |
608 |
this.$(".network-contents").css({'background-position':'33px ' + (-bgpos) + 'px'}); |
609 |
}, |
610 |
|
611 |
|
612 |
init_handlers: function() { |
613 |
var self = this; |
614 |
|
615 |
this.vms_list_toggler.click(_.bind(function(){ |
616 |
if (this.vms_list.is(":visible")) { |
617 |
this.hide_vm_list();
|
618 |
} else {
|
619 |
this.fix_left_border();
|
620 |
this.show_vm_list();
|
621 |
} |
622 |
|
623 |
this.check_empty_vms();
|
624 |
}, this));
|
625 |
|
626 |
this.$(".action-add").click(_.bind(function(e){ |
627 |
e.preventDefault(); |
628 |
this.show_connect_vms();
|
629 |
}, this))
|
630 |
|
631 |
this.$(".add-icon").click(_.bind(function(e){ |
632 |
e.preventDefault(); |
633 |
this.show_connect_vms();
|
634 |
}, this))
|
635 |
|
636 |
this.$(".net-actions .destroy a").click(_.bind(function(e){ |
637 |
e.preventDefault(); |
638 |
this.confirm_destroy();
|
639 |
}, this));
|
640 |
|
641 |
this.$(".net-actions button.no").click(function(e){ |
642 |
e.preventDefault(); |
643 |
var el = $(this); |
644 |
el.closest(".confirm_single").hide();
|
645 |
el.parent().parent().find("a.selected").removeClass("selected"); |
646 |
}); |
647 |
|
648 |
this.$(".net-actions button.yes").click(function(e){ |
649 |
e.preventDefault(); |
650 |
var el = $(this); |
651 |
el.closest(".confirm_single").hide();
|
652 |
el.parent().parent().find(".selected").removeClass("selected"); |
653 |
self.network.remove(function() {
|
654 |
self.network.set({state:"DESTROY"}); |
655 |
el.closest(".confirm_single").removeClass("in-progress"); |
656 |
}); |
657 |
el.closest(".confirm_single").addClass("in-progress"); |
658 |
}); |
659 |
|
660 |
snf.ui.main.bind("view:change", _.bind(function(v) { |
661 |
if (v == "networks" ){ return } |
662 |
this.$(".confirm_single").hide(); |
663 |
this.$("a.selected").removeClass("selected"); |
664 |
}, this));
|
665 |
|
666 |
$(window).bind("resize", _.bind(function() { |
667 |
this.fix_left_border();
|
668 |
}, this));
|
669 |
}, |
670 |
|
671 |
show_connect_vms: function() { |
672 |
this.$(".confirm_single").hide(); |
673 |
this.$("a.selected").removeClass("selected"); |
674 |
var vms = this.network.get_connectable_vms(); |
675 |
this.parent_view.connect_machines_view.show_vms(this.network, |
676 |
vms, [], |
677 |
_.bind(this.connect_vms, this)); |
678 |
}, |
679 |
|
680 |
confirm_destroy: function() { |
681 |
this.$(".destroy .confirm_single").show(); |
682 |
this.$(".destroy a").addClass("selected"); |
683 |
}, |
684 |
|
685 |
connect_vms: function(vms) { |
686 |
_.each(vms, _.bind(function(vm){
|
687 |
this.network.add_vm(vm);
|
688 |
}, this));
|
689 |
|
690 |
this.parent_view.connect_machines_view.hide();
|
691 |
}, |
692 |
|
693 |
create_el: function() { |
694 |
var el = this.$(this.tpl).clone().attr("id", "network-" + this.network.id) |
695 |
return el;
|
696 |
}, |
697 |
|
698 |
init_vm_handlers: function() { |
699 |
storage.vms.bind("add", _.bind(this.vm_added_handler, this, "add")); |
700 |
storage.vms.bind("network:connect", _.bind(this.vm_changed_handler, this, "connect")); |
701 |
storage.vms.bind("change", _.bind(this.vm_changed_handler, this, "change")); |
702 |
storage.vms.bind("reset", _.bind(this.vm_changed_handler, this, "reset")); |
703 |
storage.vms.bind("remove", _.bind(this.vm_removed_handler, this, "remove")); |
704 |
storage.vms.bind("network:disconnect", _.bind(this.vm_removed_handler, this, "disconnect")); |
705 |
}, |
706 |
|
707 |
get_vm_id: function(vm) { |
708 |
return this.vm_id_tpl.format(this.network.id, vm.id) |
709 |
}, |
710 |
|
711 |
vm: function(vm) { |
712 |
return $(this.get_vm_id(vm)) |
713 |
}, |
714 |
|
715 |
vm_added_handler: function(action, vm) { |
716 |
if (!this.network.contains_vm(vm)) { return } |
717 |
this.add_or_update_vm(vm);
|
718 |
this.update_layout();
|
719 |
this.fix_left_border();
|
720 |
}, |
721 |
|
722 |
vm_changed_handler: function(action, vms, model, changes) { |
723 |
var vms = vms || [];
|
724 |
// reset or change
|
725 |
if (action == "reset") { |
726 |
vms = vms; |
727 |
} else {
|
728 |
if (!_.isArray(vms)) {
|
729 |
vms = [vms] |
730 |
} |
731 |
} |
732 |
|
733 |
if (action == "connect") { |
734 |
vms = [model]; |
735 |
} |
736 |
|
737 |
_.each(vms, _.bind(function(vm) {
|
738 |
if (!this.network.contains_vm(vm)) { return } |
739 |
this.add_or_update_vm(vm);
|
740 |
}, this));
|
741 |
this.update_layout();
|
742 |
}, |
743 |
|
744 |
vm_removed_handler: function(action, vm, model) { |
745 |
if (action == "disconnect") { vm = model }; |
746 |
this.fix_left_border();
|
747 |
this.remove_vm(vm);
|
748 |
this.update_layout();
|
749 |
}, |
750 |
|
751 |
remove_vm: function(vm) { |
752 |
if (this.vm(vm).length) { |
753 |
this.vm(vm).remove();
|
754 |
try {
|
755 |
delete this.vm_views[vm.id] |
756 |
} catch (err) {
|
757 |
} |
758 |
} |
759 |
}, |
760 |
|
761 |
create_vm: function(vm) { |
762 |
vm_el = $(this.vm_tpl).clone().attr({id:this.get_vm_id(vm).replace("#","")}); |
763 |
this.vms_list.append(vm_el);
|
764 |
this.post_vm_add(vm);
|
765 |
|
766 |
if (!this.vm_views[vm.id]) { |
767 |
vm_view = this.vm_views[vm.id] = new views.NetworkVMView(vm, this, this.firewall, vm_el); |
768 |
} |
769 |
}, |
770 |
|
771 |
add_or_update_vm: function(vm) { |
772 |
if (!vm || !this.network.contains_vm(vm)) { return }; |
773 |
|
774 |
var vm_el = this.vm(vm); |
775 |
var vm_view = this.vm_views[vm.id]; |
776 |
|
777 |
if (vm_el.length == 0) { |
778 |
this.create_vm(vm);
|
779 |
} |
780 |
|
781 |
if (vm_view) { vm_view.update_layout() };
|
782 |
|
783 |
this.update_vm(vm);
|
784 |
this.post_vm_update(vm);
|
785 |
}, |
786 |
update_vm: function(vm){}, |
787 |
post_vm_add: function(vm){}, |
788 |
post_vm_update: function(vm){}, |
789 |
|
790 |
update_vms: function(vms) { |
791 |
if (!vms) { vms = this.network.vms.list() }; |
792 |
_.each(vms, _.bind(function(vm){
|
793 |
this.add_or_update_vm(vm);
|
794 |
}, this));
|
795 |
}, |
796 |
|
797 |
check_empty_vms: function() { |
798 |
if (this.network.vms.get().length == 0) { |
799 |
this.hide_vm_list();
|
800 |
} |
801 |
}, |
802 |
|
803 |
vms_empty: function() { |
804 |
return this.network.vms.get().length == 0; |
805 |
}, |
806 |
|
807 |
remove: function() { |
808 |
$(this.el).remove(); |
809 |
}, |
810 |
|
811 |
update_layout: function() { |
812 |
// has vms ???
|
813 |
this.check_empty_vms(this.network.vms.list()); |
814 |
|
815 |
// is expanded ???
|
816 |
//
|
817 |
// whats the network status ???
|
818 |
//
|
819 |
this.$(".machines-count").text(this.network.vms.get().length); |
820 |
|
821 |
var net_name = this.network.get("name"); |
822 |
if (net_name == "public") { net_name = "Internet" } |
823 |
this.$(".name-div span.name").text(net_name); |
824 |
|
825 |
if (this.rename_view) { |
826 |
this.rename_view.update_layout();
|
827 |
} |
828 |
|
829 |
this.$(".net-status").text(this.network.state_message()); |
830 |
|
831 |
if (this.network.in_progress()) { |
832 |
this.$(".spinner").show(); |
833 |
this.$(".network-indicator").addClass("in-progress"); |
834 |
} else {
|
835 |
this.$(".spinner").hide(); |
836 |
this.$(".network-indicator").removeClass("in-progress"); |
837 |
} |
838 |
|
839 |
if (this.network.get("state") == "DESTROY") { |
840 |
this.$(".spinner").show(); |
841 |
this.$(".state").addClass("destroying-state"); |
842 |
} |
843 |
} |
844 |
}) |
845 |
|
846 |
views.PublicNetworkView = views.NetworkModelView.extend({ |
847 |
firewall: true, |
848 |
tpl: "#public-template", |
849 |
vm_tpl: "#public-machine-template", |
850 |
vm_id_tpl: "#network-{0}-vm-{1}", |
851 |
|
852 |
update_vm: function(vm) { |
853 |
} |
854 |
}) |
855 |
|
856 |
views.PrivateNetworkView = views.NetworkModelView.extend({ |
857 |
tpl: "#private-template", |
858 |
vm_tpl: "#private-machine-template", |
859 |
vm_id_tpl: "#network-{0}-vm-{1}" |
860 |
}) |
861 |
|
862 |
views.NetworksView = views.View.extend({ |
863 |
|
864 |
view_id: "networks", |
865 |
pane: "#networks-pane", |
866 |
el: "#networks-pane", |
867 |
|
868 |
initialize: function() { |
869 |
// elements shortcuts
|
870 |
this.create_cont = this.$("#networks-createcontainer"); |
871 |
this.container = this.$("#networks-container"); |
872 |
this.public_list = this.$(".public-networks"); |
873 |
this.private_list = this.$(".private-networks"); |
874 |
|
875 |
views.NetworksView.__super__.initialize.call(this);
|
876 |
this.init_handlers();
|
877 |
this.network_views = {};
|
878 |
this.update_networks(storage.networks.models);
|
879 |
|
880 |
this.create_view = new views.NetworkCreateView(); |
881 |
this.connect_machines_view = new views.NetworkConnectVMsOverlay(); |
882 |
|
883 |
}, |
884 |
|
885 |
exists: function(net) { |
886 |
return this.network_views[net.id]; |
887 |
}, |
888 |
|
889 |
add_or_update: function(net) { |
890 |
var nv = this.exists(net); |
891 |
if (!nv) {
|
892 |
nv = this.create_network_view(net)
|
893 |
this.network_views[net.id] = nv;
|
894 |
|
895 |
if (net.is_public()) {
|
896 |
this.public_list.append(nv.el);
|
897 |
this.public_list.show();
|
898 |
} else {
|
899 |
this.private_list.append(nv.el);
|
900 |
this.private_list.show();
|
901 |
} |
902 |
} |
903 |
|
904 |
// update vms
|
905 |
// for cases where network servers list
|
906 |
// get updated after vm addition and
|
907 |
// vm_added_handler fails to append the
|
908 |
// vm to the list
|
909 |
nv.update_vms(); |
910 |
nv.update_layout(); |
911 |
}, |
912 |
|
913 |
create_network_view: function(net) { |
914 |
if (net.is_public()) {
|
915 |
return new views.PublicNetworkView(net, this); |
916 |
} |
917 |
return new views.PrivateNetworkView(net, this); |
918 |
}, |
919 |
|
920 |
init_handlers: function() { |
921 |
storage.networks.bind("add", _.bind(this.network_added_handler, this, "add")); |
922 |
storage.networks.bind("change", _.bind(this.network_changed_handler, this, "change")); |
923 |
storage.networks.bind("reset", _.bind(this.network_changed_handler, this, "reset")); |
924 |
storage.networks.bind("remove", _.bind(this.network_removed_handler, this, "remove")); |
925 |
|
926 |
this.$("#networkscreate").click(_.bind(function(e){ |
927 |
e.preventDefault(); |
928 |
this.create_view.show();
|
929 |
}, this));
|
930 |
|
931 |
}, |
932 |
|
933 |
update_networks: function(nets) { |
934 |
_.each(nets, _.bind(function(net){
|
935 |
if (net.get("status") == "DELETED") { return } |
936 |
view = this.add_or_update(net);
|
937 |
}, this));
|
938 |
}, |
939 |
|
940 |
show: function() { |
941 |
this.container.show();
|
942 |
$(this.el).show(); |
943 |
}, |
944 |
|
945 |
network_added_handler: function(type, net) { |
946 |
this.update_networks([net]);
|
947 |
}, |
948 |
|
949 |
network_changed_handler: function(type, models) { |
950 |
var nets = [];
|
951 |
if (type == "change") { |
952 |
nets = [models] |
953 |
} else {
|
954 |
nets = models.models; |
955 |
} |
956 |
|
957 |
this.update_networks(nets)
|
958 |
}, |
959 |
|
960 |
network_removed_handler: function(type, net) { |
961 |
this.remove_net(net)
|
962 |
if (this.private_list.find(".network").length == 0) { |
963 |
this.private_list.hide();
|
964 |
} |
965 |
}, |
966 |
|
967 |
network_added: function(net) { |
968 |
return this.network_views[net.id]; |
969 |
}, |
970 |
|
971 |
get_network_view: function(net) { |
972 |
return this.network_views[net.id]; |
973 |
}, |
974 |
|
975 |
remove_net: function(net) { |
976 |
if (this.network_added(net)) { |
977 |
var view = this.get_network_view(net); |
978 |
view.remove(); |
979 |
delete this.network_views[net.id]; |
980 |
} |
981 |
}, |
982 |
|
983 |
__update_layout: function() { |
984 |
} |
985 |
}); |
986 |
|
987 |
})(this);
|