Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_networks_view.js @ 80b91e9f

History | View | Annotate | Download (46.5 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
        get_next_available_subnet: function() {
319
            var auto_tpl = synnefo.config.automatic_network_range_format;
320
            if (!auto_tpl) {
321
                return null
322
            }
323
            var index = 0;
324
            var subnet = auto_tpl.format(index);
325
            var networks = synnefo.storage.networks;
326
            var check_existing = function(n) { return n.get('cidr') == subnet }
327
            while (networks.filter(check_existing).length > 0 && index <= 255) {
328
                index++;
329
                subnet = auto_tpl.format(index); 
330
            }
331
            return subnet;
332
        },
333

    
334
        create: function() {
335
            this.create_button.addClass("in-progress");
336

    
337
            var name = this.text.val();
338
            var dhcp = this.dhcp_select.is(":checked");
339
            var subnet = null;
340
            var type = this.type_select.val();
341

    
342
            if (this.disable_network_type) { type = null };
343

    
344
            if (dhcp) {
345
                if (this.subnet_select.val() == "custom") {
346
                    subnet = this.subnet_custom.val();
347
                } else if (this.subnet_select.val() == "auto") {
348
                    subnet = this.get_next_available_subnet()
349
                } else {
350
                    subnet = this.subnet_select.val();
351
                }
352
                
353
            }
354

    
355
            snf.storage.networks.create(name, type, subnet, dhcp, _.bind(function(){
356
                this.hide();
357
            }, this));
358
        },
359

    
360
        beforeOpen: function() {
361
            this.create_button.removeClass("in-progress")
362
            this.text.closest(".form-field").removeClass("error");
363
            this.text.val("");
364
            this.text.show();
365
            this.text.focus();
366
            this.subnet_custom.val("");
367
            this.subnet_select.val("auto");
368
            this.dhcp_select.attr("checked", true);
369
            this.type_select.val(_.keys(synnefo.config.network_available_types)[0]);
370
            this.check_dhcp_form();
371
        },
372

    
373
        onOpen: function() {
374
            this.text.focus();
375
        }
376
    });
