Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (20.6 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
ui.changeRadiobuttonState = function(radiobtn_link) {
277
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
278
}
279

    
280
ui.checkOneRadioButton = function(radiobtn_link) {
281
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
282
}
283

    
284

    
285
// toggle expand arrrow  and corresponding 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

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

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

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

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

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

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

    
366

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

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

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

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

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

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

    
423

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

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

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

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

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

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

    
471

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

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

    
497

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

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

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

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

    
529

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

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

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

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

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

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

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

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

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

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

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

    
635
    // to remove the visual effect that shows that the process of disconnect is in progress
636
    // we must not have pending clicks (incomplite hide process)
637
    var countClicks = 0;
638

    
639
    // normally there's a confirmation window before you disconnect
640
    $('.act').click(function(e){
641
        e.preventDefault();
642
        countClicks++;
643
        $(this).toggleClass('open',0);
644
        var img = $(this).closest('.item').find('.img-wrap .snf-font');
645
        img.addClass('reboot-progress');
646
        var self = this;
647
        setTimeout(function() {
648
            countClicks--;
649
            var connections_list = $(self).closest('.connections').children('li:not(".hidden")');
650
            if(connections_list.length>1) {
651
                $(self).closest('li').slideUp(function(){
652
                    console.log(img)
653
                    $(self).closest('li').addClass('hidden');
654
                    if(countClicks== 0) img.removeClass('reboot-progress');
655
                });
656
            }
657
            else {
658
            $(self).closest('.item').slideUp('slow', function(){
659
                    img.removeClass('reboot-progress')
660
                    $(self).closest('li').addClass('hidden');
661
                    if(countClicks== 0) img.removeClass('reboot-progress');
662
                });
663
            }
664
        }, 3000)
665
    })
666

    
667
    // end of connected details js
668
})
669

    
670

    
671
$(window).resize(function(e){
672
    ui.setElminHeight($('.details'));
673
    ui.setElminHeight($('.lt-bar'));
674
    ui.setElHeight($('.scroll-wrap'));
675
})