Statistics
| Branch: | Tag: | Revision:

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

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

    
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
        $('.step').animate({
70
            marginLeft: (-pos).toString() + '%'
71
        }, {
72
            complete: _.bind(function() {
73
                // assuming all the following take place
74
                // instantaneously within a single browser 
75
                // render cycle, no flickering should occur.
76
                current.removeClass("current");
77
                window.scroll(0, 0);
78
                step.css({
79
                    left: '0',
80
                    top: '0'
81
                });
82
                $('.step').css({
83
                    marginLeft: '0'
84
                });
85
                $('body').css('overflow','visible');
86
            }, this)
87
        });
88
        },
89

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

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

    
112
        initEvents: function() {
113
                ui.wizard.setDimensions();
114
                $(document).keydown(function(e) {
115
                        // right arrow keyCode == 39
116
                        if ($('.wizard:visible').length != 0) {
117
                                if (e.keyCode == 39 && ui.wizard.current_step != (ui.wizard.total_step)) {
118
                                        ui.wizard.goNext();
119
                                        return false;
120
                                }
121
                                // left arrow keyCode == 37
122
                                else if (e.keyCode == 37 && ui.wizard.current_step != 1) {
123
                                        ui.wizard.goPrev();
124
                                        return false;
125
                                }
126
                                // ESC
127
                                else if (e.keyCode == 27 && ui.wizard.current_step == 1) {
128
                                        ui.wizard.close();
129
                                }
130
                        }
131
                });
132

    
133
                ui.wizard.btns.next.click(function(e) {
134
                        ui.wizard.goNext();
135
                })
136

    
137
                ui.wizard.btns.previous.click(function(e) {
138
                        ui.wizard.goPrev();
139
                });
140
        },
141

    
142
        // for the carousel index
143
        indicateStep: function(step) {
144
                $('.wizard .top .sub-menu[data-step]').hide();
145
                $('.wizard .top .sub-menu[data-step=' + step + ']').fadeIn();
146

    
147
                $('.nums').children().removeClass('current');
148
                $('.nums li:not(".current")').show().css("display", "inline");
149
                $('.nums li:nth-child(' + ui.wizard.current_step + ')').addClass('current');
150
                $('.nums li.current').hide();
151
                $('.nums li.current').fadeIn('slow').css("display", "inline");
152
                $('.nums .current').prevAll('li').hide();
153
        },
154

    
155
        // changes the text of next and previous buttons
156
        setMovementTags: function() {
157
                if (ui.wizard.current_step == 1) {
158
                        ui.wizard.btns.previous.find('span').html('CANCEL');
159
                        ui.wizard.btns.next.find('span').html('NEXT');
160
                } else if (ui.wizard.current_step == ui.wizard.total_step) {
161
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
162
                        ui.wizard.btns.next.find('span').html('CREATE');
163
                } else {
164
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
165
                        ui.wizard.btns.next.find('span').html('NEXT');
166
                }
167
        },
168

    
169
        close: function() {
170
                $('body').removeClass('with-overlay');
171
                $('.overlay-area').fadeOut(400, function() {
172
                        $('.overlay-div').hide();
173
                });
174
                ui.wizard.resetWizard();
175
        },
176
        // manually sets elements to a initial state
177
        resetWizard: function() {
178
                ui.wizard.current_step = 1;
179
                $('.step').removeAttr('style');
180
                $('.bottom').show();
181
                ui.wizard.indicateStep(ui.wizard.current_step);
182
                ui.wizard.setMovementTags();
183
                $('.networks-area .more').show();
184
                $('.details').hide();
185
                $('.vm-name input').removeAttr('value');
186
                $('.advanced-conf-options').hide();
187
                $('.snf-color-picker').hide();
188
                ui.wizard.preselectElements('.wizard');
189
        },
