Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_create_view.js @ 283bb7de

History | View | Annotate | Download (33.1 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
        validate_selected_flavor: function() {
333
            if (!this.flavor_is_valid(this.current_flavor)) {
334
                this.select_valid_flavor();
335
            }
336
        },
337

    
338
        reset_flavors: function() {
339
            this.$(".flavor-opts-list .option").remove();
340
            this.create_flavors();
341
        },
342

    
343
        update_predefined_flavors: function() {
344
            this.predefined.find("li").remove();
345
            _.each(this.predefined_flavors_keys, _.bind(function(key) {
346
                var val = this.predefined_flavors[key];
347
                var el = $(('<li class="predefined-selection" id="predefined-flavor-{0}">' +
348
                           '{1}</li>').format(key, _(key).capitalize()));
349

    
350
                this.predefined.append(el);
351
                el.data({flavor: storage.flavors.get_flavor(val.cpu, val.ram, val.disk, this.flavors)})
352
                el.click(_.bind(function() {
353
                    this.handle_predefined_click(el);
354
                }, this))
355
            }, this));
356
            this.update_valid_predefined();
357
        },
358

    
359
        handle_predefined_click: function(el) {
360
            if (el.hasClass("disabled")) { return };
361
            this.set_current(el.data("flavor"))
362
        },
363

    
364
        select_valid_flavor: function() {
365
            var found = false;
366
            var self = this;
367
            _.each(this.flavors, function(flv) {
368
                if (self.flavor_is_valid(flv)) {
369
                    found = flv;
370
                    return false;
371
                }
372
            });
373
            
374
            if (found) {
375
                this.set_current(found);
376
            } else {
377
                this.current_flavor = undefined;
378
                this.validate();
379
                this.$("li.predefined-selection").addClass("disabled");
380
                this.$(".flavor-opts-list li").removeClass("selected");
381
            }
382
        },
383

    
384
        update_valid_predefined: function() {
385
            this.update_unavailable_values();
386
            var self = this;
387
            this.valid_predefined = _.select(_.map(this.predefined_flavors, function(flv, key){
388
                var existing = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
389
                // non existing
390
                if (!existing) {
391
                    return false;
392
                }
393
                
394
                // not available for image
395
                if (self.unavailable_values && self.unavailable_values.disk.indexOf(existing.get_disk_size()) > -1) {
396
                    return false
397
                }
398

    
399
                return key;
400
            }), function(ret) { return ret });
401
            
402
            $("li.predefined-selection").addClass("disabled");
403
            _.each(this.valid_predefined, function(key) {
404
                $("#predefined-flavor-" + key).removeClass("disabled");
405
            })
406
        },
407

    
408
        update_selected_predefined: function() {
409
            var self = this;
410
            this.predefined.find("li").removeClass("selected");
411

    
412
            _.each(this.valid_predefined, function(key){
413
                var flv = self.predefined_flavors[key];
414
                var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
415

    
416
                if (exists && (exists.id == self.current_flavor.id)) {
417
                    $("#predefined-flavor-" + key).addClass("selected");
418
                }
419
            })
420
        },
421
        
422
        update_flavors_data: function() {
423
            this.flavors = storage.flavors.active();
424
            this.flavors_data = storage.flavors.get_data(this.flavors);
425
            
426
            var self = this;
427
            var set = false;
428
            
429
            // FIXME: validate current flavor
430
            
431
            if (!this.current_flavor) {
432
                _.each(this.valid_predefined, function(key) {
433
                    var flv = self.predefined_flavors[key];
434
                    var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
435
                    if (exists && !set) {
436
                        self.set_current(exists);
437
                        set = true;
438
                    }
439
                })
440
            }
441

    
442
            this.update_unavailable_values();
443
        },
444

    
445
        update_unavailable_values: function() {
446
            if (!this.current_image) { this.unavailable_values = {disk:[], ram:[], cpu:[]}; return };
447
            this.unavailable_values = storage.flavors.unavailable_values_for_image(this.current_image);
448
        },
449
        
450
        flavor_is_valid: function(flv) {
451
            if (!flv) { return false };
452

    
453
            var existing = storage.flavors.get_flavor(flv.get("cpu"), flv.get("ram"), flv.get("disk"), this.flavors);
454
            if (!existing) { return false };
455
            
456
            if (this.unavailable_values && (this.unavailable_values.disk.indexOf(parseInt(flv.get("disk")) * 1000) > -1)) {
457
                return false;
458
            }
459
            return true;
460
        },
461
            
462
        set_valid_current_for: function(t, val) {
463
            var found = this.flavors[0];
464
            _.each(this.flavors, function(flv) {
465
                if (flv.get(t) == val) {
466
                    found = flv;
467
                }
468
            });
469

    
470
            this.set_current(found);
471
            this.validate_selected_flavor();
472
        },
473

    
474
        set_current: function(flv) {
475

    
476
            if (!flv) {
477
                // user clicked on invalid combination
478
                // force the first available choice for the
479
                // type of option he last clicked
480
                this.set_valid_current_for.apply(this, this.last_choice);
481
                return;
482
            }
483

    
484
            this.current_flavor = flv;
485
            this.trigger("change");
486
            if (this.current_flavor) {
487
                this.update_selected_flavor();
488
                this.update_selected_predefined();
489
            }
490
            
491
            this.validate();
492
        },
493
        
494
        select_default_flavor: function() {
495
               
496
        },
497

    
498
        update_selected_from_ui: function() {
499
            this.set_current(this.ui_selected());
500
        },
501
        
502
        update_disabled_flavors: function() {
503
            this.$(".flavor-options.disk li").removeClass("disabled");
504
            if (!this.unavailable_values) { return }
505
            
506
            this.$("#create-vm-flavor-options .flavor-options.disk li").each(_.bind(function(i, el){
507
                var el_value = $(el).data("value") * 1000;
508
                if (this.unavailable_values.disk.indexOf(el_value) > -1) {
509
                    $(el).addClass("disabled");
510
                };
511
            }, this));
512
        },
513

    
514
        create_flavors: function() {
515
            var flavors = this.get_active_flavors();
516
            var valid_flavors = this.get_valid_flavors();
517
            this.__added_flavors = {'cpu':[], 'ram':[], 'disk':[]};
518

    
519
            _.each(flavors, _.bind(function(flv){
520
                this.add_flavor(flv);
521
            }, this));
522
            
523
            var self = this;
524
            this.$(".flavor-options li.option").click(function(){
525
                var el = $(this);
526

    
527
                if (el.hasClass("disabled")) { return }
528

    
529
                el.parent().find(".option").removeClass("selected");
530
                el.addClass("selected");
531
                
532
                if (el.hasClass("mem")) { self.last_choice = ["ram", $(this).data("value")] }
533
                if (el.hasClass("cpu")) { self.last_choice = ["cpu", $(this).data("value")] }
534
                if (el.hasClass("disk")) { self.last_choice = ["disk", $(this).data("value")] }
535

    
536
                self.update_selected_from_ui();
537
            })
538
        },
539
        
540
        ui_selected: function() {
541
            var args = [this.$(".option.cpu.selected").data("value"), 
542
                this.$(".option.mem.selected").data("value"), 
543
                this.$(".option.disk.selected").data("value"),
544
            this.flavors];
545

    
546
            var flv = storage.flavors.get_flavor.apply(storage.flavors, args);
547
            return flv;
548
        },
549

    
550
        update_selected_flavor: function() {
551
            var flv = this.current_flavor;
552
            if (!flv) { return }
553
            this.$(".option").removeClass("selected");
554

    
555
            this.$(".option.cpu.value-" + flv.get("cpu")).addClass("selected");
556
            this.$(".option.mem.value-" + flv.get("ram")).addClass("selected");
557
            this.$(".option.disk.value-" + flv.get("disk")).addClass("selected");
558
        },
559
        
560
        __added_flavors: {'cpu':[], 'ram':[], 'disk':[]},
561
        add_flavor: function(flv) {
562
            var values = {'cpu': flv.get('cpu'), 'mem': flv.get('ram'), 'disk': flv.get('disk')};
563

    
564
            disabled = "";
565
            
566
            if (this.__added_flavors.cpu.indexOf(values.cpu) == -1) {
567
                var cpu = $(('<li class="option cpu value-{0} {1}">' + 
568
                             '<span class="value">{0}</span>' + 
569
                             '<span class="metric">x</span></li>').format(values.cpu, disabled)).data('value', values.cpu);
570
                this.cpus.append(cpu);
571
                this.__added_flavors.cpu.push(values.cpu);
572
            }
573

    
574
            if (this.__added_flavors.ram.indexOf(values.mem) == -1) {
575
                var mem = $(('<li class="option mem value-{0}">' + 
576
                             '<span class="value">{0}</span>' + 
577
                             '<span class="metric">MB</span></li>').format(values.mem)).data('value', values.mem);
578
                this.mems.append(mem);
579
                this.__added_flavors.ram.push(values.mem);
580
            }
581

    
582
            if (this.__added_flavors.disk.indexOf(values.disk) == -1) {
583
                var disk = $(('<li class="option disk value-{0}">' + 
584
                              '<span class="value">{0}</span>' + 
585
                              '<span class="metric">GB</span></li>').format(values.disk)).data('value', values.disk);
586
                this.disks.append(disk);
587
                this.__added_flavors.disk.push(values.disk)
588
            }
589

    
590
            
591
        },
592
        
593
        get_active_flavors: function() {
594
            return storage.flavors.active();
595
        },
596

    
597
        get_valid_flavors: function() {
598
            return this.flavors;
599
        },
600

    
601
        update_layout: function() {
602
            this.update_selected_flavor();
603
            this.update_disabled_flavors();
604
            this.validate();
605
            this.validate_selected_flavor();
606
        },
607

    
608
        reset: function() {
609
            this.current_image = storage.images.at(0);
610
            this.flavors = [];
611
            this.flavors_data = {'cpu':[], 'mem':[], 'disk':[]};
612
            this.update_flavors_data();
613
        },
614

    
615
        validate: function() {
616
            if (!this.current_flavor) {
617
                this.parent.$(".form-action.next").hide();
618
            } else {
619
                this.parent.$(".form-action.next").show();
620
            }
621
        },
622

    
623
        get: function() {
624
            return {'flavor': this.current_flavor}
625
        }
626

    
627
    });
