Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.5 kB)

1
// all functions use pixels
2
ui.wizard ={
3
        current_step: undefined,
4
        total_step: 4,
5
        current_position: undefined,
6
        relocation: undefined,
7
        btns: {
8
                start: undefined,
9
                close: undefined,
10
                next: undefined,
11
                previous: undefined,
12
                expand_down: undefined
13
        },
14

    
15
        set_container_height: function(step) {
16
                if (step === true) {
17
                        step =0;
18
                } else {
19
                        step = ui.wizard.current_step;
20
                }
21
                var topHeight = $('.top').height();
22
                var stepHeight = $('.step-'+(step+1)+'').height();
23
                var res = topHeight+stepHeight;
24
                console.log('step', step);
25
                if (step == 2) {
26
                        $('.wizard-content').removeAttr('style');
27
                } else {
28
                        $('.wizard-content').height(res);
29
                }
30
    },
31
    set_container_height_back: function(step) {
32
                if (step === true) {
33
                        step =1;
34
                } else {
35
                        step = ui.wizard.current_step;
36
                }
37
                var topHeight = $('.top').height();
38
                var stepHeight = $('.step-'+step+'').height();
39
                var res = topHeight+stepHeight;
40
                console.log('step', step);
41
                if (step == 3) {
42
                        $('.wizard-content').removeAttr('style');
43
                } else {
44
                        $('.wizard-content').height(res);
45
                }
46
    },
47

    
48

    
49
        // sets the width and height of the carousel and its divs
50
        set_dimensions: function() {
51
                console.log('set dimentions');
52
                ui.wizard.relocation = $(window).width();
53
                console.log(ui.wizard.relocation);
54
                $('.vm-wizard-carousel').children('div.step').width(ui.wizard.relocation);
55
                ui.wizard.set_container_height(true);
56
        },
57

    
58
        // sets the width and height of the carousel and its divs when the screen is resized (in PIXELS)
59
        adjust_to_resized_screen: function() {
60
                $(window).resize(function() {
61
                        console.log('resized');
62
                        ui.wizard.set_dimensions();
63

    
64
                        if(ui.wizard.current_position<=0) {
65
                                // the carousel moves to left -> this is the case that acts on resize
66
                                ui.wizard.current_position = -((ui.wizard.current_step-1)*ui.wizard.relocation);
67
                        }
68
                        else
69
                                ui.wizard.current_position = (ui.wizard.current_step-1)*ui.wizard.relocation;
70

    
71
                        $('.vm-wizard-carousel').css('left', ui.wizard.current_position+'px');
72
                })
73
        },
74

    
75
        
76

    
77
        move_to_step: function() {
78
                var speed =500;
79

    
80
                // carousel movement when right or left arrow is pressed
81
                $(document).keydown(function(e) {
82

    
83
                        // right arrow keyCode == 39
84
                        if(e.keyCode==39 && ui.wizard.current_step!=ui.wizard.total_step) {
85
                                console.log('right arrow');
86
                                go_next();
87
                                return false;
88
                        }
89
                        // left arrow keyCode == 37
90
                        else if(e.keyCode==37 && ui.wizard.current_step!=1) {
91
                                console.log('left arrow');
92
                                go_prev();
93
                                return false;
94
                        }
95
                        // enter keyCode == 13
96
                        else if (e.keyCode==13 && ui.wizard.current_step==ui.wizard.total_step) {
97
                                console.log('enter -> close');
98
                                ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area')
99
                        }
100
                        // esc keyCode == 27
101
                        else if(e.keyCode== 27 && ui.wizard.current_step==1) {
102
                                console.log('esc -> close');
103
                                ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area')
104
                        }
105
                });
106
                
107
                // when the button "next" is pressed show the next step (if there is a next step)
108
                ui.wizard.btns.next.click(function(e){
109
                        // e.preventDefault();
110
                        go_next();
111
                })
112

    
113
                // when the button "previous" is pressed show the previous step (if there is a previous step)
114
                ui.wizard.btns.previous.click(function(e){
115
                        // e.preventDefault();
116
                        go_prev();
117
                });
118

    
119
                function go_next() {
120
                        console.log('you clicked next!')
121
                        ui.wizard.set_container_height();
122
                        if(ui.wizard.current_step < ui.wizard.total_step){
123
                                ui.wizard.current_step++;
124
                                ui.wizard.current_position -=ui.wizard.relocation;
125
                                $('.vm-wizard-carousel').finish();
126
                                $('.vm-wizard-carousel').animate({left: ui.wizard.current_position+'px'}, speed);
127
                                ui.wizard.indicate_step(ui.wizard.current_step);
128
                                ui.wizard.set_movement_tags(ui.wizard.current_step, ui.wizard.btns.previous, ui.wizard.btns.next);
129
                                if(ui.wizard.current_step == 2) {
130
                                        $('.sub-menu[data-step=2] li:first').find('a').focus();
131
                                }
132
                                else if(ui.wizard.current_step == 3) {
133
                                        setTimeout(function() { $('.vm-name').find('input').focus() }, speed/2);
134
                                }
135
                        }
136
                        else {
137
                                console.log('This is the last step.');
138
                                ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area');
139
                        }
140
                }
141

    
142
                function go_prev() {
143
                        if(ui.wizard.current_step > 1){
144
                                --ui.wizard.current_step;
145
                                ui.wizard.set_container_height_back();
146
                                ui.wizard.current_position +=ui.wizard.relocation;
147
                                $('.vm-wizard-carousel').finish();
148
                                $('.vm-wizard-carousel').animate({left: ui.wizard.current_position+'px'}, speed);
149
                                ui.wizard.indicate_step(ui.wizard.current_step);
150
                                ui.wizard.set_movement_tags(ui.wizard.current_step, ui.wizard.btns.previous, ui.wizard.btns.next);
151

    
152
                                if(ui.wizard.current_step == 2) {
153
                                        $('.sub-menu[data-step=2] li:first').find('a').focus();
154
                                }
155
                                else if(ui.wizard.current_step == 3) {
156
                                        setTimeout(function() { $('.vm-name').find('input').focus() }, speed/2);
157
                                }
158
                        }
159
                        else {
160
                                console.log('This is the 1st step.')
161
                                ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area');
162
                        }
163
                }
164
        },
165

    
166
        // sets the width and height of the steps and of the carousel (in PIXELS)
167
        initialize_relocation: function(){
168
                        console.log('initialize_relocation');
169
                        ui.wizard.btns.start.click(function(e) {
170
                                e.preventDefault();        
171
                                ui.wizard.reset();
172
                                ui.wizard.adjust_to_resized_screen();
173
                                ui.wizard.set_dimensions();
174
                        })
175
        },
176

    
177
        // for the carousel index
178
        indicate_step: function(step) {
179
                $('.wizard .top .sub-menu[data-step]').hide();
180
                $('.wizard .top .sub-menu[data-step='+step+']').fadeIn();
181
                $('.nums').children().removeClass('current');
182
                $('.nums li').show();
183
                //$('.nums li:nth-child('+ui.wizard.current_step+'').addClass('current');
184
                $('.nums').children().find('*:contains("'+ui.wizard.current_step+'")').parent('li').addClass('current');
185
                $('.nums .current').prevAll('li').hide();
186
        },
187

    
188
        // changes the text of next and previous buttons
189
        set_movement_tags: 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
                }
