Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / okeanos.js @ 0467b6cd

History | View | Annotate | Download (46.1 kB)

1
/*
2
* Various functions that will be used throughout all templates
3
* are inside ui Object
4
*/
5

    
6
ui = {};
7
/*
8
* ui.wizards get populated in vm-wizard.js
9
* here is the declaration only
10
*/
11
ui.wizard = {};
12
ui.checkbox = {};
13
ui.radiobtn = {};
14

    
15
/* when closeEl el is clicked, its parent with class divToCloseClass slidesUp */
16
ui.closeDiv = function(closeEl, divToCloseClass) {
17
    closeEl.click(function(e){
18
        e.preventDefault();
19
        $(this).parents(divToCloseClass).slideUp('slow');
20
    });
21
}
22

    
23

    
24
ui.trimChars = function( str, chars) {
25
    if ( str.length>chars){
26
        return $.trim(str).substring(0, chars)+ "...";
27
    } else {
28
        return str;
29
    }
30
}
31

    
32

    
33
/* Sets element min-height
34
* Used for .details, .lt-bar divs
35
*/
36
ui.setElminHeight = function(el){
37
    var WindowHeight = $(window).height();
38
    var header = $('.header').outerHeight();
39
    var actions = $('.actions-bar').height();
40
    var h1= WindowHeight - (header+actions);
41
    el.css('min-height', h1);
42
}
43

    
44
/* Sets element height
45
* Used for .details, .lt-bar divs
46
*/
47
ui.setElHeight = function(el){
48
    var WindowHeight = $(window).height();
49
    var header = $('.header').outerHeight();
50
    var actions = $('.actions-bar').height();
51
    var h1= WindowHeight - (header+actions);
52
    el.css('height', h1);
53
}
54

    
55
/* Sets element max-height
56
* Used for div.storage-progress
57
*/
58
ui.setElmaxHeight = function(el){
59
    var WindowHeight = $(window).height();
60
    var header = $('.header').outerHeight();
61
    var actions = $('.actions-bar').height();
62
    var h1= WindowHeight - (header+actions);
63
    el.css('max-height', h1);
64
}
65

    
66
/* 
67
* Logic for Entities actions. Present in items_list pages
68
* Available categories are :
69
*  - both/single ( for multiple entities/single entities)
70
*  - running/off ( for running/off entities)
71
*  - permanent ( for entities always active )
72
* Can be used for pithos as well
73
* Available categories are :
74
* - files ( for files only actions)
75
* - folders ( for folders only actions)
76
* - all ( for files/folders actions)
77
*/
78
ui.entitiesActionsEnabled = function(){
79
    var all = $('.snf-checkbox-checked').length;
80
    var running = $('.snf-checkbox-checked').parents('li.running').length;
81
    var off = $('.snf-checkbox-checked').parents('li.off').length;
82
    var files = $('.snf-checkbox-checked').parents('li.file').length;
83
    var folders = $('.snf-checkbox-checked').parents('li.folder').length;
84

    
85
    console.log(files,'files');
86
    console.log(folders,'folders');
87

    
88
    $('.lt-bar .lt-actions li:not(.permanent) a').removeClass('active');
89

    
90
    if ( ( files * folders )>0 ) {
91
        $('.lt-actions li.all a').addClass('active');
92
    } else {
93
        if ( files>0 ) {
94
            $('.lt-actions li.files a').addClass('active');
95
        }
96
        if ( folders>0 ){
97
            $('.lt-actions li.folders a').addClass('active');
98
        }
99
    }
100

    
101
    if ( (running*off) > 0 ){
102
         $('.lt-actions li.both a').addClass('active');
103
         $('.lt-actions li.single a').removeClass('active');
104
    } else {
105
        if (running > 0) {
106
            $('.lt-actions li.both a').addClass('active');
107
            $('.lt-actions li.running a').addClass('active');
108
        } else if (off>0) {
109
            $('.lt-actions li.both a').addClass('active');
110
            $('.lt-actions li.off a').addClass('active');
111
        }
112
        if ( all > 1 ) {
113
            $('.lt-actions li.single a').removeClass('active');
114
        }
115
    }
116
}
117

    
118
ui.inactiveActions = function() {
119

    
120
    // Availble actions: connect, reboot, shut, destroy, start
121
    // These actions will be DISABLED
122
    var statesActions ={
123
        'off'      : ['connect', 'reboot', 'shut'],
124
        'error'    : ['connect', 'reboot', 'shut', 'start'],
125
        'building' : ['reboot', 'start'],
126
        'running'  : ['start'],
127
        'rebooting': ['start'],
128
        'starting' : ['start'],
129
        'shutting' : ['connect', 'reboot', 'shut']
130
    } ;
131

    
132
    _.each (statesActions, function(val, key) {
133
        _.each(val, function(value) {
134
            var el = '.' + key + ' .' + value;
135
            $(el).addClass('inactive');
136
        });
137
    })
138
}
139

    
140
ui.detailsCustom = function(area) {
141
    // position last connected item
142
    var el = area.find('.item').last();
143
    // -2 is the border width;
144
    var moveY = el.find('.connections >li').last().outerHeight(true) - 2;
145
    el.css('top',moveY);
146
    el.css('marginTop', -moveY);
147
}
148

    
149
ui.firewallSetup = function(){
150
    $('.firewall').each(function(){
151
        $(this).removeClass('fully unprotected basic');
152
        $(this).addClass($(this).data('firewall'));
153
        if($(this).hasClass('unprotected')){
154
            $(this).find('p').first().find('em').html('off');
155
        } else {
156
            $(this).find('p').first().find('em').html('on');
157
        }
158
    });
159
}
160

    
161

    
162

    
163
/*
164
* In order for the editable value functionality to work, the html markup
165
* should be:
166
    <div class="editable">
167
        <span class="input-txt">editable value</span>
168
        <input type="text">
169
        <a href="#" class="edit">edit</a>
170
        <a href="#" class="save">save</a>
171
        <a href="#" class="cancel">cancel</a>
172
    </div>
173
*/
174
ui.editable = function(){
175

    
176
/*
177
* resetForm hides save and cancel buttons,
178
* text input and shows input-txt. resetForm does not alter
179
* input-txt content.
180
*/
181

    
182
    function resetForm(e, elem) {
183
        var el = elem.parents('.editable');
184
        el.find('input[type="text"]').hide();
185
        el.find('a.cancel, a.save').hide();
186
        el.find('a.edit, .input-txt').show();
187
    }
188

    
189
/* 
190
* showForm hides input-txt, shows save and cancel buttons and
191
* sets input value to input-txt content.
192
*/
193
    function showForm(e,elem) {
194
        e.stopPropagation(); 
195
        e.preventDefault();
196
        var el = elem.parents('.editable'); 
197
        el.find('input[type="text"]').val(el.find('.input-txt').html());
198
        el.find('input[type="text"]').show().focus();
199
        el.find('a.cancel, a.save').show();
200
        elem.hide();
201
        el.find('.input-txt').hide();
202
    }
203

    
204
/*
205
setValue sets input-txt value to the input value.
206
Makes sure that the input value is not empty.
207
TODO:
208
Ajax request to submit form
209
*/
210

    
211
    function setValue(elem) {
212
        var el = elem.parents('.editable');
213
        if( el.find('input[type="text"]').val() ) {
214
            el.find('.input-txt').html(el.find('input[type="text"]').val());
215
        }
216
    }
217

    
218

    
219
    $('.editable .edit').click(function(e){
220
        showForm(e, $(this));
221
    })
222

    
223
    $('.editable .cancel').click(function(e){
224
        e.stopPropagation();
225
        e.preventDefault();
226
        resetForm(e, $(this));
227
    })
228

    
229
    $('.editable .save').click(function(e){
230
        e.stopPropagation();
231
        e.preventDefault();
232
        setValue($(this));
233
        resetForm(e, $(this));
234

    
235
    })
236

    
237

    
238
    $('.editable input[type="text"]').click(function(e){
239
        e.stopPropagation();
240
    })
241

    
242
    $('.editable input[type="text"]').keyup(function(e){
243
        if(e.keyCode == 13) { 
244
            setValue($(this));
245
            resetForm(e, $(this));
246
            
247
        }
248
    
249
    })
250

    
251
    $('html').click(function(e) {
252
        resetForm(e, $('.editable a.cancel'));
253
    });
254

    
255
}
256

    
257
/* TODO: better overlay functionality */
258
ui.overlay = function() {
259
    $('[data-overlay-id]').click(function(e){
260
        e.preventDefault();
261
        var el = $(this);
262
        var id = '#'+el.data('overlay-id');
263

    
264
        $('.overlay-area-custom').fadeIn(100);
265
        $('body').addClass('with-overlay');
266
        $(id).fadeIn('slow');
267
        if (id=='#network-wizard') {
268
            $(id).find('input').first().focus();
269
            return false;
270
        }
271
        $(id).find('a').first().focus();
272
    });
273
}
274

    
275
function goToByScroll(id){
276
      // Remove "link" from the ID
277
    id = id.replace("link", "");
278
      // Scroll
279
    $('html,body').animate({
280
        scrollTop: $("#"+id).offset().top},
281
        'slow');
282
}
283

    
284

    
285
// toggle expand arrrow  and  area
286
// todo: one function for all the areas we reveal
287
ui.expandDownArea = function(arrow_link, area) {
288
    var arrow_link = $(arrow_link);
289
    var area = $(area);
290
            arrow_link.find('span.snf-arrow-up, span.snf-arrow-down').toggleClass('snf-arrow-up snf-arrow-down');
291
            // $('.wizard-content').removeAttr('style');
292
            area.stop().slideToggle(600, function() {
293
                if (area.is(':visible')) {
294
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
295
                } else {
296
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
297
                }
298

    
299
            });
300
}
301

    
302
// toggle checkbox and area
303
ui.slideHiddenArea = function(checkbox_link, area) {
304
    area.stop().slideToggle(400, function() {
305
        if (area.is(':visible')) {
306
            ui.checkbox.check(checkbox_link);
307
        } else {
308
           ui.checkbox.uncheck(checkbox_link);
309
        }
310
    });
311
};
312
/* Tabs functionality
313
* tabsEl is the div/ul with the links to the sections and the sections that
314
* with toggle have class sectionEl.
315
* Markup needed is an href to each a with the id of the relative section.
316
*/
317

    
318
ui.tabsSection = function(link, sectionEl) {
319
    sectionEl.hide();
320
    var el = $(link.attr('href'));
321
    el.stop(true, true).show(0);
322
    el.css('opacity',0);
323
    ui.detailsCustom(el);
324
    el.animate({
325
        'opacity':1,
326
    }, 500)
327
}
328

    
329
ui.tabs = function(tabsEl, sectionEl) {
330
    var tabLink = tabsEl.find('a');
331
    ui.replaceClass(tabLink.find('.active'), 'outline', 'full', 'snf-');
332
    function href(a) {
333
        return a.attr('href');
334
    }
335
    tabLink.click(function(e){
336
        e.preventDefault();
337
        if ( $(this).hasClass('active')){
338
             return false;
339
        } else {
340
            window.location.hash = $(this).attr('href');
341
            ui.replaceClass($(this).parents(tabsEl).find('.active'), 'full', 'outline', 'snf-');
342
            $(this).parents(tabsEl).find('a').removeClass('active');
343
            $(this).addClass('active');
344
            ui.replaceClass($(this), 'outline', 'full', 'snf-');
345
            ui.tabsSection( $(this), sectionEl);
346
        }
347
    })
348
}
349

    
350
// the function replaces part of the class of a span that is placed inside an a element
351
// the class is a word with the form : firstSubStr+*+str1 and it will be converted to: firstSubStr+*+str2
352
// it must be uniquely recognized by the firstSubStr and the str1.
353

    
354
ui.replaceClass = function(elements, str1, str2, firstSubStr) {
355
    $.each($(elements), function() {
356
        var classOld = $(this).find('span').attr('class');
357
        var classNew;
358
        if(classOld != undefined && classOld.indexOf(str1) != -1) {
359
            if(classOld.indexOf(' ') == -1) {
360
                classNew = classOld.replace(str1, str2);
361
                $(this).find('.'+classOld).removeClass(classOld).addClass(classNew);
362
            }
363
            else {
364
                // the string starts with the firstSubStr and after the end of the word there's a space
365
                var expr1 = new RegExp('^'+firstSubStr+'+(\\S*)+'+str1+'+(\\s+)', 'g');
366

    
367
                // the word is between spaces
368
                var expr2 = new RegExp('(\\s+)'+firstSubStr+'+(\\S*)+'+str1+'+(\\s+)', 'g');
369

    
370
                // before the word there's at least one space and the string ends with this word
371
                var expr3 = new RegExp('(\\s+)'+firstSubStr+'+(\\S*)+'+str1+'$', 'g');
372

    
373
                // spaces all over the string
374
                var spacesExp = new RegExp('\\s+', 'g');
375

    
376
                if(classOld.match(expr1) != null) {
377
                    classToReplace = classOld.match(expr1);
378
                }
379
                else if(classOld.match(expr2) != null) {
380
                    classToReplace = classOld.match(expr2);
381
                }
382
                else if (classOld.match(expr3) != null) {
383
                    classToReplace = classOld.match(expr3);
384
                }
385
                classToReplace = classToReplace.toString().replace(spacesExp,"");
386
                classNew = classToReplace.replace(str1, str2);
387
                $(this).find('.'+classToReplace).removeClass(classToReplace).addClass(classNew);
388
            }
389
        }
390
    });
391
}
392

    
393
// in a page with tabs, allow navigation with hash tags
394
ui.hashTabs = function(tabsEl, sectionEl){
395

    
396

    
397
}
398

    
399
// in a page with tabs, allow navigation with hash tags
400
ui.hashViews = function(viewsEl, sectionEl){
401
    var hash = window.location.hash;
402
    if (!(hash)){
403
        return;
404
    }
405
    var link = viewsEl.find('a[href="'+hash+'"]');
406
    console.log('link', link);
407
    if (link.length<=0){
408
        return;
409
    }
410
    viewsEl.find('a span').removeClass('current');
411
    link.find('span').addClass('current');
412
    sectionEl.removeClass('grid-view list-view');
413
    var sectionClass = hash.slice(1) + '-view';
414
    sectionEl.addClass(sectionClass);
415
}
416

    
417
ui.ltBarToggle = function(speed){
418

    
419
    var cmarg = parseInt($('.lt-bar').width()) - parseInt($('.toggle-lt-bar').outerWidth(true));        
420
    var ctemp = cmarg / parseInt($('.lt-bar').width());
421
    var cvelocity = parseInt($('.lt-bar').width())/speed;
422
    var cdelay = parseInt($('.toggle-lt-bar').outerWidth(true))/cvelocity;
423

    
424
    $('.lt-bar').click(function(e) {
425
        e.stopPropagation();
426
    })
427
    $('.toggle-lt-bar').click(function(e){
428
        e.preventDefault();
429
        e.stopPropagation();
430
        var self =this;
431
        if($(this).hasClass('fix-position')) {
432
            var marg = parseInt($(self).css('marginLeft')) - cmarg;
433
            $(this).animate({
434
                'margin-left': marg,
435
            }, ctemp * speed, 'linear', function(){
436
                $(self).removeAttr('style');
437
                $(self).removeClass('fix-position');
438
            });
439
            $('.lt-bar').animate({
440
                width: 'toggle'
441
            }, speed, 'linear');
442
        }
443
        else {
444
            $(this).addClass('fix-position');
445
           var scrollBarWidth = 14;
446
            var marg = parseInt($(self).css('marginLeft')) + cmarg - scrollBarWidth;
447
            setTimeout(function(){
448
                $(self).animate({
449
                    'margin-left': marg,
450
                }, speed, 'linear')
451
            }, cdelay);
452
            $('.lt-bar').animate({
453
                width: 'toggle'
454
            }, speed, 'linear', ui.setCustomScrollBar);
455
        }
456
    });
457
}
458

    
459

    
460
ui.setCustomScrollBar = function() {
461
    $('.scroll-pane-arrows').jScrollPane({
462
        showArrows: true,
463
        horizontalGutter: 10,
464
        verticalDragMinHeight: 300,
465
        verticalDragMaxHeight: 300,
466
        mouseWheelSpeed: 50
467
});
468
}
469

    
470
function bytesToSize(bytes) {
471
    var sizes = [ 'n/a', 'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
472
    var i = +Math.floor(Math.log(bytes) / Math.log(1024));
473
    return  (bytes / Math.pow(1024, i)).toFixed( 0 ) + sizes[ isNaN( bytes ) ? 0 : i+1 ];
474
}
475
function date_ddmmmyytime(date)
476
{
477
  var d = date.getDate();
478
  var m = date.getMonth();
479
  var y = date.getYear();
480
  if(y >= 100)
481
  {
482
    y -= 100;
483
    y += 2000;
484
  }
485

    
486
    var curr_hour = date.getHours();
487

    
488
    if (curr_hour < 12){
489
        a_p = "am";
490
    } else {
491
       a_p = "pm";
492
    }
493

    
494
    if (curr_hour == 0) {
495
       curr_hour = 12;
496
    }
497
    if (curr_hour > 12){
498
       curr_hour = curr_hour - 12;
499
    }
500

    
501
    var curr_min = date.getMinutes();
502

    
503
  return "" +
504
    (d<10?"0"+d:d) + "/" +m + "/" + y + ' '+curr_hour + ":" + curr_min + a_p;
505
}
506

    
507
 // returns the file class/extension of a file
508
ui.mimeToExt = function( mimetype) {
509
  var mimeExt = {
510
    'image/jpeg': 'jpg',
511
    'image/png': 'png',
512
    'application/pdf': 'pdf',
513
    'text/plain': 'txt',
514
  };
515
  console.log(mimetype);
516
  return mimeExt[mimetype] || 'unknown';
517
}
518

    
519
$(document).ready(function(){
520

    
521
    $('html').click(function(e) {
522
        // not sure if we want to hide the error window after every click in the ui
523
        if($('.communication-error').css('bottom') == '0px') {
524
            $('.communication-error').animate({bottom: "-151px"});
525
        }
526
    });
527

    
528
    if($('.vms.entities').length!=0){
529
        ui.inactiveActions();
530
    };
531
    ui.setElminHeight($('.main > .details'));
532
    ui.setElminHeight($('.lt-bar'));
533
    ui.setElmaxHeight($('.storage-progress'));
534
    ui.setElHeight($('.scroll-wrap'));
535
    $('#hd-search .hd-icon-search').click(function(e){
536
        var that = this;
537
        $(this).parents('.hd-search').toggleClass('hd-open');
538
        if ($(this).parents('.hd-search').hasClass('hd-open')) {
539
            $(that).parents('.hd-search').find('input[type="search"]').focus();
540
        } else {
541
            $(that).parents('.hd-search').find('input[type="search"]').val('');
542
        }
543
    })
544

    
545
    $('.header .login').mouseenter(function(e){
546
        $(this).find('ul').stop(true, true).slideDown(200);
547
    });
548
    $('.header .login').mouseleave(function(e){
549
        $(this).find('ul').stop(true, true).slideUp(200);
550
    });
551

    
552
    $('.entities a').click(function(){
553
        if ($(this).attr('data-reveal-id') && !($(this).hasClass('inactive'))) {
554
            $('.entities li .more').hide();
555
        }
556
    });
557

    
558
    $('.inactive').click(function(e){
559
        e.stopPropagation();
560
        e.preventDefault();
561
     });
562

    
563
    $('.arrows').on('click','.inactive', function(e){
564
        e.preventDefault();
565
        e.stopPropagation();
566
    });
567

    
568

    
569
    $('.lt-actions a:not(.active)').click(function(e){
570
        e.preventDefault();
571
    })
572

    
573
    if ($('.entities .items-list >li').length == 1) {
574
        $('.body-wrapper').addClass('no-vm');
575
    };
576
    $('.entities li .more').each(function(){
577
        var width = $(this).parent('li').outerWidth()  + 20;
578
        $(this).css('width', width);
579
    });
580

    
581
    $('.items-list li .img-wrap').on("mouseenter", function(e) {
582
        var that = this;
583
        if ($(this).parents('.entities').hasClass('grid-view')) {
584
            if ($(that).parent('.container').siblings('.more').length>0) {
585
                $(that).parent('.container').fadeOut(50,'easeInCirc');
586
                $(that).parent('.container').siblings('.more').fadeIn(150,'easeInCirc');
587
            }
588
        }
589
    });
590
    $('.entities li .more').mouseleave(function(e) {
591
        $(this).fadeOut(50, function() {
592
            $(this).siblings('.container').fadeIn(50);
593
        });
594
    });
595
    $('.grid-view .items-list > li').mouseleave(function(e){
596
        var that = this;
597
        setTimeout(function(){
598
            $(that).find('.more').fadeOut(200, function() {
599
                $(this).siblings('.container').fadeIn('fast');
600
            });
601
        },50)
602
    });
603

    
604
    ui.closeDiv($('.info .close'), '.info');
605
    ui.editable();
606
    ui.overlay();
607
    ui.colorPickerVisible = 0;
608

    
609
    $("a.disabled").each(function() {
610
        $(this).removeAttr('href');
611
    });
612
    
613
    $("a.disabled").click(function(e) {
614
        e.preventDefault();
615
    });
616

    
617
    $('.main-actions li a').click(function(e){
618
        if (!($(this).hasClass('active'))) {
619
            e.preventDefault();
620
        }
621
    });
622

    
623
    $('.main-actions li a').click(function(e){
624
        if (!($(this).hasClass('active'))) {
625
            e.preventDefault();
626
        }
627
    });
628
    $('.overlay-area-custom').children('.close').click(function(e){
629
        e.preventDefault();
630
        e.stopPropagation();
631
        $(this).parents('.overlay-area-custom').hide();
632
        $(this).parents('.overlay-area-custom').find($('.overlay-div')).hide();
633
        $('body').removeClass('with-overlay');
634
    })
635

    
636
    $('.browse-files').click(function(e){
637
        e.preventDefault();
638
    });
639

    
640
    $('.main .files').magnificPopup({
641
        delegate: 'a.show.image',
642
        type: 'image',
643
        tLoading: 'Loading image #%curr%...',
644
        mainClass: 'mfp-img-mobile',
645
        gallery: {
646
            enabled: true,
647
            navigateByImgClick: true,
648
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
649
        },
650
        image: {
651
            tError: 'The image could not be loaded.',
652
            titleSrc: function(item) {
653
                return item.el.data('title');
654
            }
655
        }
656
    });
657

    
658
    if($('#picker-1').length>0) {
659
        $('#picker-1').farbtastic('#color-1');
660
    };
661
    if($('#picker-2').length>0) {
662
        $('#picker-2').farbtastic('#color-2');
663
    };
664
    if($('#sb-search').length>0) {
665
        new UISearch( document.getElementById( 'sb-search' ) );
666
    }
667

    
668
    if ($('.toggle-lt-bar').length>0) {
669
        ui.ltBarToggle(400);
670
    }
671

    
672
    /* grid/list view for items-list */
673

    
674
    $('.actions-bar .list, .actions-bar .grid').click(function(e){
675
        //e.preventDefault();
676
        if (!($(this).find('span').hasClass('current'))) {
677
            $('.actions-bar .grid span, .actions-bar .list span').removeClass('current');
678
            $(this).find('span').addClass('current');
679
            $('.entities').toggleClass('grid-view list-view');
680
        }
681
    });
682

    
683

    
684

    
685
    // set filter visible
686
    $('.filter-menu .filter').click(function(e) {
687
        e.preventDefault();
688
        $(this).parents('.filter-menu').toggleClass('current');
689
    });
690

    
691
    // temp function used to demonstrate the visual effect of the building state of vm
692
    $('[data-status="building"] .btn5.temp').click(function(e) {
693
        e.preventDefault();
694
        $(this).siblings('.container').find('.complete').toggleClass('build-progress');
695
    });
696

    
697
    $('[data-status="rebooting"] .btn5.temp').click(function(e) {
698
        e.preventDefault();
699
        $(this).siblings('.container').find('.snf-pc-full').toggleClass('reboot-progress');
700
    })
701

    
702
    // //temp function to preventDefault of links in modal
703
    // $('.reveal-modal a:not(".close-reveal-modal, .generate-key-btn, .import-key-btn")').click(function(e){
704
    //     e.preventDefault();
705
    //     $('a.close-reveal-modal').trigger('click');
706
    // });
707

    
708
     // temp btn to show communication error message
709
    $('.temp-for-btns .communication-error-btn').click(function(e) {
710
         e.preventDefault();
711
         e.stopPropagation();
712
         $('.communication-error').animate({bottom: "0px"});
713
     });
714

    
715
    $('.communication-error a').click(function(e) {
716
        e.preventDefault();
717
        e.stopPropagation();
718
        $('.communication-error').animate({bottom: "-151px"});
719

    
720
    });
721
    $('.communication-error').click(function(e) {
722
        e.stopPropagation();
723
    });
724

    
725
    $('.show-add-tag').click(function(e) {
726
    e.preventDefault();
727
    $(this).parents('.tags-area, .tags').find('.snf-color-picker').slideDown('slow', function() {
728
        $('#hide-add-tag-dummy').scrollintoview({
729
            'duration': 'slow'
730
        });
731
    });
732
    ui.colorPickerVisible = 1;
733
    });
734

    
735
    $('.hide-add-tag').click(function(e) {
736
    e.preventDefault();
737
    $(this).parents('.tags-area, .tags').find('.snf-color-picker').slideUp(400, function() {
738
        $('.show-add-tag').first().scrollintoview({
739
            'duration': 'slow'
740
        });
741
        ui.colorPickerVisible = 0;
742
    });
743
    });
744

    
745
    // connected details js
746
    ui.detailsCustom($('#disk-connected'));
747
    ui.detailsCustom($('#network-connected'));
748
    ui.detailsCustom($('#vm-connected'));
749
    ui.firewallSetup();
750

    
751
    $('.firewall').mouseenter(function(e){
752
        $(this).css('z-index',2);
753
        $(this).find('.more').stop(true, true).slideDown(200);
754
    });
755
    $('.firewall').mouseleave(function(e){
756
        $(this).css('z-index',1);
757
        $(this).find('.more').stop(true, true).slideUp(200);
758
    });
759
    ui.tabs($('.tabs'), $('.content'));
760
    ui.hashTabs($('.tabs'), $('.content'));
761
    ui.hashViews($('.actions-bar .rt-actions'), $('.entities'));
762

    
763
    $('.act').click(function(e) {
764
        $(this).addClass('pending last');
765
    });
766

    
767
    $('.remove .cancel').click(function(e) {
768
        e.preventDefault();
769
        $('a.close-reveal-modal').trigger('click');
770
        $('.last').removeClass('pending last');
771
    });
772

    
773
    $('.remove .ok').click(function(e) {
774
        e.preventDefault();
775
        $('a.close-reveal-modal').trigger('click');
776
        var connection_img = $('.connections').find('.last');
777
        connection_img.addClass('open', 0);
778
        connection_img.removeClass('last');
779
        var img = connection_img.closest('.item').find('.img-wrap .snf-font');
780
        img.addClass('reboot-progress');
781
        setTimeout(function() {
782
            var connections_list = connection_img.closest('.connections').children('li:not(".hidden")');
783
            if(connections_list.length>1) {
784
                connection_img.closest('li').slideUp(function(){
785
                    connection_img.closest('li').addClass('hidden');
786
                    if(connections_list.find('.pending').length == 0) {
787
                      img.removeClass('reboot-progress');
788
                    }
789
                });
790
            }
791
            else {
792
                connection_img.closest('.item').slideUp(function(){
793
                    img.closest('li').addClass('hidden');
794
                    img.removeClass('reboot-progress');
795
                    img.removeClass('reboot-progress');
796
                });
797
            }
798
            connection_img.removeClass('pending');
799
        }, 3000)
800
    });
801
    // end of connected details js
802

    
803
    ui.replaceClass($('a.current, a.active'), 'outline', 'full', 'snf-');
804

    
805
    $('.tag-data').mouseover(function() {
806
        $(this).find('.delete').css({visibility: 'visible'})
807
    });
808
    $('.tag-data').mouseleave(function(){
809
        $(this).find('.delete').css({visibility: 'hidden'})
810
    });
811
    $('.tag-data .delete').click(function(e) {
812
        e.preventDefault();
813
        $(this).closest('div').fadeOut('slow');
814
    });
815

    
816
    $('.storage-progress').hover(
817
        function(e){
818
            $(this).find('.details').stop(false,true).slideDown();
819
        },
820
        function(e){
821
            $(this).find('.details').stop(false,true).slideUp();
822
        }
823
    );
824

    
825
    $('.btn-more').mouseenter(function(e) {
826
        $(this).find('.explain').stop(true, true).show('slow');
827
    });
828

    
829
    $('.btn-more').click(function(e) {
830
        e.preventDefault();
831
        e.stopPropagation();
832
        $('.btn-more').removeClass('clicked');
833
        $('.btn-more').siblings('ul').hide();
834
        $(this).addClass('clicked');
835
        $(this).siblings('ul').stop(true, true).slideDown('slow');
836
    });
837
    $('.containers .project').mouseleave(function(e){
838
        $(this).find('ul').fadeOut();
839
        $(this).find('.btn-more').removeClass('clicked');
840
    })
841

    
842
    if ($('.containers .btn-more').length>0) {
843
        $('body').click(function(e){
844
            $('.btn-more').removeClass('clicked');
845
            $('.btn-more').siblings('ul').fadeOut();
846
        })
847
    }
848

    
849
    // add a <span> element inside the content of each a.wrap-a element
850
    $('a.wrap-a').wrapInner('<span></span>');
851

    
852

    
853
    $('.side-actions .bottom .reassign').click(function(e){
854
        e.preventDefault();
855
        $(this).parents('.bottom').find('ul').hide();
856
        $(this).parents('.bottom').find('ul.options').show();
857
    });
858

    
859
    $('.side-actions ul.options').mouseleave(function(e){
860
        $(this).hide();
861
        $(this).siblings('ul').removeAttr('style');
862
    })
863

    
864
})
865

    
866

    
867
$(window).resize(function(e){
868
    ui.setElminHeight($('.main > .details'));
869
    ui.setElminHeight($('.lt-bar'));
870
    ui.setElHeight($('.scroll-wrap'));
871
    ui.setCustomScrollBar();
872
    ui.setElmaxHeight($('.storage-progress'));
873

    
874
})
875
;/*
876
* functions concerning checkboxes and radiobuttons links
877
*/
878

    
879

    
880

    
881
/*ui.checkbox = {
882
    changeState : function(checkbox_link) {
883
        $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
884
        $(checkbox_link).closest('li').toggleClass('selected');
885
    },
886
    check : function(checkbox_link) {
887
        $(checkbox_link).find('span').removeClass('snf-checkbox-unchecked').addClass('snf-checkbox-checked');
888
        $(checkbox_link).closest('li').addClass('selected');
889
    },
890
    uncheck : function(checkbox_link) {
891
        $(checkbox_link).find('span').removeClass('snf-checkbox-checked').addClass('snf-checkbox-unchecked');
892
        $(checkbox_link).closest('li').removeClass('selected');
893
    },
894

895
    reset: function(area) {
896
        $(area).find('.snf-radio-checked').not('.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
897
        $(area).find('.snf-radio-unchecked.prechecked').toggleClass('snf-radio-checked snf-radio-unchecked');
898
    }
899

900
};
901
*/
902

    
903
ui.radiobtn = {
904
    changeState: function(radiobtn_link) {
905
        $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
906
        $(radiobtn_link).closest('li').addClass('selected');
907
    },
908
    obtainOneChecked : function(radiobtn_link) {
909
        $(radiobtn_link).closest('ul').find('li').removeClass('selected');
910
        $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
911
    },
912
    reset : function(area) {
913
        $(area).find('.snf-checkbox-checked').not('.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
914
        $(area).find('.snf-checkbox-unchecked.prechecked').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
915
    }
916

    
917
};
918

    
919
    $(document).ready(function(){
920

    
921
        // checkboxes binds
922

    
923
/*        $('.check').click(function(e) {
924
            e.preventDefault();
925
            e.stopPropagation();
926
            ui.checkbox.changeState(this);
927
            ui.entitiesActionsEnabled();
928
        });
929
*/
930

    
931
        $('.lt-bar .select .check').click(function(e) {
932
            $(this).siblings('em').toggle();
933
            if ( $(this).find('span').hasClass('snf-checkbox-unchecked')){
934
                ui.checkbox.uncheck($('.list-view  li:not(".not-selectable") .check'));
935
            } else {
936
                ui.checkbox.check($('.list-view  li:not(".not-selectable") .check'));
937
            }
938
        });
939

    
940
        $('.trigger-checkbox.has-more .check').click(function(e) {
941
            ui.slideHiddenArea(this, $(this).parent().next('.more'));
942
        });
943

    
944
        $('.dhcp-option .check').click(function(e) {
945
            $(this).parents('li').siblings().find('ul.subnet-options').parent('li').toggle();
946
        });
947

    
948
        // for lis that we want to change the checkbox state
949
        $('.trigger-checkbox').click(function(e){
950
            $(this).find('.check').trigger('click');
951
        });
952

    
953
        $('.trigger-checkbox').find('a').click(function(e){
954
            e.stopPropagation();
955
        });
956

    
957
         // for checkboxes created after document.ready
958
        $('.items-list').on('click','.check', function(e){
959
            e.preventDefault();
960
            e.stopPropagation();
961
            ui.changeCheckboxState(this);
962
        });
963

    
964
        // radiobuttons binds
965

    
966
        $('.radiobtn').click(function(e) {
967
            e.stopPropagation();
968
            e.preventDefault();
969
            if($(this).find('span').hasClass('snf-radio-unchecked')) {
970
                ui.radiobtn.obtainOneChecked(this);
971
                ui.radiobtn.changeState(this);
972
            }
973
        });
974

    
975
        $('.subnet-options .radiobtn').click(function(e) {
976
            if($(this).closest('li').hasClass('manual')) {
977
                $(this).siblings('.input').show();
978
            }
979
            else {
980
                $(this).closest('li').siblings('.manual').find('.input').hide();
981
            }
982
        });       
983

    
984
        $('.trigger-radiobtn').click(function(e) {
985
            $(this).find('.radiobtn').trigger('click');
986
        });
987

    
988
        $('.firewall .more  .radiobtn').click(function(e){
989
            $(this).parents('.firewall').removeAttr('data-firewall');
990
            $(this).parents('.firewall').data('firewall', $(this).parent().attr('class'));
991
            ui.firewallSetup();
992
        });
993

    
994
    });
995
;/*
996
* functions regarding all wizards
997
*/
998

    
999
ui.wizard = {
1000
        domID: undefined,
1001
        currentStep: undefined,
1002
        totalStep: undefined,
1003
        btns: {
1004
                start: undefined,
1005
                close: undefined,
1006
                next: undefined,
1007
                previous: undefined,
1008
        },
1009
        areas : {
1010
                top: {
1011
                        header: undefined,
1012
                        menu: undefined
1013
                },
1014
                middle: undefined,
1015
                bottom: undefined
1016
        },
1017
        speed: 500,
1018
        onMove: 0,
1019

    
1020
        getCurrentStep: function(){
1021
                return ui.wizard.domID.find('.step').filter('.current').first();
1022
        },
1023

    
1024
        getNextStep: function() {
1025
        return ui.wizard.getCurrentStep().next();
1026
    },
1027

    
1028
    getPreviousStep: function() {
1029
        return ui.wizard.getCurrentStep().prev();
1030
    },
1031

    
1032
    getScrollOffset: function() {
1033
        return document.body.scrollTop;
1034
    },
1035

    
1036
        submitData: function() {
1037
                console.log('submit data dummy function');
1038
        },
1039

    
1040

    
1041

    
1042
        move: function(stepToDisplay, pos) {
1043
                ui.wizard.onMove = 1;
1044
                ui.wizard.focusFun();
1045
                ui.wizard.indicateStep();
1046
                ui.wizard.setMovementTags();
1047
                $('body').css('overflow','hidden');
1048
                // the current visible pane
1049
        var current = this.getCurrentStep();
1050
        // Set next pane position on the right of the current one
1051
        // Add current to the next pane, so that it will become
1052
        // visible
1053
        stepToDisplay.css({
1054
            left: pos.toString() + '%'
1055
        }).addClass("current");
1056
        // identify the current scroll position. Use it to
1057
        // set next pane top position. We assume all panes
1058
        // are within the scroll context of the main window.
1059
        stepToDisplay.css({
1060
            top: this.getScrollOffset() + 'px'
1061
        });
1062
        ui.wizard.domID.find('.step').animate({
1063
            marginLeft: (-pos).toString() + '%'
1064
        }, {
1065
                                complete: _.bind(function() {
1066
                // assuming all the following take place
1067
                // instantaneously within a single browser 
1068
                // render cycle, no flickering should occur.
1069
                current.removeClass("current");
1070
                            window.scroll(0, 0);
1071
                stepToDisplay.css({
1072
                    left: '0',
1073
                    top: '0'
1074
                });
1075
                $('.step').css({
1076
                    marginLeft: '0'
1077
                });
1078
                $('body').css('overflow','visible');
1079
                if (ui.wizard.currentStep == 3 ){
1080
                                        $('.vm-name input').first().focus();
1081
                }
1082
                $('.actions-bar').show();
1083
                ui.wizard.onMove = 0;
1084
            }, this)
1085
        });
1086

    
1087
        },
1088

    
1089
        data_next_array: ['el0', 'el2', 'el4', 'el7'],
1090

    
1091
        focusFun: function() {
1092
                var step = ui.wizard.currentStep;
1093
                var nextBtn = ui.wizard.btns.next;
1094

    
1095
                $('.firstfocus-' + step + '').first().focus();
1096
                nextBtn.removeAttr('data-next');
1097
                nextBtn.attr('data-next', ui.wizard.data_next_array[step]);
1098
        },
1099

    
1100
        goNext: function() {
1101
                var next = ui.wizard.getNextStep();
1102
                if (ui.wizard.onMove == 0) {
1103
                        ui.wizard.currentStep++;
1104
                        ui.wizard.move(next, 100);
1105
                }
1106
        },
1107

    
1108
        goPrev: function() {
1109
                var prev = ui.wizard.getPreviousStep();
1110
                if (ui.wizard.onMove == 0) {
1111
                        ui.wizard.currentStep--;
1112
                        ui.wizard.move(prev, -100);
1113
                }
1114
        },
1115

    
1116
        indicateStep: function() {
1117
                var step =ui.wizard.currentStep;
1118
                var menus = ui.wizard.areas.top.menu;
1119
                var wizardHeader = ui.wizard.areas.top.header;
1120
                
1121
                menus.find('.sub-menu').hide();
1122
                menus.find('.sub-menu[data-step='+step+']').fadeIn();
1123
                wizardHeader.children().removeClass('current');
1124
                wizardHeader.find('li:not(".current")').show().css("display", "inline");
1125
                wizardHeader.find('li:nth-child(' + step + ')').addClass('current');
1126
                wizardHeader.find('li.current').hide();
1127
                wizardHeader.find('li.current').fadeIn('slow').css("display", "inline");
1128
                wizardHeader.find('.current').prevAll('li').hide();
1129
        },
1130

    
1131
        // changes the text of next and previous buttons
1132
        setMovementTags: function() {
1133
                var step = ui.wizard.currentStep;
1134
                var totalStep = ui.wizard.totalStep;
1135
                var prevBtnLabel = ui.wizard.btns.previous.find('span');
1136
                var nextBtnLabel = ui.wizard.btns.next.find('span');
1137
                
1138
                if(totalStep == 1) {
1139
                        prevBtnLabel.html('CANCEL');
1140
                        nextBtnLabel.html('CREATE');
1141
                }
1142
                else {
1143
                        if (step == 1) {
1144
                                prevBtnLabel.html('CANCEL');
1145
                                nextBtnLabel.html('NEXT');
1146
                        } else if (step == totalStep) {
1147
                                prevBtnLabel.html('PREVIOUS');
1148
                                nextBtnLabel.html('CREATE');
1149
                        } else if (ui.wizard.vm.hideNext()){
1150
                                // ***
1151
                                ui.wizard.btns.next.hide();
1152
                        } else {
1153
                                prevBtnLabel.html('PREVIOUS');
1154
                                nextBtnLabel.html('NEXT');
1155
                        }
1156
                }
1157
        },
1158
        close: function() {
1159
                console.log('1');
1160
                $('body').removeClass('with-overlay');
1161
                $('.overlay-area-custom').fadeOut(400, function() {
1162
                        $('.overlay-div').hide();
1163
                        ui.wizard.reset();
1164
                });
1165
        },
1166

    
1167
        reset: function() {
1168
                var wizard = ui.wizard.domID;
1169
                var wizardType = wizard.attr('id');
1170

    
1171
                if(wizardType == 'vm-wizard') {
1172
                        wizard.find('.networks-area .more').show();
1173
                        wizard.find('.details').hide();
1174
                        wizard.find('.os li').hide();
1175
                        wizard.find('.advanced-conf-options').hide();
1176
                        wizard.find('.snf-color-picker').hide();
1177

    
1178
                }
1179
                else if(wizardType == 'network-wizard') {
1180
                        wizard.find('li .manual .input').hide();
1181
                wizard.find('ul.subnet-options').parent('li').show();
1182
                }
1183

    
1184
                ui.wizard.currentStep = 1;
1185
                wizard.find('.step').removeAttr('style');
1186
                wizard.find('.bottom').show();
1187
                wizard.find('input').val('');
1188
                ui.wizard.indicateStep();
1189
                ui.wizard.setMovementTags();
1190
                ui.wizard.preselectElements(wizardType);
1191
        },
1192

    
1193
        preselectElements: function(wizardType) {
1194
                var wizard = ui.wizard.domID;
1195

    
1196
                wizard.find('.current').not('.preselected').removeClass('current');
1197
                wizard.find('.preselected').not('.current').addClass('current');
1198
                wizard.find('.custom.dropdown.medium a:first').addClass('current'); // to fix
1199
                ui.radiobtn.reset(wizard);
1200
                ui.checkbox.reset(wizard);
1201
                wizard.find('.snf-arrow-up.preselected').toggleClass('snf-arrow-up snf-arrow-down');
1202
                if(wizardType == 'vm-wizard') {
1203
                        var preselectedImgCategory = wizard.find('.top .sub-menu[data-step=1] .preselected').data('img-type');
1204
                        wizard.find('.os .'+preselectedImgCategory).show();
1205
                }
1206
        },
1207

    
1208
        // use the showExplanatory and hideExplanatory functions
1209
        // when there's html structure as the following:
1210
        //         <div class="title">
1211
        //                 <p>EXPLANATORY TEXT</p>
1212
        //         </div>
1213
        //         <div class="options-bar">
1214
        //                 <div>
1215
        //                        <a href="">link</a><
1216
        //                 </div>
1217
        //         </div>
1218
        showExplanatoryText: function(link) {
1219
                var paragraph = $(link).parents('.options-bar').siblings('.title').find('p');
1220
                // var text = $(link).data('help');
1221
                // paragraph.html(text);
1222
                paragraph.css('visibility', 'visible');
1223
        },
1224

    
1225
        hideExplanatoryText: function(link) {
1226
                var paragraph = $(link).parents('.options-bar').siblings('.title').find('p');
1227
                paragraph.css('visibility', 'hidden');
1228
        },
1229

    
1230
        vm: {
1231
                hideNext: function() {
1232
                        if(ui.wizard.currentStep == 2 && $('.flavor a.disabled').hasClass('small')) {
1233
                                return true;
1234
                        }
1235
                        else {
1236
                                return false;
1237
                        }
1238
                },
1239
                getSize: function(elem) {
1240
                        if ($(elem).hasClass('small')) {
1241
                                return 'small';
1242
                        } else if ($(elem).hasClass('medium')) {
1243
                                return 'medium';
1244
                        } else if ($(elem).hasClass('large')) {
1245
                                return 'large';
1246
                        }
1247
                },
1248

    
1249

    
1250
                pickResources: function(resource) {
1251
                        var wizard = ui.wizard.domID;
1252
                        wizard.find('.flavor .with-flavor a:not(.' + resource + ')').removeClass('current');
1253
                        wizard.find('.flavor .with-flavor a.' + resource + '').addClass('current');
1254
                },
1255

    
1256
                showImageCategory: function(imagesCategory) {
1257
                        var wizard = ui.wizard.domID;
1258
                        var selectedImages = $(imagesCategory).data('img-type');
1259
                        wizard.find(imagesCategory).closest('.sub-menu').find('.current').removeClass('current');
1260
                        wizard.find(imagesCategory).addClass('current');
1261
                        wizard.find('.os li').hide();
1262
                        wizard.find('.os .details').hide();
1263
                        wizard.find('.os').find('.'+selectedImages).fadeIn();
1264
                }
1265
        },
1266
        network : { }
1267
}
1268

    
1269
$(document).ready(function() {
1270

    
1271
        if($('.wizard').length>0) {
1272
                var wizardType = $('.wizard').attr('id');
1273
                var wizard = ui.wizard.domID =$('#'+wizardType);
1274
                ui.wizard.totalStep = ui.wizard.domID.find('.step').length;
1275
                ui.wizard.currentStep = 1;
1276
                var topHeaderArea = ui.wizard.areas.top.header = wizard.find('.top .nums');
1277
                var topMenuArea = ui.wizard.areas.top.menu = wizard.find('.top .menus');
1278
                var middleArea = ui.wizard.areas.middle = wizard.find('.middle');
1279
                var bottomArea = ui.wizard.areas.bottom = wizard.find('.bottom');
1280

    
1281
                var prevBtn = ui.wizard.btns.previous = ui.wizard.areas.bottom.find('.nav.prev');
1282
                var nextBtn = ui.wizard.btns.next = ui.wizard.areas.bottom.find('.nav.next');
1283
                var closeBtn = ui.wizard.btns.close = wizard.find('.close');
1284

    
1285
                // various functions for creation wizards
1286
                closeBtn.click(function(e) {
1287
                        e.preventDefault();
1288
                        ui.wizard.close();
1289
                });
1290

    
1291
                nextBtn.click(function(e) {
1292
                        e.preventDefault();
1293
                        if (ui.wizard.currentStep == ui.wizard.totalStep ){
1294
                                ui.wizard.submitData();
1295
                                ui.wizard.close();
1296
                                return false;
1297
                        }
1298
                        ui.wizard.goNext();
1299

    
1300
                });
1301

    
1302
                prevBtn.click(function(e) {
1303
                        e.preventDefault();
1304
                        if (ui.wizard.currentStep == 1 ){
1305
                                ui.wizard.close();
1306
                                return false;
1307
                        }
1308
                        ui.wizard.goPrev();
1309
                });
1310

    
1311

    
1312
                $(document).keydown(function(e) {
1313
                        var exp1 = $('.vm-name input').is(':focus') && $('.vm-name input').val().length>0;
1314
                        var exp2 = $('.form-item input').is(':focus') && $('.form-item input').val().length>0;
1315
                        // right arrow keyCode == 39
1316
                        if (wizard.is(':visible').length != 0) {
1317
                                if (e.keyCode == 39 && ui.wizard.currentStep != (ui.wizard.totalStep) &&(!exp1) && (!exp2)) {
1318
                                        ui.wizard.goNext();
1319
                                        return false;
1320
                                }
1321
                                // left arrow keyCode == 37
1322
                                else if (e.keyCode == 37 && ui.wizard.currentStep != 1 &&(!exp1) &&(!exp2)) {
1323
                                        ui.wizard.goPrev();
1324
                                        return false;
1325
                                }
1326
                                // ESC
1327
                                else if (e.keyCode == 27 && ui.wizard.currentStep == 1) {
1328
                                        ui.wizard.close();
1329
                                }
1330
                        }
1331
                });
1332

    
1333
                // focus and tabs functionality
1334
                 wizard.find('a').keyup(function(e) {
1335
                        var self = this;
1336
                        if (e.keyCode == 9 || e.which == 9) {
1337
                                if (e.shiftKey) {
1338
                                } else {
1339
                                        //Focus next input
1340
                                        if ($(self).attr('data-next')) {
1341
                                                $(self).focusout(function(e) {
1342
                                                        var classname = $(self).data('next');
1343
                                                        wizard.find('.' + classname + '').first().focus();
1344
                                                });
1345
                                        }
1346
                                }
1347
                        }
1348
                });
1349

    
1350
                // navigation and numbers functions
1351
                wizard.find('.nav.previous').focus(function(e) {
1352
                        $(this).addClass('active');
1353
                });
1354

    
1355
                wizard.find('.nav.previous').focusout(function(e) {
1356
                        $(this).removeClass('active');
1357

    
1358
                });
1359

    
1360
                if(wizardType == 'vm-wizard') {
1361
                        
1362
                        // step-1: pick OS
1363
                        wizard.find('.os > li').keydown(function(e) {
1364
                          if(e.keyCode == 13) {
1365
                            $(this).trigger("click", true);
1366
                            e.preventDefault();
1367
                          }
1368
                        });
1369
                        wizard.find('.os .btn-col a').keydown(function(e) {
1370
                          if(e.keyCode == 13) {
1371
                            $(this).trigger("click", true);
1372
                            e.preventDefault();
1373
                            e.stopPropagation();
1374
                          }
1375
                        });
1376

    
1377
                        wizard.find('.os > li').click(function(e, wasEnterKey) {
1378
                                e.preventDefault();
1379
                                if ( $(this).hasClass('current') && wasEnterKey) {
1380
                                        ui.wizard.goNext();
1381
                                }
1382
                                wizard.find('.os >li').removeClass('current');
1383
                                $(this).addClass('current');
1384
                        });
1385

    
1386
                        wizard.find('.os .btn-col a').click(function(e) {
1387
                                e.preventDefault();
1388
                                e.stopPropagation();
1389
                                $(this).toggleClass('current');
1390
                                var self = this;
1391
                                $(this).parents('li').find('.details').stop().slideToggle();
1392
                        });
1393

    
1394
                        wizard.find('.top .sub-menu[data-step=1] a').click(function(e) {
1395
                                e.preventDefault();
1396
                                ui.wizard.vm.showImageCategory(this);
1397
                        });
1398

    
1399
                        wizard.find('.os .name-col').focus(function(e) {
1400
                                $(this).parents('li').addClass('hover');
1401
                        });
1402

    
1403
                        wizard.find('.os .name-col').focusout(function(e) {
1404
                                $(this).parents('li').removeClass('hover');
1405
                        });
1406

    
1407
                        // step-2: pick flavor
1408
                        var disabledElems = wizard.find('.flavor a.disabled');
1409
                        var disabledElemsNum = disabledElems.length;
1410
                        var size;
1411
                        if(disabledElemsNum>0) {
1412
                                for(i=0; i<disabledElemsNum; i++) {
1413
                                        size = ui.wizard.vm.getSize(disabledElems.get(i));
1414
                                        wizard.find('.sub-menu[data-step=2] a[data-size=' + size + ']').removeClass('current').addClass('disabled');
1415
                                        wizard.find('.flavor .'+size).removeClass('current preselected');
1416
                                        if(size == 'small') {
1417
                                                wizard.find('.flavor .vm-storage-selection a').removeClass('current preselected');
1418

    
1419
                                        }
1420
                                }
1421
                        }
1422
                        wizard.find('.sub-menu a[data-size]:not(.disabled)').click(function(e) {
1423
                                e.preventDefault();
1424
                                $(this).parents('.sub-menu').find('a').removeClass('current');
1425
                                $(this).addClass('current');
1426
                                ui.wizard.vm.pickResources($(this).data('size'));
1427
                        });
1428

    
1429
                        wizard.find('.flavor .options:not(.vm-storage-selection) a:not(.disabled)').click(function(e) {
1430
                                e.preventDefault();
1431
                                wizard.find('.sub-menu a[data-size]').removeClass('current');
1432
                                $(this).parents('.options').find('a').removeClass('current');
1433
                                $(this).addClass('current');
1434
                                size = ui.wizard.vm.getSize(this);
1435
                                var count = wizard.find('.options.with-flavor .' + size + '.current').length;
1436
                                if (count == 3) {
1437
                                        wizard.find('.sub-menu[data-step=2] a[data-size=' + size + ']').addClass('current');
1438
                                }
1439
                        });
1440

    
1441
                        wizard.find('.flavor .options.vm-storage-selection a').click(function(e) {
1442
                                e.preventDefault();
1443
                                $(this).parents('.options').find('a').removeClass('current');
1444
                                $(this).addClass('current');
1445
                        });
1446

    
1447
                        wizard.find('.flavor .options a').hover(
1448
                                function() {
1449
                                        ui.wizard.showExplanatoryText($(this))
1450
                                }, function() {
1451
                                        ui.wizard.hideExplanatoryText($(this))
1452
                                });
1453

    
1454
                        wizard.find('.sub-menu[data-step=2] li:last').find('a').focusout(function(e) {
1455
                                $('.step-2').find('.dropdown a:first').focus();
1456
                        });
1457

    
1458
                        // step-3: custom options
1459
                        wizard.find('.expand-arrow').click(function(e) {
1460
                                e.preventDefault();
1461
                                ui.expandDownArea(this, wizard.find('.advanced-conf-options'));
1462
                        });
1463
                }
1464

    
1465
                else if (wizardType == 'volume-wizard') {
1466
                        // step-1
1467
                        wizard.find('.volume_options .options a').hover(
1468
                                function() {
1469
                                        ui.wizard.showExplanatoryText($(this))
1470
                                }, function() {
1471
                                        ui.wizard.hideExplanatoryText($(this))
1472
                                });
1473
                        wizard.find('.volume_options .options a:not(.disabled)').click(function(e) {
1474
                                e.preventDefault();
1475
                                if(!$(this).hasClass('current')) {
1476
                                        $(this).closest('.options').find('.current').removeClass('current');
1477
                                        $(this).addClass('current');
1478
                                }
1479
                        });
1480
                }
1481

    
1482
        }
1483

    
1484
})
1485
;/*
1486
* functions that are used to the ssh0keys creation wizard
1487
*/
1488

    
1489
var wizard = $('#sshkeys-wizard');
1490
$(document).ready(function(){
1491
        generateArea = wizard.find('.generate-key-area');
1492
        generateBtn = wizard.find('.generate-key-btn');
1493
        importArea = wizard.find('.import-key-area');
1494
        importBtn = wizard.find('.import-key-btn');
1495
        editBtn = wizard.find('.edit');
1496
        importCloseBtn = importArea.find('.buttons a');
1497
        generateCloseBtn = generateArea.find('.buttons a');
1498

    
1499
        generateBtn.click(function(e) {
1500
                e.preventDefault();
1501
                if(!generateArea.is(':visible')) {
1502
                        // set the right color to the buttons
1503
                        $(this).addClass('current');
1504
                        $(this).siblings('a').removeClass('current');
1505

    
1506
                        if(importArea.is(':visible')) {
1507
                                importArea.stop().fadeOut(200, function() {
1508
                                generateArea.stop().slideDown();
1509
                                });
1510
                        }
1511
                        else {
1512
                                generateArea.stop().slideDown();
1513
                        }
1514
                }
1515
        });
1516

    
1517
        importBtn.click(function(e) {
1518
                e.preventDefault();
1519
                if(!importArea.is(':visible')) {
1520
                        // set the right color to the buttons
1521
                        $(this).addClass('current');
1522
                        $(this).siblings('a').removeClass('current');
1523

    
1524
                        if(generateArea.is(':visible')) {
1525
                                generateArea.stop().fadeOut(200, function() {
1526
                                importArea.stop().slideDown();
1527
                                });
1528
                        }
1529
                        else {
1530
                                importArea.stop().slideDown();
1531
                        }
1532
                }
1533
        });
1534

    
1535
        importCloseBtn.click(function(e) {
1536
                e.preventDefault();
1537
                e.stopPropagation();
1538
                importArea.slideUp();
1539
                importBtn.removeClass('current');
1540
        });
1541

    
1542
        generateCloseBtn.click(function(e) {
1543
                e.preventDefault();
1544
                e.stopPropagation();
1545
                generateArea.slideUp();
1546
                generateBtn.removeClass('current');
1547
        });
1548
})