628

    
629
    views.CreateSubmitView = views.CreateVMStepView.extend({
630
        step: 3,
631
        initialize: function() {
632
            views.CreateSubmitView.__super__.initialize.apply(this, arguments);
633
            this.roles = this.$("li.predefined-meta.role .values");
634
            this.confirm = this.$(".confirm-params ul");
635
            this.name = this.$("input.rename-field");
636
            this.name_changed = false;
637
            this.init_suggested_roles();
638
            this.init_handlers();
639
        },
640

    
641
        init_suggested_roles: function() {
642
            var cont = this.roles;
643
            cont.empty();
644
            
645
            // TODO: get suggested from snf.api.conf
646
            _.each(window.SUGGESTED_ROLES, function(r){
647
                var el = $('<span class="val">{0}</span>'.format(r));
648
                el.data("value", r);
649
                cont.append(el);
650
                el.click(function() {
651
                    $(this).parent().find(".val").removeClass("selected");
652
                    $(this).toggleClass("selected");
653
                })
654
            })
655
        },
656

    
657
        init_handlers: function() {
658
            this.name.bind("keypress", _.bind(function(e) {
659
                this.name_changed = true;
660
                if (e.keyCode == 13) { this.parent.submit() };    
661
            }, this));
662

    
663
            this.name.bind("click", _.bind(function() {
664
                if (!this.name_changed) {
665
                    this.name.val("");
666
                }
667
            }, this))
668
        },
669

    
670
        show: function() {
671
            views.CreateSubmitView.__super__.show.apply(this, arguments);
672
            this.update_layout();
673
        },
674
        
675
        update_flavor_details: function() {
676
            var flavor = this.parent.get_params().flavor;
677

    
678
            function set_detail(sel, key) {
679
                var val = key;
680
                if (key == undefined) { val = flavor.get(sel) };
681
                this.$(".confirm-cont.flavor .flavor-" + sel + " .value").text(val)
682
            }
683
            
684
            set_detail("cpu", flavor.get("cpu") + "x");
685
            set_detail("ram", flavor.get("ram") + " MB");
686
            set_detail("disk", util.readablizeBytes(flavor.get("disk") * 1024 * 1024 * 1024));
687
        },
688

    
689
        update_image_details: function() {
690
            var image = this.parent.get_params().image;
691

    
692
            function set_detail(sel, key) {
693
                var val = key;
694
                if (key == undefined) { val = image.get(sel) };
695
                this.$(".confirm-cont.image .image-" + sel + " .value").text(val)
696
            }
697
            
698
            set_detail("description");
699
            set_detail("name");
700
            set_detail("os", _(image.get("OS")).capitalize());
701
            set_detail("gui", image.get("GUI"));
702
            set_detail("size", image.get_readable_size());
703
            set_detail("kernel");
704
        },
705

    
706
        update_layout: function() {
707
            var params = this.parent.get_params();
708
            if (!params.image || !params.flavor) { return }
709

    
710
            if (!params.image) { return }
711
            var vm_name = "My {0} server".format(params.image.get("name"));
712
            var orig_name = vm_name;
713
            
714
            var existing = true;
715
            var j = 0;
716

    
717
            while (existing && !this.name_changed) {
718
                var existing = storage.vms.select(function(vm){return vm.get("name") == vm_name}).length
719
                if (existing) {
720
                    j++;
721
                    vm_name = orig_name + " " + j;
722
                }
723
            }
724
            if (!_(this.name.val()).trim() || !this.name_changed) {
725
                this.name.val(vm_name);
726
            }
727

    
728
            this.confirm.find("li.image .value").text(params.flavor.get("image"));
729
            this.confirm.find("li.cpu .value").text(params.flavor.get("cpu") + "x");
730
            this.confirm.find("li.mem .value").text(params.flavor.get("ram"));
731
            this.confirm.find("li.disk .value").text(params.flavor.get("disk"));
732

    
733
            if (!this.name_changed && this.parent.visible()) {
734
                if (!$.browser.msie && !$.browser.opera) {
735
                    this.$("#create-vm-name").select();
736
                } else {
737
                    window.setTimeout(_.bind(function(){
738
                        this.$("#create-vm-name").select();
739
                    }, this), 400)
740
                }
741
            }
742
            
743
            var img = snf.ui.helpers.os_icon_path(params.image.get("OS"))
744
            this.name.css({backgroundImage:"url({0})".format(img)})
745

    
746
            this.update_image_details();
747
            this.update_flavor_details();
748
        },
749

    
750
        reset: function() {
751
            this.roles.find(".val").removeClass("selected");
752
            this.name_changed = false;
753
            this.update_layout();
754
        },
755

    
756
        get_meta: function() {
757
            if (this.roles.find(".selected").length == 0) {
758
                return false;
759
            }
760

    
761
            var role = $(this.roles.find(".selected").get(0)).data("value");
762
            return {'Role': role }
763
        },
764

    
765
        get: function() {
766
            var val = {'name': this.name.val() };
767
            if (this.get_meta()) {
768
                val.metadata = this.get_meta();
769
            }
770
            
771
            return val;
772
        }
773
    });
