root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_networks_view.js @ d233867a
History | View | Annotate | Download (22.7 kB)
1 |
// Copyright 2013 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 || {};
|
46 |
var views = snf.views = snf.views || {}
|
47 |
|
48 |
// shortcuts
|
49 |
var bb = root.Backbone;
|
50 |
|
51 |
// logging
|
52 |
var logger = new snf.logging.logger("SNF-VIEWS"); |
53 |
var debug = _.bind(logger.debug, logger);
|
54 |
|
55 |
views.NetworkCreateView = views.Overlay.extend({ |
56 |
view_id: "network_create_view", |
57 |
content_selector: "#networks-create-content", |
58 |
css_class: 'overlay-networks-create overlay-info', |
59 |
overlay_id: "network-create-overlay", |
60 |
|
61 |
title: "Create new private network", |
62 |
subtitle: "Networks", |
63 |
|
64 |
initialize: function(options) { |
65 |
views.NetworkCreateView.__super__.initialize.apply(this);
|
66 |
|
67 |
this.create_button = this.$("form .form-action.create"); |
68 |
this.text = this.$(".network-create-name"); |
69 |
this.form = this.$("form"); |
70 |
|
71 |
this.dhcp_select = this.$("#network-create-dhcp"); |
72 |
this.type_select = this.$("#network-create-type"); |
73 |
this.subnet_select = this.$("#network-create-subnet"); |
74 |
this.subnet_custom = this.$("#network-create-subnet-custom"); |
75 |
|
76 |
this.dhcp_form = this.$("#network-create-dhcp-fields"); |
77 |
|
78 |
this.subnet_select.find(".subnet").remove(); |
79 |
_.each(synnefo.config.network_suggested_subnets, function(subnet){
|
80 |
this.subnet_select.append($('<option value='+subnet+' class="subnet">'+subnet+'</option>')); |
81 |
}, this);
|
82 |
|
83 |
this.type_select.find(".subnet").remove(); |
84 |
_.each(synnefo.config.network_available_types, function(name, value){
|
85 |
this.type_select.append($('<option value='+value+' class="subnet">'+name+'</option>')); |
86 |
}, this);
|
87 |
|
88 |
if (_.keys(synnefo.config.network_available_types).length <= 1) { |
89 |
this.type_select.closest(".form-field").hide(); |
90 |
} |
91 |
|
92 |
this.check_dhcp_form();
|
93 |
this.init_handlers();
|
94 |
}, |
95 |
|
96 |
reset_dhcp_form: function() { |
97 |
this.subnet_select.find("option")[0].selected = 1; |
98 |
this.subnet_custom.val(""); |
99 |
}, |
100 |
|
101 |
check_dhcp_form: function() { |
102 |
if (this.dhcp_select.is(":checked")) { |
103 |
this.dhcp_form.show();
|
104 |
} else {
|
105 |
this.dhcp_form.hide();
|
106 |
} |
107 |
|
108 |
if (this.subnet_select.val() == "custom") { |
109 |
this.subnet_custom.show();
|
110 |
} else {
|
111 |
this.subnet_custom.hide();
|
112 |
} |
113 |
}, |
114 |
|
115 |
init_handlers: function() { |
116 |
|
117 |
this.dhcp_select.click(_.bind(function(e){ |
118 |
this.check_dhcp_form();
|
119 |
this.reset_dhcp_form();
|
120 |
}, this));
|
121 |
|
122 |
this.subnet_select.change(_.bind(function(e){ |
123 |
this.check_dhcp_form();
|
124 |
if (this.subnet_custom.is(":visible")) { |
125 |
this.subnet_custom.focus();
|
126 |
} |
127 |
}, this));
|
128 |
|
129 |
this.create_button.click(_.bind(function(e){ |
130 |
this.submit();
|
131 |
}, this));
|
132 |
|
133 |
this.form.submit(_.bind(function(e){ |
134 |
e.preventDefault(); |
135 |
this.submit;
|
136 |
return false; |
137 |
}, this))
|
138 |
|
139 |
this.text.keypress(_.bind(function(e){ |
140 |
if (e.which == 13) {this.submit()}; |
141 |
},this))
|
142 |
}, |
143 |
|
144 |
submit: function() { |
145 |
if (this.validate()) { |
146 |
this.create();
|
147 |
}; |
148 |
}, |
149 |
|
150 |
validate: function() { |
151 |
// sanitazie
|
152 |
var t = this.text.val(); |
153 |
t = t.replace(/^\s+|\s+$/g,""); |
154 |
this.text.val(t);
|
155 |
|
156 |
if (this.text.val() == "") { |
157 |
this.text.closest(".form-field").addClass("error"); |
158 |
this.text.focus();
|
159 |
return false; |
160 |
} else {
|
161 |
this.text.closest(".form-field").removeClass("error"); |
162 |
} |
163 |
|
164 |
if (this.dhcp_select.is(":checked")) { |
165 |
if (this.subnet_select.val() == "custom") { |
166 |
var sub = this.subnet_custom.val(); |
167 |
sub = sub.replace(/^\s+|\s+$/g,""); |
168 |
this.subnet_custom.val(sub);
|
169 |
|
170 |
if (!synnefo.util.IP_REGEX.exec(this.subnet_custom.val())) { |
171 |
this.subnet_custom.closest(".form-field").prev().addClass("error"); |
172 |
return false; |
173 |
} else {
|
174 |
this.subnet_custom.closest(".form-field").prev().removeClass("error"); |
175 |
} |
176 |
}; |
177 |
} |
178 |
|
179 |
return true; |
180 |
}, |
181 |
|
182 |
get_next_available_subnet: function() { |
183 |
var auto_tpl = synnefo.config.automatic_network_range_format;
|
184 |
if (!auto_tpl) {
|
185 |
return null |
186 |
} |
187 |
var index = 0; |
188 |
var subnet = auto_tpl.format(index);
|
189 |
var networks = synnefo.storage.networks;
|
190 |
var check_existing = function(n) { return n.get('cidr') == subnet } |
191 |
while (networks.filter(check_existing).length > 0 && index <= 255) { |
192 |
index++; |
193 |
subnet = auto_tpl.format(index); |
194 |
} |
195 |
return subnet;
|
196 |
}, |
197 |
|
198 |
create: function() { |
199 |
this.create_button.addClass("in-progress"); |
200 |
|
201 |
var name = this.text.val(); |
202 |
var dhcp = this.dhcp_select.is(":checked"); |
203 |
var subnet = null; |
204 |
var type = this.type_select.val(); |
205 |
|
206 |
if (dhcp) {
|
207 |
if (this.subnet_select.val() == "custom") { |
208 |
subnet = this.subnet_custom.val();
|
209 |
} else if (this.subnet_select.val() == "auto") { |
210 |
subnet = this.get_next_available_subnet()
|
211 |
} else {
|
212 |
subnet = this.subnet_select.val();
|
213 |
} |
214 |
|
215 |
} |
216 |
|
217 |
snf.storage.networks.create(name, type, subnet, dhcp, _.bind(function(){
|
218 |
this.hide();
|
219 |
}, this));
|
220 |
}, |
221 |
|
222 |
beforeOpen: function() { |
223 |
this.create_button.removeClass("in-progress") |
224 |
this.text.closest(".form-field").removeClass("error"); |
225 |
this.text.val(""); |
226 |
this.text.show();
|
227 |
this.text.focus();
|
228 |
this.subnet_custom.val(""); |
229 |
this.subnet_select.val("auto"); |
230 |
this.dhcp_select.attr("checked", true); |
231 |
this.type_select.val(_.keys(synnefo.config.network_available_types)[0]); |
232 |
this.check_dhcp_form();
|
233 |
}, |
234 |
|
235 |
onOpen: function() { |
236 |
this.text.focus();
|
237 |
} |
238 |
}); |
239 |
|
240 |
views.NetworkPortView = views.ext.ModelView.extend({ |
241 |
tpl: '#network-port-view-tpl', |
242 |
|
243 |
vm_logo_url: function(vm) { |
244 |
if (!this.model.get('vm')) { return '' } |
245 |
return synnefo.ui.helpers.vm_icon_path(this.model.get('vm'), 'medium'); |
246 |
}, |
247 |
|
248 |
set_confirm: function() { |
249 |
var parent = this.parent_view.parent_view.el; |
250 |
parent.addClass("subactionpending");
|
251 |
}, |
252 |
|
253 |
unset_confirm: function() { |
254 |
var parent = this.parent_view.parent_view.el; |
255 |
parent.removeClass("subactionpending");
|
256 |
}, |
257 |
|
258 |
post_init_element: function() { |
259 |
this.in_progress = false; |
260 |
this.firewall = this.$(".firewall-content").hide(); |
261 |
this.firewall_toggler = this.$(".firewall-toggle"); |
262 |
this.firewall_apply = this.$(".firewall-apply"); |
263 |
this.firewall_legends = this.firewall.find(".checkbox-legends"); |
264 |
this.firewall_inputs = this.firewall.find("input"); |
265 |
this.firewall_apply = this.firewall.find("button"); |
266 |
this.firewall_visible = false; |
267 |
|
268 |
this.firewall_toggler.click(_.bind(function() { |
269 |
this.toggle_firewall();
|
270 |
}, this));
|
271 |
|
272 |
this.firewall.find(".checkbox-legends, input").click( |
273 |
_.bind(this.handle_firewall_choice_click, this)); |
274 |
this.update_firewall();
|
275 |
}, |
276 |
|
277 |
toggle_firewall: function(e, hide, cb) { |
278 |
hide = hide === undefined ? false : hide; |
279 |
if (hide) {
|
280 |
this.firewall.stop().hide();
|
281 |
} else {
|
282 |
if (!cb) { cb = function() {}} |
283 |
this.firewall.slideToggle(cb);
|
284 |
} |
285 |
this.firewall_toggler.toggleClass("open"); |
286 |
this.firewall_visible = this.firewall_toggler.hasClass("open"); |
287 |
if (!this.firewall_visible) { |
288 |
this.firewall_apply.fadeOut(50); |
289 |
} |
290 |
this.update_firewall();
|
291 |
}, |
292 |
|
293 |
post_hide: function() { |
294 |
views.NetworkPortView.__super__.post_hide.apply(this);
|
295 |
if (this.firewall_visible) { |
296 |
this.toggle_firewall({}, true); |
297 |
} |
298 |
}, |
299 |
|
300 |
handle_firewall_choice_click: function(e) { |
301 |
var el = $(e.currentTarget); |
302 |
if (el.get(0).tagName == "INPUT") { |
303 |
el = el.next(); |
304 |
} |
305 |
var current = this.model.get("firewall_status"); |
306 |
var selected = el.prev().val();
|
307 |
|
308 |
el.parent().find("input").attr("checked", false); |
309 |
el.prev().attr("checked", true); |
310 |
|
311 |
if (selected != current) {
|
312 |
this.firewall_apply.show();
|
313 |
} else {
|
314 |
this.firewall_apply.hide();
|
315 |
} |
316 |
}, |
317 |
|
318 |
disconnect_port: function(model, e) { |
319 |
e && e.stopPropagation(); |
320 |
this.model.actions.reset_pending();
|
321 |
this.model.disconnect(_.bind(this.disconnect_port_complete, this)); |
322 |
}, |
323 |
|
324 |
disconnect_port_complete: function() { |
325 |
}, |
326 |
|
327 |
set_firewall: function() { |
328 |
var value = this.get_selected_value(); |
329 |
this.firewall_apply.addClass("in-progress"); |
330 |
this.model.set({'pending_firewall': value}); |
331 |
this.model.set_firewall(value, this.set_firewall_complete, |
332 |
this.set_firewall_complete);
|
333 |
this.in_progress = true; |
334 |
}, |
335 |
|
336 |
set_firewall_complete: function() { |
337 |
this.in_progress = false; |
338 |
this.toggle_firewall({}, false, _.bind(function() { |
339 |
this.firewall_apply.removeClass("in-progress").show(); |
340 |
}, this));
|
341 |
}, |
342 |
|
343 |
get_selected_value: function() { |
344 |
return this.firewall_inputs.filter(":checked").val(); |
345 |
}, |
346 |
|
347 |
update_firewall: function() { |
348 |
var value = this.model.get("firewall_status"); |
349 |
var value_selector = "[value=" + value + "]" |
350 |
var status_span = this.firewall_toggler.find("span span"); |
351 |
var current_choice = this.firewall_inputs.filter(value_selector); |
352 |
|
353 |
if (_.contains(["PROTECTED", "ENABLED"], value)) { |
354 |
status_span.removeClass("firewall-off").addClass("firewall-on"); |
355 |
status_span.text("On");
|
356 |
} else {
|
357 |
status_span.removeClass("firewall-on").addClass("firewall-off"); |
358 |
status_span.text("Off");
|
359 |
} |
360 |
|
361 |
this.firewall_inputs.attr("checked", false); |
362 |
this.firewall_legends.removeClass("current"); |
363 |
current_choice.attr("checked", true) |
364 |
current_choice.next().addClass("current");
|
365 |
}, |
366 |
|
367 |
show_vm_details: function() { |
368 |
var vm = this.model.get('vm'); |
369 |
if (vm) { snf.ui.main.show_vm_details(vm) }
|
370 |
} |
371 |
}); |
372 |
|
373 |
views.NetworkPortCollectionView = views.ext.CollectionView.extend({ |
374 |
tpl: '#network-port-collection-view-tpl', |
375 |
model_view_cls: views.NetworkPortView,
|
376 |
rivets_view: true, |
377 |
get_rivet_object: function() { |
378 |
return {
|
379 |
model: this.collection.network |
380 |
} |
381 |
}, |
382 |
resolve_storage_object: function() { |
383 |
return this.collection |
384 |
}, |
385 |
|
386 |
show_connect_vms_overlay: function() { |
387 |
this.parent_view.show_connect_vms_overlay();
|
388 |
} |
389 |
}); |
390 |
|
391 |
views.NetworkView = views.ext.ModelView.extend({ |
392 |
tpl: '#network-view-tpl', |
393 |
auto_bind: ['connect_vm'], |
394 |
post_init_element: function() { |
395 |
this.ports = this.$(".ports.nested-model-list"); |
396 |
this.ports.hide();
|
397 |
this.ports_toggler = this.$(".network-ports-toggler"); |
398 |
this.ports_toggler.click(this.toggle_ports); |
399 |
this.ports_visible = false; |
400 |
}, |
401 |
|
402 |
toggle_ports: function(e, hide) { |
403 |
hide = hide === undefined ? false : hide; |
404 |
if (hide) {
|
405 |
this.ports.stop().hide();
|
406 |
} else {
|
407 |
this.ports.stop().slideToggle();
|
408 |
} |
409 |
this.ports_toggler.find(".cont-toggler").toggleClass("open"); |
410 |
this.ports_visible = this.ports_toggler.find(".cont-toggler").hasClass("open"); |
411 |
}, |
412 |
|
413 |
post_hide: function() { |
414 |
views.NetworkView.__super__.post_hide.apply(this);
|
415 |
if (this.ports_visible) { |
416 |
this.toggle_ports({}, true); |
417 |
} |
418 |
}, |
419 |
|
420 |
status_map: {
|
421 |
'ACTIVE': 'Active', |
422 |
'CONNECTING': 'Connecting', |
423 |
'DISCONNECTING': 'Disconnecting', |
424 |
'REMOVING': 'Destroying' |
425 |
}, |
426 |
|
427 |
status_cls_map: {
|
428 |
'ACTIVE': 'status-active', |
429 |
'DISCONNECTING': 'status-progress', |
430 |
'CONNECTING': 'status-progress', |
431 |
'REMOVING': 'status-progress' |
432 |
}, |
433 |
|
434 |
status_cls: function(status) { |
435 |
return this.status_cls_map[this.model.get('ext_status')] |
436 |
}, |
437 |
|
438 |
status_display: function(status) { |
439 |
var status;
|
440 |
if (this.model.id == "snf-combined-public-network") { |
441 |
return "Internet" |
442 |
} |
443 |
|
444 |
var cidr = this.model.get('cidr'); |
445 |
var status = this.model.get('ext_status'); |
446 |
if (status != 'REMOVING' && cidr) { |
447 |
return cidr
|
448 |
} |
449 |
return this.status_map[status]; |
450 |
}, |
451 |
|
452 |
connect_vms: function(vms, cb) { |
453 |
var finished = 0; |
454 |
var completed = function() { |
455 |
finished++; |
456 |
if (finished == vms.length) {
|
457 |
cb(); |
458 |
} |
459 |
} |
460 |
_.each(vms, function(vm) {
|
461 |
this.model.connect_vm(vm, completed);
|
462 |
}, this);
|
463 |
}, |
464 |
|
465 |
remove: function(model, e) { |
466 |
e && e.stopPropagation(); |
467 |
this.model.actions.reset_pending();
|
468 |
this.model.destroy({
|
469 |
success: _.bind(function() { |
470 |
this.model.set({status: 'REMOVING'}); |
471 |
this.model.set({ext_status: 'REMOVING'}); |
472 |
// force status display update
|
473 |
this.model.set({cidr: 'REMOVING'}); |
474 |
}, this),
|
475 |
silent: true |
476 |
}); |
477 |
}, |
478 |
|
479 |
show_connect_vms_overlay: function() { |
480 |
var view = new views.NetworkConnectVMsOverlay(); |
481 |
vms = this.model.pluggable_vms();
|
482 |
var cb = _.bind(function(vms) { |
483 |
view.set_in_progress(); |
484 |
var cbinner = function() { |
485 |
view.hide(); |
486 |
delete view;
|
487 |
} |
488 |
this.connect_vms(vms, cbinner);
|
489 |
}, this);
|
490 |
view.show_vms(this.model, vms, [], cb, "subtitle"); |
491 |
} |
492 |
|
493 |
}); |
494 |
|
495 |
views.NetworksCollectionView = views.ext.CollectionView.extend({ |
496 |
collection: storage.networks,
|
497 |
collection_name: 'networks', |
498 |
model_view_cls: views.NetworkView,
|
499 |
create_view_cls: views.NetworkCreateView,
|
500 |
|
501 |
init: function() { |
502 |
this.public_added = false; |
503 |
views.NetworksCollectionView.__super__.init.apply(this, arguments); |
504 |
}, |
505 |
|
506 |
check_empty: function() { |
507 |
views.NetworksCollectionView.__super__.check_empty.apply(this, arguments); |
508 |
//if (this.$(".private").children().length == 0) {
|
509 |
//this.$(".private").hide();
|
510 |
//} else {
|
511 |
//this.$(".private").show();
|
512 |
//}
|
513 |
}, |
514 |
|
515 |
add_model: function(m) { |
516 |
if (m.get('is_public') && !this.public_added) { |
517 |
this.combined_public = new models.networks.CombinedPublicNetwork(); |
518 |
this.combined_public_view = new views.NetworkView({ |
519 |
model: this.combined_public |
520 |
}); |
521 |
this.add_model_view(this.combined_public_view, this.combined_public, 0); |
522 |
this.combined_public_view.$("i").hide(); |
523 |
this.public_added = true; |
524 |
} |
525 |
return views.NetworksCollectionView.__super__.add_model.call(this, m); |
526 |
}, |
527 |
|
528 |
remove_model: function(m) { |
529 |
if (m.id == 'snf-combined-public-network') { |
530 |
return;
|
531 |
} else {
|
532 |
return views.NetworksCollectionView.__super__.remove_model.call(this, m); |
533 |
} |
534 |
}, |
535 |
|
536 |
get_model_view_cls: function(m) { |
537 |
if (!this.public_added) { |
538 |
} |
539 |
if (m.get('is_public')) { |
540 |
return false; |
541 |
} |
542 |
return views.NetworksCollectionView.__super__.get_model_view_cls.apply(this, [m]); |
543 |
}, |
544 |
|
545 |
parent_for_model: function(m) { |
546 |
if (m.get('is_public')) { |
547 |
return this.list_el.find(".public"); |
548 |
} else {
|
549 |
return this.list_el.find(".private"); |
550 |
} |
551 |
} |
552 |
}); |
553 |
|
554 |
views.NetworksPaneView = views.ext.PaneView.extend({ |
555 |
id: "pane", |
556 |
el: '#networks-pane', |
557 |
collection_view_cls: views.NetworksCollectionView,
|
558 |
collection_view_selector: '#networks-list-view' |
559 |
}); |
560 |
|
561 |
views.NetworkConnectVMsOverlay = views.Overlay.extend({ |
562 |
title: "Connect machine", |
563 |
overlay_id: "overlay-select-vms", |
564 |
content_selector: "#network-vms-select-content", |
565 |
css_class: "overlay-info", |
566 |
allow_multiple: true, |
567 |
|
568 |
initialize: function() { |
569 |
views.NetworkConnectVMsOverlay.__super__.initialize.apply(this);
|
570 |
this.list = this.$(".vms-list ul"); |
571 |
this.empty_message = this.$(".empty-message"); |
572 |
// flag for submit handler to avoid duplicate bindings
|
573 |
this.submit_handler_set = false; |
574 |
this.in_progress = false; |
575 |
}, |
576 |
|
577 |
handle_vm_click: function(el) { |
578 |
if (!this.allow_multiple) { |
579 |
$(el).closest("ul").find(".selected").removeClass("selected"); |
580 |
$(el).addClass("selected"); |
581 |
} else {
|
582 |
$(el).toggleClass("selected"); |
583 |
} |
584 |
}, |
585 |
|
586 |
init_handlers: function() { |
587 |
var self = this; |
588 |
this.list.find("li").click(function() { |
589 |
self.handle_vm_click($(this)); |
590 |
}); |
591 |
|
592 |
if (!this.submit_handler_set) { |
593 |
// avoid duplicate submits
|
594 |
this.el.find(".create, .assign").click(_.bind(function() { |
595 |
if (!this.in_progress) { |
596 |
this.submit();
|
597 |
} |
598 |
}, this));
|
599 |
this.submit_handler_set = true; |
600 |
} |
601 |
}, |
602 |
|
603 |
reset: function() { |
604 |
this.list.find("li").remove(); |
605 |
}, |
606 |
|
607 |
beforeOpen: function() { |
608 |
this.reset();
|
609 |
this.update_layout();
|
610 |
}, |
611 |
|
612 |
vm: function(vm) { |
613 |
if (vm.id) { var id = vm.id } else {var id = vm} |
614 |
return this.list.find(".vm-" + id); |
615 |
}, |
616 |
|
617 |
get_selected: function() { |
618 |
return this.list.find(".selected").map(function() {return $(this).data('vm')}) |
619 |
}, |
620 |
|
621 |
update_layout: function() { |
622 |
this.unset_in_progress();
|
623 |
this.in_progress = false; |
624 |
|
625 |
if (this.vms.length == 0) { |
626 |
this.empty_message.show();
|
627 |
} else {
|
628 |
this.empty_message.hide();
|
629 |
} |
630 |
|
631 |
_.each(this.vms, _.bind(function(vm){ |
632 |
var html = '<li class="vm option options-object vm-{0}">' + |
633 |
'<div class="options-object-cont">' +
|
634 |
'{2}' +
|
635 |
'<span class="title">{1}</span>' +
|
636 |
'<span class="value">{3}</span></div>' +
|
637 |
'</li>';
|
638 |
var el = $(html.format(vm.id, |
639 |
util.truncate(_.escape(vm.get("name")), 23), |
640 |
snf.ui.helpers.vm_icon_tag(vm, "small", {'class':'os'}), |
641 |
_.escape(vm.get_os()) |
642 |
)); |
643 |
el.data({vm:vm, vm_id:vm.id}); |
644 |
this.list.append(el);
|
645 |
|
646 |
vm.bind("remove", function(){ el.remove()}) |
647 |
vm.bind("change:name", function(i,v){el.find(".title").text(v)}) |
648 |
}, this));
|
649 |
|
650 |
this.init_handlers();
|
651 |
this.set_selected();
|
652 |
}, |
653 |
|
654 |
set_selected: function() { |
655 |
_.each(this.selected, _.bind(function(el){ |
656 |
this.vm(el).addClass("selected"); |
657 |
}, this));
|
658 |
}, |
659 |
|
660 |
set_in_progress: function() { |
661 |
this.$(".form-action").addClass("in-progress"); |
662 |
this.in_progress = true; |
663 |
}, |
664 |
|
665 |
unset_in_progress: function() { |
666 |
this.$(".form-action").removeClass("in-progress"); |
667 |
this.in_progress = false; |
668 |
}, |
669 |
|
670 |
show_vms: function(network, vms, selected, callback, subtitle) { |
671 |
this.network = network;
|
672 |
this.reset();
|
673 |
if (network) {
|
674 |
this.set_subtitle(network.escape("name")); |
675 |
} else {
|
676 |
this.set_subtitle(subtitle);
|
677 |
} |
678 |
|
679 |
this.vms = vms;
|
680 |
this.selected = selected;
|
681 |
this.cb = callback;
|
682 |
this.unset_in_progress();
|
683 |
this.show(true); |
684 |
}, |
685 |
|
686 |
submit: function() { |
687 |
if (!this.get_selected().length) { return } |
688 |
this.cb(this.get_selected()); |
689 |
} |
690 |
}); |
691 |
|
692 |
})(this);
|