377

    
378
    views.NetworkNICView = views.View.extend({
379

    
380
        initialize: function(nic, parent, firewall_controls, el) {
381
            this.firewall_controls = firewall_controls || false;
382
            this.nic = nic;
383
            this.vm = nic.get_vm();
384
            // parent view di
385
            this.parent = parent;
386
            // TODO make it better
387
            this.el = el || this.parent.get_nic_view(nic);
388

    
389
            this.init_layout();
390
            this.update_layout();
391

    
392
            this.disconnect = this.$(".action-disconnect");
393
            this.confirm_el = this.$(".confirm_single");
394
            this.cancel = this.$("button.no");
395
            this.confirm = this.$("button.yes");
396
            this.details = this.$(".action-details");
397
            this.vm_connect = this.$(".machine-connect");
398
            this.actions = this.$(".net-vm-actions");
399

    
400
            this.init_handlers();
401
            this.connect_overlay = new views.VMConnectView();
402
            
403
            this.firewall_view = undefined;
404
            if (this.firewall_controls) {
405
                this.firewall_view = new views.FirewallEditView(this.nic, this.parent.network, this);
406
            }
407

    
408
        },
409
        
410
        reset_all_net_actions: function(act_types) {
411
            synnefo.storage.networks.each(function(n){
412
                var actions = n.get('actions');
413
                _.each(act_types, function(type){
414
                    actions.remove_all(type);
415
                })
416
            })
417
        },
418

    
419
        init_handlers: function() {
420
            if (!this.parent.network.is_public()) {
421
                this.disconnect.click(_.bind(function(e){
422
                    e.preventDefault();
423
                    this.reset_all_net_actions(['destroy','disconnect']);
424
                    this.parent.network.get("actions").remove_all("disconnect");
425
                    this.parent.network.get("actions").add("disconnect", this.nic.id);
426
                    this.parent.network.get("actions").remove("destroy");
427
                }, this));
428
                this.cancel.click(_.bind(function(e){
429
                    this.parent.network.get("actions").remove("disconnect", this.nic.id);
430
                    e.preventDefault()
431
                }, this));
432

    
433
                this.confirm.click(_.bind(function(e){
434
                    e.preventDefault()
435
                    this.disconnect_nic();
436
                    this.confirm_el.hide();
437
                    this.disconnect.removeClass("selected");
438
                    this.actions.find("a").removeClass("visible");
439
                }, this));
440

    
441
                snf.ui.main.bind("view:change", _.bind(function(v) {
442
                    if (v == "networks" ){ return }
443
                    this.confirm_el.hide();
444
                    this.disconnect.removeClass("selected");
445
                }, this));
446

    
447
                this.$(".remove-icon").click(_.bind(function(){
448
                    this.reset_all_net_actions(['destroy','disconnect']);
449
                    this.parent.network.get("actions").remove_all("disconnect");
450
                    this.parent.network.get("actions").add("disconnect", this.nic.id);
451
                    this.parent.network.get("actions").remove("destroy");
452
                }, this));
453

    
454
                this.vm_connect.click(_.bind(function() {
455
                    this.connect_overlay.show(this.vm);
456
                }, this));
457
                
458
                this.parent.network.bind("change:actions", _.bind(function(model, action){
459
                    if (this.parent.network.get("actions").contains("disconnect", this.nic.id)) {
460
                        this.confirm_disconnect();
461
                    } else {
462
                        this.cancel_disconnect();
463
                    }
464
                }, this));
465
            }
466
            
467
            var vm = this.vm;
468
            this.details.click(function(e){
469
                e.preventDefault();
470
                snf.ui.main.show_vm_details(vm);
471
            });
472

    
473
        },
474

    
475
        cancel_disconnect: function() {
476
            this.confirm_el.hide();
477
            this.disconnect.removeClass("selected");
478
            this.$(".net-vm-actions a").removeClass("visible");
479
        },
480

    
481
        confirm_disconnect: function() {
482
            this.confirm_el.show();
483
            this.disconnect.addClass("selected");
484
            this.$(".net-vm-actions a").addClass("visible");
485
        },
486

    
487
        init_layout: function() {
488
            if (!this.firewall_controls) { return };
489
        },
490

    
491
        update_layout: function() {
492
            this.$(".vm-name").text(snf.util.truncate(this.vm.get("name"), 40));
493
            this.$("img.logo").attr("src", ui.helpers.vm_icon_path(this.vm, "medium"));
494

    
495
            if (this.firewall_view) {
496
                this.$(".ipv4-text").text(this.nic.get_v4_address());
497
                this.$(".ipv6-text").text(this.nic.get_v6_address());
498
            }
499

    
500
            if (this.firewall_view) {
501
                this.firewall_view.update_layout();
502
            }
503

    
504
            if (!this.firewall_view) {
505
                this.$(".ip4-container").hide();
506
                this.$(".ip6-container").hide();
507
                
508
                if (this.nic.get("ipv4")) {
509
                    this.$(".ipv4-text").text(this.nic.get_v4_address());
510
                    this.$(".ip4-container").show();
511
                    this.$(".machine-connect .content").hide();
512
                } else if (this.nic.get("ipv6")) {
513
                    this.$(".ipv6-text").text(this.nic.get_v6_address());
514
                    this.$(".ip6-container").show();
515
                    this.$(".machine-connect .content").hide();
516
                } else {
517
                    this.$(".machine-connect .content").show();
518
                }
519
            } else {
520
            }
521
        },
522

    
523
        disconnect_nic: function() {
524
            this.$("a.selected").removeClass("selected");
525
            this.nic.get_network().remove_nic(this.nic);
526
        },
527
    })
528

    
529
    views.NetworkModelRenameView = views.View.extend({
530
        initialize: function(parent, network) {
531
            this.parent = parent;
532
            this.network = network;
533
            this.el = this.parent.el.find(".name-div");
534

    
535
            this.icon = this.$(".rename-network");
536
            this.save = this.$("span.save");
537
            this.cancel = this.$("span.cancel");
538
            this.buttons = this.$(".editbuttons");
539
            this.name = this.$("span.name");
540
            this.editing = false;
541
            this.init_handlers();
542
            this.update_layout();
543
        },
544

    
545
        init_handlers: function() {
546
            this.icon.click(_.bind(function(){
547
                this.editing = true;
548
                this.update_layout();
549
            }, this));
550
            this.cancel.click(_.bind(function(){
551
                this.editing = false;
552
                this.update_layout();
553
            }, this));
554
            this.save.click(_.bind(function(){
555
                this.submit();
556
            }, this))
557
        },
558
        
559
        submit: function() {
560
            var value = _(this.input.val()).trim();
561
            if (value == "") { return }
562

    
563
            this.network.rename(value, _.bind(function(){
564
                this.editing = false;
565
                this.update_layout();
566
            }, this));
567
        },
568

    
569
        create_input: function() {
570
            this.input = $('<input type="text" class="network-rename-input" />');
571
            this.input.val(this.network.get("name"));
572
            this.el.append(this.input);
573
            this.input.focus();
574
            this.input.bind("keydown", _.bind(function(ev){
575
                ev.keyCode = ev.keyCode || ev.which;
576
                if (ev.keyCode == 13) { this.submit(); };
577
                if (ev.keyCode == 27) {this.editing = false; this.update_layout()};
578
            }, this));
579
        },
580

    
581
        remove_input: function() {
582
            if (!this.input) { return }
583
            this.input.remove();
584
        },
585

    
586
        update_layout: function() {
587
            if (this.editing) {
588
                if (this.buttons.is(":visible")) { return }
589
                this.icon.hide();
590
                this.buttons.show();
591
                this.create_input();
592
                this.name.hide();
593
            } else {
594
                this.buttons.hide();
595
                this.remove_input();
596
                this.name.show();
597
                this.icon.show();
598
            }
599
        }
600
    })