774

    
775
    views.CreateVMView = views.Overlay.extend({
776
        
777
        view_id: "create_vm_view",
778
        content_selector: "#createvm-overlay-content",
779
        css_class: 'overlay-createvm overlay-info',
780
        overlay_id: "metadata-overlay",
781

    
782
        subtitle: false,
783
        title: "Create new machine",
784

    
785
        initialize: function(options) {
786
            views.CreateVMView.__super__.initialize.apply(this);
787
            this.current_step = 1;
788

    
789
            this.password_view = new views.VMCreationPasswordView();
790

    
791
            this.steps = [];
792
            this.steps[1] = new views.CreateImageSelectView(this);
793
            this.steps[1].bind("change", _.bind(function(data) {this.trigger("image:change", data)}, this));
794

    
795
            this.steps[2] = new views.CreateFlavorSelectView(this);
796
            this.steps[3] = new views.CreateSubmitView(this);
797

    
798
            this.cancel_btn = this.$(".create-controls .cancel");
799
            this.next_btn = this.$(".create-controls .next");
800
            this.prev_btn = this.$(".create-controls .prev");
801
            this.submit_btn = this.$(".create-controls .submit");
802

    
803
            this.history = this.$(".steps-history");
804
            this.history_steps = this.$(".steps-history .steps-history-step");
805
            
806
            this.init_handlers();
807
        },
808

    
809
        init_handlers: function() {
810
            var self = this;
811
            this.next_btn.click(_.bind(function(){
812
                this.set_step(this.current_step + 1);
813
                this.update_layout();
814
            }, this))
815
            this.prev_btn.click(_.bind(function(){
816
                this.set_step(this.current_step - 1);
817
                this.update_layout();
818
            }, this))
819
            this.cancel_btn.click(_.bind(function(){
820
                this.close_all();
821
            }, this))
822
            this.submit_btn.click(_.bind(function(){
823
                this.submit();
824
            }, this))
825
            
826
            this.history.find(".completed").live("click", function() {
827
                var step = parseInt($(this).attr("id").replace("vm-create-step-history-", ""));
828
                self.set_step(step);
829
                self.update_layout();
830
            })
831
        },
832

    
833
        set_step: function(st) {
834
        },
835
        
836
        validate: function(data) {
837
            if (_(data.name).trim() == "") {
838
                this.$(".form-field").addClass("error");
839
                return false;
840
            } else {
841
                return true;
842
            }
843
        },
844

    
845
        submit: function() {
846
            if (this.submiting) { return };
847
            var data = this.get_params();
848
            var meta = {};
849
            if (this.validate(data)) {
850
                this.submit_btn.addClass("in-progress");
851
                this.submiting = true;
852
                if (data.metadata) { meta = data.metadata; }
853
                storage.vms.create(data.name, data.image, data.flavor, meta, {}, _.bind(function(data){
854
                    this.close_all();
855
                    this.password_view.show(data.server.adminPass, data.server.id);
856
                    this.submiting = false;
857
                }, this));
858
            }
859
        },
860

    
861
        close_all: function() {
862
            this.hide();
863
        },
864

    
865
        reset: function() {
866
            this.current_step = 1;
867

    
868
            this.steps[1].reset();
869
            this.steps[2].reset();
870
            this.steps[3].reset();
871

    
872
            this.steps[1].show();
873
            this.steps[2].show();
874
            this.steps[3].show();
875

    
876
            this.submit_btn.removeClass("in-progress");
877
        },
878

    
879
        onShow: function() {
880
            this.reset()
881
            this.update_layout();
882
        },
883

    
884
        update_layout: function() {
885
            this.show_step(this.current_step);
886
            this.current_view.update_layout();
887
        },
888

    
889
        beforeOpen: function() {
890
            this.submiting = false;
891
            this.reset();
892
            this.current_step = 1;
893
            this.$(".steps-container").css({"margin-left":0 + "px"});
894
            this.show_step(1);
895
        },
896
        
897
        set_step: function(step) {
898
            if (step <= 1) {
899
                step = 1
900
            }
901
            if (step > this.steps.length - 1) {
902
                step = this.steps.length - 1;
903
            }
904
            this.current_step = step;
905
        },
906

    
907
        show_step: function(step) {
908
            this.current_view = this.steps[step];
909
            this.update_controls();
910

    
911
            this.steps[step].show();
912
            var width = this.el.find('.container').width();
913
            var left = (step -1) * width * -1;
914
            this.$(".steps-container").animate({"margin-left": left + "px"}, 300);
915

    
916
            this.update_steps_history();
917
        },
918

    
919
        update_steps_history: function() {
920
            var self = this;
921
            function get_step(s) {
922
                return self.history.find(".step" + s + "h");
923
            }
924
            
925
            var current_step = parseInt(this.current_view.step);
926
            _.each(this.steps, function(stepv) {
927
                var step = parseInt(stepv.step);
928
                get_step(step).removeClass("completed").removeClass("current");
929
                if (step == current_step) {
930
                    get_step(step).removeClass("completed").addClass("current");
931
                }
932
                if (step < current_step) {
933
                    get_step(step).removeClass("current").addClass("completed");
934
                }
935
            });
936
        },
937

    
938
        update_controls: function() {
939
            var step = this.current_step;
940
            if (step == 1) {
941
                this.prev_btn.hide();
942
                this.cancel_btn.show();
943
            } else {
944
                this.prev_btn.show();
945
                this.cancel_btn.hide();
946
            }
947
            
948
            if (step == this.steps.length - 1) {
949
                this.next_btn.hide();
950
                this.submit_btn.show();
951
            } else {
952
                this.next_btn.show();
953
                this.submit_btn.hide();
954
            }
955
        },
956

    
957
        get_params: function() {
958
            return _.extend({}, this.steps[1].get(), this.steps[2].get(), this.steps[3].get());
959
        }
960
    });
961
    
962
})(this);
963