Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (22.5 kB)

1
// Copyright 2013 GRNET S.A. All rights reserved.
2
// 
3
// Redistribution and use in source and binary forms, with or
4
// without modification, are permitted provided that the following
5
// conditions are met:
6
// 
7
//   1. Redistributions of source code must retain the above
8
//      copyright notice, this list of conditions and the following
9
//      disclaimer.
10
// 
11
//   2. Redistributions in binary form must reproduce the above
12
//      copyright notice, this list of conditions and the following
13
//      disclaimer in the documentation and/or other materials
14
//      provided with the distribution.
15
// 
16
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
// POSSIBILITY OF SUCH DAMAGE.
28
// 
29
// The views and conclusions contained in the software and
30
// documentation are those of the authors and should not be
31
// interpreted as representing official policies, either expressed
32
// or implied, of GRNET S.A.
33
// 
34

    
35
;(function(root){
36
    
37
    // root
38
    var root = root;
39
    
40
    // setup namepsaces
41
    var snf = root.synnefo = root.synnefo || {};
42
    var models = snf.models = snf.models || {}
43
    var storage = snf.storage = snf.storage || {};
44
    var ui = snf.ui = snf.ui || {};
45
    var util = snf.util || {};
46
    var views = snf.views = snf.views || {}
47

    
48
    // shortcuts
49
    var bb = root.Backbone;
50
    
51
    // logging
52
    var logger = new snf.logging.logger("SNF-VIEWS");
53
    var debug = _.bind(logger.debug, logger);
54
    
55
    views.NetworkCreateView = views.Overlay.extend({
56
        view_id: "network_create_view",
57
        content_selector: "#networks-create-content",
58
        css_class: 'overlay-networks-create overlay-info',
59
        overlay_id: "network-create-overlay",
60

    
61
        title: "Create new private network",
62
        subtitle: "Networks",
63

    
64
        initialize: function(options) {
65
            views.NetworkCreateView.__super__.initialize.apply(this);
66

    
67
            this.create_button = this.$("form .form-action.create");
68
            this.text = this.$(".network-create-name");
69
            this.form = this.$("form");
70

    
71
            this.dhcp_select = this.$("#network-create-dhcp");
72
            this.type_select = this.$("#network-create-type");
73
            this.subnet_select = this.$("#network-create-subnet");
74
            this.subnet_custom = this.$("#network-create-subnet-custom");
75
                
76
            this.dhcp_form = this.$("#network-create-dhcp-fields");
77
            
78
            this.subnet_select.find(".subnet").remove();
79
            _.each(synnefo.config.network_suggested_subnets, function(subnet){
80
                this.subnet_select.append($('<option value='+subnet+' class="subnet">'+subnet+'</option>'));
81
            }, this);
82

    
83
            this.type_select.find(".subnet").remove();
84
            _.each(synnefo.config.network_available_types, function(name, value){
85
                this.type_select.append($('<option value='+value+' class="subnet">'+name+'</option>'));
86
            }, this);
87
            
88
            if (_.keys(synnefo.config.network_available_types).length <= 1) {
89
                this.type_select.closest(".form-field").hide();
90
            }
91

    
92
            this.check_dhcp_form();
93
            this.init_handlers();
94
        },
95

    
96
        reset_dhcp_form: function() {
97
          this.subnet_select.find("option")[0].selected = 1;
98
          this.subnet_custom.val("");
99
        },
100

    
101
        check_dhcp_form: function() {
102
            if (this.dhcp_select.is(":checked")) {
103
                this.dhcp_form.show();
104
            } else {
105
                this.dhcp_form.hide();
106
            }
107
            
108
            if (this.subnet_select.val() == "custom") {
109
                this.subnet_custom.show();
110
            } else {
111
                this.subnet_custom.hide();
112
            }
113
        },
114

    
115
        init_handlers: function() {
116

    
117
            this.dhcp_select.click(_.bind(function(e){
118
                this.check_dhcp_form();
119
                this.reset_dhcp_form();
120
            }, this));
121

    
122
            this.subnet_select.change(_.bind(function(e){
123
                this.check_dhcp_form();
124
                if (this.subnet_custom.is(":visible")) {
125
                    this.subnet_custom.focus();
126
                }
127
            }, this));
128

    
129
            this.create_button.click(_.bind(function(e){
130
                this.submit();
131
            }, this));
132

    
133
            this.form.submit(_.bind(function(e){
134
                e.preventDefault();
135
                this.submit;
136
                return false;
137
            }, this))
138

    
139
            this.text.keypress(_.bind(function(e){
140
                if (e.which == 13) {this.submit()};
141
            },this))
142
        },
143

    
144
        submit: function() {
145
            if (this.validate()) {
146
                this.create();
147
            };
148
        },
149
        
150
        validate: function() {
151
            // sanitazie
152
            var t = this.text.val();
153
            t = t.replace(/^\s+|\s+$/g,"");
154
            this.text.val(t);
155

    
156
            if (this.text.val() == "") {
157
                this.text.closest(".form-field").addClass("error");
158
                this.text.focus();
159
                return false;
160
            } else {
161
                this.text.closest(".form-field").removeClass("error");
162
            }
163
            
164
            if (this.dhcp_select.is(":checked")) {
165
                if (this.subnet_select.val() == "custom") {
166
                    var sub = this.subnet_custom.val();
167
                    sub = sub.replace(/^\s+|\s+$/g,"");
168
                    this.subnet_custom.val(sub);
169
                        
170
                    if (!synnefo.util.IP_REGEX.exec(this.subnet_custom.val())) {
171
                        this.subnet_custom.closest(".form-field").prev().addClass("error");
172
                        return false;
173
                    } else {
174
                        this.subnet_custom.closest(".form-field").prev().removeClass("error");
175
                    }
176
                };
177
            }
178

    
179
            return true;
180
        },
181
        
182
        get_next_available_subnet: function() {
183
            var auto_tpl = synnefo.config.automatic_network_range_format;
184
            if (!auto_tpl) {
185
                return null
186
            }
187
            var index = 0;
188
            var subnet = auto_tpl.format(index);
189
            var networks = synnefo.storage.networks;
190
            var check_existing = function(n) { return n.get('cidr') == subnet }
191
            while (networks.filter(check_existing).length > 0 && index <= 255) {
192
                index++;
193
                subnet = auto_tpl.format(index); 
194
            }
195
            return subnet;
196
        },
197

    
198
        create: function() {
199
            this.create_button.addClass("in-progress");
200

    
201
            var name = this.text.val();
202
            var dhcp = this.dhcp_select.is(":checked");
203
            var subnet = null;
204
            var type = this.type_select.val();
205

    
206
            if (dhcp) {
207
                if (this.subnet_select.val() == "custom") {
208
                    subnet = this.subnet_custom.val();
209
                } else if (this.subnet_select.val() == "auto") {
210
                    subnet = this.get_next_available_subnet()
211
                } else {
212
                    subnet = this.subnet_select.val();
213
                }
214
                
215
            }
216

    
217
            snf.storage.networks.create(name, type, subnet, dhcp, _.bind(function(){
218
                this.hide();
219
            }, this));
220
        },
221

    
222
        beforeOpen: function() {
223
            this.create_button.removeClass("in-progress")
224
            this.text.closest(".form-field").removeClass("error");
225
            this.text.val("");
226
            this.text.show();
227
            this.text.focus();
228
            this.subnet_custom.val("");
229
            this.subnet_select.val("auto");
230
            this.dhcp_select.attr("checked", true);
231
            this.type_select.val(_.keys(synnefo.config.network_available_types)[0]);
232
            this.check_dhcp_form();
233
        },
234

    
235
        onOpen: function() {
236
            this.text.focus();
237
        }
238
    });
239

    
240
    views.NetworkPortView = views.ext.ModelView.extend({
241
      tpl: '#network-port-view-tpl',
242
      
243
      vm_logo_url: function(vm) {
244
        if (!this.model.get('vm')) { return '' }
245
        return synnefo.ui.helpers.vm_icon_path(this.model.get('vm'), 'medium');
246
      },
247
      
248
      set_confirm: function() {
249
        var parent = this.parent_view.parent_view.el;
250
        parent.addClass("subactionpending");
251
      },
252

    
253
      unset_confirm: function() {
254
        var parent = this.parent_view.parent_view.el;
255
        parent.removeClass("subactionpending");
256
      },
257

    
258
      post_init_element: function() {
259
        this.in_progress = false;
260
        this.firewall = this.$(".firewall-content").hide();
261
        this.firewall_toggler = this.$(".firewall-toggle");
262
        this.firewall_apply = this.$(".firewall-apply");
263
        this.firewall_legends = this.firewall.find(".checkbox-legends");
264
        this.firewall_inputs = this.firewall.find("input");
265
        this.firewall_apply = this.firewall.find("button");
266
        this.firewall_visible = false;
267

    
268
        this.firewall_toggler.click(_.bind(function() {
269
          this.toggle_firewall();
270
        }, this));
271

    
272
        this.firewall.find(".checkbox-legends, input").click(
273
          _.bind(this.handle_firewall_choice_click, this));
274
        this.update_firewall();
275
      },
276
      
277
      toggle_firewall: function(e, hide, cb) {
278
          hide = hide === undefined ? false : hide;
279
          if (hide) {
280
            this.firewall.stop().hide();
281
          } else {
282
            if (!cb) { cb = function() {}}
283
            this.firewall.slideToggle(cb);
284
          }
285
          this.firewall_toggler.toggleClass("open");
286
          this.firewall_visible = this.firewall_toggler.hasClass("open");
287
          if (!this.firewall_visible) {
288
            this.firewall_apply.fadeOut(50);
289
          }
290
          this.update_firewall();
291
      },
292
    
293
      post_hide: function() {
294
        views.NetworkPortView.__super__.post_hide.apply(this);
295
        if (this.firewall_visible) {
296
          this.toggle_firewall({}, true);
297
        }
298
      },
299

    
300
      handle_firewall_choice_click: function(e) {
301
          var el = $(e.currentTarget);
302
          if (el.get(0).tagName == "INPUT") {
303
            el = el.next();
304
          }
305
          var current = this.model.get("firewall_status");
306
          var selected = el.prev().val();
307

    
308
          el.parent().find("input").attr("checked", false);
309
          el.prev().attr("checked", true);
310

    
311
          if (selected != current) {
312
            this.firewall_apply.show();
313
          } else {
314
            this.firewall_apply.hide();
315
          }
316
      },
317
      
318
      disconnect_port: function(model, e) {
319
        e && e.stopPropagation();
320
        this.model.actions.reset_pending();
321
        this.model.disconnect(_.bind(this.disconnect_port_complete, this));
322
      },
323

    
324
      disconnect_port_complete: function() {
325
      },
326

    
327
      set_firewall: function() {
328
        var value = this.get_selected_value();
329
        this.firewall_apply.addClass("in-progress");
330
        this.model.set({'pending_firewall': value});
331
        this.model.set_firewall(value, this.set_firewall_complete, 
332
                                       this.set_firewall_complete);
333
        this.in_progress = true;
334
      },
335
      
336
      set_firewall_complete: function() {
337
        this.in_progress = false;
338
        this.toggle_firewall({}, false, _.bind(function() {
339
          this.firewall_apply.removeClass("in-progress").show();
340
        }, this));
341
      },
342

    
343
      get_selected_value: function() {
344
        return this.firewall_inputs.filter(":checked").val();
345
      },
346

    
347
      update_firewall: function() {
348
        var value = this.model.get("firewall_status");
349
        var value_selector = "[value=" + value + "]"
350
        var status_span = this.firewall_toggler.find("span span");
351
        var current_choice = this.firewall_inputs.filter(value_selector);
352

    
353
        if (_.contains(["PROTECTED", "ENABLED"], value)) {
354
          status_span.removeClass("firewall-off").addClass("firewall-on");
355
          status_span.text("On");
356
        } else {
357
          status_span.removeClass("firewall-on").addClass("firewall-off");
358
          status_span.text("Off");
359
        }
360
        
361
        this.firewall_inputs.attr("checked", false);
362
        this.firewall_legends.removeClass("current");
363
        current_choice.attr("checked", true)
364
        current_choice.next().addClass("current");
365
      },
366

    
367
      show_vm_details: function() {
368
        var vm = this.model.get('vm');
369
        if (vm) { snf.ui.main.show_vm_details(vm) }
370
      }
371
    });
372

    
373
    views.NetworkPortCollectionView = views.ext.CollectionView.extend({
374
      tpl: '#network-port-collection-view-tpl',
375
      model_view_cls: views.NetworkPortView,
376
      rivets_view: true,
377
      get_rivet_object: function() {
378
        return {
379
          model: this.collection.network
380
        }
381
      },
382
      resolve_storage_object: function() {
383
        return this.collection
384
      },
385

    
386
      show_connect_vms_overlay: function() {
387
        this.parent_view.show_connect_vms_overlay();
388
      }
389
    });
390

    
391
    views.NetworkView = views.ext.ModelView.extend({
392
      tpl: '#network-view-tpl',
393
      auto_bind: ['connect_vm'],
394
      post_init_element: function() {
395
        this.ports = this.$(".ports.nested-model-list");
396
        this.ports.hide();
397
        this.ports_toggler = this.$(".network-ports-toggler");
398
        this.ports_toggler.click(this.toggle_ports);
399
        this.ports_visible = false;
400
      },
401

    
402
      toggle_ports: function(e, hide) {
403
        hide = hide === undefined ? false : hide;
404
        if (hide) {
405
          this.ports.stop().hide();
406
        } else {
407
          this.ports.stop().slideToggle();
408
        }
409
        this.ports_toggler.find(".cont-toggler").toggleClass("open");
410
        this.ports_visible = this.ports_toggler.find(".cont-toggler").hasClass("open");
411
      },
412

    
413
      post_hide: function() {
414
        views.NetworkView.__super__.post_hide.apply(this);
415
        if (this.ports_visible) {
416
          this.toggle_ports({}, true);
417
        }
418
      },
419
      
420
      status_map: {
421
        'ACTIVE': 'Active',
422
        'CONNECTING': 'Connecting',
423
        'DISCONNECTING': 'Disconnecting',
424
        'REMOVING': 'Removing'
425
      },
426

    
427
      status_cls_map: {
428
        'ACTIVE': 'status-active',
429
        'DISCONNECTING': 'status-progress',
430
        'CONNECTING': 'status-progress',
431
        'REMOVING': 'status-progress'
432
      },
433
      
434
      status_cls: function(status) {    
435
        return this.status_cls_map[this.model.get('ext_status')]
436
      },
437

    
438
      status_display: function(status) {
439
        var status;
440
        if (this.model.id == "snf-combined-public-network") {
441
          return "Internet"
442
        }
443

    
444
        status = this.status_map[this.model.get('ext_status')];
445
        return status;
446
      },
447
      
448
      connect_vms: function(vms, cb) {
449
        var finished = 0;
450
        var completed = function() {
451
          finished++;
452
          if (finished == vms.length) {
453
            cb();
454
          }
455
        }
456
        _.each(vms, function(vm) {
457
          this.model.connect_vm(vm, completed);
458
        }, this);
459
      },
460
      
461
      remove: function(model, e) {
462
        e && e.stopPropagation();
463
        this.model.actions.reset_pending();
464
        this.model.destroy({
465
          complete: _.bind(function() {
466
            this.model.set({status: 'REMOVING'})
467
            this.model.set({ext_status: 'REMOVING'})
468
          }, this),
469
          silent: true
470
        });
471
      },
472

    
473
      show_connect_vms_overlay: function() {
474
        var view = new views.NetworkConnectVMsOverlay();
475
        vms = this.model.pluggable_vms();
476
        var cb = _.bind(function(vms) {
477
          view.set_in_progress();
478
          var cbinner = function() {
479
            view.hide();
480
            delete view;
481
          }
482
          this.connect_vms(vms, cbinner);
483
        }, this);
484
        view.show_vms(this.model, vms, [], cb, "subtitle");
485
      }
486

    
487
    });
488
    
489
    views.NetworksCollectionView = views.ext.CollectionView.extend({
490
      collection: storage.networks,
491
      collection_name: 'networks',
492
      model_view_cls: views.NetworkView,
493
      create_view_cls: views.NetworkCreateView,
494
      
495
      init: function() {
496
        this.public_added = false;
497
        views.NetworksCollectionView.__super__.init.apply(this, arguments);
498
      },
499
      
500
      check_empty: function() {
501
        views.NetworksCollectionView.__super__.check_empty.apply(this, arguments);
502
        //if (this.$(".private").children().length == 0) {
503
          //this.$(".private").hide();
504
        //} else {
505
          //this.$(".private").show();
506
        //}
507
      },
508

    
509
      add_model: function(m) {
510
        if (m.get('is_public') && !this.public_added) {
511
          this.combined_public = new models.networks.CombinedPublicNetwork();
512
          this.combined_public_view = new views.NetworkView({
513
            model: this.combined_public
514
          });
515
          this.add_model_view(this.combined_public_view, this.combined_public, 0);
516
          this.combined_public_view.$("i").hide();
517
          this.public_added = true;
518
        }
519
        return views.NetworksCollectionView.__super__.add_model.call(this, m);
520
      },
521

    
522
      remove_model: function(m) {
523
        if (m.id == 'snf-combined-public-network') {
524
          return;
525
        } else {
526
          return views.NetworksCollectionView.__super__.remove_model.call(this, m);
527
        }
528
      },
529

    
530
      get_model_view_cls: function(m) {
531
        if (!this.public_added) {
532
        }
533
        if (m.get('is_public')) {
534
          return false;
535
        }
536
        return views.NetworksCollectionView.__super__.get_model_view_cls.apply(this, [m]);
537
      },
538
      
539
      parent_for_model: function(m) {
540
        if (m.get('is_public')) {
541
          return this.list_el.find(".public");
542
        } else {
543
          return this.list_el.find(".private");
544
        }
545
      }
546
    });
547

    
548
    views.NetworksPaneView = views.ext.PaneView.extend({
549
      id: "pane",
550
      el: '#networks-pane',
551
      collection_view_cls: views.NetworksCollectionView,
552
      collection_view_selector: '#networks-list-view'
553
    });
554

    
555
    views.NetworkConnectVMsOverlay = views.Overlay.extend({
556
        title: "Connect machine",
557
        overlay_id: "overlay-select-vms",
558
        content_selector: "#network-vms-select-content",
559
        css_class: "overlay-info",
560
        allow_multiple: true,
561

    
562
        initialize: function() {
563
            views.NetworkConnectVMsOverlay.__super__.initialize.apply(this);
564
            this.list = this.$(".vms-list ul");
565
            this.empty_message = this.$(".empty-message");
566
            // flag for submit handler to avoid duplicate bindings
567
            this.submit_handler_set = false;
568
            this.in_progress = false;
569
        },
570
        
571
        handle_vm_click: function(el) {
572
            if (!this.allow_multiple) {
573
              $(el).closest("ul").find(".selected").removeClass("selected");
574
              $(el).addClass("selected");
575
            } else {
576
              $(el).toggleClass("selected");
577
            }
578
        },
579

    
580
        init_handlers: function() {
581
            var self = this;
582
            this.list.find("li").click(function() {
583
                self.handle_vm_click($(this));
584
            });
585
            
586
            if (!this.submit_handler_set) {
587
                // avoid duplicate submits
588
                this.el.find(".create, .assign").click(_.bind(function() {
589
                  if (!this.in_progress) {
590
                    this.submit();
591
                  }
592
                }, this));
593
                this.submit_handler_set = true;
594
            }
595
        },
596

    
597
        reset: function() {
598
            this.list.find("li").remove();
599
        },
600

    
601
        beforeOpen: function() {
602
            this.reset();
603
            this.update_layout();
604
        },
605
        
606
        vm: function(vm) {
607
            if (vm.id) { var id = vm.id } else {var id = vm}
608
            return this.list.find(".vm-" + id);
609
        },
610

    
611
        get_selected: function() {
612
            return this.list.find(".selected").map(function() {return $(this).data('vm')})
613
        },
614

    
615
        update_layout: function() {
616
            this.unset_in_progress();
617
            this.in_progress = false;
618

    
619
            if (this.vms.length == 0) {
620
                this.empty_message.show();
621
            } else {
622
                this.empty_message.hide();
623
            }
624

    
625
            _.each(this.vms, _.bind(function(vm){
626
                var html = '<li class="vm option options-object vm-{0}">' +
627
                           '<div class="options-object-cont">' +
628
                           '{2}' + 
629
                           '<span class="title">{1}</span>' + 
630
                           '<span class="value">{3}</span></div>' + 
631
                           '</li>';
632
                var el = $(html.format(vm.id, 
633
                       util.truncate(_.escape(vm.get("name")), 23), 
634
                       snf.ui.helpers.vm_icon_tag(vm, "small", {'class':'os'}),
635
                       _.escape(vm.get_os())
636
                ));
637
                el.data({vm:vm, vm_id:vm.id});
638
                this.list.append(el);
639

    
640
                vm.bind("remove", function(){ el.remove()})
641
                vm.bind("change:name", function(i,v){el.find(".title").text(v)})
642
            }, this));
643
            
644
            this.init_handlers();
645
            this.set_selected();
646
        },
647

    
648
        set_selected: function() {
649
            _.each(this.selected, _.bind(function(el){
650
                this.vm(el).addClass("selected");
651
            }, this));
652
        },
653
        
654
        set_in_progress: function() {
655
          this.$(".form-action").addClass("in-progress");
656
          this.in_progress = true;
657
        },
658

    
659
        unset_in_progress: function() {
660
          this.$(".form-action").removeClass("in-progress");
661
          this.in_progress = false;
662
        },
663

    
664
        show_vms: function(network, vms, selected, callback, subtitle) {
665
            this.network = network;
666
            this.reset();
667
            if (network) {
668
              this.set_subtitle(network.escape("name"));
669
            } else {
670
              this.set_subtitle(subtitle);
671
            }
672

    
673
            this.vms = vms;
674
            this.selected = selected;
675
            this.cb = callback;
676
            this.unset_in_progress();
677
            this.show(true);
678
        },
679

    
680
        submit: function() {
681
            if (!this.get_selected().length) { return }
682
            this.cb(this.get_selected());
683
        }
684
    });
685
 
686
})(this);