601

    
602
    views.FirewallEditView = views.View.extend({
603

    
604
        initialize: function(nic, network, parent) {
605
            this.parent = parent;
606
            this.vm = nic.get_vm();
607
            this.nic = nic;
608
            this.network = network;
609
            this.el = this.parent.el;
610

    
611
            views.FirewallEditView.__super__.initialize.apply(this);
612

    
613
            // elements
614
            this.toggler = this.$(".firewall-toggle");
615
            this.indicator = this.$(".machines-label span");
616
            this.progress = this.$(".network-progress-indicator");
617
            this.content = this.$(".firewall-content");
618
            this.inputs = this.$("input[type=radio]");
619
            this.labels = this.$("span.checkbox-legends, label.checkbox-legends");
620
            this.apply = this.$(".firewall-apply");
621

    
622
            this.$(".firewall-content").hide();
623
            this.$(".firewall-content input[type=radio]").attr("name", "firewall-opt-for-{0}".format(this.vm.id))
624
            var mode = this.vm.get_firewall_profile();
625
            this.$(".firewall-content input[value={0}]".format(mode)).attr("checked", true);
626

    
627
            this.init_handlers();
628
            this.update_layout();
629

    
630
            var self = this;
631
            this.nic.bind("change:pending_firewall_sending", function(nic, value) {
632
                if (value) {
633
                    self.apply.addClass("in-progress");       
634
                    self.progress.show();
635
                } else {
636
                    self.apply.removeClass("in-progress");       
637
                    self.progress.hide();
638
                    self.toggler.click();
639
                }
640
            });
641

    
642
            this.nic.bind("change:firewallProfile", function(nic){
643
                self.update_layout();
644
                self.reset_value();
645
            })
646

    
647
        },
648
        
649
        _get_selected: function() {
650
            return this.inputs.filter(":checked");
651
        },
652

    
653
        reset_selected: function() {
654
        },
655

    
656
        submit: function() {
657
        },
658

    
659
        reset_value: function() {
660
            this.inputs.filter("[value={0}]".format(
661
              this.nic.get('firewallProfile'))).attr("checked", true);
662
        },
663

    
664
        init_handlers: function() {
665
            this.toggler.click(_.bind(function(){
666
                cont = this.content;
667
                if (cont.is(":visible")) {
668
                    this.hide_firewall();
669
                    this.reset_value();
670
                } else {
671
                    this.show_firewall();
672
                }
673

    
674
                $(window).trigger("resize");
675
            }, this))
676
            
677
            this.apply.click(_.bind(function(){
678
                this.nic.set_firewall(this.value());
679
            }, this))
680

    
681
            this.inputs.change(_.bind(function(){
682
                this.update_selected();
683
            }, this))
684
            
685
            var self = this;
686
            this.$(".checkbox-legends").click(function(el) {
687
                var el = $(this);
688
                el.prev().click();
689
                self.update_selected();
690
            })
691
        },
692

    
693
        update_selected: function() {
694
            this.update_layout();
695
        },
696

    
697
        show_firewall: function() {
698
            this.content.slideDown(100, function(){$(window).trigger("resize")});
699
            this.toggler.addClass("open");
700
        },
701

    
702
        hide_firewall: function() {
703
            this.content.slideUp(100, function(){$(window).trigger("resize")});
704
            this.toggler.removeClass("open");
705
        },
706

    
707
        value: function() {
708
            return this._get_selected().val();
709
        },
710

    
711
        update_layout: function() {
712
            if (this.value() == this.vm.get_firewall_profile()) {
713
                this.apply.hide();
714
            } else {
715
                this.apply.show();
716
            }
717

    
718
            var profile = this.vm.get_firewall_profile();
719
            if (this.vm.has_firewall(this.network.id)) {
720
                this.$(".firewall-toggle .label span").text("On");
721
                this.$(".firewall-toggle .label span").removeClass("firewall-off");
722
                this.$(".firewall-toggle .label span").addClass("firewall-on");
723
            } else {
724
                this.$(".firewall-toggle .label span").text("Off");
725
                this.$(".firewall-toggle .label span").removeClass("firewall-on");
726
                this.$(".firewall-toggle .label span").addClass("firewall-off");
727
            }
728
            
729
            this.$("span.checkbox-legends").removeClass("current");
730
            this.inputs.filter("[value={0}]".format(profile)).next().addClass("current");
731
            
732
        }
733
    })
