Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.7 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

    
15
        setDimensions: function() {
16
                $('.vm-wizard-carousel').css('width', 100 * ui.wizard.total_step + '%');
17
                $('.vm-wizard-carousel .step').css('width', 100 / ui.wizard.total_step + '%');
18
        },
19
        getCurrent: function(){
20
                return $('.step').filter('.current');
21
        },
22

    
23
        getNextStep: function() {
24
        return ui.wizard.getCurrent().next();
25
    },
26

    
27
    getPreviousStep: function() {
28
        return ui.wizard.getCurrent().prev();
29
    },
30

    
31
    getScrollOffset: function() {
32
        return document.body.scrollTop;
33
    },
34

    
35
        submitData: function() {
36
                console.log('submit data dummy function');
37
        },
38

    
39
        data_next_array: ['el0', 'el2', 'el4', 'el7'],
40

    
41
        focusFun: function() {
42
                $('.firstfocus-' + ui.wizard.current_step + '').first().focus();
43
                $('.nav.next').removeAttr('data-next');
44
                $('.nav.next').attr('data-next', ui.wizard.data_next_array[ui.wizard.current_step]);
45
        },
46

    
47
        move: function(step, pos) {
48

    
49
                ui.wizard.focusFun();
50
                ui.wizard.indicateStep(ui.wizard.current_step);
51
                ui.wizard.setMovementTags(ui.wizard.current_step, ui.wizard.btns.previous, ui.wizard.btns.next);
52
                $('body').css('overflow','hidden');
53
                // the current visible pane
54
        current = this.getCurrent();
55
        // Set next pane position on the right of the current one
56
        // Add current to the next pane, so that it will become
57
        // visible
58
        console.log('1');
59
        step.css({
60
            left: pos.toString() + '%'
61
        }).addClass("current");
62

    
63
        // identify the current scroll position. Use it to
64
        // set next pane top position. We assume all panes
65
        // are within the scroll context of the main window.
66
        step.css({
67
            top: this.getScrollOffset() + 'px'
68
        });
69
        window.scroll(0, 0);
70
        $('.step').finish().animate({
71
            marginLeft: (-pos).toString() + '%'
72
        }, {
73
            complete: _.bind(function() {
74
                // assuming all the following take place
75
                // instantaneously within a single browser 
76
                // render cycle, no flickering should occur.
77
                current.removeClass("current");
78
                step.css({
79
                    left: '0',
80
                    top: '0'
81
                });
82
                $('.step').css({
83
                    marginLeft: '0'
84
                });
85
                $('body').css('overflow','visible');
86
                if (ui.wizard.current_step == 3 ){
87
                                        $('.vm-name input').first().focus();
88
                }
89
            }, this)
90
        });
91
        },
92

    
93
        goNext: function() {
94
                var next = ui.wizard.getNextStep();
95
                if (!next) {
96
                        ui.wizard.submitData();
97
                        ui.wizard.close();
98
                        return;
99
                }
100
                ui.wizard.current_step++;
101
                ui.wizard.move(next, 100);
102
        },
103

    
104
        goPrev: function() {
105
                var prev = ui.wizard.getPreviousStep();
106
                if (!prev) {
107
                        ui.wizard.close();
108
                        return;
109
                }
110
                ui.wizard.current_step--;
111
                ui.wizard.move(prev, -100);
112
        },
113

    
114
        initEvents: function() {
115
                ui.wizard.setDimensions();
116

    
117

    
118
                $(document).keydown(function(e) {
119
                        var exp = $('.vm-name input').is(':focus') && $('.vm-name input').val().length>0 && ui.wizard.current_step ==3;
120
                        console.log('exp',exp);
121
                        // right arrow keyCode == 39
122
                        if ($('.wizard:visible').length != 0) {
123
                                if (e.keyCode == 39 && ui.wizard.current_step != (ui.wizard.total_step) &&(!exp)) {
124
                                        ui.wizard.goNext();
125
                                        return false;
126
                                }
127
                                // left arrow keyCode == 37
128
                                else if (e.keyCode == 37 && ui.wizard.current_step != 1 &&(!exp)) {
129
                                        ui.wizard.goPrev();
130
                                        return false;
131
                                }
132
                                // ESC
133
                                else if (e.keyCode == 27 && ui.wizard.current_step == 1) {
134
                                        ui.wizard.close();
135
                                }
136
                        }
137
                });
138

    
139
                ui.wizard.btns.next.click(function(e) {
140
                        if (ui.wizard.current_step == ui.wizard.total_step ){
141
                                ui.wizard.submitData();
142
                                ui.wizard.close();
143
                                return false;
144
                        }
145
                        ui.wizard.goNext();
146
                });
147

    
148
                ui.wizard.btns.previous.click(function(e) {
149
                        if (ui.wizard.current_step == 1 ){
150
                                ui.wizard.close();
151
                                return false;
152
                        }
153
                        ui.wizard.goPrev();
154
                });
155
        },
156

    
157
        // for the carousel index
