Statistics
| Branch: | Tag: | Revision:

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

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

    
20
        submitData: function (){
21
                console.log('submit data dummy function');
22
        },
23

    
24
        data_next_array: ['test1','el2','el4','el7'],
25

    
26
        focusFun: function(){
27
                // $('.nav.next').attr('data-step',ui.wizard.current_step);
28
                // $('.nav.next').removeAttr('data-next');
29
                // $('.nav.next').attr('data-next', ui.wizard.data_next_array[ui.wizard.current_step]);
30
                // $('.'+ui.wizard.data_next_array[ui.wizard.current_step]+'').first().focus();
31
                // console.log('focusFun',ui.wizard.data_next_array[ui.wizard.current_step]);
32
        },
33

    
34
        move: function () {
35
                var percentage = -(ui.wizard.current_step-1)*100+'%';
36
                $('#dummy-link-'+ui.wizard.current_step+'').scrollintoview({
37
                        'duration':0,
38
                });
39
                ui.wizard.setStepHeight($('.step-'+ui.wizard.current_step+''));
40
                $('.vm-wizard-carousel').stop(true, true).animate({ 'left': percentage }, ui.wizard.speed);
41
                ui.wizard.focusFun();
42
                ui.wizard.indicateStep(ui.wizard.current_step);
43
                ui.wizard.setMovementTags(ui.wizard.current_step, ui.wizard.btns.previous, ui.wizard.btns.next);
44
                 if(ui.wizard.current_step == 3 && $('.step-3 .advanced-conf-options:visible').length == 0) {
45
                        setTimeout(function() { $('.vm-name').find('input').focus() }, ui.wizard.speed/2);
46
                }
47
        },
48

    
49
        goNext: function () {
50
                if(ui.wizard.current_step < ui.wizard.total_step){
51
                        ui.wizard.current_step++;
52
                        ui.wizard.move();
53
                }
54
                else {
55
                        ui.wizard.submitData();
56
                        ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area');
57
                }
58
        },
59

    
60
        goPrev:function() {
61
                if(ui.wizard.current_step > 1){
62
                        ui.wizard.current_step--;
63
                        ui.wizard.move();
64
                }
65
                else {
66
                        ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area');
67
                }
68
        },
69

    
70
        initEvents: function(){
71
                ui.wizard.setDimensions();
72
                $(document).keydown(function(e) {
73
                        // right arrow keyCode == 39
74
                        if($('.vm-wizard-carousel:visible').length != 0) {
75
                                if(e.keyCode==39 && ui.wizard.current_step!=(ui.wizard.total_step)) {
76
                                        ui.wizard.goNext();
77
                                        return false;
78
                                }
79
                                // left arrow keyCode == 37
80
                                else if(e.keyCode==37 && ui.wizard.current_step!=1) {
81
                                        ui.wizard.goPrev();
82
                                        return false;
83
                                }
84
                                // ESC
85
                                else if(e.keyCode== 27 && ui.wizard.current_step==1) {
86
                                        ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area')
87
                                }
88
                        }
89
                });
90
                
91
                ui.wizard.btns.next.click(function(e){
92
                        ui.wizard.goNext();
93
                })
94

    
95
                ui.wizard.btns.previous.click(function(e){
96
                        ui.wizard.goPrev();
97
                });
98
        },
99

    
100
        // for the carousel index
101
        indicateStep: function(step) {
102
                $('.wizard .top .sub-menu[data-step]').hide();
103
                $('.wizard .top .sub-menu[data-step='+step+']').fadeIn();
104
                $('.nums').children().removeClass('current');
105
                $('.nums li').show();
106
                $('.nums li:nth-child('+ui.wizard.current_step+')').addClass('current');
107
                $('.nums .current').prevAll('li').hide();
108
        },
109

    
110
        // changes the text of next and previous buttons
111
        setMovementTags: function() {
112
                if (ui.wizard.current_step==1) {
113
                        ui.wizard.btns.previous.find('span').html('CANCEL');
114
                        ui.wizard.btns.next.find('span').html('NEXT');
115
                }
116
                else if(ui.wizard.current_step==ui.wizard.total_step) {
117
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
118
                        ui.wizard.btns.next.find('span').html('CREATE');
119
                }
120
                else {
121
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
122
                        ui.wizard.btns.next.find('span').html('NEXT');
123
                }
124
        },