734

    
735
    views.NetworkModelView = views.View.extend({
736
        
737
        firewall: false,
738

    
739
        initialize: function(network, view) {
740
            this.parent_view = view;
741
            this.network = network;
742
            this.main_view_id = this.main_view_id ? this.main_view_id : "networks_view_" + network.id;
743
            this.is_public = network.is_public();
744

    
745
            this.init_nics_handlers();
746
            
747
            this.view_id = "networks_view_" + network.id;
748
            views.NetworkModelView.__super__.initialize.call(this);
749

    
750
            this.nics_views = {};
751

    
752
            this.el = this.create_el();
753

    
754
            // element helpers
755
            this.nics_list = this.$(".machines-list");
756
            this.nics_list_toggler = this.$(".list-toggle");
757
            
758
            this.init_handlers();
759
            this.init_toggler_handlers();
760
            this.update_nics();
761
            this.update_layout();
762

    
763
            this.hide_nics_list();
764
            this.nics_list.hide();
765

    
766
            this.rename_view = undefined;
767
            if (!this.network.is_public()) {
768
                // allow network rename for non public networks only
769
                this.rename_view = new views.NetworkModelRenameView(this, network);
770
            }
771
            
772
            var self = this;
773
            this.network.bind('change:status', function() {
774
                self.update_layout();
775
            });
776

    
777
        },
778

    
779
        init_nics_handlers: function() {
780
            storage.nics.bind("add", _.bind(this.nic_added_handler, this, "add"));
781
            storage.nics.bind("change", _.bind(this.nic_changed_handler, this, "change"));
782
            storage.nics.bind("reset", _.bind(this.nic_changed_handler, this, "reset"));
783
            storage.nics.bind("remove", _.bind(this.nic_removed_handler, this, "remove"));
784
        },
785

    
786

    
787
        show_nics_list: function() {
788
            //if (this.nics_empty()) { return }
789
            var self = this;
790
            this.nics_list_toggler.addClass("open");
791
            this.nics_list.slideDown(function(){
792
                $(window).trigger("resize");
793
            }).closest(".network").addClass("expand");
794
            this.$(".empty-network-slot").slideDown();
795
            this.nics_visible = true;
796
        },
797

    
798
        hide_nics_list: function() {
799
            this.nics_list_toggler.removeClass("open");
800
            this.nics_list.slideUp(function(){
801
                $(window).trigger("resize");
802
            }).closest(".network").removeClass("expand");
803
            this.$(".empty-network-slot").slideUp();
804
            this.nics_visible = false;
805
        },
806
        
807
        init_toggler_handlers: function() {
808
            this.nics_list_toggler.click(_.bind(function(){
809
                if (this.nics_list.is(":visible")) {
810
                    this.hide_nics_list();
811
                } else {
812
                    this.show_nics_list();
813
                    this.fix_left_border();
814
                }
815

    
816
            }, this));
817
            $(window).bind("resize", _.bind(function() {
818
                this.fix_left_border();
819
            }, this));
820
        },
821

    
822
        init_handlers: function() {
823
            var self = this;
824

    
825

    
826
            this.$(".action-add").click(_.bind(function(e){
827
                e.preventDefault();
828
                this.network.get("actions").remove("destroy");
829
                this.show_connect_vms();
830
            }, this))
831

    
832
            this.$(".add-icon").click(_.bind(function(e){
833
                e.preventDefault();
834
                this.show_connect_vms();
835
            }, this))
836

    
837
            this.$(".net-actions .destroy a").click(_.bind(function(e){
838
                e.preventDefault();
839
                synnefo.storage.networks.each(function(n) {
840
                    n.get('actions').remove_all("disconnect");
841
                    if (!synnefo.config.network_allow_multiple_destory) {
842
                        n.get('actions').remove_all("destroy");
843
                    }
844
                });
845
                self.network.get("actions").add("destroy");
846
                self.network.get("actions").remove_all("disconnect");
847
            }, this));
848

    
849
            self.network.bind("change:actions", _.bind(function(net, action) {
850
                if (this.network.get("actions").contains("destroy")) {
851
                    this.confirm_destroy();
852
                } else {
853
                    this.cancel_destroy();
854
                }
855
            }, this));
856
            
857

    
858
            // reset pending destory action after successful removal
859
            self.network.bind("remove", _.bind(function(net){
860
                net.get("actions").remove_all("destroy");
861
            }));
862

    
863
            this.$(".net-actions button.no").click(function(e){
864
                e.preventDefault();
865
                self.network.get("actions").remove("destroy");
866
            });
867

    
868
            this.$(".net-actions button.yes").click(function(e){
869
                e.preventDefault();
870
                var el = $(this);
871
                el.closest(".confirm_single").hide();
872
                el.parent().parent().find(".selected").removeClass("selected");
873
                self.network.call('destroy', {}, function(){
874
                    el.closest(".confirm_single").removeClass("in-progress");
875
                });
876
                el.closest(".confirm_single").addClass("in-progress");
877
            });
878

    
879
            snf.ui.main.bind("view:change", _.bind(function(v) {
880
                if (v == "networks" ){ return }
881
                this.$(".confirm_single").hide();
882
                this.$("a.selected").removeClass("selected");
883
            }, this));
884
            
885
            this.$(".empty-network-slot").hide();
886
        },
887

    
888
        show_connect_vms: function() {
889
            this.$(".confirm_single").hide();
890
            this.$("a.selected").removeClass("selected");
891
            var vms = this.network.get_connectable_vms();
892
            this.parent_view.connect_machines_view.show_vms(this.network,
893
                                                            vms, [], 
894
                                                            _.bind(this.connect_vms, this));
895
        },
896

    
897
        cancel_destroy: function() {
898
            this.$(".net-actions .destroy .confirm_single").hide();
899
            this.$(".net-actions .destroy a.selected").removeClass("selected");
900
            this.$(".net-actions a").removeClass("visible");
901
        },
902

    
903
        confirm_destroy: function() {
904
            this.$(".destroy .confirm_single").show();
905
            this.$(".destroy a").addClass("selected");
906
            this.$(".net-actions a").addClass("visible");
907
        },
908

    
909
        connect_vms: function(vms) {
910
            _.each(vms, _.bind(function(vm){
911
                this.network.add_vm(vm);
912
            }, this));
913

    
914
            this.parent_view.connect_machines_view.hide();
915
        },
916

    
917
        create_el: function() {
918
            return this.$(this.tpl).clone().attr("id", this.main_view_id);
919
        },
920

    
921
        get_nic_id: function(nic) {
922
            return this.nic_id_tpl.format(nic.id);
923
        },
924

    
925
        get_nic_view: function(nic) {
926
            return $(this.get_nic_id(nic));
927
        },
928
        
929
        nic_in_network: function(nic) {
930
          return nic.get_network().id == this.network.id;
931
        },
932

    
933
        nic_added_handler: function(action, nic) {
934
            if (!this.nic_in_network(nic)) { return };
935
            this.add_or_update_nic(nic);
936
            this.update_layout();
937
            this.fix_left_border();
938
        },
939

    
940
        nic_changed_handler: function(action, nics, model, changes) {
941
            var nics = nics || [];
942

    
943
            // reset or update
944
            if (action == "reset") {
945
                nics = nics;
946
            } else {
947
                if (!_.isArray(nics)) {
948
                    nics = [nics]
949
                }
950
            }
951
            
952
            _.each(nics, _.bind(function(nic) {
953
                if (!this.nic_in_network(nic)) { return };
954
                this.add_or_update_nic(nic);
955
            }, this));
956

    
957
            this.update_layout();
958
        },
959

    
960
        nic_removed_handler: function(action, nic, model) {
961
            if (!this.nic_in_network(nic)) { return };
962
            this.fix_left_border();
963
            this.remove_nic(nic);
964
            this.update_layout();
965
        },
966

    
967
        remove_nic: function(nic) {
968
            var nic_view = this.get_nic_view(nic);
969
            if (nic_view.length) {
970
                nic_view.remove();
971
                try {
972
                    delete this.nics_views[nic.id]
973
                } catch (err) {
974
                }
975
            }
976
        },
977
        
978
        create_nic_view: function(nic) {
979
            var nic_el = $(this.nic_tpl).clone().attr({
980
                id: this.get_nic_id(nic).replace("#","")
981
            });
982
            this.nics_list.append(nic_el);
983
            this.post_nic_add(nic);
984

    
985
            if (!this.nics_views[nic.id]) {
986
                var nic_view = this.nics_views[nic.id] = new views.NetworkNICView(nic, this, this.firewall, nic_el);
987
            }
988
        },
989

    
990
        add_or_update_nic: function(nic) {
991
            if (!nic) { return };
992
                
993
            var nic_el = this.get_nic_view(nic);
994
            var nic_view = this.nics_views[nic.id];
995

    
996
            if (nic_el.length == 0) {
997
                nic_view = this.create_nic_view(nic);
998
            }
999
            
1000
            if (nic_view) { nic_view.update_layout() };
1001

    
1002
            this.update_nic(nic);
1003
            this.post_nic_update(nic);
1004
        },
1005

    
1006
        update_nic: function(vm){},
1007
        post_nic_add: function(vm){},
1008
        post_nic_update: function(vm){},
1009
        
1010
        get_nics: function() {
1011
          return this.network.get_nics();
1012
        },
1013

    
1014
        update_nics: function(nics) {
1015
            if (!nics) { nics = this.get_nics() };
1016
            _.each(nics, _.bind(function(nic){
1017
                this.add_or_update_nic(nic);
1018
            }, this));
1019
        },
1020

    
1021
        check_empty_nics: function() {
1022
            if (this.get_nics().length == 0) {
1023
                this.hide_nics_list();
1024
            }
1025
        },
1026

    
1027
        nics_empty: function() {
1028
            return this.get_nics().length == 0;
1029
        },
1030

    
1031
        remove: function() {
1032
            $(this.el).remove();
1033
        },
1034
        
1035
        get_name: function() {
1036
          var net_name = this.network.get('name');
1037
          if (net_name == "public") { net_name = "Internet" };
1038
          return net_name;
1039
        },
1040

    
1041
        update_layout: function() {
1042
            // has vms ???
1043
            this.check_empty_nics();
1044

    
1045
            // is expanded ???
1046
            //
1047
            // whats the network status ???
1048
            //
1049
            this.$(".machines-count").text(this.get_nics().length);
1050

    
1051
            var net_name = this.get_name();
1052
            this.$(".name-div span.name").text(net_name);
1053

    
1054
            if (this.rename_view) {
1055
                this.rename_view.update_layout();
1056
            }
1057
            
1058
            this.$(".net-status").text(this.network.state_message());
1059

    
1060
            if (this.network.in_progress())  {
1061
                this.$(".spinner").show();
1062
                this.$(".network-indicator").addClass("in-progress");
1063
            } else {
1064
                this.$(".spinner").hide();
1065
                this.$(".network-indicator").removeClass("in-progress");
1066
            }
1067
                
1068
            if (this.network.get('state') == 'PENDING') {
1069
                this.el.addClass("pending");
1070
            } else {
1071
                this.el.removeClass("pending");
1072
            }
1073

    
1074
            if (this.network.get('state') == 'ERROR') {
1075
                this.el.addClass("in-error");
1076
                this.$(".network-indicator").addClass("error-state");
1077
            } else {
1078
                this.el.removeClass("in-error");
1079
                this.$(".network-indicator").removeClass("error-state");
1080
            }
1081

    
1082
            if (synnefo.config.network_strict_destroy) {
1083
                if (this.get_nics().length == 0 && 
1084
                        !this.network.in_progress()) {
1085
                    this.el.removeClass("disable-destroy");
1086
                } else {
1087
                    this.el.addClass("disable-destroy");
1088
                }
1089
            }
1090

    
1091
            if (this.network.get("state") == "DESTROY") {
1092
                this.$(".spinner").show();
1093
                this.$(".state").addClass("destroying-state");
1094
                this.$(".actions").hide();
1095
            } else {
1096
                this.$(".state").removeClass("destroying-state");
1097
                this.$(".actions").show();
1098
                this.$(".actions a").removeClass("visible");
1099
            }
1100
        },
1101

    
1102
        // fix left border position
1103
        fix_left_border: function(force) {
1104
            if (!this.nics_visible && !force) { return };
1105
            
1106
            var imgheight = 2783;
1107
            var opened_vm_height = 133 + 18;
1108
            var closed_vm_height = 61 + 20;
1109
            var additional_height = 25;
1110

    
1111
            if (!this.is_public) { 
1112
                imgheight = 2700;
1113
                additional_height = 65;
1114
            };
1115
            
1116
            var contents = this.$(".network-contents");
1117
            var last_vm = this.$(".network-machine:last .cont-toggler.open").length;
1118
            var last_vm_height = closed_vm_height;
1119
            if (last_vm > 0){
1120
                last_vm_height = opened_vm_height;
1121
            }
1122

    
1123
            var nics_opened = this.$(".network-machine .cont-toggler.open").length;
1124
            var nics_closed = this.$(".network-machine").length - nics_opened;
1125

    
1126
            var calc_height = (nics_opened * opened_vm_height) + (nics_closed * closed_vm_height) + additional_height; 
1127
            var bgpos = imgheight - calc_height + last_vm_height - 30;
1128
            this.$(".network-contents").css({'background-position':'33px ' + (-bgpos) + 'px'});
1129
        }
1130
    })
