Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / common.js @ 112e5dd2

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

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

    
21

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

    
30

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

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

    
53
/* 
54
* Logic for Entities actions. Present in items_list pages
55
* Available categories are :
56
*  - both/single ( for multiple entities/single entities)
57
*  - running/off ( for running/off entities)
58
*  - permanent ( for entities always active )
59
*/
60
ui.entitiesActionsEnabled = function(){
61
    var all = $('.snf-checkbox-checked').length;
62
    var running = $('.snf-checkbox-checked').parents('li.running').length;
63
    var off = $('.snf-checkbox-checked').parents('li.off').length;
64
    console.log(off, 'actions here');
65
    $('.lt-bar .lt-actions li:not(.permanent) a').removeClass('active');
66
    if ( (running*off) > 0 ){
67
         $('.lt-actions li.both a').addClass('active');
68
         $('.lt-actions li.single a').removeClass('active');
69
    } else {
70
        if (running > 0) {
71
            $('.lt-actions li.both a').addClass('active');
72
            $('.lt-actions li.running a').addClass('active');
73
        } else if (off>0) {
74
            $('.lt-actions li.both a').addClass('active');
75
            $('.lt-actions li.off a').addClass('active');
76
        }
77
        if ( all > 1 ) {
78
            $('.lt-actions li.single a').removeClass('active');
79
        }
80
    }
81
}
82

    
83
ui.inactiveActions = function() {
84

    
85
    // Availble actions: connect, reboot, shut, destroy, start
86
    // These actions will be DISABLED
87
    var statesActions ={
88
        'off'      : ['connect', 'reboot', 'shut'],
89
        'error'    : ['connect', 'reboot', 'shut', 'start'],
90
        'building' : ['reboot', 'start'],
91
        'running'  : ['start'],
92
        'rebooting': ['start'],
93
        'starting' : ['start'],
94
        'shutting' : ['connect', 'reboot', 'shut']
95
    } ;
96

    
97
    _.each (statesActions, function(val, key) {
98
        _.each(val, function(value) {
99
            var el = '.' + key + ' .' + value;
100
            $(el).addClass('inactive');
101
        });
102
    })
103
}
104

    
105
ui.detailsCustom = function(id) {
106
    // position last connected item
107
    var el = $('#'+id+'.connected .item').last();
108
    // -2 is the border width;
109
    var moveY = el.find('.connections >li').last().outerHeight(true) - 2;
110
    el.css('top',moveY);
111
    console.log(moveY);
112
    el.css('marginTop', -moveY);
113
}
114

    
115
ui.firewallSetup = function(){
116
    $('.firewall').each(function(){
117
        $(this).removeClass('fully unprotected basic');
118
        $(this).addClass($(this).data('firewall'));
119
        if($(this).hasClass('unprotected')){
120
            $(this).find('p').first().find('em').html('off');
121
        } else {
122
            $(this).find('p').first().find('em').html('on');
123
        }
124
    });
125
    $('.firewall .more span').each(function(e){
126
        var that = this;
127
        if ($(this).hasClass('snf-radio-checked')){
128
            $(that).siblings('em').html('on');
129
        } else {
130
            $(that).siblings('em').html('off');
131
        }
132
    })
133
}
134

    
135

    
136
/*
137
* In order for the editable value functionality to work, the html markup
138
* should be:
139
    <div class="editable">
140
        <span class="input-txt">editable value</span>
141
        <input type="text">
142
        <a href="#" class="edit">edit</a>
143
        <a href="#" class="save">save</a>
144
        <a href="#" class="cancel">cancel</a>
145
    </div>
146
*/
147
ui.editable = function(){
148

    
149
/*
150
* resetForm hides save and cancel buttons,
151
* text input and shows input-txt. resetForm does not alter
152
* input-txt content.
153
*/
154

    
155
    function resetForm(e, elem) {
156
        var el = elem.parents('.editable');
157
        el.find('input[type="text"]').hide();
158
        el.find('a.cancel, a.save').hide();
159
        el.find('a.edit, .input-txt').show();
160
        el.find('.delete').show();
161
    }
162

    
163
/* 
164
* showForm hides input-txt, shows save and cancel buttons and
165
* sets input value to input-txt content.
166
*/
167
    function showForm(e,elem) {
168
        e.stopPropagation(); 
169
        e.preventDefault();
170
        var el = elem.parents('.editable'); 
171
        el.find('input[type="text"]').val(el.find('.input-txt').html());
172
        el.find('input[type="text"]').show();
173
        el.find('a.cancel, a.save').show();
174
        elem.hide();
175
        el.find('.input-txt').hide();
176
        if(el.find('.delete').length != 0) {
177
            el.find('.delete').hide();
178
        }
179

    
180
    }
181

    
182
/*
183
setValue sets input-txt value to the input value.
184
Makes sure that the input value is not empty.
185
TODO:
186
Ajax request to submit form
187
*/
188

    
189
    function setValue(elem) {
190
        var el = elem.parents('.editable');
191
        if( el.find('input[type="text"]').val() ) {
192
            el.find('.input-txt').html(el.find('input[type="text"]').val());
193
        }
194
    }
195

    
196

    
197
    $('.editable .edit').click(function(e){
198
        showForm(e, $(this));
199
    })
200

    
201
    $('.editable .cancel').click(function(e){
202
        e.stopPropagation();
203
        e.preventDefault();
204
        resetForm(e, $(this));
205
    })
206

    
207
    $('.editable .save').click(function(e){
208
        e.stopPropagation();
209
        e.preventDefault();
210
        setValue($(this));
211
        resetForm(e, $(this));
212

    
213
    })
214

    
215

    
216
    $('.editable input[type="text"]').click(function(e){
217
        e.stopPropagation();
218
    })
219

    
220
    $('.editable input[type="text"]').keyup(function(e){
221
        if(e.keyCode == 13) { 
222
            setValue($(this));
223
            resetForm(e, $(this));
224
            
225
        }
226
    
227
    })
228

    
229
    $('html').click(function(e) {
230
        resetForm(e, $('.editable a.cancel'));
231
        // not sure if we want to hide the error window after every click in the ui
232
        if($('.communication-error').css('bottom') == '0px') {
233
            $('.communication-error').animate({bottom: "-151px"});
234
        }
235
    });
236

    
237
}
238

    
239
/* TODO: better overlay functionality */
240
ui.overlay = function() {
241
    $('[data-overlay-id]').click(function(e){
242
        e.preventDefault();
243
        var el = $(this);
244
        var id = '#'+el.data('overlay-id');
245

    
246
        $('.overlay-area-custom').fadeIn(100);
247
        $('body').addClass('with-overlay');
248
        $(id).fadeIn('slow');
249
        if (id=='#network-wizard') {
250
            $(id).find('input').first().focus();
251
            return false;
252
        }
253
        $(id).find('a').first().focus();
254
    });
255
}
256

    
257
function goToByScroll(id){
258
      // Remove "link" from the ID
259
    id = id.replace("link", "");
260
      // Scroll
261
    $('html,body').animate({
262
        scrollTop: $("#"+id).offset().top},
263
        'slow');
264
}
265

    
266

    
267
/*
268
* functions concerning checkboxes and radiobuttons links
269
*/
270

    
271
ui.changeCheckboxState =function(checkbox_link) {
272
    $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
273
    ui.entitiesActionsEnabled();
274
}
275

    
276
// to use the above functions use first the ui.checkOneRadioButton and then ui.changeRadiobuttonState
277
ui.checkOneRadioButton = function(radiobtn_link) {
278
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
279
}
280
ui.changeRadiobuttonState = function(radiobtn_link) {
281
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
282
}
283

    
284

    
285

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

    
300
            });
