Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.7 kB)

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

    
35
;(function(root){
36
    
37
    // root
38
    var root = root;
39
    
40
    // setup namepsaces
41
    var snf = root.synnefo = root.synnefo || {};
42
    var 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
        if (!this.model.get('vm')) { return }
51
        return style.format(this.get_vm_icon_path(this.model.get('vm'), 
52
                                                  'medium'));
53
      },
54

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

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

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

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

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

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

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

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

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

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

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

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

    
136
      remove: function(model, e) {
137
        e && e.stopPropagation();
138
        this.model.actions.reset_pending();
139
        this.model.destroy({
140
          success: _.bind(function() {
141
            this.model.set({status: 'REMOVING'});
142
          }, this),
143
          silent: true
144
        });
145
      }
146
    });
147
      
148
    views.IpCollectionView = views.ext.CollectionView.extend({
149
      collection: storage.floating_ips,
150
      collection_name: 'floating_ips',
151
      model_view_cls: views.IpView,
152
      create_view: undefined, // no create overlay for IPs
153
      initialize: function() {
154
        views.IpCollectionView.__super__.initialize.apply(this, arguments);
155
        this.connect_view = new views.IPConnectVmOverlay();
156
        this.creating = false;
157
      },
158
      
159
      set_creating: function() {
160
        this.creating = true;
161
        this.create_button.addClass("in-progress");
162
      },
163
      
164
      reset_creating: function() {
165
        this.creating = false;
166
        this.create_button.removeClass("in-progress");
167
      },
168

    
169
      handle_create_click: function() {
170
        if (this.creating) { 
171
          return
172
        }
173

    
174
        this.set_creating();
175
        network = synnefo.storage.networks.get_floating_ips_network();
176
        this.collection.create({
177
          floatingip: {}
178
        }, 
179
        {
180
          complete: _.bind(function() {
181
            this.creating = false;
182
            this.reset_creating();
183
            this.collection.fetch();
184
        }, this)});
185
      }
186
    });
187

    
188
    views.IpsPaneView = views.ext.PaneView.extend({
189
      el: '#ips-pane',
190
      collection_view_cls: views.IpCollectionView,
191
    });
192

    
193
    views.IPConnectVmOverlay = views.NetworkConnectVMsOverlay.extend({
194
        css_class: "overlay-info connect-ip",
195
        title: "Connect IP to machine",
196
        allow_multiple: false,
197

    
198
        show_vms: function(ip, vms, selected, callback, subtitle) {
199
            views.IPConnectVmOverlay.__super__.show_vms.call(this, 
200
                  undefined, vms, selected, callback, subtitle);
201
            this.ip = ip;
202
            this.set_desc("Select machine to assign <em>" + 
203
                          ip.escape('floating_ip_address') + 
204
                          "</em> to.");
205
        },
206
        
207
        set_desc: function(desc) {
208
            this.$(".description p").html(desc);
209
        }
210

    
211
    });
212

    
213
})(this);