1131

    
1132
    views.PublicNetworkView = views.NetworkModelView.extend({
1133
        firewall: true,
1134
        tpl: "#public-template",
1135
        nic_tpl: "#public-nic-template",
1136
        nic_id_tpl: "#nic-{0}",
1137
        
1138
        initialize: function(network, view) {
1139
          views.PublicNetworkView.__super__.initialize.call(this, network, view);
1140
          this.fix_left_border(1);
1141
        },
1142

    
1143
        init_handlers: function(vm) {
1144
          $(window).bind("resize", _.bind(function() {
1145
              this.fix_left_border();
1146
          }, this));
1147
        }
1148
    });
1149

    
1150
    views.GroupedPublicNetworkView = views.PublicNetworkView.extend({
1151
        main_view_id: "grouped-public",
1152

    
1153
        initialize: function(network, view) {
1154
          this.networks = {};
1155
          this.add_network(network);
1156
          views.GroupedPublicNetworkView.__super__.initialize.call(this, 
1157
                                                                   network, 
1158
                                                                   view);
1159
        },
1160
          
1161
        get_name: function() {
1162
          return synnefo.config.grouped_public_network_name || views.GroupedPublicNetworkView.__super__.get_name.call(this);
1163
        },
1164

    
1165
        nic_in_network: function(nic) {
1166
          var nic_net  = nic.get_network();
1167
          return _.filter(this.networks, function(n) { 
1168
            return nic_net.id == n.id;
1169
          }).length > 0;
1170
        },
1171

    
1172
        get_nics: function() {
1173
          var n = _.flatten(_.map(this.networks, function(n){ return n.get_nics(); }));
1174
          return n
1175
        },
1176

    
1177
        add_network: function(net) {
1178
          this.networks[net.id] = net;
1179
        },
1180

    
1181
        remove_network: function(net) {
1182
          delete this.networks[net.id];
1183
          this.update_nics();
1184
        }
1185

    
1186
    })
