Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_networks_view.js @ 3635e7ac

History | View | Annotate | Download (44.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 || {};
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
    
56
    views.NetworkConnectVMsOverlay = views.Overlay.extend({
57
        title: "Connect machine",
58
        overlay_id: "overlay-select-vms",
59
        content_selector: "#network-vms-select-content",
60
        css_class: "overlay-info",
61

    
62
        initialize: function() {
63
            views.NetworkConnectVMsOverlay.__super__.initialize.apply(this);
64
            this.list = this.$(".vms-list ul");
65
            this.empty_message = this.$(".empty-message");
66

    
67
            // flag for submit handler to avoid duplicate bindings
68
            this.submit_handler_set = false;
69
        },
70
        
71
        init_handlers: function() {
72
            var self = this;
73
            this.list.find("li").click(function(){
74
                $(this).toggleClass("selected");
75
            });
76
            
77
            if (!this.submit_handler_set) {
78
                // avoid duplicate submits
79
                this.el.find(".create").click(_.bind(function() {
80
                    this.submit();
81
                }, this));
82
                this.submit_handler_set = true;
83
            }
84
        },
85

    
86
        reset: function() {
87
            this.list.find("li").remove();
88
        },
89

    
90
        beforeOpen: function() {
91
            this.reset();
92
            this.update_layout();
93
        },
94
        
95
        vm: function(vm) {
96
            if (vm.id) { var id = vm.id } else {var id = vm}
97
            return this.list.find(".vm-" + id);
98
        },
99

    
100
        get_selected: function() {
101
            return this.list.find(".selected").map(function() {return $(this).data('vm')})
102
        },
103

    
104
        update_layout: function() {
105
            if (this.vms.length == 0) {
106
                this.empty_message.show();
107
            } else {
108
                this.empty_message.hide();
109
            }
110

    
111
            _.each(this.vms, _.bind(function(vm){
112
                
113
                var html = '<li class="vm option options-object vm-{0}">' +
114
                           '<div class="options-object-cont">' +
115
                           '{2}' + 
116
                           '<span class="title">{1}</span>' + 
117
                           '<span class="value">{3}</span></div>' + 
118
                           '</li>';
119
                var el = $(html.format(vm.id, 
120
                                       util.truncate(_.escape(vm.get("name")), 23), 
121
                                       snf.ui.helpers.vm_icon_tag(vm, "small", {'class':'os'}),
122
                                       _.escape(vm.get_os())
123
                                      ))
124
                el.data({vm:vm, vm_id:vm.id})
125
                this.list.append(el);
126

    
127
                vm.bind("remove", function(){ el.remove()})
128
                vm.bind("change:name", function(i,v){el.find(".title").text(v)})
129
            }, this));
130
            
131
            this.init_handlers();
132
            this.set_selected();
133
        },
134

    
135
        set_selected: function() {
136
            _.each(this.selected, _.bind(function(el){
137
                this.vm(el).addClass("selected");
138
            }, this));
139
        },
140

    
141
        show_vms: function(network, vms, selected, callback) {
142
            this.network = network;
143
            this.reset();
144
            this.set_subtitle(network.escape("name"));
145
            this.vms = vms;
146
            if (!synnefo.config.network_allow_duplicate_vm_nics) {
147
                this.vms = _.filter(this.vms, function(vm) {
148
                    return !vm.connected_to(this.network);
149
                }, this);
150
            }
151

    
152
            this.selected = selected;
153
            this.cb = callback;
154
            this.show();
155
        },
156

    
157
        submit: function() {
158
            this.cb(this.get_selected());
159
        }
160
    })
161

    
162
    views.NetworkActionsView = views.View.extend({
163
        
164
        initialize: function(view, net, el, opts) {
165
            this.parent = view
166
            this.network = net;
167
            this.el = el;
168
            
169
            this.actions = this.$(".actions");
170
            this.selected = undefined;
171

    
172
            this.destroy = this.$(".actions .destroy a");
173
            this.connect = this.$(".actions .add");
174

    
175
            this.init_handlers();
176
            this.update_layout();
177
        },
178

    
179
        init_handlers: function() {
180
            this.connect.click(_.bind(function(e){
181
                e.preventDefault();
182
            }))
183
        },
184

    
185
        update_layout: function() {
186
        }
187
    });
188

    
189
    views.NetworkCreateView = views.Overlay.extend({
190
        view_id: "network_create_view",
191
        content_selector: "#networks-create-content",
192
        css_class: 'overlay-networks-create overlay-info',
193
        overlay_id: "network-create-overlay",
194

    
195
        title: "Create new private network",
196
        subtitle: "Networks",
197

    
198
        initialize: function(options) {
199
            views.NetworkCreateView.__super__.initialize.apply(this);
200

    
201
            this.create_button = this.$("form .form-action.create");
202
            this.text = this.$(".network-create-name");
203
            this.form = this.$("form");
204

    
205
            this.dhcp_select = this.$("#network-create-dhcp");
206
            this.type_select = this.$("#network-create-type");
207
            this.subnet_select = this.$("#network-create-subnet");
208
            this.subnet_custom = this.$("#network-create-subnet-custom");
209
                
210
            this.dhcp_form = this.$("#network-create-dhcp-fields");
211
            
212
            this.subnet_select.find(".subnet").remove();
213
            _.each(synnefo.config.network_suggested_subnets, function(subnet){
214
                this.subnet_select.append($('<option value='+subnet+' class="subnet">'+subnet+'</option>'));
215
            }, this);
216

    
217
            this.type_select.find(".subnet").remove();
218
            _.each(synnefo.config.network_available_types, function(name, value){
219
                this.type_select.append($('<option value='+value+' class="subnet">'+name+'</option>'));
220
            }, this);
221
            
222
            this.disable_network_type = false;
223
            if (_.keys(synnefo.config.network_available_types).length <= 1) {
224
                this.disable_network_type = true;
225
                this.type_select.closest(".form-field").hide();
226
            }
227

    
228
            this.check_dhcp_form();
229
            this.init_handlers();
230
        },
231

    
232
        reset_dhcp_form: function() {
233
          this.subnet_select.find("option")[0].selected = 1;
234
          this.subnet_custom.val("");
235
        },
236

    
237
        check_dhcp_form: function() {
238
            if (this.dhcp_select.is(":checked")) {
239
                this.dhcp_form.show();
240
            } else {
241
                this.dhcp_form.hide();
242
            }
243
            
244
            if (this.subnet_select.val() == "custom") {
245
                this.subnet_custom.show();
246
            } else {
247
                this.subnet_custom.hide();
248
            }
249
        },
250

    
251
        init_handlers: function() {
252

    
253
            this.dhcp_select.click(_.bind(function(e){
254
                this.check_dhcp_form();
255
                this.reset_dhcp_form();
256
            }, this));
257

    
258
            this.subnet_select.change(_.bind(function(e){
259
                this.check_dhcp_form();
260
                if (this.subnet_custom.is(":visible")) {
261
                    this.subnet_custom.focus();
262
                }
263
            }, this));
264

    
265
            this.create_button.click(_.bind(function(e){
266
                this.submit();
267
            }, this));
268

    
269
            this.form.submit(_.bind(function(e){
270
                e.preventDefault();
271
                this.submit;
272
                return false;
273
            }, this))
274

    
275
            this.text.keypress(_.bind(function(e){
276
                if (e.which == 13) {this.submit()};
277
            },this))
278
        },
279

    
280
        submit: function() {
281
            if (this.validate()) {
282
                this.create();
283
            };
284
        },
285
        
286
        validate: function() {
287
            // sanitazie
288
            var t = this.text.val();
289
            t = t.replace(/^\s+|\s+$/g,"");
290
            this.text.val(t);
291

    
292
            if (this.text.val() == "") {
293
                this.text.closest(".form-field").addClass("error");
294
                this.text.focus();
295
                return false;
296
            } else {
297
                this.text.closest(".form-field").removeClass("error");
298
            }
299
            
300
            if (this.dhcp_select.is(":checked")) {
301
                if (this.subnet_select.val() == "custom") {
302
                    var sub = this.subnet_custom.val();
303
                    sub = sub.replace(/^\s+|\s+$/g,"");
304
                    this.subnet_custom.val(sub);
305
                        
306
                    if (!synnefo.util.IP_REGEX.exec(this.subnet_custom.val())) {
307
                        this.subnet_custom.closest(".form-field").prev().addClass("error");
308
                        return false;
309
                    } else {
310
                        this.subnet_custom.closest(".form-field").prev().removeClass("error");
311
                    }
312
                };
313
            }
314

    
315
            return true;
316
        },
317

    
318
        create: function() {
319
            this.create_button.addClass("in-progress");
320

    
321
            var name = this.text.val();
322
            var dhcp = this.dhcp_select.is(":checked");
323
            var subnet = null;
324
            var type = this.type_select.val();
325

    
326
            if (this.disable_network_type) { type = null };
327

    
328
            if (dhcp) {
329
                if (this.subnet_select.val() == "custom") {
330
                    subnet = this.subnet_custom.val();
331
                } else if (this.subnet_select.val() == "auto") {
332
                    subnet = null;
333
                } else {
334
                    subnet = this.subnet_select.val();
335
                }
336
                
337
            }
338

    
339
            snf.storage.networks.create(name, type, subnet, dhcp, _.bind(function(){
340
                this.hide();
341
            }, this));
342
        },
343

    
344
        beforeOpen: function() {
345
            this.create_button.removeClass("in-progress")
346
            this.text.closest(".form-field").removeClass("error");
347
            this.text.val("");
348
            this.text.show();
349
            this.text.focus();
350
            this.subnet_custom.val("");
351
            this.subnet_select.val("auto");
352
            this.dhcp_select.attr("checked", true);
353
            this.type_select.val(_.keys(synnefo.config.network_available_types)[0]);
354
            this.check_dhcp_form();
355
        },
356

    
357
        onOpen: function() {
358
            this.text.focus();
359
        }
360
    });
361

    
362
    views.NetworkNICView = views.View.extend({
363

    
364
        initialize: function(nic, parent, firewall_controls, el) {
365
            this.firewall_controls = firewall_controls || false;
366
            this.nic = nic;
367
            this.vm = nic.get_vm();
368
            // parent view di
369
            this.parent = parent;
370
            // TODO make it better
371
            this.el = el || this.parent.get_nic_view(nic);
372

    
373
            this.init_layout();
374
            this.update_layout();
375

    
376
            this.disconnect = this.$(".action-disconnect");
377
            this.confirm_el = this.$(".confirm_single");
378
            this.cancel = this.$("button.no");
379
            this.confirm = this.$("button.yes");
380
            this.details = this.$(".action-details");
381
            this.vm_connect = this.$(".machine-connect");
382

    
383
            this.init_handlers();
384
            this.connect_overlay = new views.VMConnectView();
385
            
386
            this.firewall_view = undefined;
387
            if (this.firewall_controls) {
388
                this.firewall_view = new views.FirewallEditView(this.nic, this.parent.network, this);
389
            }
390

    
391
        },
392
        
393
        reset_all_net_actions: function(act_types) {
394
            synnefo.storage.networks.each(function(n){
395
                var actions = n.get('actions');
396
                _.each(act_types, function(type){
397
                    actions.remove_all(type);
398
                })
399
            })
400
        },
401

    
402
        init_handlers: function() {
403
            if (!this.parent.network.is_public()) {
404
                this.disconnect.click(_.bind(function(e){
405
                    e.preventDefault();
406
                    this.reset_all_net_actions(['destroy','disconnect']);
407
                    this.parent.network.get("actions").remove_all("disconnect");
408
                    this.parent.network.get("actions").add("disconnect", this.nic.id);
409
                    this.parent.network.get("actions").remove("destroy");
410
                }, this));
411
                this.cancel.click(_.bind(function(e){
412
                    this.parent.network.get("actions").remove("disconnect", this.nic.id);
413
                    e.preventDefault()
414
                }, this));
415

    
416
                this.confirm.click(_.bind(function(e){
417
                    e.preventDefault()
418
                    this.disconnect_nic();
419
                    this.confirm_el.hide();
420
                    this.disconnect.removeClass("selected");
421
                }, this));
422

    
423
                snf.ui.main.bind("view:change", _.bind(function(v) {
424
                    if (v == "networks" ){ return }
425
                    this.confirm_el.hide();
426
                    this.disconnect.removeClass("selected");
427
                }, this));
428

    
429
                this.$(".remove-icon").click(_.bind(function(){
430
                    this.reset_all_net_actions(['destroy','disconnect']);
431
                    this.parent.network.get("actions").remove_all("disconnect");
432
                    this.parent.network.get("actions").add("disconnect", this.nic.id);
433
                    this.parent.network.get("actions").remove("destroy");
434
                }, this));
435

    
436
                this.vm_connect.click(_.bind(function() {
437
                    this.connect_overlay.show(this.vm);
438
                }, this));
439
                
440
                this.parent.network.bind("change:actions", _.bind(function(model, action){
441
                    if (this.parent.network.get("actions").contains("disconnect", this.nic.id)) {
442
                        this.confirm_disconnect();
443
                    } else {
444
                        this.cancel_disconnect();
445
                    }
446
                }, this));
447
            }
448
            
449
            var vm = this.vm;
450
            this.details.click(function(e){
451
                e.preventDefault();
452
                snf.ui.main.show_vm_details(vm);
453
            });
454

    
455
        },
456

    
457
        cancel_disconnect: function() {
458
            this.confirm_el.hide();
459
            this.disconnect.removeClass("selected");
460
            this.$(".net-vm-actions a").removeClass("visible");
461
        },
462

    
463
        confirm_disconnect: function() {
464
            this.confirm_el.show();
465
            this.disconnect.addClass("selected");
466
            this.$(".net-vm-actions a").addClass("visible");
467
        },
468

    
469
        init_layout: function() {
470
            if (!this.firewall_controls) { return };
471
        },
472

    
473
        update_layout: function() {
474
            this.$(".vm-name").text(snf.util.truncate(this.vm.get("name"), 40));
475
            this.$("img.logo").attr("src", ui.helpers.vm_icon_path(this.vm, "medium"));
476

    
477
            if (this.firewall_view) {
478
                this.$(".ipv4-text").text(this.nic.get_v4_address());
479
                this.$(".ipv6-text").text(this.nic.get_v6_address());
480
            }
481

    
482
            if (this.firewall_view) {
483
                this.firewall_view.update_layout();
484
            }
485

    
486
            if (!this.firewall_view) {
487
                this.$(".ip4-container").hide();
488
                this.$(".ip6-container").hide();
489
                
490
                if (this.nic.get("ipv4")) {
491
                    this.$(".ipv4-text").text(this.nic.get_v4_address());
492
                    this.$(".ip4-container").show();
493
                    this.$(".machine-connect .content").hide();
494
                } else if (this.nic.get("ipv6")) {
495
                    this.$(".ipv6-text").text(this.nic.get_v6_address());
496
                    this.$(".ip6-container").show();
497
                    this.$(".machine-connect .content").hide();
498
                } else {
499
                    this.$(".machine-connect .content").show();
500
                }
501
            } else {
502
            }
503
        },
504

    
505
        disconnect_nic: function() {
506
            this.$("a.selected").removeClass("selected");
507
            this.nic.get_network().remove_nic(this.nic);
508
        },
509
    })
510

    
511
    views.NetworkModelRenameView = views.View.extend({
512
        initialize: function(parent, network) {
513
            this.parent = parent;
514
            this.network = network;
515
            this.el = this.parent.el.find(".name-div");
516

    
517
            this.icon = this.$(".rename-network");
518
            this.save = this.$("span.save");
519
            this.cancel = this.$("span.cancel");
520
            this.buttons = this.$(".editbuttons");
521
            this.name = this.$("span.name");
522
            this.editing = false;
523
            this.init_handlers();
524
            this.update_layout();
525
        },
526

    
527
        init_handlers: function() {
528
            this.icon.click(_.bind(function(){
529
                this.editing = true;
530
                this.update_layout();
531
            }, this));
532
            this.cancel.click(_.bind(function(){
533
                this.editing = false;
534
                this.update_layout();
535
            }, this));
536
            this.save.click(_.bind(function(){
537
                this.submit();
538
            }, this))
539
        },
540
        
541
        submit: function() {
542
            var value = _(this.input.val()).trim();
543
            if (value == "") { return }
544

    
545
            this.network.rename(value, _.bind(function(){
546
                this.editing = false;
547
                this.update_layout();
548
            }, this));
549
        },
550

    
551
        create_input: function() {
552
            this.input = $('<input type="text" class="network-rename-input" />');
553
            this.input.val(this.network.get("name"));
554
            this.el.append(this.input);
555
            this.input.focus();
556
            this.input.bind("keydown", _.bind(function(ev){
557
                ev.keyCode = ev.keyCode || ev.which;
558
                if (ev.keyCode == 13) { this.submit(); };
559
                if (ev.keyCode == 27) {this.editing = false; this.update_layout()};
560
            }, this));
561
        },
562

    
563
        remove_input: function() {
564
            if (!this.input) { return }
565
            this.input.remove();
566
        },
567

    
568
        update_layout: function() {
569
            if (this.editing) {
570
                if (this.buttons.is(":visible")) { return }
571
                this.icon.hide();
572
                this.buttons.show();
573
                this.create_input();
574
                this.name.hide();
575
            } else {
576
                this.buttons.hide();
577
                this.remove_input();
578
                this.name.show();
579
                this.icon.show();
580
            }
581
        }
582
    })
583

    
584
    views.FirewallEditView = views.View.extend({
585
        initialize: function(nic, network, parent) {
586
            this.parent = parent;
587
            this.vm = nic.get_vm();
588
            this.nic = nic;
589
            this.network = network;
590
            this.el = this.parent.el;
591

    
592
            views.FirewallEditView.__super__.initialize.apply(this);
593

    
594
            // elements
595
            this.toggler = this.$(".firewall-toggle");
596
            this.indicator = this.$(".machines-label span");
597
            this.progress = this.$(".network-progress-indicator");
598
            this.content = this.$(".firewall-content");
599
            this.inputs = this.$("input[type=radio]");
600
            this.labels = this.$("span.checkbox-legends, label.checkbox-legends");
601
            this.apply = this.$(".firewall-apply");
602

    
603
            this.$(".firewall-content").hide();
604
            this.$(".firewall-content input[type=radio]").attr("name", "firewall-opt-for-{0}".format(this.vm.id))
605
            var mode = this.vm.get_firewall_profile();
606
            this.$(".firewall-content input[value={0}]".format(mode)).attr("checked", true);
607

    
608
            this.init_handlers();
609
            this.update_layout();
610

    
611
            var self = this;
612
            this.nic.bind("change:pending_firewall_sending", function(nic, value) {
613
                if (value) {
614
                    self.apply.addClass("in-progress");       
615
                    self.progress.show();
616
                } else {
617
                    self.apply.removeClass("in-progress");       
618
                    self.progress.hide();
619
                    self.toggler.click();
620
                }
621
            });
622

    
623
            this.nic.bind("change:firewallProfile", function(nic){
624
                self.update_layout();
625
                self.reset_value();
626
            })
627

    
628
        },
629
        
630
        _get_selected: function() {
631
            return this.inputs.filter(":checked");
632
        },
633

    
634
        reset_selected: function() {
635
        },
636

    
637
        submit: function() {
638
        },
639

    
640
        reset_value: function() {
641
            this.inputs.filter("[value={0}]".format(
642
              this.nic.get('firewallProfile'))).attr("checked", true);
643
        },
644

    
645
        init_handlers: function() {
646
            this.toggler.click(_.bind(function(){
647
                cont = this.content;
648
                if (cont.is(":visible")) {
649
                    this.hide_firewall();
650
                    this.reset_value();
651
                } else {
652
                    this.show_firewall();
653
                }
654

    
655
                $(window).trigger("resize");
656
            }, this))
657
            
658
            this.apply.click(_.bind(function(){
659
                this.nic.set_firewall(this.value());
660
            }, this))
661

    
662
            this.inputs.change(_.bind(function(){
663
                this.update_selected();
664
            }, this))
665
            
666
            var self = this;
667
            this.$(".checkbox-legends").click(function(el) {
668
                var el = $(this);
669
                el.prev().click();
670
                self.update_selected();
671
            })
672
        },
673

    
674
        update_selected: function() {
675
            this.update_layout();
676
        },
677

    
678
        show_firewall: function() {
679
            this.content.slideDown(100, function(){$(window).trigger("resize")});
680
            this.toggler.addClass("open");
681
        },
682

    
683
        hide_firewall: function() {
684
            this.content.slideUp(100, function(){$(window).trigger("resize")});
685
            this.toggler.removeClass("open");
686
        },
687

    
688
        value: function() {
689
            return this._get_selected().val();
690
        },
691

    
692
        update_layout: function() {
693
            if (this.value() == this.vm.get_firewall_profile()) {
694
                this.apply.hide();
695
            } else {
696
                this.apply.show();
697
            }
698

    
699
            var profile = this.vm.get_firewall_profile();
700
            if (this.vm.has_firewall(this.network.id)) {
701
                this.$(".firewall-toggle .label span").text("On");
702
                this.$(".firewall-toggle .label span").removeClass("firewall-off");
703
                this.$(".firewall-toggle .label span").addClass("firewall-on");
704
            } else {
705
                this.$(".firewall-toggle .label span").text("Off");
706
                this.$(".firewall-toggle .label span").removeClass("firewall-on");
707
                this.$(".firewall-toggle .label span").addClass("firewall-off");
708
            }
709
            
710
            this.$("span.checkbox-legends").removeClass("current");
711
            this.inputs.filter("[value={0}]".format(profile)).next().addClass("current");
712
            
713
        }
714
    })
715

    
716
    views.NetworkModelView = views.View.extend({
717
        
718
        firewall: false,
719

    
720
        initialize: function(network, view) {
721
            this.parent_view = view;
722
            this.network = network;
723
            this.main_view_id = this.main_view_id ? this.main_view_id : "networks_view_" + network.id;
724
            this.is_public = network.is_public();
725

    
726
            this.init_nics_handlers();
727
            
728
            this.view_id = "networks_view_" + network.id;
729
            views.NetworkModelView.__super__.initialize.call(this);
730

    
731
            this.nics_views = {};
732

    
733
            this.el = this.create_el();
734

    
735
            // element helpers
736
            this.nics_list = this.$(".machines-list");
737
            this.nics_list_toggler = this.$(".list-toggle");
738
            
739
            this.init_handlers();
740
            this.init_toggler_handlers();
741
            this.update_nics();
742
            this.update_layout();
743

    
744
            this.hide_nics_list();
745
            this.nics_list.hide();
746

    
747
            this.rename_view = undefined;
748
            if (!this.network.is_public()) {
749
                // allow network rename for non public networks only
750
                this.rename_view = new views.NetworkModelRenameView(this, network);
751
            }
752
            
753
            var self = this;
754
            this.network.bind('change:status', function() {
755
                self.update_layout();
756
            });
757

    
758
        },
759

    
760
        init_nics_handlers: function() {
761
            storage.nics.bind("add", _.bind(this.nic_added_handler, this, "add"));
762
            storage.nics.bind("change", _.bind(this.nic_changed_handler, this, "change"));
763
            storage.nics.bind("reset", _.bind(this.nic_changed_handler, this, "reset"));
764
            storage.nics.bind("remove", _.bind(this.nic_removed_handler, this, "remove"));
765
        },
766

    
767

    
768
        show_nics_list: function() {
769
            if (this.nics_empty()) { return }
770
            this.nics_list_toggler.addClass("open");
771
            this.nics_list.slideDown(function(){
772
                $(window).trigger("resize");
773
            }).closest(".network").addClass("expand");
774
            this.$(".empty-network-slot").show();
775
            this.nics_visible = true;
776
        },
777

    
778
        hide_nics_list: function() {
779
            this.nics_list_toggler.removeClass("open");
780
            this.nics_list.slideUp(function(){
781
                $(window).trigger("resize");
782
            }).closest(".network").removeClass("expand");
783
            this.$(".empty-network-slot").hide();
784
            this.nics_visible = false;
785
        },
786
        
787
        init_toggler_handlers: function() {
788
            this.nics_list_toggler.click(_.bind(function(){
789
                if (this.nics_list.is(":visible")) {
790
                    this.hide_nics_list();
791
                } else {
792
                    this.fix_left_border();
793
                    this.show_nics_list();
794
                }
795

    
796
                this.check_empty_nics();
797
            }, this));
798
        },
799

    
800
        init_handlers: function() {
801
            var self = this;
802

    
803

    
804
            this.$(".action-add").click(_.bind(function(e){
805
                e.preventDefault();
806
                this.network.get("actions").remove("destroy");
807
                this.show_connect_vms();
808
            }, this))
809

    
810
            this.$(".add-icon").click(_.bind(function(e){
811
                e.preventDefault();
812
                this.show_connect_vms();
813
            }, this))
814

    
815
            this.$(".net-actions .destroy a").click(_.bind(function(e){
816
                e.preventDefault();
817
                synnefo.storage.networks.each(function(n) {
818
                    n.get('actions').remove_all("disconnect");
819
                    if (!synnefo.config.network_allow_multiple_destory) {
820
                        n.get('actions').remove_all("destroy");
821
                    }
822
                });
823
                self.network.get("actions").add("destroy");
824
                self.network.get("actions").remove_all("disconnect");
825
            }, this));
826

    
827
            self.network.bind("change:actions", _.bind(function(net, action) {
828
                if (this.network.get("actions").contains("destroy")) {
829
                    this.confirm_destroy();
830
                } else {
831
                    this.cancel_destroy();
832
                }
833
            }, this));
834
            
835

    
836
            // reset pending destory action after successful removal
837
            self.network.bind("remove", _.bind(function(net){
838
                net.get("actions").remove_all("destroy");
839
            }));
840

    
841
            this.$(".net-actions button.no").click(function(e){
842
                e.preventDefault();
843
                self.network.get("actions").remove("destroy");
844
            });
845

    
846
            this.$(".net-actions button.yes").click(function(e){
847
                e.preventDefault();
848
                var el = $(this);
849
                el.closest(".confirm_single").hide();
850
                el.parent().parent().find(".selected").removeClass("selected");
851
                self.network.call('destroy', {}, function(){
852
                    el.closest(".confirm_single").removeClass("in-progress");
853
                });
854
                el.closest(".confirm_single").addClass("in-progress");
855
            });
856

    
857
            snf.ui.main.bind("view:change", _.bind(function(v) {
858
                if (v == "networks" ){ return }
859
                this.$(".confirm_single").hide();
860
                this.$("a.selected").removeClass("selected");
861
            }, this));
862

    
863
            $(window).bind("resize", _.bind(function() {
864
                this.fix_left_border();
865
            }, this));
866
        },
867

    
868
        show_connect_vms: function() {
869
            this.$(".confirm_single").hide();
870
            this.$("a.selected").removeClass("selected");
871
            var vms = this.network.get_connectable_vms();
872
            this.parent_view.connect_machines_view.show_vms(this.network,
873
                                                            vms, [], 
874
                                                            _.bind(this.connect_vms, this));
875
        },
876

    
877
        cancel_destroy: function() {
878
            this.$(".net-actions .destroy .confirm_single").hide();
879
            this.$(".net-actions .destroy a.selected").removeClass("selected");
880
            this.$(".net-actions a").removeClass("visible");
881
        },
882

    
883
        confirm_destroy: function() {
884
            this.$(".destroy .confirm_single").show();
885
            this.$(".destroy a").addClass("selected");
886
            this.$(".net-actions a").addClass("visible");
887
        },
888

    
889
        connect_vms: function(vms) {
890
            _.each(vms, _.bind(function(vm){
891
                this.network.add_vm(vm);
892
            }, this));
893

    
894
            this.parent_view.connect_machines_view.hide();
895
        },
896

    
897
        create_el: function() {
898
            return this.$(this.tpl).clone().attr("id", this.main_view_id);
899
        },
900

    
901
        get_nic_id: function(nic) {
902
            return this.nic_id_tpl.format(nic.id);
903
        },
904

    
905
        get_nic_view: function(nic) {
906
            return $(this.get_nic_id(nic));
907
        },
908
        
909
        nic_in_network: function(nic) {
910
          return nic.get_network().id != this.network.id;
911
        },
912

    
913
        nic_added_handler: function(action, nic) {
914
            if (!this.nic_in_network(nic)) { return };
915
            this.add_or_update_nic(nic);
916
            this.update_layout();
917
            this.fix_left_border();
918
        },
919

    
920
        nic_changed_handler: function(action, nics, model, changes) {
921
            var nics = nics || [];
922

    
923
            // reset or update
924
            if (action == "reset") {
925
                nics = nics;
926
            } else {
927
                if (!_.isArray(nics)) {
928
                    nics = [nics]
929
                }
930
            }
931
            
932
            _.each(nics, _.bind(function(nic) {
933
                if (!this.nic_in_network(nic)) { return };
934
                this.add_or_update_nic(nic);
935
            }, this));
936

    
937
            this.update_layout();
938
        },
939

    
940
        nic_removed_handler: function(action, nic, model) {
941
            if (!this.nic_in_network(nic)) { return };
942
            this.fix_left_border();
943
            this.remove_nic(nic);
944
            this.update_layout();
945
        },
946

    
947
        remove_nic: function(nic) {
948
            var nic_view = this.get_nic_view(nic);
949
            if (nic_view.length) {
950
                nic_view.remove();
951
                try {
952
                    delete this.nics_views[nic.id]
953
                } catch (err) {
954
                }
955
            }
956
        },
957
        
958
        create_nic_view: function(nic) {
959
            var nic_el = $(this.nic_tpl).clone().attr({
960
                id: this.get_nic_id(nic).replace("#","")
961
            });
962
            this.nics_list.append(nic_el);
963
            this.post_nic_add(nic);
964

    
965
            if (!this.nics_views[nic.id]) {
966
                var nic_view = this.nics_views[nic.id] = new views.NetworkNICView(nic, this, this.firewall, nic_el);
967
            }
968
        },
969

    
970
        add_or_update_nic: function(nic) {
971
            if (!nic) { return };
972
                
973
            var nic_el = this.get_nic_view(nic);
974
            var nic_view = this.nics_views[nic.id];
975

    
976
            if (nic_el.length == 0) {
977
                nic_view = this.create_nic_view(nic);
978
            }
979
            
980
            if (nic_view) { nic_view.update_layout() };
981

    
982
            this.update_nic(nic);
983
            this.post_nic_update(nic);
984
        },
985

    
986
        update_nic: function(vm){},
987
        post_nic_add: function(vm){},
988
        post_nic_update: function(vm){},
989
        
990
        get_nics: function() {
991
          return this.network.get_nics();
992
        },
993

    
994
        update_nics: function(nics) {
995
            if (!nics) { nics = this.get_nics() };
996
            _.each(nics, _.bind(function(nic){
997
                this.add_or_update_nic(nic);
998
            }, this));
999
        },
1000

    
1001
        check_empty_nics: function() {
1002
            if (this.get_nics().length == 0) {
1003
                this.hide_nics_list();
1004
            }
1005
        },
1006

    
1007
        nics_empty: function() {
1008
            return this.get_nics().length == 0;
1009
        },
1010

    
1011
        remove: function() {
1012
            $(this.el).remove();
1013
        },
1014
        
1015
        get_name: function() {
1016
          var net_name = this.network.get('name');
1017
          if (net_name == "public") { net_name = "Internet" };
1018
          return net_name;
1019
        },
1020

    
1021
        update_layout: function() {
1022
            // has vms ???
1023
            this.check_empty_nics();
1024

    
1025
            // is expanded ???
1026
            //
1027
            // whats the network status ???
1028
            //
1029
            this.$(".machines-count").text(this.get_nics().length);
1030

    
1031
            var net_name = this.get_name();
1032
            this.$(".name-div span.name").text(net_name);
1033

    
1034
            if (this.rename_view) {
1035
                this.rename_view.update_layout();
1036
            }
1037
            
1038
            this.$(".net-status").text(this.network.state_message());
1039

    
1040
            if (this.network.in_progress())  {
1041
                this.$(".spinner").show();
1042
                this.$(".network-indicator").addClass("in-progress");
1043
            } else {
1044
                this.$(".spinner").hide();
1045
                this.$(".network-indicator").removeClass("in-progress");
1046
            }
1047
                
1048
            if (this.network.get('state') == 'PENDING') {
1049
                this.el.addClass("pending");
1050
            } else {
1051
                this.el.removeClass("pending");
1052
            }
1053

    
1054
            if (this.network.get('state') == 'ERROR') {
1055
                this.el.addClass("in-error");
1056
                this.$(".network-indicator").addClass("error-state");
1057
            } else {
1058
                this.el.removeClass("in-error");
1059
                this.$(".network-indicator").removeClass("error-state");
1060
            }
1061

    
1062
            if (synnefo.config.network_strict_destroy) {
1063
                if (this.get_nics().length == 0 && 
1064
                        !this.network.in_progress()) {
1065
                    this.el.removeClass("disable-destroy");
1066
                } else {
1067
                    this.el.addClass("disable-destroy");
1068
                }
1069
            }
1070

    
1071
            if (this.network.get("state") == "DESTROY") {
1072
                this.$(".spinner").show();
1073
                this.$(".state").addClass("destroying-state");
1074
                this.$(".actions").hide();
1075
            }
1076
        },
1077

    
1078
        // fix left border position
1079
        fix_left_border: function() {
1080
            if (!this.nics_visible) { return };
1081
            
1082
            var imgheight = 2783;
1083
            var opened_vm_height = 133 + 20;
1084
            var closed_vm_height = 61 + 20;
1085
            var additional_height = 25;
1086

    
1087
            if (!this.is_public) { 
1088
                imgheight = 2700;
1089
                additional_height = 65;
1090
            };
1091
            
1092
            var contents = this.$(".network-contents");
1093
            var last_vm = this.$(".network-machine:last .cont-toggler.open").length;
1094
            var last_vm_height = closed_vm_height;
1095
            if (last_vm > 0){
1096
                last_vm_height = opened_vm_height;
1097
            }
1098

    
1099
            var nics_opened = this.$(".network-machine .cont-toggler.open").length;
1100
            var nics_closed = this.$(".network-machine").length - nics_opened;
1101

    
1102
            var calc_height = (nics_opened * opened_vm_height) + (nics_closed * closed_vm_height) + additional_height; 
1103
            var bgpos = imgheight - calc_height + last_vm_height - 30;
1104
            this.$(".network-contents").css({'background-position':'33px ' + (-bgpos) + 'px'});
1105
        }
1106
    })
1107

    
1108
    views.PublicNetworkView = views.NetworkModelView.extend({
1109
        firewall: true,
1110
        tpl: "#public-template",
1111
        nic_tpl: "#public-nic-template",
1112
        nic_id_tpl: "#nic-{0}",
1113
        
1114
        initialize: function(network, view) {
1115
          views.PublicNetworkView.__super__.initialize.call(this, network, view);
1116
        },
1117

    
1118
        init_handlers: function(vm) {}
1119
    });
1120

    
1121
    views.GroupedPublicNetworkView = views.PublicNetworkView.extend({
1122
        main_view_id: "grouped-public",
1123

    
1124
        initialize: function(network, view) {
1125
          this.networks = {};
1126
          this.add_network(network);
1127
          views.GroupedPublicNetworkView.__super__.initialize.call(this, 
1128
                                                                   network, 
1129
                                                                   view);
1130
        },
1131
          
1132
        get_name: function() {
1133
          return synnefo.config.grouped_network_name || views.GroupedPublicNetworkView.__super__.get_name.call(this);
1134
        },
1135

    
1136
        nic_in_network: function(nic) {
1137
          var nic_net  = nic.get_network();
1138
          return _.filter(this.networks, function(n) { 
1139
            return nic_net.id == n.id;
1140
          }).length > 0;
1141
        },
1142

    
1143
        get_nics: function() {
1144
          var n = _.flatten(_.map(this.networks, function(n){ return n.get_nics(); }));
1145
          return n
1146
        },
1147

    
1148
        add_network: function(net) {
1149
          this.networks[net.id] = net;
1150
        },
1151

    
1152
        remove_network: function(net) {
1153
          delete this.networks[net.id];
1154
          this.update_nics();
1155
        }
1156

    
1157
    })
1158
    
1159
    views.PrivateNetworkView = views.NetworkModelView.extend({
1160
        tpl: "#private-template",
1161
        nic_tpl: "#private-nic-template",
1162
        nic_id_tpl: "#nic-{0}"
1163
    })
1164

    
1165
    views.NetworksView = views.View.extend({
1166
        
1167
        view_id: "networks",
1168
        pane: "#networks-pane",
1169
        el: "#networks-pane",
1170

    
1171
        initialize: function() {
1172
            // elements shortcuts
1173
            this.create_cont = this.$("#networks-createcontainer");
1174
            this.container = this.$("#networks-container");
1175
            this.public_list = this.$(".public-networks");
1176
            this.private_list = this.$(".private-networks");
1177
            views.NetworksView.__super__.initialize.call(this);
1178
            this.init_handlers();
1179
            this.network_views = {};
1180
            this.public_network = false;
1181
            this.update_networks(storage.networks.models);
1182
            this.create_view = new views.NetworkCreateView();
1183
            this.connect_machines_view = new views.NetworkConnectVMsOverlay();
1184
        },
1185
        
1186
        exists: function(net) {
1187
            return this.network_views[net.id];
1188
        },
1189

    
1190
        add_or_update: function(net) {
1191
            var nv = this.exists(net);
1192
            if (!nv) {
1193
                if (net.is_public()){
1194
                  if (synnefo.config.group_public_networks) {
1195
                    if (!this.public_network) {
1196
                      // grouped public not initialized
1197
                      this.public_network = this.create_network_view(net);
1198
                    } else {
1199
                      // grouped public initialized, append
1200
                      this.public_network.add_network(net);
1201
                    }
1202
                    nv = this.public_network;
1203
                  } else {
1204
                    // no grouped view asked, fallback to default create
1205
                    nv = this.create_network_view(net);
1206
                  }
1207
                } else {
1208
                  nv = this.create_network_view(net);
1209
                }
1210

    
1211
                this.network_views[net.id] = nv;
1212
                
1213
                if (net.is_public()) {
1214
                    this.public_list.append(nv.el);
1215
                    this.public_list.show();
1216
                } else {
1217
                    this.private_list.append(nv.el);
1218
                    this.private_list.show();
1219
                }
1220
            }
1221

    
1222
            // update vms
1223
            // for cases where network servers list
1224
            // get updated after vm addition and
1225
            // vm_added_handler fails to append the
1226
            // vm to the list
1227
            nv.update_nics();
1228
            nv.update_layout();
1229
        },
1230
        
1231
        create_network_view: function(net) {
1232
            if (net.is_public()) {
1233
                if (synnefo.config.group_public_networks) {
1234
                  if (self.public_network) { return self.public_network }
1235
                  return new views.GroupedPublicNetworkView(net, this);
1236
                } else {
1237
                  return new views.PublicNetworkView(net, this);
1238
                }
1239
            }
1240
            return new views.PrivateNetworkView(net, this);
1241
        },
1242
        
1243
        init_handlers: function() {
1244
            storage.networks.bind("add", _.bind(this.network_added_handler, this, "add"));
1245
            storage.networks.bind("change", _.bind(this.network_changed_handler, this, "change"));
1246
            storage.networks.bind("reset", _.bind(this.network_changed_handler, this, "reset"));
1247
            storage.networks.bind("remove", _.bind(this.network_removed_handler, this, "remove"));
1248

    
1249
            this.$("#networkscreate").click(_.bind(function(e){
1250
                e.preventDefault();
1251
                this.create_view.show();
1252
            }, this));
1253
            
1254
        },
1255

    
1256
        update_networks: function(nets) {
1257
            _.each(nets, _.bind(function(net){
1258
                if (net.get("status") == "DELETED") { return };
1259
                view = this.add_or_update(net);
1260
            }, this));
1261
        },
1262

    
1263
        show: function() {
1264
            this.container.show();
1265
            $(this.el).show();
1266
        },
1267

    
1268
        network_added_handler: function(type, net) {
1269
            this.update_networks([net]);
1270
        },
1271

    
1272
        network_changed_handler: function(type, models) {
1273
            var nets = [];
1274
            if (type == "change") {
1275
                nets = [models]
1276
            } else {
1277
                nets = models.models;
1278
            }
1279

    
1280
            this.update_networks(nets)
1281
        },
1282

    
1283
        network_removed_handler: function(type, net) {
1284
            this.remove_net(net)
1285
            if (this.private_list.find(".network").length == 0) {
1286
                this.private_list.hide();
1287
            }
1288
            
1289
        },
1290

    
1291
        network_added: function(net) {
1292
            return this.network_views[net.id];
1293
        },
1294

    
1295
        get_network_view: function(net) {
1296
            return this.network_views[net.id];
1297
        },
1298

    
1299
        remove_net: function(net) {
1300
            if (this.network_added(net)) {
1301
                var view = this.get_network_view(net);
1302
                if (view == this.public_network) {
1303
                  this.public_network.remove_network(net);
1304
                } else {
1305
                  view.remove();
1306
                }
1307
                delete this.network_views[net.id];
1308
            }
1309
        },
1310

    
1311
        __update_layout: function() {
1312
        }
1313
    });
1314

    
1315
})(this);