Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.4 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 cls, icon_state;
50
        var style = "background-image: url('{0}')";
51
        var vm = this.model.get('vm')
52
        if (!vm) { return }
53
        this.$(".model-logo").removeClass("state1 state2 state3 state4");
54
        icon_state = vm.is_active() ? "on" : "off";
55
        if (icon_state == "on") {
56
          cls = "state1"
57
        } else {
58
          cls = "state2"
59
        }
60
        this.$(".model-logo").addClass(cls);
61
        return style.format(this.get_vm_icon_path(this.model.get('vm'), 
62
                                                  'medium2'));
63
      },
64

    
65
      get_vm_icon_path: function(vm, icon_type) {
66
        var os = vm.get_os();
67
        var icons = window.os_icons || views.IconView.VM_OS_ICONS;
68

    
69
        if (icons.indexOf(os) == -1) {
70
          os = "unknown";
71
        }
72

    
73
        return views.IconView.VM_OS_ICON_TPLS()[icon_type].format(os);
74
      },
75

    
76
      disconnect: function() {
77
        this.model.actions.reset_pending();
78
        this.model.set({status: 'DISCONNECTING'});
79
      }
80
    });
81

    
82
    views.IpView = views.ext.ModelView.extend({
83
      status_map: {
84
        'CONNECTED': 'In use',
85
        'ACTIVE': 'In use',
86
        'CONNECTING': 'Attaching',
87
        'DISCONNECTING': 'Detaching',
88
        'DOWN': 'In use',
89
        'DISCONNECTED': 'Available',
90
        'REMOVING': 'Destroying'
91
      },
92

    
93
      status_cls_map: {
94
        'CONNECTED': 'status-active',
95
        'ACTIVE': 'status-active',
96
        'CONNECTING': 'status-progress',
97
        'DISCONNECTING': 'status-progress',
98
        'DOWN': 'status-inactive',
99
        'DISCONNECTED': 'status-inactive',
100
        'UP': 'status-active',
101
        'REMOVING': 'status-progress destroying-state',
102
      },
103

    
104
      tpl: '#ip-view-tpl',
105
      auto_bind: ['connect_vm'],
106
        
107
      status_cls: function() {
108
        return this.status_cls_map[this.model.get('status')];
109
      },
110

    
111
      status_display: function(v) {
112
        return this.status_map[this.model.get('status')];
113
      },
114
      
115
      model_icon: function() {
116
        var img = 'ip-icon-detached.png';
117
        var src = synnefo.config.images_url + '/{0}';
118
        if (this.model.get('port')) {
119
          img = 'ip-icon.png';
120
        }
121
        return src.format(img);
122
      },
123

    
124
      show_connect_overlay: function() {
125
        this.model.actions.reset_pending();
126
        var vms = this.model.get("network").connectable_vms;
127
        var overlay = this.parent_view.connect_view;
128
        overlay.show_vms(this.model, vms, [], this.connect_vm);
129
      },
130
      
131
      disconnect: function(model, e) {
132
        e && e.stopPropagation();
133
        this.model.actions.reset_pending();
134
        this.model.disconnect(this.disconnect_complete)
135
      },
136

    
137
      disconnect_complete: function() {
138
      },
139

    
140
      connect_vm: function(vms) {
141
        var overlay = this.parent_view.connect_view;
142
        overlay.set_in_progress();
143
        _.each(vms, function(vm) {
144
          vm.connect_floating_ip(this.model, this.connect_complete);
145
        }, this);
146
      },
147

    
148
      connect_complete: function() {
149
        var overlay = this.parent_view.connect_view;
150
        overlay.hide();
151
        overlay.unset_in_progress();
152
        this.model.set({'status': 'CONNECTING'});
153
      },
154

    
155
      remove: function(model, e) {
156
        e && e.stopPropagation();
157
        this.model.actions.reset_pending();
158
        this.model.destroy({
159
          success: _.bind(function() {
160
            this.model.set({status: 'REMOVING'});
161
          }, this),
162
          silent: true
163
        });
164
      }
165
    });
166
      
167
    views.IpCollectionView = views.ext.CollectionView.extend({
168
      collection: storage.floating_ips,
169
      collection_name: 'floating_ips',
170
      model_view_cls: views.IpView,
171
      create_view: undefined, // no create overlay for IPs
172
      quota_key: 'cyclades.floating_ip',
173
      initialize: function() {
174
        views.IpCollectionView.__super__.initialize.apply(this, arguments);
175
        this.connect_view = new views.IPConnectVmOverlay();
176
        this.creating = false;
177
      },
178
      
179
      set_creating: function() {
180
        this.creating = true;
181
        this.create_button.addClass("in-progress");
182
      },
183
      
184
      reset_creating: function() {
185
        this.creating = false;
186
        this.create_button.removeClass("in-progress");
187
      },
188

    
189
      handle_create_click: function() {
190
        if (this.creating) { 
191
          return
192
        }
193

    
194
        this.set_creating();
195
        network = synnefo.storage.networks.get_floating_ips_network();
196
        this.collection.create({
197
          floatingip: {}
198
        }, 
199
        {
200
          success: _.bind(function() {
201
            this.post_create();
202
          }, this),
203
          complete: _.bind(function() {
204
            this.creating = false;
205
            this.reset_creating();
206
            this.collection.fetch();
207
        }, this)});
208
      }
209
    });
210

    
211
    views.IpsPaneView = views.ext.PaneView.extend({
212
      el: '#ips-pane',
213
      collection_view_cls: views.IpCollectionView,
214
    });
215

    
216
    views.IPConnectVmOverlay = views.NetworkConnectVMsOverlay.extend({
217
        css_class: "overlay-info connect-ip",
218
        title: "Connect IP to machine",
219
        allow_multiple: false,
220

    
221
        show_vms: function(ip, vms, selected, callback, subtitle) {
222
            views.IPConnectVmOverlay.__super__.show_vms.call(this, 
223
                  undefined, vms, selected, callback, subtitle);
224
            this.ip = ip;
225
            this.set_desc("Select machine to assign <em>" + 
226
                          ip.escape('floating_ip_address') + 
227
                          "</em> to.");
228
        },
229
        
230
        set_desc: function(desc) {
231
            this.$(".description p").html(desc);
232
        }
233

    
234
    });
235

    
236
})(this);