Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_networks_view.js @ 29cf98c9

History | View | Annotate | Download (46 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

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

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

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

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

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

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

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

    
471
        },
472

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

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

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

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

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

    
498
            if (this.firewall_view) {
499
                this.firewall_view.update_layout();
500
            }
501

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

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

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

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

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

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

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

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

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

    
600
    views.FirewallEditView = views.View.extend({
601

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

    
609
            views.FirewallEditView.__super__.initialize.apply(this);
610

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

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

    
625
            this.init_handlers();
626
            this.update_layout();
627

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

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

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

    
651
        reset_selected: function() {
652
        },
653

    
654
        submit: function() {
655
        },
656

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

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

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

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

    
691
        update_selected: function() {
692
            this.update_layout();
693
        },
694

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

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

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

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

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

    
733
    views.NetworkModelView = views.View.extend({
734
        
735
        firewall: false,
736

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

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

    
748
            this.nics_views = {};
749

    
750
            this.el = this.create_el();
751

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

    
761
            this.hide_nics_list();
762
            this.nics_list.hide();
763

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

    
775
        },
776

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

    
784

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

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

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

    
820
        init_handlers: function() {
821
            var self = this;
822

    
823

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

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

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

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

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

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

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

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

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

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

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

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

    
912
            this.parent_view.connect_machines_view.hide();
913
        },
914

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

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

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

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

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

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

    
955
            this.update_layout();
956
        },
957

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

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

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

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

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

    
1000
            this.update_nic(nic);
1001
            this.post_nic_update(nic);
1002
        },
1003

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

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

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

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

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

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

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

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

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

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

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

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

    
1089
            if (this.network.get("state") == "DESTROY") {
1090
                this.$(".spinner").show();
1091
                this.$(".state").addClass("destroying-state");
1092
                this.$(".actions").hide();
1093
            }
1094
        },
1095

    
1096
        // fix left border position
1097
        fix_left_border: function() {
1098
            if (!this.nics_visible) { return };
1099
            
1100
            var imgheight = 2783;
1101
            var opened_vm_height = 133 + 18;
1102
            var closed_vm_height = 61 + 20;
1103
            var additional_height = 25;
1104

    
1105
            if (!this.is_public) { 
1106
                imgheight = 2700;
1107
                additional_height = 65;
1108
            };
1109
            
1110
            var contents = this.$(".network-contents");
1111
            var last_vm = this.$(".network-machine:last .cont-toggler.open").length;
1112
            var last_vm_height = closed_vm_height;
1113
            if (last_vm > 0){
1114
                last_vm_height = opened_vm_height;
1115
            }
1116

    
1117
            var nics_opened = this.$(".network-machine .cont-toggler.open").length;
1118
            var nics_closed = this.$(".network-machine").length - nics_opened;
1119

    
1120
            var calc_height = (nics_opened * opened_vm_height) + (nics_closed * closed_vm_height) + additional_height; 
1121
            var bgpos = imgheight - calc_height + last_vm_height - 30;
1122
            this.$(".network-contents").css({'background-position':'33px ' + (-bgpos) + 'px'});
1123
        }
1124
    })
1125

    
1126
    views.PublicNetworkView = views.NetworkModelView.extend({
1127
        firewall: true,
1128
        tpl: "#public-template",
1129
        nic_tpl: "#public-nic-template",
1130
        nic_id_tpl: "#nic-{0}",
1131
        
1132
        initialize: function(network, view) {
1133
          views.PublicNetworkView.__super__.initialize.call(this, network, view);
1134
        },
1135

    
1136
        init_handlers: function(vm) {}
1137
    });
1138

    
1139
    views.GroupedPublicNetworkView = views.PublicNetworkView.extend({
1140
        main_view_id: "grouped-public",
1141

    
1142
        initialize: function(network, view) {
1143
          this.networks = {};
1144
          this.add_network(network);
1145
          views.GroupedPublicNetworkView.__super__.initialize.call(this, 
1146
                                                                   network, 
1147
                                                                   view);
1148
        },
1149
          
1150
        get_name: function() {
1151
          return synnefo.config.grouped_public_network_name || views.GroupedPublicNetworkView.__super__.get_name.call(this);
1152
        },
1153

    
1154
        nic_in_network: function(nic) {
1155
          var nic_net  = nic.get_network();
1156
          return _.filter(this.networks, function(n) { 
1157
            return nic_net.id == n.id;
1158
          }).length > 0;
1159
        },
1160

    
1161
        get_nics: function() {
1162
          var n = _.flatten(_.map(this.networks, function(n){ return n.get_nics(); }));
1163
          return n
1164
        },
1165

    
1166
        add_network: function(net) {
1167
          this.networks[net.id] = net;
1168
        },
1169

    
1170
        remove_network: function(net) {
1171
          delete this.networks[net.id];
1172
          this.update_nics();
1173
        }
1174

    
1175
    })
1176
    
1177
    views.PrivateNetworkView = views.NetworkModelView.extend({
1178
        tpl: "#private-template",
1179
        nic_tpl: "#private-nic-template",
1180
        nic_id_tpl: "#nic-{0}"
1181
    })