301
}
302

    
303

    
304
/* Tabs functionality
305
* tabsEl is the div/ul with the links to the sections and the sections that
306
* with toggle have class sectionEl.
307
* Markup needed is an href to each a with the id of the relative section.
308
*/
309
ui.tabs = function(tabsEl, sectionEl) {
310
    var tabLink = tabsEl.find('a');
311
    function href(a) {
312
        return a.attr('href');
313
    }
314
    tabLink.click(function(e){
315
        e.preventDefault();
316
        if ( $(this).hasClass('active')){
317
             return false;
318
        } else {
319
            $(this).parents(tabsEl).find('a').removeClass('active');
320
            $(this).addClass('active');
321
            $(sectionEl).hide();
322
            sectionVis = $(href($(this)));
323
            sectionVisID = sectionVis.attr('id');
324
            $(href($(this))).stop(true,true).fadeIn(500, function(){
325
                ui.detailsCustom(sectionVisID);
326
            });
327
        }
328
    })
329
}
330

    
331
$(document).ready(function(){
332

    
333
    if($('.vms.entities').length!=0){
334
        ui.inactiveActions();
335
    };
336
    ui.setElminHeight($('.main > .details'));
337
    ui.setElminHeight($('.lt-bar'));
338
    ui.setElHeight($('.scroll-wrap'));
339
    $('#hd-search .hd-icon-search').click(function(e){
340
        var that = this;
341
        $(this).parents('.hd-search').toggleClass('hd-open');
342
        if ($(this).parents('.hd-search').hasClass('hd-open')) {
343
            $(that).parents('.hd-search').find('input[type="search"]').focus();
344
        } else {
345
            $(that).parents('.hd-search').find('input[type="search"]').val('');
346
        }
347
    })
348

    
349
    $('.header .login').mouseenter(function(e){
350
        $(this).find('ul').stop(true, true).slideDown(200);
351
    });
352
    $('.header .login').mouseleave(function(e){
353
        $(this).find('ul').stop(true, true).slideUp(200);
354
    });
355

    
356
    $('.entities a').click(function(){
357
        if ($(this).attr('data-reveal-id') && !($(this).hasClass('inactive'))) {
358
            $('.entities li .more').hide();
359
        }
360
    });
361

    
362
    $('.inactive').click(function(e){
363
        e.stopPropagation();
364
        e.preventDefault();
365
     })
366

    
367

    
368
    $('.lt-actions a:not(.active)').click(function(e){
369
        e.preventDefault();
370
    })
371

    
372
    if ($('.entities .items-list >li').length == 1) {
373
        $('.overlay-wrapper').addClass('no-vm');
374
    };
375
    $('.entities li .more').each(function(){
376
        var width = $(this).parent('li').outerWidth()  + 20;
377
        $(this).css('width', width);
378
    });
379

    
380
    $('.items-list li .img-wrap').on("mouseenter", function(e) {
381
        var that = this;
382
        if ($(this).parents('.entities').hasClass('grid-view')) {
383
            if ($(that).parent('.container').siblings('.more').length>0) {
384
                $(that).parent('.container').fadeOut(50,'easeInCirc');
385
                $(that).parent('.container').siblings('.more').fadeIn(150,'easeInCirc');
386
            }
387
        }
388
    });
389
    $('.entities li .more').mouseleave(function(e) {
390
        $(this).fadeOut(50, function() {
391
            $(this).siblings('.container').fadeIn(50);
392
        });
393
    });
394
    $('.grid-view .items-list > li').mouseleave(function(e){
395
        var that = this;
396
        setTimeout(function(){
397
            $(that).find('.more').fadeOut(200, function() {
398
                $(this).siblings('.container').fadeIn('fast');
399
            });
400
        },50)
401
    });
402

    
403
    ui.closeDiv($('.info .close'), '.info');
404
    ui.editable();
405
    ui.overlay();
406
    ui.colorPickerVisible = 0;
407

    
408
    $("a.disabled").each(function() {
409
        $(this).removeAttr('href');
410
    });
411
    
412
    $("a.disabled").click(function(e) {
413
        e.preventDefault();
414
    });
415

    
416
    // checkbox: basic reaction on click (checked, unchecked)
417
    // see wizard
418
    $('.check').click(function(e){
419
        e.preventDefault();
420
        e.stopPropagation();
421
        ui.changeCheckboxState(this);
422
    });
423

    
424

    
425
    $('.with-checkbox').click(function(e){
426
        e.preventDefault();
427
        e.stopPropagation();
428
        var checkbox = self.find('.check');
429
        ui.changeCheckboxState(checkbox);
430
    });
431

    
432
    $('.radio_btn').click(function(e) {
433
        e.preventDefault();
434
         var state = $(this).find('span');
435
         if(state.hasClass('snf-radio-unchecked')) {
436
            ui.checkOneRadioButton(this);
437
            ui.changeRadiobuttonState(this);
438
        }
439
    });
440

    
441
    $('.main-actions li a').click(function(e){
442
        if (!($(this).hasClass('active'))) {
443
            e.preventDefault();
444
        }
445
    });
446

    
447
    $('.main-actions li a').click(function(e){
448
        if (!($(this).hasClass('active'))) {
449
            e.preventDefault();
450
        }
451
    });
452
    $('.overlay-area-custom').children('.close').click(function(e){
453
        e.preventDefault();
454
        e.stopPropagation();
455
        $(this).parents('.overlay-area-custom').hide();
456
        $(this).parents('.overlay-area-custom').find($('.overlay-div')).hide();
457
        $('body').removeClass('with-overlay');
458
    })
459

    
460
    $('.browse-files').click(function(e){
461
        e.preventDefault();
462
    })
463

    
464
    Dropzone.options.filesDropzone = {
465
        dictDefaultMessage:'',
466
        clickable: '.browse-files',
467
        previewsContainer: '.dropzone-files',
468
        createImageThumbnails: false,
469
        dictRemoveFile: "snf-Remove file",
470
    };
471

    
472

    
473
    $('.main .files').magnificPopup({
474
        delegate: 'a.show.image',
475
        type: 'image',
476
        tLoading: 'Loading image #%curr%...',
477
        mainClass: 'mfp-img-mobile',
478
        gallery: {
479
            enabled: true,
480
            navigateByImgClick: true,
481
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
482
        },
483
        image: {
484
            tError: 'The image could not be loaded.',
485
            titleSrc: function(item) {
486
                return item.el.data('title');
487
            }
488
        }
489
    });
490

    
491
    if($('#picker').length>0) {
492
        $('#picker').farbtastic('#color');
493
    };
494
    if($('#sb-search').length>0) {
495
        new UISearch( document.getElementById( 'sb-search' ) );
496
    }
497

    
498

    
499
    /* grid/list view for items-list */
500

    
501
    $('.view-type .list').click(function(e){
502
        e.preventDefault();
503
        $('.view-type .grid span').removeClass('current');
504
        $(this).find('span').addClass('current');
505
        $('.entities').removeClass('grid-view');
506
        $('.entities').addClass('list-view');
507
    });
508

    
509
     $('.view-type .grid').click(function(e){
510
        e.preventDefault();
511
        $('.view-type .list span').removeClass('current');
512
        $(this).find('span').addClass('current');
513
        $('.entities').addClass('grid-view');
514
        $('.entities').removeClass('list-view');
515
    });
516

    
517
     $('.lt-bar .select').click(function(e){
518
        $(this).find('span').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
519
        $(this).find('em').toggle();
520
        if ( $(this).find('span').hasClass('snf-checkbox-unchecked')){
521
            $('.list-view  li .check span').removeClass('snf-checkbox-checked');
522
            $('.list-view  li .check span').addClass('snf-checkbox-unchecked');
523
        } else {
524
            $('.list-view  li .check span').addClass('snf-checkbox-checked');
525
            $('.list-view  li .check span').removeClass('snf-checkbox-unchecked');
526
        }
527
        ui.entitiesActionsEnabled();
528
     });
529

    
530

    
531
    // set filter visible
532
    $('.filter-menu .filter').click(function(e) {
533
        e.preventDefault();
534
        $(this).parents('.filter-menu').toggleClass('current');
535
    });
536

    
537
    // temp function used to demonstrate the visual effect of the building state of vm
538
    $('[data-status="building"] .btn5.temp').click(function(e) {
539
        e.preventDefault();
540
        $(this).siblings('.container').find('.complete').toggleClass('build-progress');
541
    });
542

    
543
    $('[data-status="rebooting"] .btn5.temp').click(function(e) {
544
        e.preventDefault();
545
        $(this).siblings('.container').find('.snf-pc-full').toggleClass('reboot-progress');
546
    })
547

    
548
    // //temp function to preventDefault of links in modal
549
    // $('.reveal-modal a:not(".close-reveal-modal, .generate-key-btn, .import-key-btn")').click(function(e){
550
    //     e.preventDefault();
551
    //     $('a.close-reveal-modal').trigger('click');
552
    // });
553

    
554
     // temp btn to show communication error message
555
    $('.temp-for-btns .communication-error-btn').click(function(e) {
556
         e.preventDefault();
557
         e.stopPropagation();
558
         $('.communication-error').animate({bottom: "0px"});
559
     });
560

    
561
    $('.communication-error a').click(function(e) {
562
        e.preventDefault();
563
        e.stopPropagation();
564
        $('.communication-error').animate({bottom: "-151px"});
565

    
566
    });
567
    $('.communication-error').click(function(e) {
568
        e.stopPropagation();
569
    });
570

    
571
    $('.show-add-tag').click(function(e) {
572
    e.preventDefault();
573
    $(this).parents('.tags-area, .tags').find('.snf-color-picker').slideDown('slow', function() {
574
        $('#hide-add-tag-dummy').scrollintoview({
575
            'duration': 'slow'
576
        });
577
    });
578
    ui.colorPickerVisible = 1;
579
    });
580

    
581
    $('.hide-add-tag').click(function(e) {
582
    e.preventDefault();
583
    $(this).parents('.tags-area, .tags').find('.snf-color-picker').slideUp(400, function() {
584
        $('.show-add-tag').first().scrollintoview({
585
            'duration': 'slow'
586
        });
587
        ui.colorPickerVisible = 0;
588
    });
589
    });
590

    
591
    // connected details js
592
    ui.detailsCustom('disk-connected');
593
    ui.detailsCustom('network-connected');
594
    ui.detailsCustom('vm-connected');
595
    ui.firewallSetup();
596
    $('.firewall .more  a').click(function(e){
597
        e.preventDefault();
598
        if ($(this).find('span').hasClass('snf-radio-checked')) {
599
            return false;
600
        }
601
        $(this).parents('.firewall').removeAttr('data-firewall');
602
        $(this).parents('.firewall').data('firewall', $(this).parent().attr('class'));
603
        $(this).find('span').toggleClass('snf-radio-unchecked snf-radio-checked');
604
        $(this).parent('p').siblings('p').find('span').removeClass('snf-radio-checked');
605
        $(this).parent('p').siblings('p').find('span').addClass('snf-radio-unchecked');
606
         ui.firewallSetup();
607
    });
608
    $('.firewall').mouseenter(function(e){
609
        $(this).find('.more').stop(true, true).slideDown(200);
610
    });
611
    $('.firewall').mouseleave(function(e){
612
        $(this).find('.more').stop(true, true).slideUp(200);
613
    });
614
    ui.tabs($('.tabs'), $('.content'));
615

    
616
    $('.toggle-lt-bar').click(function(e){
617
        e.preventDefault();
618
        var self =this;
619
        if($(this).hasClass('fix-position')) {
620
            $(this).fadeOut({
621
                complete: function() {
622
                $(self).removeClass('fix-position');
623
                $(self).fadeIn();
624
                },
625
                start: function() {
626
                $('.lt-bar').animate({ width: 'toggle' });
627
                }
628
        });
629
        }
630
        else {
631
            $(this).addClass('fix-position');
632
            $('.lt-bar').animate({ width: 'toggle' });
633
        }
634
    });
635

    
636

    
637
    $('.act').click(function(e) {
638
        $(this).addClass('pending last');
639
    });
640

    
641
    $('.remove .cancel').click(function(e) {
642
        e.preventDefault();
643
        $('a.close-reveal-modal').trigger('click');
644
        $('.last').removeClass('pending last');
645
    });
646

    
647
    $('.remove .ok').click(function(e) {
648
        e.preventDefault();
649
        $('a.close-reveal-modal').trigger('click');
650
        var connection_img = $('.connections').find('.last');
651
        connection_img.addClass('open', 0);
652
        connection_img.removeClass('last');;
653
        var img = connection_img.closest('.item').find('.img-wrap .snf-font');
654
        img.addClass('reboot-progress');
655
        setTimeout(function() {
656
            var connections_list = connection_img.closest('.connections').children('li:not(".hidden")');
657
            if(connections_list.length>1) {
658
                connection_img.closest('li').slideUp(function(){
659
                    console.log(img)
660
                    connection_img.closest('li').addClass('hidden');
661
                    if(connections_list.find('.pending').length == 0) {
662
                      img.removeClass('reboot-progress');
663
                    }
664
                });
665
            }
666
            else {
667
                connection_img.closest('.item').slideUp(function(){
668
                    img.removeClass('reboot-progress');
669
                    img.closest('li').addClass('hidden');
670
                    img.removeClass('reboot-progress');
671
                });
672
            }
673
            connection_img.removeClass('pending');
674
        }, 3000)
675
    });
676

    
677
    // end of connected details js
678
})
679

    
680

    
681
$(window).resize(function(e){
682
    ui.setElminHeight($('.main > .details'));
683
    ui.setElminHeight($('.lt-bar'));
684
    ui.setElHeight($('.scroll-wrap'));
685
})