1187
    
1188
    views.PrivateNetworkView = views.NetworkModelView.extend({
1189
        tpl: "#private-template",
1190
        nic_tpl: "#private-nic-template",
1191
        nic_id_tpl: "#nic-{0}"
1192
    })
1193

    
1194
    views.NetworksView = views.View.extend({
1195
        
1196
        view_id: "networks",
1197
        pane: "#networks-pane",
1198
        el: "#networks-pane",
1199

    
1200
        initialize: function() {
1201
            // elements shortcuts
1202
            this.create_cont = this.$("#networks-createcontainer");
1203
            this.container = this.$("#networks-container");
1204
            this.public_list = this.$(".public-networks");
1205
            this.private_list = this.$(".private-networks");
1206
            views.NetworksView.__super__.initialize.call(this);
1207
            this.init_handlers();
1208
            this.network_views = {};
1209
            this.public_network = false;
1210
            this.update_networks(storage.networks.models);
1211
            this.create_view = new views.NetworkCreateView();
1212
            this.connect_machines_view = new views.NetworkConnectVMsOverlay();
1213
        },
1214
        
1215
        exists: function(net) {
1216
            return this.network_views[net.id];
1217
        },
1218

    
1219
        add_or_update: function(net) {
1220
            var nv = this.exists(net);
1221
            if (!nv) {
1222
                if (net.is_public()){
1223
                  if (synnefo.config.group_public_networks) {
1224
                    if (!this.public_network) {
1225
                      // grouped public not initialized
1226
                      this.public_network = this.create_network_view(net);
1227
                    } else {
1228
                      // grouped public initialized, append
1229
                      this.public_network.add_network(net);
1230
                    }
1231
                    nv = this.public_network;
1232
                  } else {
1233
                    // no grouped view asked, fallback to default create
1234
                    nv = this.create_network_view(net);
1235
                  }
1236
                } else {
1237
                  nv = this.create_network_view(net);
1238
                }
1239

    
1240
                this.network_views[net.id] = nv;
1241
                
1242
                if (net.is_public()) {
1243
                    this.public_list.append(nv.el);
1244
                    this.public_list.show();
1245
                } else {
1246
                    this.private_list.append(nv.el);
1247
                    this.private_list.show();
1248
                }
1249
            }
1250

    
1251
            // update vms
1252
            // for cases where network servers list
1253
            // get updated after vm addition and
1254
            // vm_added_handler fails to append the
1255
            // vm to the list
1256
            nv.update_nics();
1257
            nv.update_layout();
1258
        },
1259
        
1260
        create_network_view: function(net) {
1261
            if (net.is_public()) {
1262
                if (synnefo.config.group_public_networks) {
1263
                  if (self.public_network) { return self.public_network }
1264
                  return new views.GroupedPublicNetworkView(net, this);
1265
                } else {
1266
                  return new views.PublicNetworkView(net, this);
1267
                }
1268
            }
1269
            return new views.PrivateNetworkView(net, this);
1270
        },
1271
        
1272
        init_handlers: function() {
1273
            storage.networks.bind("add", _.bind(this.network_added_handler, this, "add"));
1274
            storage.networks.bind("change", _.bind(this.network_changed_handler, this, "change"));
1275
            storage.networks.bind("reset", _.bind(this.network_changed_handler, this, "reset"));
1276
            storage.networks.bind("remove", _.bind(this.network_removed_handler, this, "remove"));
1277

    
1278
            this.$("#networkscreate").click(_.bind(function(e){
1279
              e.preventDefault();
1280
              if ($(this.$("#networkscreate")).hasClass("disabled")) { return }
1281
              this.create_view.show();
1282
            }, this));
1283
            
1284
            var self = this;
1285
            storage.networks.bind("quota_reached", function(){
1286
              self.$("#networkscreate").addClass("disabled").attr("title", 
1287
                                                            "Networks limit reached");
1288
            });
1289
            storage.networks.bind("quota_free", function(){
1290
              self.$("#networkscreate").removeClass("disabled").attr("title", 
1291
                                                            "");
1292
            });
1293
            
1294
            synnefo.ui.main.check_quotas("networks");
1295
        },
1296

    
1297
        update_networks: function(nets) {
1298
            _.each(nets, _.bind(function(net){
1299
                if (net.get("status") == "DELETED") { return };
1300
                view = this.add_or_update(net);
1301
            }, this));
1302
        },
1303

    
1304
        show: function() {
1305
            this.container.show();
1306
            $(this.el).show();
1307
        },
1308

    
1309
        network_added_handler: function(type, net) {
1310
            this.update_networks([net]);
1311
        },
1312

    
1313
        network_changed_handler: function(type, models) {
1314
            var nets = [];
1315
            if (type == "change") {
1316
                nets = [models]
1317
            } else {
1318
                nets = models.models;
1319
            }
1320

    
1321
            this.update_networks(nets)
1322
        },
1323

    
1324
        network_removed_handler: function(type, net) {
1325
            this.remove_net(net)
1326
            if (this.private_list.find(".network").length == 0) {
1327
                this.private_list.hide();
1328
            }
1329
            
1330
        },
1331

    
1332
        network_added: function(net) {
1333
            return this.network_views[net.id];
1334
        },
1335

    
1336
        get_network_view: function(net) {
1337
            return this.network_views[net.id];
1338
        },
1339

    
1340
        remove_net: function(net) {
1341
            if (this.network_added(net)) {
1342
                var view = this.get_network_view(net);
1343
                if (view == this.public_network) {
1344
                  this.public_network.remove_network(net);
1345
                } else {
1346
                  view.remove();
1347
                }
1348
                delete this.network_views[net.id];
1349
            }
1350
        },
1351

    
1352
        __update_layout: function() {
1353
        }
1354
    });
1355

    
1356
})(this);