190

    
191
        expandArea: function() {
192
                ui.wizard.btns.expand_down.click(function(e) {
193
                        ui.expandArrow(ui.wizard.btns.expand_down);
194
                        $('.wizard-content').removeAttr('style');
195
                        ui.wizard.btns.expand_down.parents('div.advanced-conf-step').find('.advanced-conf-options').stop().slideToggle(600, function() {
196
                                if ($('.advanced-conf-options:visible').length != 0) {
197
                                        ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
198
                                } else {
199
                                        ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
200
                                }
201

    
202
                        });
203
                })
204
        },
205
        focusCustom: function(el, step) {
206
                el.focus();
207
                el.attr('data-step', step);
208
        },
209

    
210
        preselectElements: function(area) {
211
                $(area).find('.current').not('.preselected').removeClass('current');
212
                $(area).find('.preselected').not('.current').addClass('current');
213
                $(area).find('.custom.dropdown.medium a:first').addClass('current'); // to fix
214
                $(area).find('.snf-radio-checked').not('.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
215
                $(area).find('.snf-radio-unchecked.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
216
                $(area).find('.snf-checkbox-checked').not('.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
217
                $(area).find('.snf-checkbox-unchecked.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
218
                $('.expand-link').find('.snf-arrow-up.preselected').toggleClass('snf-arrow-up snf-arrow-down');
219
                $('.step-1').addClass('current');
220
        },
221

    
222
        pickResources: function(resource) {
223
                $('.flavor .with-flavor a:not(.' + resource + ')').removeClass('current');
224
                $('.flavor .with-flavor a.' + resource + '').addClass('current');
225
        }
226

    
227
}
228

    
229

    
230
$(document).ready(function() {
231

    
232
        /*
233
Various functions for vm creation wizard
234
*/
235

    
236
        /* step functions */
237
        /* step-1: Pick OS */
238
        $('.wizard .os > li').click(function(e) {
239
                e.preventDefault();
240
                $('.wizard .os >li').removeClass('current');
241
                $(this).addClass('current');
242
        });
243

    
244
        $('.os .btn-col a').click(function(e) {
245
                e.preventDefault();
246
                e.stopPropagation();
247
                $(this).toggleClass('current');
248
                var self = this;
249
                $(this).parents('li').find('.details').stop().slideToggle();
250
        });
251

    
252

    
253
        /* step-2: Select flavor */
254
        $('.wizard .sub-menu a[data-size]').on("click", function(e) {
255
                // e.preventDefault();
256
                $(this).parents('.sub-menu').find('a').removeClass('current');
257
                $(this).addClass('current');
258
                ui.wizard.pickResources($(this).data('size'));
259
        });
260

    
261
        $('.wizard .flavor .options:not(".vm-storage-selection") a').click(function(e) {
262
                // e.preventDefault();
263
                $('.wizard .sub-menu a[data-size]').removeClass('current');
264
                $(this).parents('.options').find('a').removeClass('current');
265
                $(this).addClass('current');
266

    
267
                var size, count;
268
                if ($(this).hasClass('small')) {
269
                        size = 'small';
270
                } else if ($(this).hasClass('medium')) {
271
                        size = 'medium';
272
                } else if ($(this).hasClass('large')) {
273
                        size = 'large';
274
                }
275

    
276
                count = $('.wizard .step-2 .options.with-flavor .' + size + '.current').length;
277
                if (count == 3) {
278
                        $('.wizard .sub-menu[data-step=2]').find('a[data-size=' + size + ']').addClass('current');
279
                }
280
        });
281

    
282
        $('.wizard .flavor .options.vm-storage-selection a').click(function(e) {
283
                // e.preventDefault();
284
                $(this).parents('.options').find('a').removeClass('current');
285
                $(this).addClass('current');
286
        });
287

    
288
        $('.flavor .options a').hover(
289
                function() {
290
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
291
                        var text = $(this).data('help');
292
                        paragraph.html(text);
293
                        paragraph.css('visibility', 'visible');
294
                }, function() {
295
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
296
                        paragraph.css('visibility', 'hidden');
297
                }
298
        );
299
        /* step-3: Advanced options */
300

    
301
        // reaction li.click   
302
        $('.advanced-conf-options .checkbox').click(function(e) {
303
                e.preventDefault();
304
                var self = this;
305
                var checkbox = $(this).find('.check');
306
                ui.changeCheckboxState(checkbox);
307
                if ($(this).hasClass('has-more')) {
308
                        $(this).next('.more').stop().slideToggle(400, function() {
309
                                if ($(self).next('.more:visible').length != 0) {
310
                                        $(checkbox).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
311
                                } else {
312
                                        $(checkbox).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
313
                                }
314
                        });
315
                }
316
        });
317

    
318

    
319

    
320
        // reaction a.click
321
        $('.checkbox .check').click(function(e) {
322
                e.stopPropagation();
323
                var self = this;
324
                if ($(this).closest('.checkbox').hasClass('has-more')) {
325
                        console.log('hi');
326
                        $(this).parent().next('.more').stop().slideToggle(400, function() {
327
                                console.log('a2');
328
                                if ($(self).parent().next('.more:visible').length != 0) {
329
                                        $(self).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
330
                                        console.log('a3');
331
                                } else {
332
                                        $(self).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
333
                                }
334
                        });
335
                }
336
        });
337

    
338
        $('.show-add-tag').click(function(e) {
339
                e.preventDefault();
340
                $(this).parents('.tags-area').find('.snf-color-picker').slideDown('slow', function() {
341
                        $('#hide-add-tag-dummy').scrollintoview({
342
                                'duration': 'slow'
343
                        });
344
                });
345
                ui.colorPickerVisible = 1;
346
        });
347

    
348
        $('.hide-add-tag').click(function(e) {
349
                e.preventDefault();
350
                $(this).parents('.tags-area').find('.snf-color-picker').slideUp(400, function() {
351
                        $('.show-add-tag').first().scrollintoview({
352
                                'duration': 'slow'
353
                        });
354
                        ui.colorPickerVisible = 0;
355
                });
356
        });
357

    
358
        /* end of step functions */
359

    
360
        /* navigation and numbers functions */
361
        $('.nav.previous').focus(function(e) {
362
                $(this).addClass('active');
363
        });
364

    
365
        $('.nav.previous').focusout(function(e) {
366
                $(this).removeClass('active');
367

    
368
        });
369

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

    
373
        });
374

    
375
        $('.os .name-col').focus(function(e) {
376
                $(this).parents('li').addClass('hover');
377
        });
378

    
379
        $('.os .name-col').focusout(function(e) {
380
                $(this).parents('li').removeClass('hover');
381
        });
382

    
383

    
384
        /* end of navigation and numbers functions */
385

    
386

    
387
        /* focus and tabs functionality */
388

    
389
        $('a').keyup(function(e) {
390
                var self = this;
391
                if (e.keyCode == 9 || e.which == 9) {
392
                        if (e.shiftKey) {} else {
393
                                //Focus next input
394
                                if ($(self).attr('data-next')) {
395
                                        $(self).focusout(function(e) {
396
                                                var classname = $(self).data('next');
397
                                                console.log('tab goes to ', classname);
398
                                                $('.' + classname + '').first().focus();
399
                                        });
400
                                }
401
                        }
402
                }
403
        })
404

    
405
        /* end of focus and tabs functionality */
406

    
407
        $('#vm-wizard').find('a').click(function(e) {
408
                e.preventDefault();
409
        });
410
        ui.wizard.current_step = 1;
411
        ui.wizard.btns.previous = $('.bottom').find('.nav.prev');
412
        ui.wizard.btns.next = $('.bottom').find('.nav.next');
413
        ui.wizard.btns.expand_down = $('.adv-main').find('.expand-link');
414
        ui.wizard.btns.close = $('#vm-wizard').find('.close');
415
        ui.wizard.initEvents();
416
        ui.wizard.setMovementTags();
417
        ui.wizard.expandArea();
418
        ui.wizard.btns.close.click(function() {
419
                ui.wizard.close();
420
        });
421
});