Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_create_view.js @ a08a37d2

History | View | Annotate | Download (32.2 kB)

1
;(function(root){
2

    
3
    // root
4
    var root = root;
5
    
6
    // setup namepsaces
7
    var snf = root.synnefo = root.synnefo || {};
8
    var models = snf.models = snf.models || {}
9
    var storage = snf.storage = snf.storage || {};
10
    var ui = snf.ui = snf.ui || {};
11
    var util = snf.util = snf.util || {};
12

    
13
    var views = snf.views = snf.views || {}
14

    
15
    // shortcuts
16
    var bb = root.Backbone;
17

    
18

    
19
    views.VMCreationPasswordView = views.Overlay.extend({
20
        view_id: "creation_password_view",
21
        content_selector: "#creation-password-overlay",
22
        css_class: 'overlay-password overlay-info',
23
        overlay_id: "creation-password-overlay",
24

    
25
        subtitle: "",
26
        title: "Machine password",
27

    
28
        initialize: function(options) {
29
            views.FeedbackView.__super__.initialize.apply(this, arguments);
30
            _.bindAll(this, 'show_password');
31

    
32
            this.password = this.$("#new-machine-password");
33
            this.copy = this.$(".clip-copy");
34

    
35
            this.$(".show-machine").click(_.bind(function(){
36
                if (this.$(".show-machine").hasClass("in-progress")) {
37
                    return;
38
                }
39
                this.hide();
40
                snf.ui.main.show_vm_details(storage.vms.get(this.vm_id));
41
            }, this));
42

    
43
            _.bindAll(this, "handle_vm_added");
44
            storage.vms.bind("add", this.handle_vm_added);
45
        },
46

    
47
        handle_vm_added: function() {
48
            this.$(".show-machine").removeClass("in-progress");
49
        },
50
        
51
        show_password: function() {
52
            this.$(".show-machine").addClass("in-progress");
53
            this.password.text(this.pass);
54
            if (storage.vms.get(this.vm_id)) {
55
                this.$(".show-machine").removeClass("in-progress");
56
            }
57
        },
58

    
59
        onClose: function() {
60
            this.password.text("");
61
            this.vm_id = undefined;
62
        },
63
        
64
        beforeOpen: function() {
65
            try { delete this.clipboard } catch (err) { console.log(err) };
66
            this.clipboard = new util.ClipHelper(this.copy);
67
        },
68
        
69
        onOpen: function() {
70
            this.copy.show();
71
            try {
72
                this.clipboard.setText(this.pass);
73
                this.copy.show();
74
            } catch (err) {
75
                this.copy.hide();
76
            }
77
        },
78

    
79
        show: function(pass, vm_id) {
80
            this.pass = pass;
81
            this.vm_id = vm_id;
82
            
83
            views.VMCreationPasswordView.__super__.show.apply(this, arguments);
84
            this.show_password();
85
        }
86
    })
87

    
88

    
89
    
90
    views.CreateVMStepView = views.View.extend({
91
        step: "1",
92
        title: "Image",
93
        submit: false,
94

    
95
        initialize: function(view) {
96
            this.parent = view;
97
            this.el = view.$("div.create-step-cont.step-" + this.step);
98
            this.header = this.$(".step-header .step-" + this.step);
99
            this.view_id = "create_step_" + this.step;
100

    
101
            views.CreateVMStepView.__super__.initialize.apply(this);
102
        },
103

    
104
        show: function() {
105
            // show current
106
            this.el.show();
107
            this.header.addClass("current");
108
            this.header.show();
109
            this.update_layout();
110
        },
111

    
112
        reset: function() {
113
        }
114
    })
115

    
116
    views.CreateImageSelectView = views.CreateVMStepView.extend({
117

    
118
        initialize: function() {
119
            views.CreateImageSelectView.__super__.initialize.apply(this, arguments);
120

    
121
            // elements
122
            this.images_list_cont = this.$(".images-list-cont");
123
            this.images_list = this.$(".images-list-cont ul");
124
            this.image_details = this.$(".images-info-cont");
125
            this.image_details_desc = this.$(".images-info-cont .description p");
126
            this.image_details_title = this.$(".images-info-cont h4");
127
            this.image_details_size = this.$(".images-info-cont .size p");
128
            this.image_details_os = this.$(".images-info-cont .os p");
129
            this.image_details_kernel = this.$(".images-info-cont .kernel p");
130
            this.image_details_gui = this.$(".images-info-cont .gui p");
131

    
132
            this.types = this.$(".type-filter li");
133
            this.categories_list = this.$(".category-filters");
134

    
135
            // params initialization
136
            this.type_selections = ["system", "custom"]
137
            this.selected_type = "system";
138
            this.selected_categories = [];
139
            this.images = [];
140

    
141
            // update
142
            this.update_images();
143

    
144
            // handlers initialization
145
            this.init_handlers();
146
            this.init_position();
147
        },
148

    
149
        init_position: function() {
150
            //this.el.css({position: "absolute"});
151
            //this.el.css({top:"10px"})
152
        },
153
        
154
        init_handlers: function() {
155
            var self = this;
156
            this.types.live("click", function() {
157
                self.select_type($(this).attr("id").replace("type-select-",""));
158
            })
159
        },
160

    
161
        update_images: function() {
162
            this.images = storage.images.active();
163
            this.images_ids = _.map(this.images, function(img){return img.id});
164
            if (this.selected_type == "custom") { this.images = []; this.images_ids = []; }
165

    
166
            return this.images;
167
        },
168

    
169
        update_layout: function() {
170
            this.select_type(this.selected_type);
171
        },
172
        
173
        get_categories: function(images) {
174
            return [];
175
            return ["Desktop", "Server", "Linux", "Windows"];
176
        },
177

    
178
        reset_categories: function() {
179
            var categories = this.get_categories(this.images);
180
            this.categories_list.find("li").remove();
181

    
182
            _.each(categories, _.bind(function(cat) {
183
                var el = $("<li />");
184
                el.text(cat);
185
                this.categories_list.append(el);
186
            }, this));
187

    
188
            if (!categories.length) { 
189
                this.categories_list.parent().find(".clear").hide();
190
                this.categories_list.parent().find(".empty").show();
191
            } else {
192
                this.categories_list.parent().find(".clear").show();
193
                this.categories_list.parent().find(".empty").hide();
194
            }
195
        },
196
        
197
        select_type: function(type) {
198
            this.selected_type = type;
199
            this.types.removeClass("selected");
200
            this.types.filter("#type-select-" + this.selected_type).addClass("selected");
201

    
202
            this.reset_categories();
203
            this.update_images();
204
            this.reset_images();
205
            this.select_image();
206
        },
207

    
208
        select_image: function(image) {
209
            if (!image && this.images_ids.length) {
210
                if (this.selected_image && this.images_ids.indexOf(this.selected_image.id) > -1) {
211
                    image = this.selected_image;
212
                } else {
213
                    image = storage.images.get(this.images_ids[0]);
214
                }
215
            }
216

    
217
            if (!this.images_ids.length) { image = this.selected_image || undefined };
218
            
219
            this.selected_image = image;
220
            this.trigger("change", image);
221
            
222
            if (image) {
223
                this.image_details.show();
224
                this.images_list.find(".image-details").removeClass("selected");
225
                this.images_list.find(".image-details#create-vm-image-" + this.selected_image.id).addClass("selected");
226
                
227
                this.image_details_desc.text(image.get("description"));
228
                
229
                var img = snf.ui.helpers.os_icon_tag(image.get("OS"))
230
                this.image_details_title.html(img + image.get("name"));
231
                this.image_details_os.text(_(image.get("OS")).capitalize());
232
                this.image_details_kernel.text(image.get("kernel"));
233

    
234
                var size = image.get_readable_size();
235

    
236
                this.image_details_size.text(size);
237
                this.image_details_gui.text(image.get("GUI"));
238

    
239
            } else {
240
                this.image_details.hide();
241
            }
242

    
243
            this.validate();
244
        },
245

    
246
        reset_images: function() {
247
            this.images_list.find("li").remove();
248
            _.each(this.images, _.bind(function(img){
249
                this.add_image(img);
250
            }, this))
251
            
252
            if (this.images.length) {
253
                this.images_list.parent().find(".empty").hide();
254
            } else {
255
                this.images_list.parent().find(".empty").show();
256
            }
257

    
258
            this.select_image();
259
            
260
            var self = this;
261
            this.images_list.find(".image-details").click(function(){
262
                self.select_image($(this).data("image"));
263
            });
264
            
265
        },
266

    
267
        show: function() {
268
            views.CreateImageSelectView.__super__.show.apply(this, arguments);
269
        },
270

    
271
        add_image: function(img) {
272
            var image = $(('<li id="create-vm-image-{1}"' +
273
                           'class="image-details clearfix">{2}{0}' +
274
                           '<p>{4}</p><span class="size">{3}' +
275
                           '</span></li>').format(img.get("name"), 
276
                                                  img.id, 
277
                                                  snf.ui.helpers.os_icon_tag(img.get("OS")),
278
                                                  img.get_readable_size(),
279
                                                  util.truncate(img.get("description"),35)));
280
            image.data("image", img);
281
            image.data("image_id", img.id);
282
            this.images_list.append(image);
283
        },
284

    
285
        reset: function() {
286
            this.selected_image = undefined;
287
            this.reset_images();
288
        },
289

    
290
        get: function() {
291
            return {'image': this.selected_image};
292
        },
293

    
294
        validate: function() {
295
            if (!this.selected_image) {
296
                this.parent.$(".form-action.next").hide();
297
            } else {
298
                this.parent.$(".form-action.next").show();
299
            }
300
        }
301
    });
302

    
303
    views.CreateFlavorSelectView = views.CreateVMStepView.extend({
304
        step: 2,
305
        initialize: function() {
306
            views.CreateFlavorSelectView.__super__.initialize.apply(this, arguments);
307
            this.parent.bind("image:change", _.bind(this.handle_image_change, this));
308

    
309
            this.cpus = this.$(".flavors-cpu-list");
310
            this.disks = this.$(".flavors-disk-list");
311
            this.mems = this.$(".flavors-mem-list");
312

    
313
            this.predefined_flavors = SUGGESTED_FLAVORS;
314
            this.predefined_flavors_keys = _.keys(SUGGESTED_FLAVORS);
315
            this.predefined_flavors_keys = _.sortBy(this.predefined_flavors_keys, _.bind(function(k){
316
                var flv = this.predefined_flavors[k];
317
                return flv.ram * flv.cpu * flv.disk;
318
            }, this));
319

    
320
            this.predefined = this.$(".predefined-list");
321
            this.update_predefined_flavors();
322
        },
323

    
324
        handle_image_change: function(data) {
325
            this.current_image = data;
326
            this.update_valid_predefined();
327
            this.update_flavors_data();
328
            this.reset_flavors();
329
            this.update_layout();
330
        },
331

    
332
        reset_flavors: function() {
333
            this.$(".flavor-opts-list .option").remove();
334
            this.create_flavors();
335
        },
336

    
337
        update_predefined_flavors: function() {
338
            this.predefined.find("li").remove();
339
            _.each(this.predefined_flavors_keys, _.bind(function(key) {
340
                var val = this.predefined_flavors[key];
341
                var el = $(('<li class="predefined-selection" id="predefined-flavor-{0}">' +
342
                           '{1}</li>').format(key, _(key).capitalize()));
343

    
344
                this.predefined.append(el);
345
                el.data({flavor: storage.flavors.get_flavor(val.cpu, val.ram, val.disk, this.flavors)})
346
                el.click(_.bind(function() {
347
                    this.handle_predefined_click(el);
348
                }, this))
349
            }, this));
350
            this.update_valid_predefined();
351
        },
352

    
353
        handle_predefined_click: function(el) {
354
            if (el.hasClass("disabled")) { return };
355
            this.set_current(el.data("flavor"))
356
        },
357

    
358
        update_valid_predefined: function() {
359
            this.update_unavailable_values();
360
            var self = this;
361
            this.valid_predefined = _.select(_.map(this.predefined_flavors, function(flv, key){
362
                var existing = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
363
                // non existing
364
                if (!existing) {
365
                    return false;
366
                }
367
                
368
                // not available for image
369
                if (self.unavailable_values && self.unavailable_values.disk.indexOf(existing.get_disk_size()) > -1) {
370
                    return false
371
                }
372

    
373
                return key;
374
            }), function(ret) { return ret });
375
            
376
            $("li.predefined-selection").addClass("disabled");
377
            _.each(this.valid_predefined, function(key) {
378
                $("#predefined-flavor-" + key).removeClass("disabled");
379
            })
380
        },
381

    
382
        update_selected_predefined: function() {
383
            var self = this;
384
            this.predefined.find("li").removeClass("selected");
385

    
386
            _.each(this.valid_predefined, function(key){
387
                var flv = self.predefined_flavors[key];
388
                var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
389

    
390
                if (exists && (exists.id == self.current_flavor.id)) {
391
                    $("#predefined-flavor-" + key).addClass("selected");
392
                }
393
            })
394
        },
395
        
396
        update_flavors_data: function() {
397
            this.flavors = storage.flavors.active();
398
            this.flavors_data = storage.flavors.get_data(this.flavors);
399
            
400
            var self = this;
401
            var set = false;
402
            
403
            // FIXME: validate current flavor
404
            
405
            if (!this.current_flavor) {
406
                _.each(this.valid_predefined, function(key) {
407
                    var flv = self.predefined_flavors[key];
408
                    var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
409
                    if (exists && !set) {
410
                        self.set_current(exists);
411
                        set = true;
412
                    }
413
                })
414
            }
415

    
416
            this.update_unavailable_values();
417
        },
418

    
419
        update_unavailable_values: function() {
420
            if (!this.current_image) { this.unavailable_values = {disk:[], ram:[], cpu:[]}; return };
421
            this.unavailable_values = storage.flavors.unavailable_values_for_image(this.current_image);
422
        },
423
        
424
        flavor_is_valid: function(flv) {
425
            if (!flv) { return false };
426
            var existing = storage.flavors.get_flavor(flv.get("cpu"), flv.get("ram"), flv.get("disk"), this.flavors);
427
            if (!existing) { return false };
428
            if (this.unavailable_values && this.unavailable_values.disk.indexOf(flv.get("disk") > -1)) {
429
                return false
430
            }
431
            return true;
432
        },
433
            
434
        set_valid_current_for: function(t, val) {
435
            var found = this.flavors[0];
436
            _.each(this.flavors, function(flv) {
437
                if (flv.get(t) == val) {
438
                    found = flv;
439
                }
440
            });
441

    
442
            this.set_current(found);
443
        },
444

    
445
        set_current: function(flv) {
446

    
447
            if (!flv) {
448
                // user clicked on invalid combination
449
                // force the first available choice for the
450
                // type of option he last clicked
451
                this.set_valid_current_for.apply(this, this.last_choice);
452
                return;
453
            }
454

    
455
            this.current_flavor = flv;
456
            this.trigger("change");
457
            if (this.current_flavor) {
458
                this.update_selected_flavor();
459
                this.update_selected_predefined();
460
            }
461
            
462
            this.validate();
463
        },
464
        
465
        select_default_flavor: function() {
466
               
467
        },
468

    
469
        update_selected_from_ui: function() {
470
            this.set_current(this.ui_selected());
471
        },
472
        
473
        update_disabled_flavors: function() {
474
            this.$(".flavor-options.disk li").removeClass("disabled");
475
            if (!this.unavailable_values) { return }
476
            
477
            this.$("#create-vm-flavor-options .flavor-options.disk li").each(_.bind(function(i, el){
478
                var el_value = $(el).data("value") * 1000;
479
                if (this.unavailable_values.disk.indexOf(el_value) > -1) {
480
                    $(el).addClass("disabled");
481
                };
482
            }, this));
483
        },
484

    
485
        create_flavors: function() {
486
            var flavors = this.get_active_flavors();
487
            var valid_flavors = this.get_valid_flavors();
488
            this.__added_flavors = {'cpu':[], 'ram':[], 'disk':[]};
489

    
490
            _.each(flavors, _.bind(function(flv){
491
                this.add_flavor(flv);
492
            }, this));
493
            
494
            var self = this;
495
            this.$(".flavor-options li.option").click(function(){
496
                var el = $(this);
497

    
498
                if (el.hasClass("disabled")) { return }
499

    
500
                el.parent().find(".option").removeClass("selected");
501
                el.addClass("selected");
502
                
503
                if (el.hasClass("mem")) { self.last_choice = ["ram", $(this).data("value")] }
504
                if (el.hasClass("cpu")) { self.last_choice = ["cpu", $(this).data("value")] }
505
                if (el.hasClass("disk")) { self.last_choice = ["disk", $(this).data("value")] }
506

    
507
                self.update_selected_from_ui();
508
            })
509
        },
510
        
511
        ui_selected: function() {
512
            var args = [this.$(".option.cpu.selected").data("value"), 
513
                this.$(".option.mem.selected").data("value"), 
514
                this.$(".option.disk.selected").data("value"),
515
            this.flavors];
516

    
517
            var flv = storage.flavors.get_flavor.apply(storage.flavors, args);
518
            return flv;
519
        },
520

    
521
        update_selected_flavor: function() {
522
            var flv = this.current_flavor;
523
            if (!flv) { return }
524
            this.$(".option").removeClass("selected");
525

    
526
            this.$(".option.cpu.value-" + flv.get("cpu")).addClass("selected");
527
            this.$(".option.mem.value-" + flv.get("ram")).addClass("selected");
528
            this.$(".option.disk.value-" + flv.get("disk")).addClass("selected");
529
        },
530
        
531
        __added_flavors: {'cpu':[], 'ram':[], 'disk':[]},
532
        add_flavor: function(flv) {
533
            var values = {'cpu': flv.get('cpu'), 'mem': flv.get('ram'), 'disk': flv.get('disk')};
534

    
535
            disabled = "";
536
            
537
            if (this.__added_flavors.cpu.indexOf(values.cpu) == -1) {
538
                var cpu = $(('<li class="option cpu value-{0} {1}">' + 
539
                             '<span class="value">{0}</span>' + 
540
                             '<span class="metric">x</span></li>').format(values.cpu, disabled)).data('value', values.cpu);
541
                this.cpus.append(cpu);
542
                this.__added_flavors.cpu.push(values.cpu);
543
            }
544

    
545
            if (this.__added_flavors.ram.indexOf(values.mem) == -1) {
546
                var mem = $(('<li class="option mem value-{0}">' + 
547
                             '<span class="value">{0}</span>' + 
548
                             '<span class="metric">MB</span></li>').format(values.mem)).data('value', values.mem);
549
                this.mems.append(mem);
550
                this.__added_flavors.ram.push(values.mem);
551
            }
552

    
553
            if (this.__added_flavors.disk.indexOf(values.disk) == -1) {
554
                var disk = $(('<li class="option disk value-{0}">' + 
555
                              '<span class="value">{0}</span>' + 
556
                              '<span class="metric">GB</span></li>').format(values.disk)).data('value', values.disk);
557
                this.disks.append(disk);
558
                this.__added_flavors.disk.push(values.disk)
559
            }
560

    
561
            
562
        },
563
        
564
        get_active_flavors: function() {
565
            return storage.flavors.active();
566
        },
567

    
568
        get_valid_flavors: function() {
569
            return this.flavors;
570
        },
571

    
572
        update_layout: function() {
573
            this.update_selected_flavor();
574
            this.update_disabled_flavors();
575
            this.validate();
576
        },
577

    
578
        reset: function() {
579
            this.current_image = storage.images.at(0);
580
            this.flavors = [];
581
            this.flavors_data = {'cpu':[], 'mem':[], 'disk':[]};
582
            this.update_flavors_data();
583
        },
584

    
585
        validate: function() {
586
            if (!this.current_flavor) {
587
                this.parent.$(".form-action.next").hide();
588
            } else {
589
                this.parent.$(".form-action.next").show();
590
            }
591
        },
592

    
593
        get: function() {
594
            return {'flavor': this.current_flavor}
595
        }
596

    
597
    });
598

    
599
    views.CreateSubmitView = views.CreateVMStepView.extend({
600
        step: 3,
601
        initialize: function() {
602
            views.CreateSubmitView.__super__.initialize.apply(this, arguments);
603
            this.roles = this.$("li.predefined-meta.role .values");
604
            this.confirm = this.$(".confirm-params ul");
605
            this.name = this.$("input.rename-field");
606
            this.name_changed = false;
607
            this.init_suggested_roles();
608
            this.init_handlers();
609
        },
610

    
611
        init_suggested_roles: function() {
612
            var cont = this.roles;
613
            cont.empty();
614
            
615
            // TODO: get suggested from snf.api.conf
616
            _.each(window.SUGGESTED_ROLES, function(r){
617
                var el = $('<span class="val">{0}</span>'.format(r));
618
                el.data("value", r);
619
                cont.append(el);
620
                el.click(function() {
621
                    $(this).parent().find(".val").removeClass("selected");
622
                    $(this).toggleClass("selected");
623
                })
624
            })
625
        },
626

    
627
        init_handlers: function() {
628
            this.name.bind("keypress", _.bind(function(e) {
629
                this.name_changed = true;
630
                if (e.keyCode == 13) { this.parent.submit() };    
631
            }, this));
632

    
633
            this.name.bind("click", _.bind(function() {
634
                if (!this.name_changed) {
635
                    this.name.val("");
636
                }
637
            }, this))
638
        },
639

    
640
        show: function() {
641
            views.CreateSubmitView.__super__.show.apply(this, arguments);
642
            this.update_layout();
643
        },
644
        
645
        update_flavor_details: function() {
646
            var flavor = this.parent.get_params().flavor;
647

    
648
            function set_detail(sel, key) {
649
                var val = key;
650
                if (key == undefined) { val = flavor.get(sel) };
651
                this.$(".confirm-cont.flavor .flavor-" + sel + " .value").text(val)
652
            }
653
            
654
            set_detail("cpu", flavor.get("cpu") + "x");
655
            set_detail("ram", flavor.get("ram") + " MB");
656
            set_detail("disk", util.readablizeBytes(flavor.get("disk") * 1024 * 1024 * 1024));
657
        },
658

    
659
        update_image_details: function() {
660
            var image = this.parent.get_params().image;
661

    
662
            function set_detail(sel, key) {
663
                var val = key;
664
                if (key == undefined) { val = image.get(sel) };
665
                this.$(".confirm-cont.image .image-" + sel + " .value").text(val)
666
            }
667
            
668
            set_detail("description");
669
            set_detail("name");
670
            set_detail("os", _(image.get("OS")).capitalize());
671
            set_detail("gui", image.get("GUI"));
672
            set_detail("size", image.get_readable_size());
673
            set_detail("kernel");
674
        },
675

    
676
        update_layout: function() {
677
            var params = this.parent.get_params();
678
            if (!params.image || !params.flavor) { return }
679

    
680
            if (!params.image) { return }
681
            var vm_name = "My {0} server".format(params.image.get("name"));
682
            var orig_name = vm_name;
683
            
684
            var existing = true;
685
            var j = 0;
686

    
687
            while (existing && !this.name_changed) {
688
                var existing = storage.vms.select(function(vm){return vm.get("name") == vm_name}).length
689
                if (existing) {
690
                    j++;
691
                    vm_name = orig_name + " " + j;
692
                }
693
            }
694
            if (!_(this.name.val()).trim() || !this.name_changed) {
695
                this.name.val(vm_name);
696
            }
697

    
698
            this.confirm.find("li.image .value").text(params.flavor.get("image"));
699
            this.confirm.find("li.cpu .value").text(params.flavor.get("cpu") + "x");
700
            this.confirm.find("li.mem .value").text(params.flavor.get("ram"));
701
            this.confirm.find("li.disk .value").text(params.flavor.get("disk"));
702

    
703
            if (!this.name_changed && this.parent.visible()) {
704
                if (!$.browser.msie && !$.browser.opera) {
705
                    this.$("#create-vm-name").select();
706
                } else {
707
                    window.setTimeout(_.bind(function(){
708
                        this.$("#create-vm-name").select();
709
                    }, this), 400)
710
                }
711
            }
712
            
713
            var img = snf.ui.helpers.os_icon_path(params.image.get("OS"))
714
            this.name.css({backgroundImage:"url({0})".format(img)})
715

    
716
            this.update_image_details();
717
            this.update_flavor_details();
718
        },
719

    
720
        reset: function() {
721
            this.roles.find(".val").removeClass("selected");
722
            this.name_changed = false;
723
            this.update_layout();
724
        },
725

    
726
        get_meta: function() {
727
            if (this.roles.find(".selected").length == 0) {
728
                return false;
729
            }
730

    
731
            var role = $(this.roles.find(".selected").get(0)).data("value");
732
            return {'Role': role }
733
        },
734

    
735
        get: function() {
736
            var val = {'name': this.name.val() };
737
            if (this.get_meta()) {
738
                val.metadata = this.get_meta();
739
            }
740
            
741
            return val;
742
        }
743
    });
744

    
745
    views.CreateVMView = views.Overlay.extend({
746
        
747
        view_id: "create_vm_view",
748
        content_selector: "#createvm-overlay-content",
749
        css_class: 'overlay-createvm overlay-info',
750
        overlay_id: "metadata-overlay",
751

    
752
        subtitle: false,
753
        title: "Create new machine",
754

    
755
        initialize: function(options) {
756
            views.CreateVMView.__super__.initialize.apply(this);
757
            this.current_step = 1;
758

    
759
            this.password_view = new views.VMCreationPasswordView();
760

    
761
            this.steps = [];
762
            this.steps[1] = new views.CreateImageSelectView(this);
763
            this.steps[1].bind("change", _.bind(function(data) {this.trigger("image:change", data)}, this));
764

    
765
            this.steps[2] = new views.CreateFlavorSelectView(this);
766
            this.steps[3] = new views.CreateSubmitView(this);
767

    
768
            this.cancel_btn = this.$(".create-controls .cancel");
769
            this.next_btn = this.$(".create-controls .next");
770
            this.prev_btn = this.$(".create-controls .prev");
771
            this.submit_btn = this.$(".create-controls .submit");
772

    
773
            this.history = this.$(".steps-history");
774
            this.history_steps = this.$(".steps-history .steps-history-step");
775
            
776
            this.init_handlers();
777
        },
778

    
779
        init_handlers: function() {
780
            var self = this;
781
            this.next_btn.click(_.bind(function(){
782
                this.set_step(this.current_step + 1);
783
                this.update_layout();
784
            }, this))
785
            this.prev_btn.click(_.bind(function(){
786
                this.set_step(this.current_step - 1);
787
                this.update_layout();
788
            }, this))
789
            this.cancel_btn.click(_.bind(function(){
790
                this.close_all();
791
            }, this))
792
            this.submit_btn.click(_.bind(function(){
793
                this.submit();
794
            }, this))
795
            
796
            this.history.find(".completed").live("click", function() {
797
                var step = parseInt($(this).attr("id").replace("vm-create-step-history-", ""));
798
                self.set_step(step);
799
                self.update_layout();
800
            })
801
        },
802

    
803
        set_step: function(st) {
804
        },
805
        
806
        validate: function(data) {
807
            if (_(data.name).trim() == "") {
808
                this.$(".form-field").addClass("error");
809
                return false;
810
            } else {
811
                return true;
812
            }
813
        },
814

    
815
        submit: function() {
816
            if (this.submiting) { return };
817
            var data = this.get_params();
818
            var meta = {};
819
            if (this.validate(data)) {
820
                this.submit_btn.addClass("in-progress");
821
                this.submiting = true;
822
                if (data.metadata) { meta = data.metadata; }
823
                storage.vms.create(data.name, data.image, data.flavor, meta, {}, _.bind(function(data){
824
                    this.close_all();
825
                    this.password_view.show(data.server.adminPass, data.server.id);
826
                    this.submiting = false;
827
                }, this));
828
            }
829
        },
830

    
831
        close_all: function() {
832
            this.hide();
833
        },
834

    
835
        reset: function() {
836
            this.current_step = 1;
837

    
838
            this.steps[1].reset();
839
            this.steps[2].reset();
840
            this.steps[3].reset();
841

    
842
            this.steps[1].show();
843
            this.steps[2].show();
844
            this.steps[3].show();
845

    
846
            this.submit_btn.removeClass("in-progress");
847
        },
848

    
849
        onShow: function() {
850
            this.reset()
851
            this.update_layout();
852
        },
853

    
854
        update_layout: function() {
855
            this.show_step(this.current_step);
856
            this.current_view.update_layout();
857
        },
858

    
859
        beforeOpen: function() {
860
            this.submiting = false;
861
            this.reset();
862
            this.current_step = 1;
863
            this.$(".steps-container").css({"margin-left":0 + "px"});
864
            this.show_step(1);
865
        },
866
        
867
        set_step: function(step) {
868
            if (step <= 1) {
869
                step = 1
870
            }
871
            if (step > this.steps.length - 1) {
872
                step = this.steps.length - 1;
873
            }
874
            this.current_step = step;
875
        },
876

    
877
        show_step: function(step) {
878
            this.current_view = this.steps[step];
879
            this.update_controls();
880

    
881
            this.steps[step].show();
882
            var width = this.el.find('.container').width();
883
            var left = (step -1) * width * -1;
884
            this.$(".steps-container").animate({"margin-left": left + "px"}, 300);
885

    
886
            this.update_steps_history();
887
        },
888

    
889
        update_steps_history: function() {
890
            var self = this;
891
            function get_step(s) {
892
                return self.history.find(".step" + s + "h");
893
            }
894
            
895
            var current_step = parseInt(this.current_view.step);
896
            _.each(this.steps, function(stepv) {
897
                var step = parseInt(stepv.step);
898
                get_step(step).removeClass("completed").removeClass("current");
899
                if (step == current_step) {
900
                    get_step(step).removeClass("completed").addClass("current");
901
                }
902
                if (step < current_step) {
903
                    get_step(step).removeClass("current").addClass("completed");
904
                }
905
            });
906
        },
907

    
908
        update_controls: function() {
909
            var step = this.current_step;
910
            if (step == 1) {
911
                this.prev_btn.hide();
912
                this.cancel_btn.show();
913
            } else {
914
                this.prev_btn.show();
915
                this.cancel_btn.hide();
916
            }
917
            
918
            if (step == this.steps.length - 1) {
919
                this.next_btn.hide();
920
                this.submit_btn.show();
921
            } else {
922
                this.next_btn.show();
923
                this.submit_btn.hide();
924
            }
925
        },
926

    
927
        get_params: function() {
928
            return _.extend({}, this.steps[1].get(), this.steps[2].get(), this.steps[3].get());
929
        }
930
    });
931
    
932
})(this);
933