194
                else if(ui.wizard.current_step==ui.wizard.total_step) {
195
                        ui.wizard.btns.previous.find('span').html('PREVIOUS');
196
                        ui.wizard.btns.next.find('span').html('CREATE');
197
                }
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(bottom_area, main_area, total_area) {
205
                console.log('tha kleisw')
206
                $(bottom_area).hide();
207
                $(main_area).slideUp();
208
                $(total_area).slideUp();
209
        },
210

    
211
        // manually sets elements to a initial state
212
        reset: function() {
213
                ui.wizard.current_step = 1;
214
                ui.wizard.current_position = 0;
215
                
216
                $('.vm-wizard-carousel').css({left: ui.wizard.current_position+'px'});
217
                $('.bottom').show();
218
                $('.os li').removeClass('current');
219
                $('.os .btn-col a').removeClass('current');
220
                $('.os li.preselected').addClass('current');
221
                //$('.step-1').find('.current').removeClass('current');
222
                ui.wizard.indicate_step(ui.wizard.current_step);
223
                ui.wizard.set_movement_tags();
224
                $('.ssh-keys-area').find('.snf-checkbox-checked').addClass('snf-checkbox-unchecked');
225
                $('.ssh-keys-area').find('.snf-checkbox-checked').removeClass('snf-checkbox-checked');
226
                $('.networks-area .checkbox').find('.snf-checkbox-checked').addClass('snf-checkbox-unchecked');
227
                $('.networks-area .checkbox').find('.snf-checkbox-checked').removeClass('snf-checkbox-checked');
228
                $('.networks-area .more').hide();
229
                $('#vm-wizard').find('.snf-checkbox-checked').addClass('snf-checkbox-unchecked').removeClass('snf-checkbox-checked');
230
                $('#vm-wizard').find('.default').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
231
                $('.details').hide();
232
                $('.wizard .top .sub-menu[data-step="1"] ul li:first-child a').addClass('current');
233
                ui.pickResources('small');
234
                //$('.wizard .step-2 .options li a:contains(DRBD)').addClass('current')
235
                $('.vm-name input').val('');
236
                $('.advanced-conf-options').hide();
237
                $('.snf-color-picker').hide();
238

    
239

    
240
        },
