Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / vm-wizard.js @ ea6e9a08

History | View | Annotate | Download (14.2 kB)

1
ui.wizard = {
2
        current_step: undefined,
3
        total_step: $('.wizard-content .step').length,
4
        current_position: undefined,
5
        relocation: undefined,
6
        btns: {
7
                start: undefined,
8
                close: undefined,
9
                next: undefined,
10
                previous: undefined,
11
                expand_down: undefined
12
        },
13
        speed: 500,
14
        semaphore:1,
15
        vm: {
16
                getSize: function(elem) {
17
                        if ($(elem).hasClass('small')) {
18
                                return 'small';
19
                        } else if ($(elem).hasClass('medium')) {
20
                                return 'medium';
21
                        } else if ($(elem).hasClass('large')) {
22
                                return 'large';
23
                        }
24
                },
25

    
26

    
27
                pickResources: function(resource) {
28
                        $('.flavor .with-flavor a:not(.' + resource + ')').removeClass('current');
29
                        $('.flavor .with-flavor a.' + resource + '').addClass('current');
30
                },
31

    
32
                hideNext: function() {
33
                        if(ui.wizard.current_step == 2 && $('.flavor a.disabled').hasClass('small')) {
34
                                return true;
35
                        }
36
                        else {
37
                                return false;
38
                        }
39
                }
40
        },
41
        network: {},
42

    
43
        getCurrent: function(){
44
                return $('.step').filter('.current').first();
45
        },
46

    
47
        getNextStep: function() {
48
        return ui.wizard.getCurrent().next();
49
    },
50

    
51
    getPreviousStep: function() {
52
        return ui.wizard.getCurrent().prev();
53
    },
54

    
55
    getScrollOffset: function() {
56
        return document.body.scrollTop;
57
    },
58

    
59
        submitData: function() {
60
                console.log('submit data dummy function');
61
        },
62

    
63
        data_next_array: ['el0', 'el2', 'el4', 'el7'],
64

    
65
        focusFun: function() {
66
                $('.firstfocus-' + ui.wizard.current_step + '').first().focus();
67
                $('.nav.next').removeAttr('data-next');
68
                $('.nav.next').attr('data-next', ui.wizard.data_next_array[ui.wizard.current_step]);
69
        },
70

    
71
        move: function(step, pos) {
72
                ui.wizard.semaphore = 0;
73
                $('.actions-bar').hide();
74
                ui.wizard.focusFun();
75
                ui.wizard.indicateStep(ui.wizard.current_step);
76
                ui.wizard.setMovementTags(ui.wizard.current_step, ui.wizard.btns.previous, ui.wizard.btns.next);
77
                $('body').css('overflow','hidden');
78
                // the current visible pane
79
        var current = this.getCurrent();
80
        // Set next pane position on the right of the current one
81
        // Add current to the next pane, so that it will become
82
        // visible
83
        step.css({
84
            left: pos.toString() + '%'
85
        }).addClass("current");
86
        // identify the current scroll position. Use it to
87
        // set next pane top position. We assume all panes
88
        // are within the scroll context of the main window.
89
        step.css({
90
            top: this.getScrollOffset() + 'px'
91
        });
92
        $('.step').animate({
93
            marginLeft: (-pos).toString() + '%'
94
        }, {
95
                                complete: _.bind(function() {
96
                // assuming all the following take place
97
                // instantaneously within a single browser 
98
                // render cycle, no flickering should occur.
99
                current.removeClass("current");
100
                            window.scroll(0, 0);
101
                step.css({
102
                    left: '0',
103
                    top: '0'
104
                });
105
                $('.step').css({
106
                    marginLeft: '0'
107
                });
108
                $('body').css('overflow','visible');
109
                if (ui.wizard.current_step == 3 ){
110
                                        $('.vm-name input').first().focus();
111
                }
112
                $('.actions-bar').show();
113
                ui.wizard.semaphore =1;
114
            }, this)
115
        });
116

    
117
        },
118

    
119
        goNext: function() {
120
                var next = ui.wizard.getNextStep();
121
                if (ui.wizard.semaphore == 1) {
122
                        ui.wizard.current_step++;
123
                        ui.wizard.move(next, 100);
124
                }
125
        },
126

    
127
        goPrev: function() {
128
                var prev = ui.wizard.getPreviousStep();
129
                if (ui.wizard.semaphore == 1) {
130
                        ui.wizard.current_step--;
131
                        ui.wizard.move(prev, -100);
132
                }
133
        },
134

    
135
        initEvents: function() {
136
                $(document).keydown(function(e) {
137
                        var exp1 = $('.vm-name input').is(':focus') && $('.vm-name input').val().length>0;
138
                        var exp2 = $('.form-item input').is(':focus') && $('.form-item input').val().length>0;
139
                        // right arrow keyCode == 39
140
                        if ($('.wizard:visible').length != 0) {
141
                                if (e.keyCode == 39 && ui.wizard.current_step != (ui.wizard.total_step) &&(!exp1) && (!exp2)) {
142
                                        ui.wizard.goNext();
143
                                        return false;
144
                                }
145
                                // left arrow keyCode == 37
146
                                else if (e.keyCode == 37 && ui.wizard.current_step != 1 &&(!exp1) &&(!exp2)) {
147
                                        ui.wizard.goPrev();
148
                                        return false;
149
                                }
150
                                // ESC
151
                                else if (e.keyCode == 27 && ui.wizard.current_step == 1) {
152
                                        ui.wizard.close();
153
                                }
154
                        }
155
                });
156

    
157
                ui.wizard.btns.next.click(function(e) {
158
                        if (ui.wizard.current_step == ui.wizard.total_step ){
159
                                ui.wizard.submitData();
160
                                ui.wizard.close();
161
                                return false;
162
                        }
163
                        ui.wizard.goNext();
164
                });
165

    
166
                ui.wizard.btns.previous.click(function(e) {
167
                        if (ui.wizard.current_step == 1 ){
168
                                ui.wizard.close();
169
                                return false;
170
                        }
171
                        ui.wizard.goPrev();
172
                });
173
        },
174

    
175
        // for the carousel index
176
        indicateStep: function(step) {
177
                $('.wizard .top .sub-menu[data-step]').hide();
178
                $('.wizard .top .sub-menu[data-step=' + step + ']').fadeIn();
179

    
180
                $('.nums').children().removeClass('current');
181
                $('.nums li:not(".current")').show().css("display", "inline");
182
                $('.nums li:nth-child(' + ui.wizard.current_step + ')').addClass('current');
183
                $('.nums li.current').hide();
184
                $('.nums li.current').fadeIn('slow').css("display", "inline");
185
                $('.nums .current').prevAll('li').hide();
186
        },
187

    
188
        // changes the text of next and previous buttons
189
        setMovementTags: function() {
190
                if (ui.wizard.current_step == 1) {
191
                        ui.wizard.btns.previous.find('span').html('CANCEL');
192
                        ui.wizard.btns.next.find('span').html('NEXT');
193
                } else if (ui.wizard.current_step == ui.wizard.total_step) {
194
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
195
                        ui.wizard.btns.next.find('span').html('CREATE');
196
                } else if (ui.wizard.vm.hideNext()){
197
                        ui.wizard.btns.next.hide();
198
                } else {
199
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
200
                        ui.wizard.btns.next.find('span').html('NEXT');
201
                }
202
        },
203

    
204
        close: function() {
205
                $('body').removeClass('with-overlay');
206
                $('.overlay-area').fadeOut(400, function() {
207
                        $('.overlay-div').hide();
208
                });
209
                ui.wizard.reset();
210
        },
211
        // manually sets elements to a initial state
212
        reset: function() {
213
                ui.wizard.current_step = 1;
214
                $('.step').removeAttr('style');
215
                $('.bottom').show();
216
                ui.wizard.indicateStep(ui.wizard.current_step);
217
                ui.wizard.setMovementTags();
218
                $('.networks-area .more').show();
219
                $('.details').hide();
220
                $('.vm-name input').val("");
221
                $('.form-item input').val("");
222
                $('.advanced-conf-options').hide();
223
                $('.snf-color-picker').hide();
224
                ui.wizard.preselectElements('.wizard');
225
        },
226

    
227
        expandArea: function() {
228
                ui.wizard.btns.expand_down.click(function(e) {
229
                        ui.expandArrow(ui.wizard.btns.expand_down);
230
                        $('.wizard-content').removeAttr('style');
231
                        ui.wizard.btns.expand_down.parents('div.advanced-conf-step').find('.advanced-conf-options').stop().slideToggle(600, function() {
232
                                if ($('.advanced-conf-options:visible').length != 0) {
233
                                        ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
234
                                } else {
235
                                        ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
236
                                }
237

    
238
                        });
239
                })
240
        },
241
        focusCustom: function(el, step) {
242
                el.focus();
243
                el.attr('data-step', step);
244
        },
245

    
246
        preselectElements: function(area) {
247
                $(area).find('.current').not('.preselected').removeClass('current');
248
                $(area).find('.preselected').not('.current').addClass('current');
249
                $(area).find('.custom.dropdown.medium a:first').addClass('current'); // to fix
250
                $(area).find('.snf-radio-checked').not('.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
251
                $(area).find('.snf-radio-unchecked.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
252
                $(area).find('.snf-checkbox-checked').not('.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
253
                $(area).find('.snf-checkbox-unchecked.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
254
                $('.expand-link').find('.snf-arrow-up.preselected').toggleClass('snf-arrow-up snf-arrow-down');
255
                $('.step-1').addClass('current');
256
                
257
                $('.os li').hide();
258
                $('#vm-wizard .top .sub-menu[data-step=1] .preselected').data('img-type')
259
                $('.os').find('.'+$('#vm-wizard .top .sub-menu[data-step=1] .preselected').data('img-type')).show();
260
        },
261

    
262

    
263
        showImageCategory: function(imagesCategory) {
264
                $(imagesCategory).closest('.sub-menu').find('.current').removeClass('current');
265
                $(imagesCategory).addClass('current');
266
                $('.os li').hide();
267
                $('.os .details').hide();
268
                selectedImages = $(imagesCategory).data('img-type');
269
                $('.os').find('.'+selectedImages).fadeIn();
270
                
271
        
272
        }
273

    
274
}
275

    
276

    
277
$(document).ready(function() {
278

    
279
        /*
280
Various functions for vm creation wizard
281
*/
282

    
283
        /* step functions */
284
        /* step-1: Pick OS */
285
        $('.wizard .os > li').keydown(function(e) {
286
          if(e.keyCode == 13) {
287
            $(this).trigger("click", true);
288
            e.preventDefault();
289
          }
290
        });
291

    
292
        $('.wizard .os > li').click(function(e, wasEnterKey) {
293
                e.preventDefault();
294
                if ( $(this).hasClass('current') && wasEnterKey) {
295
                        ui.wizard.goNext();
296
                }
297
                $('.wizard .os >li').removeClass('current');
298
                $(this).addClass('current');
299
        });
300

    
301
        $('.os .btn-col a').click(function(e) {
302
                e.preventDefault();
303
                e.stopPropagation();
304
                $(this).toggleClass('current');
305
                var self = this;
306
                $(this).parents('li').find('.details').stop().slideToggle();
307
        });
308

    
309

    
310
        /* step-2: Select flavor */
311
        var disabledElems = $('.flavor a.disabled');
312
        var disabledElemsNum = $('.flavor a.disabled').length;
313
        if(disabledElemsNum>0) {
314
                var size;
315
                for(i=0; i<disabledElemsNum; i++) {
316
                        size = ui.wizard.vm.getSize(disabledElems.get(i));
317
                        $('.wizard .sub-menu[data-step=2]').find('a[data-size=' + size + ']').removeClass('current').addClass('disabled');
318
                        $('#vm-wizard .flavor').find('.'+size).removeClass('current preselected');
319
                        if(size == 'small') {
320
                                $('#vm-wizard .flavor .vm-storage-selection a').removeClass('current preselected');
321

    
322
                        }
323
                }
324
        }
325
        $('.wizard .sub-menu a[data-size]:not(.disabled)').on("click", function(e) {
326
                // e.preventDefault();
327
                $(this).parents('.sub-menu').find('a').removeClass('current');
328
                $(this).addClass('current');
329
                ui.wizard.vm.pickResources($(this).data('size'));
330
        });
331

    
332
        $('.wizard .flavor .options:not(".vm-storage-selection") a:not(.disabled)').click(function(e) {
333
                // e.preventDefault();
334
                $('.wizard .sub-menu a[data-size]').removeClass('current');
335
                $(this).parents('.options').find('a').removeClass('current');
336
                $(this).addClass('current');
337
                var size = ui.wizard.vm.getSize(this);
338
                var count = $('.wizard .step-2 .options.with-flavor .' + size + '.current').length;
339
                if (count == 3) {
340
                        $('.wizard .sub-menu[data-step=2]').find('a[data-size=' + size + ']').addClass('current');
341
                }
342
        });
343

    
344
        $('.wizard .flavor .options.vm-storage-selection a').click(function(e) {
345
                // e.preventDefault();
346
                $(this).parents('.options').find('a').removeClass('current');
347
                $(this).addClass('current');
348
        });
349

    
350
        $('.flavor .options a').hover(
351
                function() {
352
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
353
                        var text = $(this).data('help');
354
                        paragraph.html(text);
355
                        paragraph.css('visibility', 'visible');
356
                }, function() {
357
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
358
                        paragraph.css('visibility', 'hidden');
359
                }
360
        );
361

    
362

    
363
        /* step-3: Advanced options */
364

    
365
        // reaction li.click   
366
        $('.advanced-conf-options .checkbox').click(function(e) {
367
                e.preventDefault();
368
                var self = this;
369
                var checkbox = $(this).find('.check');
370
                ui.changeCheckboxState(checkbox);
371
                if ($(this).hasClass('has-more')) {
372
                        $(this).next('.more').stop().slideToggle(400, function() {
373
                                if ($(self).next('.more:visible').length != 0) {
374
                                        $(checkbox).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
375
                                } else {
376
                                        $(checkbox).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
377
                                }
378
                        });
379
                }
380
        });
381

    
382

    
383

    
384
        // reaction a.click
385
        $('.checkbox .check').click(function(e) {
386
                e.stopPropagation();
387
                var self = this;
388
                if ($(this).closest('.checkbox').hasClass('has-more')) {
389
                        $(this).parent().next('.more').stop().slideToggle(400, function() {
390
                                if ($(self).parent().next('.more:visible').length != 0) {
391
                                        $(self).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
392
                                } else {
393
                                        $(self).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
394
                                }
395
                        });
396
                }
397
        });
398

    
399
        $('.show-add-tag').click(function(e) {
400
                e.preventDefault();
401
                $(this).parents('.tags-area').find('.snf-color-picker').slideDown('slow', function() {
402
                        $('#hide-add-tag-dummy').scrollintoview({
403
                                'duration': 'slow'
404
                        });
405
                });
406
                ui.colorPickerVisible = 1;
407
        });
408

    
409
        $('.hide-add-tag').click(function(e) {
410
                e.preventDefault();
411
                $(this).parents('.tags-area').find('.snf-color-picker').slideUp(400, function() {
412
                        $('.show-add-tag').first().scrollintoview({
413
                                'duration': 'slow'
414
                        });
415
                        ui.colorPickerVisible = 0;
416
                });
417
        });
418

    
419
        /* end of step functions */
420

    
421
        /* navigation and numbers functions */
422
        $('.nav.previous').focus(function(e) {
423
                $(this).addClass('active');
424
        });
425

    
426
        $('.nav.previous').focusout(function(e) {
427
                $(this).removeClass('active');
428

    
429
        });
430

    
431
        $('.sub-menu[data-step=2] li:last').find('a').focusout(function(e) {
432
                $('.step-2').find('.dropdown a:first').focus();
433

    
434
        });
435

    
436
        $('.os .name-col').focus(function(e) {
437
                $(this).parents('li').addClass('hover');
438
        });
439

    
440
        $('.os .name-col').focusout(function(e) {
441
                $(this).parents('li').removeClass('hover');
442
        });
443

    
444

    
445
        /* end of navigation and numbers functions */
446

    
447

    
448
        /* focus and tabs functionality */
449

    
450
        $('a').keyup(function(e) {
451

    
452
                var self = this;
453
                if (e.keyCode == 9 || e.which == 9) {
454
                        if (e.shiftKey) {
455
                        } else {
456
                                //Focus next input
457
                                if ($(self).attr('data-next')) {
458
                                        $(self).focusout(function(e) {
459
                                                var classname = $(self).data('next');
460
                                                $('.' + classname + '').first().focus();
461
                                        });
462
                                }
463
                        }
464
                }
465
        })
466

    
467
        /* end of focus and tabs functionality */
468

    
469
        $('#vm-wizard').find('a').click(function(e) {
470
                e.preventDefault();
471
        });
472
        ui.wizard.current_step = 1;
473
        ui.wizard.btns.previous = $('.bottom').find('.nav.prev');
474
        ui.wizard.btns.next = $('.bottom').find('.nav.next');
475
        ui.wizard.btns.expand_down = $('.adv-main').find('.expand-link');
476
        ui.wizard.btns.close = $('#vm-wizard').find('.close');
477
        ui.wizard.initEvents();
478
        ui.wizard.setMovementTags();
479
        ui.wizard.expandArea();
480
        ui.wizard.btns.close.click(function() {
481
                ui.wizard.close();
482
        });
483
        
484
        $('.os .system-images').show();
485

    
486
        $('#vm-wizard .top .sub-menu[data-step=1] a').click(function() {
487
                ui.wizard.showImageCategory(this);
488
        })
489
});