1182

    
1183
    views.NetworksView = views.View.extend({
1184
        
1185
        view_id: "networks",
1186
        pane: "#networks-pane",
1187
        el: "#networks-pane",
1188

    
1189
        initialize: function() {
1190
            // elements shortcuts
1191
            this.create_cont = this.$("#networks-createcontainer");
1192
            this.container = this.$("#networks-container");
1193
            this.public_list = this.$(".public-networks");
1194
            this.private_list = this.$(".private-networks");
1195
            views.NetworksView.__super__.initialize.call(this);
1196
            this.init_handlers();
1197
            this.network_views = {};
1198
            this.public_network = false;
1199
            this.update_networks(storage.networks.models);
1200
            this.create_view = new views.NetworkCreateView();
1201
            this.connect_machines_view = new views.NetworkConnectVMsOverlay();
1202
        },
1203
        
1204
        exists: function(net) {
1205
            return this.network_views[net.id];
1206
        },
1207

    
1208
        add_or_update: function(net) {
1209
            var nv = this.exists(net);
1210
            if (!nv) {
1211
                if (net.is_public()){
1212
                  if (synnefo.config.group_public_networks) {
1213
                    if (!this.public_network) {
1214
                      // grouped public not initialized
1215
                      this.public_network = this.create_network_view(net);
1216
                    } else {
1217
                      // grouped public initialized, append
1218
                      this.public_network.add_network(net);
1219
                    }
1220
                    nv = this.public_network;
1221
                  } else {
1222
                    // no grouped view asked, fallback to default create
1223
                    nv = this.create_network_view(net);
1224
                  }
1225
                } else {
1226
                  nv = this.create_network_view(net);
1227
                }
1228

    
1229
                this.network_views[net.id] = nv;
1230
                
1231
                if (net.is_public()) {
1232
                    this.public_list.append(nv.el);
1233
                    this.public_list.show();
1234
                } else {
1235
                    this.private_list.append(nv.el);
1236
                    this.private_list.show();
1237
                }
1238
            }
1239

    
1240
            // update vms
1241
            // for cases where network servers list
1242
            // get updated after vm addition and
1243
            // vm_added_handler fails to append the
1244
            // vm to the list
1245
            nv.update_nics();
1246
            nv.update_layout();
1247
        },
1248
        
1249
        create_network_view: function(net) {
1250
            if (net.is_public()) {
1251
                if (synnefo.config.group_public_networks) {
1252
                  if (self.public_network) { return self.public_network }
1253
                  return new views.GroupedPublicNetworkView(net, this);
1254
                } else {
1255
                  return new views.PublicNetworkView(net, this);
1256
                }
1257
            }
1258
            return new views.PrivateNetworkView(net, this);
1259
        },
1260
        
1261
        init_handlers: function() {
1262
            storage.networks.bind("add", _.bind(this.network_added_handler, this, "add"));
1263
            storage.networks.bind("change", _.bind(this.network_changed_handler, this, "change"));
1264
            storage.networks.bind("reset", _.bind(this.network_changed_handler, this, "reset"));
1265
            storage.networks.bind("remove", _.bind(this.network_removed_handler, this, "remove"));
1266

    
1267
            this.$("#networkscreate").click(_.bind(function(e){
1268
              e.preventDefault();
1269
              if ($(this.$("#networkscreate")).hasClass("disabled")) { return }
1270
              this.create_view.show();
1271
            }, this));
1272
            
1273
            var self = this;
1274
            storage.networks.bind("quota_reached", function(){
1275
              self.$("#networkscreate").addClass("disabled").attr("title", 
1276
                                                            "Networks limit reached");
1277
            });
1278
            storage.networks.bind("quota_free", function(){
1279
              self.$("#networkscreate").removeClass("disabled").attr("title", 
1280
                                                            "");
1281
            });
1282
            
1283
            synnefo.ui.main.check_quotas("networks");
1284
        },
1285

    
1286
        update_networks: function(nets) {
1287
            _.each(nets, _.bind(function(net){
1288
                if (net.get("status") == "DELETED") { return };
1289
                view = this.add_or_update(net);
1290
            }, this));
1291
        },
1292

    
1293
        show: function() {
1294
            this.container.show();
1295
            $(this.el).show();
1296
        },
1297

    
1298
        network_added_handler: function(type, net) {
1299
            this.update_networks([net]);
1300
        },
1301

    
1302
        network_changed_handler: function(type, models) {
1303
            var nets = [];
1304
            if (type == "change") {
1305
                nets = [models]
1306
            } else {
1307
                nets = models.models;
1308
            }
1309

    
1310
            this.update_networks(nets)
1311
        },
1312

    
1313
        network_removed_handler: function(type, net) {
1314
            this.remove_net(net)
1315
            if (this.private_list.find(".network").length == 0) {
1316
                this.private_list.hide();
1317
            }
1318
            
1319
        },
1320

    
1321
        network_added: function(net) {
1322
            return this.network_views[net.id];
1323
        },
1324

    
1325
        get_network_view: function(net) {
1326
            return this.network_views[net.id];
1327
        },
1328

    
1329
        remove_net: function(net) {
1330
            if (this.network_added(net)) {
1331
                var view = this.get_network_view(net);
1332
                if (view == this.public_network) {
1333
                  this.public_network.remove_network(net);
1334
                } else {
1335
                  view.remove();
1336
                }
1337
                delete this.network_views[net.id];
1338
            }
1339
        },
1340

    
1341
        __update_layout: function() {
1342
        }
1343
    });
1344

    
1345
})(this);