241

    
242
        expand_area: function() {
243
                ui.wizard.btns.expand_down.click(function(e){
244
                // e.preventDefault();
245
                      ui.expandArrow(ui.wizard.btns.expand_down);
246
                ui.wizard.btns.expand_down.parents('div.advanced-conf-step').find('.advanced-conf-options').stop().slideToggle();
247
            })
248
        },
249

    
250
}
251

    
252

    
253
$(document).ready(function(){
254

    
255
        $('#vm-wizard').find('a').click(function(e) {
256
                e.preventDefault();
257
        });
258

    
259
        ui.wizard.current_step =1;
260
        ui.wizard.current_position =0;
261

    
262
        ui.wizard.btns.start =$('.new-btn, .add-new');
263
        ui.wizard.btns.previous = $('.bottom').find('.nav.prev');
264
        ui.wizard.btns.next = $('.bottom').find('.nav.next');
265
        ui.wizard.btns.expand_down =$('.adv-main').find('.expand-link');
266
        ui.wizard.btns.close =$('#vm-wizard').find('.close');
267
        ui.wizard.initialize_relocation();
268
        ui.wizard.move_to_step();
269
        ui.wizard.set_movement_tags();
270
        ui.wizard.expand_area();
271

    
272
        ui.wizard.btns.close.click(function(e) {
273
                ui.wizard.close('.bottom', '#vm-wizard', '.overlay-area')
274
        });
275

    
276
        $('.step-1 .os li:last').find('.btn-col a').focusout(function(e) {
277
                $('.nav.next').focus();
278
                $('.nav.next').addClass('active');
279
        });
280

    
281

    
282
        $('.nav.previous').focus(function(e){
283
                $(this).addClass('active');
284
        });
285

    
286
        $('.nav.previous').focusout(function(e){
287
                // $(this).addClass('active');
288
                $(this).removeClass('active');
289

    
290
        });
291

    
292

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

    
296
        });
297

    
298
        $('.os .name-col').focus(function(e){
299
                $(this).parents('li').addClass('hover');
300
        });
301

    
302
        $('.os .name-col').focusout(function(e){
303
                $(this).parents('li').removeClass('hover');
304
        });
305

    
306

    
307
        $('.checkbox .check').click(function(e) {
308
                e.stopPropagation();
309

    
310
                if($(this).closest('.checkbox').hasClass('has-more')) {
311
            $(this).parent().next('.more').slideToggle();
312
        }
313
        })        
314

    
315

    
316

    
317
});