Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_ips_view.js @ f8aa9fc3

History | View | Annotate | Download (6.2 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 views = snf.views = snf.views || {}
43
    var storage = snf.storage = snf.storage || {};
44

    
45
    views.IpPortView = views.ext.ModelView.extend({
46
      tpl: '#ip-port-view-tpl',
47
      
48
      vm_style: function() {
49
        var style = "background-image: url('{0}')";
50
        return style.format(this.get_vm_icon_path(this.model.get('vm'), 
51
                                                  'medium'));
52
      },
53

    
54
      get_vm_icon_path: function(vm, icon_type) {
55
        var os = vm.get_os();
56
        var icons = window.os_icons || views.IconView.VM_OS_ICONS;
57

    
58
        if (icons.indexOf(os) == -1) {
59
          os = "unknown";
60
        }
61

    
62
        return views.IconView.VM_OS_ICON_TPLS()[icon_type].format(os);
63
      },
64

    
65
      disconnect: function() {
66
        this.model.actions.reset_pending();
67
        this.model.set({status: 'DISCONNECTING'});
68
      }
69
    });
70

    
71
    views.IpView = views.ext.ModelView.extend({
72
      status_map: {
73
        'CONNECTED': 'Connected',
74
        'ACTIVE': 'Connected',
75
        'CONNECTING': 'Connecting',
76
        'DISCONNECTING': 'Disconnecting',
77
        'DOWN': 'Down',
78
        'DISCONNECTED': 'Not connected',
79
        'REMOVING': 'Destroying'
80
      },
81

    
82
      status_cls_map: {
83
        'CONNECTED': 'status-active',
84
        'ACTIVE': 'status-active',
85
        'CONNECTING': 'status-active',
86
        'DISCONNECTING': 'status-inactive',
87
        'DOWN': 'status-inactive',
88
        'DISCONNECTED': 'status-inactive',
89
        'UP': 'status-active',
90
        'REMOVING': 'status-progress',
91
      },
92

    
93
      tpl: '#ip-view-tpl',
94
      auto_bind: ['connect_vm'],
95
        
96
      status_cls: function() {
97
        return this.status_cls_map[this.model.get('status')];
98
      },
99

    
100
      status_display: function(v) {
101
        return this.status_map[this.model.get('status')];
102
      },
103

    
104
      show_connect_overlay: function() {
105
        this.model.actions.reset_pending();
106
        var vms = synnefo.storage.vms.models;
107
        var overlay = this.parent_view.connect_view;
108
        overlay.show_vms(this.model, vms, [], this.connect_vm);
109
      },
110
      
111
      disconnect: function(model, e) {
112
        e && e.stopPropagation();
113
        this.model.actions.reset_pending();
114
        this.model.disconnect(this.disconnect_complete)
115
      },
116

    
117
      disconnect_complete: function() {
118
        this.model.set({status: 'DISCONNECTING'})
119
      },
120

    
121
      connect_vm: function(vms) {
122
        var overlay = this.parent_view.connect_view;
123
        overlay.set_in_progress();
124
        _.each(vms, function(vm) {
125
          vm.connect_floating_ip(this.model, this.connect_complete);
126
        }, this);
127
      },
128

    
129
      connect_complete: function() {
130
        var overlay = this.parent_view.connect_view;
131
        overlay.hide();
132
        overlay.unset_in_progress();
133
      },
134

    
135
      remove: function(model, e) {
136
        e && e.stopPropagation();
137
        this.model.actions.reset_pending();
138
        this.model.destroy({
139
          success: _.bind(function() {
140
            this.model.set({status: 'REMOVING'});
141
          }, this),
142
          silent: true
143
        });
144
      }
145
    });
146
      
147
    views.IpCollectionView = views.ext.CollectionView.extend({
148
      collection: storage.floating_ips,
149
      collection_name: 'floating_ips',
150
      model_view_cls: views.IpView,
151
      create_view: views.IpCreateView,
152
      initialize: function() {
153
        views.IpCollectionView.__super__.initialize.apply(this, arguments);
154
        this.connect_view = new views.IPConnectVmOverlay();
155
      },
156

    
157
      handle_create_click: function() {
158
        network = synnefo.storage.networks.get_floating_ips_network();
159
        this.collection.create({
160
          floatingip: {}
161
        }, 
162
        {
163
          complete: _.bind(function() {
164
            this.collection.fetch();
165
        }, this)});
166
      }
167
    });
168

    
169
    views.IpsPaneView = views.ext.PaneView.extend({
170
      el: '#ips-pane',
171
      collection_view_cls: views.IpCollectionView,
172
    });
173

    
174
    views.IPConnectVmOverlay = views.NetworkConnectVMsOverlay.extend({
175
        css_class: "overlay-info connect-ip",
176
        title: "Connect IP to machine",
177
        allow_multiple: false,
178

    
179
        show_vms: function(ip, vms, selected, callback, subtitle) {
180
            views.IPConnectVmOverlay.__super__.show_vms.call(this, 
181
                  undefined, vms, selected, callback, subtitle);
182
            this.ip = ip;
183
            this.set_desc("Select machine to assign <em>" + 
184
                          ip.escape('floating_ip_address') + 
185
                          "</em> to.");
186
        },
187
        
188
        set_desc: function(desc) {
189
            this.$(".description p").html(desc);
190
        }
191

    
192
    });
193

    
194
})(this);