125

    
126
        close: function(bottom_area, main_area, total_area) {
127
                $('.step').animate({
128
                        scrollTop: 0}, 1400, 'swing', function() {
129
                                $(bottom_area).hide();
130
                                $(main_area).slideUp(600);
131
                                $(total_area).slideUp(600, function(){
132
                                        ui.wizard.reset();
133
                                        ui.wizard.preselectElements(bottom_area);
134
                            ui.wizard.preselectElements(main_area);
135
                            ui.wizard.preselectElements(total_area);
136
                                });
137
                        });
138
        },
139

    
140
        // manually sets elements to a initial state
141
        reset: function() {
142
                  ui.wizard.current_step = 1;
143
        $('.vm-wizard-carousel').css('left',0);
144
        $('.bottom').show();
145
        ui.wizard.indicateStep(ui.wizard.current_step);
146
        ui.wizard.setMovementTags();
147
        $('.networks-area .more').show();
148
        $('.details').hide();
149
        $('.vm-name input').val('');
150
        $('.advanced-conf-options').hide();
151
        $('.snf-color-picker').hide();
152
        },
153

    
154
        expandArea: function() {
155
                ui.wizard.btns.expand_down.click(function(e){
156
            ui.expandArrow(ui.wizard.btns.expand_down);
157
            $('.wizard-content').removeAttr('style');
158
            ui.wizard.btns.expand_down.parents('div.advanced-conf-step').find('.advanced-conf-options').stop().slideToggle(600, function() {
159
                                ;ui.wizard.setStepHeight($('.step-3'));
160
                        if($('.advanced-conf-options:visible').length != 0) {
161
                                ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
162
                        } 
163
                        else {
164
                                ui.wizard.btns.expand_down.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
165
                        }
166
                });
167
            })
168
        },
169
        focusCustom: function(el, step) {
170
                el.focus();
171
                el.attr('data-step',step);
172
        },
