Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.8 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: ['el0','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
                ui.wizard.setStepHeight($('.step-'+ui.wizard.current_step+''));
37
                $('.vm-wizard-carousel').stop(true, true).animate(
38
                        { 'left': percentage },
39
                        ui.wizard.speed
40
                        ,function(){
41
                                $('#dummy-link-'+ui.wizard.current_step+'').scrollintoview({'duration':0});
42
                        }
43
                );
44
                ui.wizard.focusFun();
45
                ui.wizard.indicateStep(ui.wizard.current_step);
46
                ui.wizard.setMovementTags(ui.wizard.current_step, ui.wizard.btns.previous, ui.wizard.btns.next);
47
                 if(ui.wizard.current_step == 3 && $('.step-3 .advanced-conf-options:visible').length == 0) {
48
                        setTimeout(function() { $('.vm-name').find('input').focus() }, ui.wizard.speed/2);
49
                }
50
        },
51

    
52
        goNext: function () {
53
                if(ui.wizard.current_step < ui.wizard.total_step){
54
                        ui.wizard.current_step++;
55
                        ui.wizard.move();
56
                }
57
                else {
58
                        ui.wizard.submitData();
59
                        ui.wizard.close();
60
                }
61
        },
62

    
63
        goPrev:function() {
64
                if(ui.wizard.current_step > 1){
65
                        ui.wizard.current_step--;
66
                        ui.wizard.move();
67
                }
68
                else {
69
                        ui.wizard.close();
70
                }
71
        },
72

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

    
98
                ui.wizard.btns.previous.click(function(e){
99
                        ui.wizard.goPrev();
100
                });
101
        },
102

    
103
        // for the carousel index
104
        indicateStep: function(step) {
105
                $('.wizard .top .sub-menu[data-step]').hide();
106
                $('.wizard .top .sub-menu[data-step='+step+']').fadeIn();
107
                
108
                $('.nums').children().removeClass('current');
109
                 $('.nums li:not(".current")').show();
110
                $('.nums li:nth-child('+ui.wizard.current_step+')').addClass('current');
111
                $('.nums li.current').hide();
112
                 $('.nums li.current').fadeIn('slow').css("display","inline");
113
                $('.nums .current').prevAll('li').hide();
114
        },
115

    
116
        // changes the text of next and previous buttons
117
        setMovementTags: function() {
118
                if (ui.wizard.current_step==1) {
119
                        ui.wizard.btns.previous.find('span').html('CANCEL');
120
                        ui.wizard.btns.next.find('span').html('NEXT');
121
                }
122
                else if(ui.wizard.current_step==ui.wizard.total_step) {
123
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
124
                        ui.wizard.btns.next.find('span').html('CREATE');
125
                }
126
                else {
127
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
128
                        ui.wizard.btns.next.find('span').html('NEXT');
129
                }
130
        },
131

    
132
        close: function() {
133
                $('.overlay-area').fadeOut(400, function (){
134
                $('.overlay-div').hide();
135
                });
136
                ui.wizard.resetWizard();
137
        },
138

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

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

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

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

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

    
197
}
198

    
199

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

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

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

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

    
224

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

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

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

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

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

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

    
279

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

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

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

    
315
/* end of step functions */
316

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

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

    
325
});
326

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

    
330
});
331

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

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

    
340

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

    
343

    
344
/* focus and tabs functionality */
345

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

    
359

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

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

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