158
        indicateStep: function(step) {
159
                $('.wizard .top .sub-menu[data-step]').hide();
160
                $('.wizard .top .sub-menu[data-step=' + step + ']').fadeIn();
161

    
162
                $('.nums').children().removeClass('current');
163
                $('.nums li:not(".current")').show().css("display", "inline");
164
                $('.nums li:nth-child(' + ui.wizard.current_step + ')').addClass('current');
165
                $('.nums li.current').hide();
166
                $('.nums li.current').fadeIn('slow').css("display", "inline");
167
                $('.nums .current').prevAll('li').hide();
168
        },
169

    
170
        // changes the text of next and previous buttons
171
        setMovementTags: function() {
172
                if (ui.wizard.current_step == 1) {
173
                        ui.wizard.btns.previous.find('span').html('CANCEL');
174
                        ui.wizard.btns.next.find('span').html('NEXT');
175
                } else if (ui.wizard.current_step == ui.wizard.total_step) {
176
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
177
                        ui.wizard.btns.next.find('span').html('CREATE');
178
                } else {
179
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
180
                        ui.wizard.btns.next.find('span').html('NEXT');
181
                }
182
        },
183

    
184
        close: function() {
185
                $('body').removeClass('with-overlay');
186
                $('.overlay-area').fadeOut(400, function() {
187
                        $('.overlay-div').hide();
188
                });
189
                ui.wizard.resetWizard();
190
        },
191
        // manually sets elements to a initial state
192
        resetWizard: function() {
193
                ui.wizard.current_step = 1;
194
                $('.step').removeAttr('style');
195
                $('.bottom').show();
196
                ui.wizard.indicateStep(ui.wizard.current_step);
197
                ui.wizard.setMovementTags();
198
                $('.networks-area .more').show();
199
                $('.details').hide();
200
                $('.vm-name input').val("");
201
                $('.form-item input').val("");
202
                $('.advanced-conf-options').hide();
203
                $('.snf-color-picker').hide();
204
                ui.wizard.preselectElements('.wizard');
205
        },
206

    
207
        expandArea: function() {
208
                ui.wizard.btns.expand_down.click(function(e) {
209
                        ui.expandArrow(ui.wizard.btns.expand_down);
210
                        $('.wizard-content').removeAttr('style');
211
                        ui.wizard.btns.expand_down.parents('div.advanced-conf-step').find('.advanced-conf-options').stop().slideToggle(600, function() {
212
                                if ($('.advanced-conf-options:visible').length != 0) {
213
                                        ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
214
                                } else {
215
                                        ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
216
                                }
217

    
218
                        });
219
                })
220
        },
221
        focusCustom: function(el, step) {
222
                el.focus();
223
                el.attr('data-step', step);
224
        },