173

    
174
        preselectElements: function(area) {
175
        $(area).find('.current').not('.preselected').removeClass('current');
176
        $(area).find('.preselected').not('.current').addClass('current');
177
        $(area).find('.custom.dropdown.medium a:first').addClass('current'); // to fix
178
        $(area).find('.snf-radio-checked').not('.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
179
        $(area).find('.snf-radio-unchecked.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
180
        $(area).find('.snf-checkbox-checked').not('.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
181
        $(area).find('.snf-checkbox-unchecked.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
182
        $('.expand-link').find('.snf-arrow-up.preselected').toggleClass('snf-arrow-up snf-arrow-down');
183
     },
184

    
185
    setStepHeight: function(stepEl) {
186
                var h1 = stepEl.height();
187
                var h2 = $('.wizard .top').height();
188
                var res =  h1 +h2;
189
                console.log(res);
190
            $('.wizard-content').css('height',res);
191
        },
192

    
193
        pickResources: function(resource) {
194
            $('.flavor .with-flavor a:not(.'+resource+')').removeClass('current');
195
            $('.flavor .with-flavor a.'+resource+'').addClass('current');
196
        }
197

    
198
}
199

    
200

    
201
$(document).ready(function(){
202

    
203
/*
204
Various functions for vm creation wizard
205
*/
206

    
207
/* step functions */
208
/* step-1: Pick OS */
209
         $('.wizard .os > li').click(function(e){
210
        e.preventDefault();
211
        $('.wizard .os >li').removeClass('current');
212
        $(this).addClass('current');
213
    });
214

    
215
    $('.os .btn-col a').click( function(e){
216
        e.preventDefault();
217
        e.stopPropagation();
218
        $(this).toggleClass('current');
219
        var self = this;
220
        $(this).parents('li').find('.details').stop().slideToggle('slow', function(){
221
                        ui.wizard.setStepHeight($('.step-1'));
222
        });
223
    });
224

    
225

    
226
/* step-2: Select flavor */
227
    $('.wizard .sub-menu a[data-size]').on( "click", function(e) {
228
       // e.preventDefault();
229
        $(this).parents('.sub-menu').find('a').removeClass('current');
230
        $(this).addClass('current');
231
        ui.wizard.pickResources($(this).data('size')); 
232
    });
233

    
234
    $('.wizard .flavor .options:not(".vm-storage-selection") a').click(function(e){
235
        // e.preventDefault();
236
        $('.wizard .sub-menu a[data-size]').removeClass('current');
237
        $(this).parents('.options').find('a').removeClass('current');
238
        $(this).addClass('current');
239
    });
240

    
241
          $('.wizard .flavor .options.vm-storage-selection a').click(function(e){
242
        // e.preventDefault();
243
        $(this).parents('.options').find('a').removeClass('current');
244
        $(this).addClass('current');
245
    });
246

    
247
/* step-3: Advanced options */
248

    
249
        // reaction li.click   
250
    $('.advanced-conf-options .checkbox').click(function(e){
251
        e.preventDefault();
252
        var self = this;
253
        var checkbox = $(this).find('.check');
254
        ui.changeCheckboxState(checkbox);
255
        if($(this).hasClass('has-more')) {
256
            $(this).next('.more').stop().slideToggle(400, function() {
257
                                ui.wizard.setStepHeight($('.step-3'));
258
                    if($(self).next('.more:visible').length != 0) {
259
                            $(checkbox).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
260
                    }
261
                    else {
262
                            $(checkbox).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
263
                    }
264
            });
265
        }
266
    });
267

    
268
    $('.flavor .options a').hover(
269
                function(){
270
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
271
                        var text = $(this).data('help');
272
                        paragraph.html(text);
273
                        paragraph.css('visibility','visible');
274
                }, function() {
275
                        var paragraph = $(this).parents('.options-bar').siblings('.title').find('p');
276
                        paragraph.css('visibility','hidden');
277
                 }
278
    );
279

    
280

    
281
    // reaction a.click
282
    $('.checkbox .check').click(function(e) {
283
                e.stopPropagation();
284
                var self = this;
285
                if($(this).closest('.checkbox').hasClass('has-more')) {
286
                        console.log('hi');
287
            $(this).parent().next('.more').stop().slideToggle(400, function() {
288
                    console.log('a2');
289
                    if($(self).parent().next('.more:visible').length != 0) {
290
                            $(self).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
291
                                    console.log('a3');
292
                    }
293
                    else {
294
                            $(self).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
295
                    }
296
            });
297
        }
298
        });
299

    
300
           $('.show-add-tag').click(function(e){
301
        e.preventDefault();
302
        $(this).parents('.tags-area').find('.snf-color-picker').slideDown('slow', function(){
303
                        ui.wizard.setStepHeight($('.step-3'));
304
                        goToByScroll('hide-add-tag');
305
        });
306
        ui.colorPickerVisible =1;
307
    });
308

    
309
    $('.hide-add-tag').click(function(e){
310
        e.preventDefault();
311
        $(this).parents('.snf-color-picker').slideUp('400', function(){
312
            ui.colorPickerVisible = 0;
313
        });
314
    });
315

    
316
/* end of step functions */
317

    
318
/* navigation and numbers functions */
319
$('.nav.previous').focus(function(e){
320
        $(this).addClass('active');
321
});
322

    
323
$('.nav.previous').focusout(function(e){
324
        $(this).removeClass('active');
325

    
326
});
327

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

    
331
});
332

    
333
$('.os .name-col').focus(function(e){
334
        $(this).parents('li').addClass('hover');
335
});
336

    
337
$('.os .name-col').focusout(function(e){
338
        $(this).parents('li').removeClass('hover');
339
});
340

    
341

    
342
/* end of navigation and numbers functions */
343

    
344

    
345
/* focus and tabs functionality */
346

    
347
$('a').keyup(function(e){
348
        var self = this;
349
        if (e.keyCode==9||e.which==9){
350
                if ($(self).attr('data-next')) {
351
                        $(self).focusout(function(e){
352
                                var classname = $(self).data('next');
353
                                console.log('tab goes to ',classname);
354
                                $('.'+classname+'').first().focus();
355
                        })
356
                }
357
        }
358
})
359

    
360

    
361
// make sure that is always correctly moved
362
/*$('a, input').focus(function(e){
363
        if ($(this).parents('.step').data('step')> 0) {
364
                if ( ui.wizard.current_step == ($(this).parents('.step').data('step'))) {
365
                } else {
366
                        ui.wizard.current_step = $(this).parents('.step').data('step');
367
                        ui.wizard.move();
368
                }
369
        }
370
})
371
*//* end of focus and tabs functionality */
372

    
373
        $('#vm-wizard').find('a').click(function(e) {
374
                e.preventDefault();
375
        });
376
        ui.wizard.current_step =1;
377
        ui.wizard.btns.previous = $('.bottom').find('.nav.prev');
378
        ui.wizard.btns.next = $('.bottom').find('.nav.next');
379
        ui.wizard.btns.expand_down =$('.adv-main').find('.expand-link');
380
        ui.wizard.btns.close =$('#vm-wizard').find('.close');
381
        ui.wizard.initEvents();
382
        ui.wizard.setMovementTags();
383
        ui.wizard.expandArea();
384
        ui.wizard.btns.close.click(function(e) {
385
                ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area')
386
        });
387

    
388
        $(window).resize(function() {
389
                ui.wizard.setStepHeight($('.step-'+ui.wizard.current_step+''));
390
        });
391
});