Revision 198b546d snf-cyclades-app/synnefo/ui/static/snf/js/ui/web/ui_ips_view.js

b/snf-cyclades-app/synnefo/ui/static/snf/js/ui/web/ui_ips_view.js
1
// Copyright 2011 GRNET S.A. All rights reserved.
1
// Copyright 2014 GRNET S.A. All rights reserved.
2 2
// 
3 3
// Redistribution and use in source and binary forms, with or
4 4
// without modification, are permitted provided that the following
......
41 41
    var snf = root.synnefo = root.synnefo || {};
42 42
    var views = snf.views = snf.views || {}
43 43
    var storage = snf.storage = snf.storage || {};
44
    var util = snf.util = snf.util || {};
44 45

  
45 46
    views.IpPortView = views.ext.ModelView.extend({
46 47
      tpl: '#ip-port-view-tpl',
......
119 120
      tpl: '#ip-view-tpl',
120 121
      auto_bind: ['connect_vm'],
121 122
        
123
      show_reassign_view: function() {
124
          synnefo.ui.main.ip_reassign_view.show(this.model);
125
      },
126

  
122 127
      status_cls: function() {
123 128
        return this.status_cls_map[this.model.get('status')];
124 129
      },
......
127 132
        return this.status_map[this.model.get('status')];
128 133
      },
129 134
      
135
      show_reassign_view: function() {
136
          synnefo.ui.main.ip_reassign_view.show(this.model);
137
      },
138

  
130 139
      model_icon: function() {
131 140
        var img = 'ip-icon-detached.png';
132 141
        var src = synnefo.config.images_url + '/{0}';
......
177 186
      }
178 187
    });
179 188
      
189
    views.FloatingIPCreateView = views.Overlay.extend({
190
        view_id: "ip_create_view",
191
        content_selector: "#ips-create-content",
192
        css_class: 'overlay-ip-create overlay-info',
193
        overlay_id: "ip-create-overlay",
194

  
195
        title: "Create new IP address",
196
        subtitle: "IP addresses",
197

  
198
        initialize: function(options) {
199
            views.FloatingIPCreateView.__super__.initialize.apply(this);
200

  
201
            this.create_button = this.$("form .form-action.create");
202
            this.form = this.$("form");
203
            this.project_select = this.$(".project-select");
204
            this.init_handlers();
205
        },
206

  
207
        init_handlers: function() {
208
            this.create_button.click(_.bind(function(e){
209
                this.submit();
210
            }, this));
211

  
212
            this.form.submit(_.bind(function(e){
213
                e.preventDefault();
214
                this.submit();
215
                return false;
216
            }, this))
217
        },
218

  
219
        submit: function() {
220
            if (this.validate()) {
221
                this.create();
222
            };
223
        },
224
        
225
        validate: function() {
226
            var project = this.get_project();
227
            if (!project || !project.quotas.can_fit({'cyclades.floating_ip': 1})) {
228
                this.project_select.closest(".form-field").addClass("error");
229
                this.project_select.focus();
230
                return false;
231
            }
232
            return true;
233
        },
234
        
235
        create: function() {
236
            if (this.create_button.hasClass("in-progress")) { return }
237
            this.create_button.addClass("in-progress");
238

  
239
            var project_id = this.project_select.val();
240
            var project = synnefo.storage.projects.get(project_id);
241

  
242

  
243
            var cb = _.bind(function() { 
244
              synnefo.api.trigger("quota:update");
245
              this.hide(); 
246
            }, this);
247

  
248
            snf.storage.floating_ips.create({
249
                floatingip: {
250
                  project: project_id
251
                }
252
              }, 
253
              { 
254
                complete: cb 
255
              });
256
        },
257
        
258
        update_projects: function() {
259
          this.project_select.find("option").remove();
260
          var min_ip_quota = {'cyclades.floating_ip': 1}
261
          synnefo.storage.projects.each(function(project){
262
            var el = $("<option></option>");
263
            el.attr("value", project.id);
264
            var project_name = util.truncate(project.get('name'), 34);
265
            var name = '{0} ({1} available)'.format(project_name, 
266
              project.quotas.get('cyclades.floating_ip').get('available'));
267
            el.text(name);
268
            if (!project.quotas.can_fit(min_ip_quota)) {
269
              el.attr('disabled', true);
270
            }
271
            this.project_select.append(el);
272
          }, this);
273
        },
274
        
275
        get_project: function() {
276
          var project_id = this.project_select.val();
277
          var project = synnefo.storage.projects.get(project_id);
278
          return project;
279
        },
280

  
281
        beforeOpen: function() {
282
            this.update_projects();
283
            this.create_button.removeClass("in-progress")
284
            this.$(".form-field").removeClass("error");
285
        },
286

  
287
        onOpen: function() {
288
            this.project_select.focus();
289
        }    
290
    });
291

  
180 292
    views.IpCollectionView = views.ext.CollectionView.extend({
181 293
      collection: storage.floating_ips,
182 294
      collection_name: 'floating_ips',
183 295
      model_view_cls: views.IpView,
184
      create_view: undefined, // no create overlay for IPs
185
      quota_key: 'cyclades.floating_ip',
296
      create_view_cls: views.FloatingIPCreateView,
297
      quota_key: 'ip',
186 298
      initialize: function() {
187 299
        views.IpCollectionView.__super__.initialize.apply(this, arguments);
188 300
        this.connect_view = new views.IPConnectVmOverlay();
189
        this.creating = false;
190
      },
191
      
192
      set_creating: function() {
193
        this.creating = true;
194
        this.create_button.addClass("in-progress");
195
      },
196
      
197
      reset_creating: function() {
198
        this.creating = false;
199
        this.create_button.removeClass("in-progress");
200
      },
201

  
202
      handle_create_click: function() {
203
        if (this.creating) { 
204
          return
205
        }
206

  
207
        this.set_creating();
208
        network = synnefo.storage.networks.get_floating_ips_network();
209
        this.collection.create({
210
          floatingip: {}
211
        }, 
212
        {
213
          success: _.bind(function() {
214
            this.post_create();
215
          }, this),
216
          complete: _.bind(function() {
217
            this.creating = false;
218
            this.reset_creating();
219
            this.collection.fetch();
220
        }, this)});
221 301
      }
222 302
    });
223 303

  
......
246 326

  
247 327
    });
248 328

  
329

  
249 330
})(this);

Also available in: Unified diff