225

    
226
        preselectElements: function(area) {
227
                $(area).find('.current').not('.preselected').removeClass('current');
228
                $(area).find('.preselected').not('.current').addClass('current');
229
                $(area).find('.custom.dropdown.medium a:first').addClass('current'); // to fix
230
                $(area).find('.snf-radio-checked').not('.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
231
                $(area).find('.snf-radio-unchecked.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
232
                $(area).find('.snf-checkbox-checked').not('.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
233
                $(area).find('.snf-checkbox-unchecked.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
234
                $('.expand-link').find('.snf-arrow-up.preselected').toggleClass('snf-arrow-up snf-arrow-down');
235
                $('.step-1').addClass('current');
236
        },
237

    
238
        pickResources: function(resource) {
239
                $('.flavor .with-flavor a:not(.' + resource + ')').removeClass('current');
240
                $('.flavor .with-flavor a.' + resource + '').addClass('current');
241
        }
242

    
243
}
244

    
245

    
246
$(document).ready(function() {
247

    
248
        /*
249
Various functions for vm creation wizard
250
*/
251

    
252
        /* step functions */
253
        /* step-1: Pick OS */
254
        $('.wizard .os > li').click(function(e) {
255
                e.preventDefault();
256
                $('.wizard .os >li').removeClass('current');
257
                $(this).addClass('current');
258
        });
259

    
260
        $('.os .btn-col a').click(function(e) {
261
                e.preventDefault();
262
                e.stopPropagation();
263
                $(this).toggleClass('current');
264
                var self = this;
265
                $(this).parents('li').find('.details').stop().slideToggle();
266
        });
267

    
268

    
269
        /* step-2: Select flavor */
270
        $('.wizard .sub-menu a[data-size]').on("click", function(e) {
271
                // e.preventDefault();
272
                $(this).parents('.sub-menu').find('a').removeClass('current');
273
                $(this).addClass('current');
274
                ui.wizard.pickResources($(this).data('size'));
275
        });
276

    
277
        $('.wizard .flavor .options:not(".vm-storage-selection") a').click(function(e) {
278
                // e.preventDefault();
279
                $('.wizard .sub-menu a[data-size]').removeClass('current');
280
                $(this).parents('.options').find('a').removeClass('current');
281
                $(this).addClass('current');
282

    
283
                var size, count;
284
                if ($(this).hasClass('small')) {
285
                        size = 'small';
286
                } else if ($(this).hasClass('medium')) {
287
                        size = 'medium';
288
                } else if ($(this).hasClass('large')) {
289
                        size = 'large';
290
                }
291

    
292
                count = $('.wizard .step-2 .options.with-flavor .' + size + '.current').length;
293
                if (count == 3) {
294
                        $('.wizard .sub-menu[data-step=2]').find('a[data-size=' + size + ']').addClass('current');
295
                }
296
        });
297

    
298
        $('.wizard .flavor .options.vm-storage-selection a').click(function(e) {
299
                // e.preventDefault();
300
                $(this).parents('.options').find('a').removeClass('current');
301
                $(this).addClass('current');
302
        });
303

    
304
        $('.flavor .options a').hover(
305
                function() {
306
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
307
                        var text = $(this).data('help');
308
                        paragraph.html(text);
309
                        paragraph.css('visibility', 'visible');
310
                }, function() {
311
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
312
                        paragraph.css('visibility', 'hidden');
313
                }
314
        );
315
        /* step-3: Advanced options */
316

    
317
        // reaction li.click   
318
        $('.advanced-conf-options .checkbox').click(function(e) {
319
                e.preventDefault();
320
                var self = this;
321
                var checkbox = $(this).find('.check');
322
                ui.changeCheckboxState(checkbox);
323
                if ($(this).hasClass('has-more')) {
324
                        $(this).next('.more').stop().slideToggle(400, function() {
325
                                if ($(self).next('.more:visible').length != 0) {
326
                                        $(checkbox).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
327
                                } else {
328
                                        $(checkbox).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
329
                                }
330
                        });
331
                }
332
        });
333

    
334

    
335

    
336
        // reaction a.click
337
        $('.checkbox .check').click(function(e) {
338
                e.stopPropagation();
339
                var self = this;
340
                if ($(this).closest('.checkbox').hasClass('has-more')) {
341
                        console.log('hi');
342
                        $(this).parent().next('.more').stop().slideToggle(400, function() {
343
                                console.log('a2');
344
                                if ($(self).parent().next('.more:visible').length != 0) {
345
                                        $(self).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
346
                                        console.log('a3');
347
                                } else {
348
                                        $(self).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
349
                                }
350
                        });
351
                }
352
        });
353

    
354
        $('.show-add-tag').click(function(e) {
355
                e.preventDefault();
356
                $(this).parents('.tags-area').find('.snf-color-picker').slideDown('slow', function() {
357
                        $('#hide-add-tag-dummy').scrollintoview({
358
                                'duration': 'slow'
359
                        });
360
                });
361
                ui.colorPickerVisible = 1;
362
        });
363

    
364
        $('.hide-add-tag').click(function(e) {
365
                e.preventDefault();
366
                $(this).parents('.tags-area').find('.snf-color-picker').slideUp(400, function() {
367
                        $('.show-add-tag').first().scrollintoview({
368
                                'duration': 'slow'
369
                        });
370
                        ui.colorPickerVisible = 0;
371
                });
372
        });
373

    
374
        /* end of step functions */
375

    
376
        /* navigation and numbers functions */
377
        $('.nav.previous').focus(function(e) {
378
                $(this).addClass('active');
379
        });
380

    
381
        $('.nav.previous').focusout(function(e) {
382
                $(this).removeClass('active');
383

    
384
        });
385

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

    
389
        });
390

    
391
        $('.os .name-col').focus(function(e) {
392
                $(this).parents('li').addClass('hover');
393
        });
394

    
395
        $('.os .name-col').focusout(function(e) {
396
                $(this).parents('li').removeClass('hover');
397
        });
398

    
399

    
400
        /* end of navigation and numbers functions */
401

    
402

    
403
        /* focus and tabs functionality */
404

    
405
        $('a').keyup(function(e) {
406
                var self = this;
407
                if (e.keyCode == 9 || e.which == 9) {
408
                        if (e.shiftKey) {} else {
409
                                //Focus next input
410
                                if ($(self).attr('data-next')) {
411
                                        $(self).focusout(function(e) {
412
                                                var classname = $(self).data('next');
413
                                                console.log('tab goes to ', classname);
414
                                                $('.' + classname + '').first().focus();
415
                                        });
416
                                }
417
                        }
418
                }
419
        })
420

    
421
        /* end of focus and tabs functionality */
422

    
423
        $('#vm-wizard').find('a').click(function(e) {
424
                e.preventDefault();
425
        });
426
        ui.wizard.current_step = 1;
427
        ui.wizard.btns.previous = $('.bottom').find('.nav.prev');
428
        ui.wizard.btns.next = $('.bottom').find('.nav.next');
429
        ui.wizard.btns.expand_down = $('.adv-main').find('.expand-link');
430
        ui.wizard.btns.close = $('#vm-wizard').find('.close');
431
        ui.wizard.initEvents();
432
        ui.wizard.setMovementTags();
433
        ui.wizard.expandArea();
434
        ui.wizard.btns.close.click(function() {
435
                ui.wizard.close();
436
        });
437
});