Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (18.3 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
/* Sets mainArea min-height
31
* Used for .details div
32
*/
33
ui.setElminHeight = function(el){
34
    var WindowHeight = $(window).height();
35
    var header = $('.header').outerHeight();
36
    var actions = $('.actions').outerHeight();
37
    var h1= WindowHeight - (header+actions);
38
    el.css('min-height', h1);
39
}
40

    
41

    
42
/* 
43
* Logic for Entities actions. Present in items_list pages
44
* Available categories are :
45
*  - both/single ( for multiple entities/single entities)
46
*  - running/off ( for running/off entities)
47
*  - permanent ( for entities always active )
48
*/
49
ui.entitiesActionsEnabled = function(){
50
    var all = $('.snf-checkbox-checked').length;
51
    var running = $('.snf-checkbox-checked').parents('li.running').length;
52
    var off = $('.snf-checkbox-checked').parents('li.off').length;
53
    console.log(off, 'actions here');
54
    $('.lt-bar .lt-actions li:not(.permanent) a').removeClass('active');
55
    if ( (running*off) > 0 ){
56
         $('.lt-actions li.both a').addClass('active');
57
         $('.lt-actions li.single a').removeClass('active');
58
    } else {
59
        if (running > 0) {
60
            $('.lt-actions li.both a').addClass('active');
61
            $('.lt-actions li.running a').addClass('active');
62
        } else if (off>0) {
63
            $('.lt-actions li.both a').addClass('active');
64
            $('.lt-actions li.off a').addClass('active');
65
        }
66
        if ( all > 1 ) {
67
            $('.lt-actions li.single a').removeClass('active');
68
        }
69
    }
70
}
71

    
72
ui.inactiveActions = function() {
73

    
74
    // Availble actions: connect, reboot, shut, destroy, start
75
    // These actions will be DISABLED
76
    var statesActions ={
77
        'off'      : ['connect', 'reboot', 'shut'],
78
        'error'    : ['connect', 'reboot', 'shut', 'start'],
79
        'building' : ['reboot', 'start'],
80
        'running'  : ['start'],
81
        'rebooting': ['start'],
82
        'starting' : ['start'],
83
        'shutting' : ['connect', 'reboot', 'shut']
84
    } ;
85

    
86
    _.each (statesActions, function(val, key) {
87
        _.each(val, function(value) {
88
            var el = '.' + key + ' .' + value;
89
            $(el).addClass('inactive');
90
        });
91
    })
92
}
93

    
94
ui.detailsCustom = function() {
95
    // position last connected item
96
    var el = $('.connected .item').last();
97
    // -2 is the border width;
98
    var moveY = el.find('.connections >li').last().outerHeight(true) -2;
99
    el.css('top',moveY);
100
    el.css('marginTop', -moveY);
101
}
102

    
103
ui.firewallSetup = function(){
104
    $('.firewall').each(function(){
105
        $(this).removeClass('fully unprotected basic');
106
        $(this).addClass($(this).data('firewall'));
107
        if($(this).hasClass('unprotected')){
108
            $(this).find('p').first().find('em').html('off');
109
        } else {
110
            $(this).find('p').first().find('em').html('on');
111
        }
112
    });
113
    $('.firewall .more span').each(function(e){
114
        var that = this;
115
        if ($(this).hasClass('snf-radio-checked')){
116
            $(that).siblings('em').html('on');
117
        } else {
118
            $(that).siblings('em').html('off');
119
        }
120
    })
121
}
122

    
123

    
124
/*
125
* In order for the editable value functionality to work, the html markup
126
* should be:
127
    <div class="editable">
128
        <span class="input-txt">editable value</span>
129
        <input type="text">
130
        <a href="#" class="edit">edit</a>
131
        <a href="#" class="save">save</a>
132
        <a href="#" class="cancel">cancel</a>
133
    </div>
134
    <a href="#" class="delete">delete</a>
135
*/
136
ui.editable = function(){
137

    
138
/*
139
* resetForm hides save and cancel buttons,
140
* text input and shows input-txt. resetForm does not alter
141
* input-txt content.
142
*/
143

    
144
    function resetForm(e, elem) {
145
        var el = elem.parents('.editable');
146
        el.find('input[type="text"]').hide();
147
        el.find('a.cancel, a.save').hide();
148
        el.find('a.edit, .input-txt').show();
149
        el.find('.delete').show();
150
    }
151

    
152
/* 
153
* showForm hides input-txt, shows save and cancel buttons and
154
* sets input value to input-txt content.
155
*/
156
    function showForm(e,elem) {
157
        e.stopPropagation(); 
158
        e.preventDefault();
159
        var el = elem.parents('.editable'); 
160
        el.find('input[type="text"]').val(el.find('.input-txt').html());
161
        el.find('input[type="text"]').show();
162
        el.find('a.cancel, a.save').show();
163
        elem.hide();
164
        el.find('.input-txt').hide();
165
        if(el.find('.delete').length != 0) {
166
            el.find('.delete').hide();
167
        }
168

    
169
    }
170

    
171
/*
172
setValue sets input-txt value to the input value.
173
Makes sure that the input value is not empty.
174
TODO:
175
Ajax request to submit form
176
*/
177

    
178
    function setValue(elem) {
179
        var el = elem.parents('.editable');
180
        if( el.find('input[type="text"]').val() ) {
181
            el.find('.input-txt').html(el.find('input[type="text"]').val());
182
        }
183
    }
184

    
185

    
186
    $('.editable .edit').click(function(e){
187
        showForm(e, $(this));
188
    })
189

    
190
    $('.editable .cancel').click(function(e){
191
        e.stopPropagation();
192
        e.preventDefault();
193
        resetForm(e, $(this));
194
    })
195

    
196
    $('.editable .save').click(function(e){
197
        e.stopPropagation();
198
        e.preventDefault();
199
        setValue($(this));
200
        resetForm(e, $(this));
201

    
202
    })
203

    
204

    
205
    $('.editable input[type="text"]').click(function(e){
206
        e.stopPropagation();
207
    })
208

    
209
    $('.editable input[type="text"]').keyup(function(e){
210
        if(e.keyCode == 13) { 
211
            setValue($(this));
212
            resetForm(e, $(this));
213
            
214
        }
215
    
216
    })
217

    
218
    $('html').click(function(e) {
219
        resetForm(e, $('.editable a.cancel'));
220
        // not sure if we want to hide the error window after every click in the ui
221
        if($('.communication-error').css('bottom') == '0px') {
222
            $('.communication-error').animate({bottom: "-151px"});
223
        }
224
    });
225

    
226
}
227

    
228
/* TODO: better overlay functionality */
229
ui.overlay = function() {
230
    $('[data-overlay-id]').click(function(e){
231
        e.preventDefault();
232
        var el = $(this);
233
        var id = '#'+el.data('overlay-id');
234

    
235
        $('.overlay-area-custom').fadeIn(100);
236
        $('body').addClass('with-overlay');
237
        $(id).fadeIn('slow');
238
        if (id=='#network-wizard') {
239
            $(id).find('input').first().focus();
240
            return false;
241
        }
242
        $(id).find('a').first().focus();
243
    });
244
}
245

    
246
function goToByScroll(id){
247
      // Remove "link" from the ID
248
    id = id.replace("link", "");
249
      // Scroll
250
    $('html,body').animate({
251
        scrollTop: $("#"+id).offset().top},
252
        'slow');
253
}
254

    
255

    
256
/*
257
* functions concerning checkboxes and radiobuttons links
258
*/
259

    
260
ui.changeCheckboxState =function(checkbox_link) {
261
    $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
262
    ui.entitiesActionsEnabled();
263
}
264

    
265
ui.changeRadiobuttonState = function(radiobtn_link) {
266
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
267
}
268

    
269
ui.checkOneRadioButton = function(radiobtn_link) {
270
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
271
}
272

    
273

    
274
// toggle expand arrrow  and corresponding area
275
// todo: one function for all the areas we reveal
276
ui.expandDownArea = function(arrow_link, area) {
277
    var arrow_link = $(arrow_link);
278
    var area = $(area);
279
            arrow_link.find('span.snf-arrow-up, span.snf-arrow-down').toggleClass('snf-arrow-up snf-arrow-down');
280
            // $('.wizard-content').removeAttr('style');
281
            area.stop().slideToggle(600, function() {
282
                if (area.is(':visible')) {
283
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
284
                } else {
285
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
286
                }
287

    
288
            });
289
}
290

    
291

    
292
/* Tabs functionality
293
* tabsEl is the div/ul with the links to the sections and the sections that
294
* with toggle have class sectionEl.
295
* Markup needed is an href to each a with the id of the relative section.
296
*/
297
ui.tabs = function(tabsEl, sectionEl) {
298
    var tabLink = tabsEl.find('a');
299
    function href(a) {
300
        return a.attr('href');
301
    }
302
    tabLink.click(function(e){
303
        e.preventDefault();
304
        if ( $(this).hasClass('active')){
305
             return false;
306
        } else {
307
            $(this).parents(tabsEl).find('a').removeClass('active');
308
            $(this).addClass('active');
309
            $(sectionEl).hide();
310
            $(href($(this))).stop(true,true).slideDown(500);
311
        }
312

    
313
    })
314
}
315

    
316
$(document).ready(function(){
317

    
318
    if($('.vms.entities').length!=0){
319
        ui.inactiveActions();
320
    };
321
    ui.setElminHeight($('.details'));
322
    $('#hd-search .hd-icon-search').click(function(e){
323
        var that = this;
324
        $(this).parents('.hd-search').toggleClass('hd-open');
325
        if ($(this).parents('.hd-search').hasClass('hd-open')) {
326
            $(that).parents('.hd-search').find('input[type="search"]').focus();
327
        } else {
328
            $(that).parents('.hd-search').find('input[type="search"]').val('');
329
        }
330
    })
331

    
332
    $('.header .login').mouseenter(function(e){
333
        $(this).find('ul').stop(true, true).slideDown(200);
334
    });
335
    $('.header .login').mouseleave(function(e){
336
        $(this).find('ul').stop(true, true).slideUp(200);
337
    });
338

    
339
    $('.entities a').click(function(){
340
        if ($(this).attr('data-reveal-id') && !($(this).hasClass('inactive'))) {
341
            $('.entities li .more').hide();
342
        }
343
    });
344

    
345
    $('.inactive').click(function(e){
346
        e.stopPropagation();
347
        e.preventDefault();
348
     })
349

    
350

    
351
    $('.lt-actions a:not(.active)').click(function(e){
352
        e.preventDefault();
353
    })
354

    
355
    if ($('.entities .items-list >li').length == 1) {
356
        $('.overlay-wrapper').addClass('no-vm');
357
    };
358
    $('.entities li .more').each(function(){
359
        var width = $(this).parent('li').outerWidth()  + 20;
360
        $(this).css('width', width);
361
    });
362

    
363
    $('.items-list li .img-wrap').on("mouseenter", function(e) {
364
        var that = this;
365
        if ($(this).parents('.entities').hasClass('grid-view')) {
366
            if ($(that).parent('.container').siblings('.more').length>0) {
367
                $(that).parent('.container').fadeOut(50,'easeInCirc');
368
                $(that).parent('.container').siblings('.more').fadeIn(150,'easeInCirc');
369
            }
370
        }
371
    });
372
    $('.entities li .more').mouseleave(function(e) {
373
        $(this).fadeOut(50, function() {
374
            $(this).siblings('.container').fadeIn(50);
375
        });
376
    });
377
    $('.grid-view .items-list > li').mouseleave(function(e){
378
        var that = this;
379
        setTimeout(function(){
380
            $(that).find('.more').fadeOut(200, function() {
381
                $(this).siblings('.container').fadeIn('fast');
382
            });
383
        },50)
384
    });
385

    
386
    ui.closeDiv($('.info .close'), '.info');
387
    ui.editable();
388
    ui.overlay();
389
    ui.colorPickerVisible = 0;
390

    
391
    $("a.disabled").each(function() {
392
        $(this).removeAttr('href');
393
    });
394
    
395
    $("a.disabled").click(function(e) {
396
        e.preventDefault();
397
    });
398

    
399
    // checkbox: basic reaction on click (checked, unchecked)
400
    // see wizard
401
    $('.check').click(function(e){
402
        e.preventDefault();
403
        e.stopPropagation();
404
        ui.changeCheckboxState(this);
405
    });
406

    
407

    
408
    $('.with-checkbox').click(function(e){
409
        e.preventDefault();
410
        e.stopPropagation();
411
        var checkbox = self.find('.check');
412
        ui.changeCheckboxState(checkbox);
413
    });
414

    
415
    $('.radio_btn').click(function(e) {
416
        e.preventDefault();
417
         var state = $(this).find('span');
418
         if(state.hasClass('snf-radio-unchecked')) {
419
            ui.checkOneRadioButton(this);
420
            ui.changeRadiobuttonState(this);
421
        }
422
    })
423

    
424
    $('.main-actions li a').click(function(e){
425
        if (!($(this).hasClass('active'))) {
426
            e.preventDefault();
427
        }
428
    })
429
    $('.scroll-pane').jScrollPane();
430

    
431
    // $('.main .items-list .title em').each(function(){
432
    //     $(this).html( ui.trimChars($(this).html(), 20) );
433

    
434
    // })
435

    
436
    $('.main-actions li a').click(function(e){
437
        if (!($(this).hasClass('active'))) {
438
            e.preventDefault();
439
        }
440
    })
441
    $('.overlay-area-custom').children('.close').click(function(e){
442
        e.preventDefault();
443
        e.stopPropagation();
444
        $(this).parents('.overlay-area-custom').hide();
445
        $(this).parents('.overlay-area-custom').find($('.overlay-div')).hide();
446
        $('body').removeClass('with-overlay');
447
    })
448

    
449
    $('.browse-files').click(function(e){
450
        e.preventDefault();
451
    })
452

    
453
    Dropzone.options.filesDropzone = {
454
        dictDefaultMessage:'',
455
        clickable: '.browse-files',
456
        previewsContainer: '.dropzone-files',
457
        createImageThumbnails: false,
458
        dictRemoveFile: "snf-Remove file",
459
    };
460

    
461

    
462
    $('.main .files').magnificPopup({
463
        delegate: 'a.show.image',
464
        type: 'image',
465
        tLoading: 'Loading image #%curr%...',
466
        mainClass: 'mfp-img-mobile',
467
        gallery: {
468
            enabled: true,
469
            navigateByImgClick: true,
470
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
471
        },
472
        image: {
473
            tError: 'The image could not be loaded.',
474
            titleSrc: function(item) {
475
                return item.el.data('title');
476
            }
477
        }
478
    });
479

    
480
    if($('#picker').length>0) {
481
        $('#picker').farbtastic('#color');
482
    };
483
    if($('#sb-search').length>0) {
484
        new UISearch( document.getElementById( 'sb-search' ) );
485
    }
486

    
487

    
488
    /* grid/list view for items-list */
489

    
490
    $('.view-type .list').click(function(e){
491
        e.preventDefault();
492
        $('.view-type .grid span').removeClass('current');
493
        $(this).find('span').addClass('current');
494
        $('.entities').removeClass('grid-view');
495
        $('.entities').addClass('list-view');
496
    });
497

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

    
506
     $('.lt-bar .select').click(function(e){
507
        $(this).find('span').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
508
        $(this).find('em').toggle();
509
        if ( $(this).find('span').hasClass('snf-checkbox-unchecked')){
510
            $('.list-view  li .check span').removeClass('snf-checkbox-checked');
511
            $('.list-view  li .check span').addClass('snf-checkbox-unchecked');
512
        } else {
513
            $('.list-view  li .check span').addClass('snf-checkbox-checked');
514
            $('.list-view  li .check span').removeClass('snf-checkbox-unchecked');
515
        }
516
        ui.entitiesActionsEnabled();
517
     });
518

    
519

    
520
    // set filter visible
521
    $('.filter-menu .filter').click(function(e) {
522
        e.preventDefault();
523
        $(this).parents('.filter-menu').toggleClass('current');
524
    });
525

    
526
    // temp function used to demonstrate the visual effect of the building state of vm
527
    $('[data-status="building"] .btn5.temp').click(function(e) {
528
        e.preventDefault();
529
        $(this).siblings('.container').find('.complete').toggleClass('build-progress');
530
    });
531

    
532
    $('[data-status="rebooting"] .btn5.temp').click(function(e) {
533
        e.preventDefault();
534
        $(this).siblings('.container').find('.snf-PC_fill').toggleClass('reboot-progress');
535
    })
536

    
537
    // //temp function to preventDefault of links in modal
538
    // $('.reveal-modal a:not(".close-reveal-modal, .generate-key-btn, .import-key-btn")').click(function(e){
539
    //     e.preventDefault();
540
    //     $('a.close-reveal-modal').trigger('click');
541
    // });
542

    
543
     // temp btn to show communication error message
544
    $('.temp-for-btns .communication-error-btn').click(function(e) {
545
         e.preventDefault();
546
         e.stopPropagation();
547
         $('.communication-error').animate({bottom: "0px"});
548
     });
549

    
550
    $('.communication-error a').click(function(e) {
551
        e.preventDefault();
552
        e.stopPropagation();
553
        $('.communication-error').animate({bottom: "-151px"});
554

    
555
    });
556
    $('.communication-error').click(function(e) {
557
        e.stopPropagation();
558
    });
559

    
560
    $('.show-add-tag').click(function(e) {
561
    e.preventDefault();
562
    $(this).parents('.tags-area, .tags').find('.snf-color-picker').slideDown('slow', function() {
563
        $('#hide-add-tag-dummy').scrollintoview({
564
            'duration': 'slow'
565
        });
566
    });
567
    ui.colorPickerVisible = 1;
568
    });
569

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

    
580
    // connected details js
581
    ui.detailsCustom();
582
    ui.firewallSetup();
583
    $('.firewall .more  a').click(function(e){
584
        e.preventDefault();
585
        if ($(this).find('span').hasClass('snf-radio-checked')) {
586
            return false;
587
        }
588
        $(this).parents('.firewall').removeAttr('data-firewall');
589
        $(this).parents('.firewall').data('firewall', $(this).parent().attr('class'));
590
        $(this).find('span').toggleClass('snf-radio-unchecked snf-radio-checked');
591
        $(this).parent('p').siblings('p').find('span').removeClass('snf-radio-checked');
592
        $(this).parent('p').siblings('p').find('span').addClass('snf-radio-unchecked');
593
         ui.firewallSetup();
594
    });
595
    $('.firewall').mouseenter(function(e){
596
        $(this).find('.more').stop(true, true).slideDown(200);
597
    });
598
    $('.firewall').mouseleave(function(e){
599
        $(this).find('.more').stop(true, true).slideUp(200);
600
    });
601
    ui.tabs($('.tabs'), $('.content'));
602
    // end of connected details js
603
})
604

    
605

    
606
$(window).resize(function(e){
607
    ui.setElminHeight($('.details'));
608
    $('.scroll-pane').jScrollPane();
609
})