Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (14.2 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 lt-sidebar height. Useful for jscrollpane scrollbar */
31
ui.setSidebarHeight = function(){
32
    var WindowHeight = $(window).height();
33
    var h1= WindowHeight - $('.header').outerHeight();
34
    var h2= $('.main').outerHeight();
35
    $('.lt-sidebar').height((h2>h1) ? h2 : h1);
36
    $('.lt-bar').height((h2>h1) ? h2 : h1);
37
}
38

    
39

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

    
70
ui.inactiveActions = function() {
71

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

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

    
92

    
93
/*
94
* In order for the editable value functionality to work, the html markup
95
* should be:
96
    <div class="editable">
97
        <span class="input-txt">editable value</span>
98
        <input type="text">
99
        <a href="#" class="edit">edit</a>
100
        <a href="#" class="save">save</a>
101
        <a href="#" class="cancel">cancel</a>
102
    </div>
103
*/
104
ui.editable = function(){
105

    
106
/*
107
* resetForm hides save and cancel buttons,
108
* text input and shows input-txt. resetForm does not alter
109
* input-txt content.
110
*/
111

    
112
    function resetForm(e, elem) {
113
        var el = elem.parents('.editable');
114
        el.find('input[type="text"]').hide();
115
        el.find('a.cancel, a.save').hide();
116
        el.find('a.edit, .input-txt').show();
117
    }
118

    
119
/* 
120
* showForm hides input-txt, shows save and cancel buttons and
121
* sets input value to input-txt content.
122
*/
123
    function showForm(e,elem) {
124
        e.stopPropagation(); 
125
        e.preventDefault();
126
        var el = elem.parents('.editable'); 
127
        el.find('input[type="text"]').val(el.find('.input-txt').html());
128
        el.find('input[type="text"]').show();
129
        el.find('a.cancel, a.save').show();
130
        elem.hide();
131
        el.find('.input-txt').hide();
132

    
133
    }
134

    
135
/*
136
setValue sets input-txt value to the input value.
137
Makes sure that the input value is not empty.
138
TODO:
139
Ajax request to submit form
140
*/
141

    
142
    function setValue(elem) {
143
        var el = elem.parents('.editable');
144
        if( el.find('input[type="text"]').val() ) {
145
            el.find('.input-txt').html(el.find('input[type="text"]').val());
146
        }
147
    }
148

    
149

    
150
    $('.editable .edit').click(function(e){
151
        showForm(e, $(this));
152
    })
153

    
154
    $('.editable .cancel').click(function(e){
155
        e.stopPropagation();
156
        e.preventDefault();
157
        resetForm(e, $(this));
158
    })
159

    
160
    $('.editable .save').click(function(e){
161
        e.stopPropagation();
162
        e.preventDefault();
163
        setValue($(this));
164
        resetForm(e, $(this));
165

    
166
    })
167

    
168

    
169
    $('.editable input[type="text"]').click(function(e){
170
        e.stopPropagation();
171
    })
172

    
173
    $('.editable input[type="text"]').keyup(function(e){
174
        if(e.keyCode == 13) { 
175
            setValue($(this));
176
            resetForm(e, $(this));
177
            
178
        }
179
    
180
    })
181

    
182
    $('html').click(function(e) {
183
        resetForm(e, $('.editable a.cancel'));
184
    });
185

    
186
}
187

    
188
/* TODO: better overlay functionality */
189
ui.overlay = function() {
190
    $('[data-overlay-id]').click(function(e){
191
        e.preventDefault();
192
        var el = $(this);
193
        var id = '#'+el.data('overlay-id');
194

    
195
        $('.overlay-area-custom').fadeIn(100);
196
        $('body').addClass('with-overlay');
197
        $(id).fadeIn('slow');
198
        if (id=='#network-wizard') {
199
            $(id).find('input').first().focus();
200
            return false;
201
        }
202
        $(id).find('a').first().focus();
203
    });
204
}
205

    
206
function goToByScroll(id){
207
      // Remove "link" from the ID
208
    id = id.replace("link", "");
209
      // Scroll
210
    $('html,body').animate({
211
        scrollTop: $("#"+id).offset().top},
212
        'slow');
213
}
214

    
215

    
216
/*
217
* functions concerning checkboxes and radiobuttons links
218
*/
219

    
220
ui.changeCheckboxState =function(checkbox_link) {
221
    $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
222
    ui.entitiesActionsEnabled();
223
}
224

    
225
ui.changeRadiobuttonState = function(radiobtn_link) {
226
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
227
}
228

    
229
ui.checkOneRadioButton = function(radiobtn_link) {
230
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
231
}
232

    
233

    
234
// toggle expand arrrow  and corresponding area
235
// todo: one function for all the areas we reveal
236
ui.expandDownArea = function(arrow_link, area) {
237
    var arrow_link = $(arrow_link);
238
    var area = $(area);
239
            arrow_link.find('span.snf-arrow-up, span.snf-arrow-down').toggleClass('snf-arrow-up snf-arrow-down');
240
            // $('.wizard-content').removeAttr('style');
241
            area.stop().slideToggle(600, function() {
242
                if (area.is(':visible')) {
243
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
244
                } else {
245
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
246
                }
247

    
248
            });
249
}
250

    
251
$(document).ready(function(){
252

    
253
    if($('.vms.entities').length!=0){
254
        ui.inactiveActions();
255
    };
256
    ui.setSidebarHeight();
257
    $('#hd-search .hd-icon-search').click(function(e){
258
        var that = this;
259
        $(this).parents('.hd-search').toggleClass('hd-open');
260
        if ($(this).parents('.hd-search').hasClass('hd-open')) {
261
            $(that).parents('.hd-search').find('input[type="search"]').focus();
262
        } else {
263
            $(that).parents('.hd-search').find('input[type="search"]').val('');
264
        }
265
    })
266

    
267
    $('.header .login').mouseenter(function(e){
268
        $(this).find('ul').stop(true, true).slideDown(200);
269
    });
270
    $('.header .login').mouseleave(function(e){
271
        $(this).find('ul').stop(true, true).slideUp(200);
272
    });
273

    
274
    $('.entities a').click(function(){
275
        if ($(this).attr('data-reveal-id') && !($(this).hasClass('inactive'))) {
276
            $('.entities li .more').hide();
277
        }
278
    });
279

    
280
    $('.inactive').click(function(e){
281
        e.stopPropagation();
282
        e.preventDefault();
283
     })
284

    
285

    
286
    $('.lt-actions a:not(.active)').click(function(e){
287
        e.preventDefault();
288
    })
289

    
290
    if ($('.entities .items-list >li').length == 1) {
291
        $('.overlay-wrapper').addClass('no-vm');
292
    };
293
    $('.entities li .more').each(function(){
294
        var width = $(this).parent('li').outerWidth()  + 20;
295
        $(this).css('width', width);
296
    });
297

    
298
    $('.items-list li .img-wrap').on("mouseenter", function(e) {
299
        var that = this;
300
        if ($(this).parents('.entities').hasClass('grid-view')) {
301
            if ($(that).parent('.container').siblings('.more').length>0) {
302
                $(that).parent('.container').fadeOut(50,'easeInCirc');
303
                $(that).parent('.container').siblings('.more').fadeIn(150,'easeInCirc');
304
            }
305
        }
306
    });
307
    $('.entities li .more').mouseleave(function(e) {
308
        $(this).fadeOut(50, function() {
309
            $(this).siblings('.container').fadeIn(50);
310
        });
311
    });
312
    $('.grid-view .items-list > li').mouseleave(function(e){
313
        var that = this;
314
        setTimeout(function(){
315
            $(that).find('.more').fadeOut(200, function() {
316
                $(this).siblings('.container').fadeIn('fast');
317
            });
318
        },50)
319
    });
320

    
321
    ui.closeDiv($('.info .close'), '.info');
322
    ui.editable();
323
    ui.overlay();
324
    ui.colorPickerVisible = 0;
325

    
326
    $("a.disabled").each(function() {
327
        $(this).removeAttr('href');
328
    });
329
    
330
    $("a.disabled").click(function(e) {
331
        e.preventDefault();
332
    });
333

    
334
    // checkbox: basic reaction on click (checked, unchecked)
335
    // see wizard
336
    $('.check').click(function(e){
337
        e.preventDefault();
338
        e.stopPropagation();
339
        ui.changeCheckboxState(this);
340
    });
341

    
342

    
343
    $('.with-checkbox').click(function(e){
344
        e.preventDefault();
345
        e.stopPropagation();
346
        var checkbox = self.find('.check');
347
        ui.changeCheckboxState(checkbox);
348
    });
349

    
350
    $('.radio_btn').click(function(e) {
351
        e.preventDefault();
352
         var state = $(this).find('span');
353
         if(state.hasClass('snf-radio-unchecked')) {
354
            ui.checkOneRadioButton(this);
355
            ui.changeRadiobuttonState(this);
356
        }
357
    })
358

    
359
    $('.main-actions li a').click(function(e){
360
        if (!($(this).hasClass('active'))) {
361
            e.preventDefault();
362
        }
363
    })
364
    $('.scroll-pane').jScrollPane();
365

    
366
    // $('.main .items-list .title em').each(function(){
367
    //     $(this).html( ui.trimChars($(this).html(), 20) );
368

    
369
    // })
370

    
371
    $('.main-actions li a').click(function(e){
372
        if (!($(this).hasClass('active'))) {
373
            e.preventDefault();
374
        }
375
    })
376
    $('.overlay-area-custom').children('.close').click(function(e){
377
        e.preventDefault();
378
        e.stopPropagation();
379
        console.log('blah')
380
        $(this).parents('.overlay-area-custom').hide();
381
        $(this).parents('.overlay-area-custom').find($('.overlay-div')).hide();
382
        $('body').removeClass('with-overlay');
383
    })
384

    
385
    $('.browse-files').click(function(e){
386
        e.preventDefault();
387
    })
388

    
389
    Dropzone.options.filesDropzone = {
390
        dictDefaultMessage:'',
391
        clickable: '.browse-files',
392
        previewsContainer: '.dropzone-files',
393
        createImageThumbnails: false,
394
        dictRemoveFile: "snf-Remove file",
395
    };
396

    
397

    
398
    $('.main .files').magnificPopup({
399
        delegate: 'a.show.image',
400
        type: 'image',
401
        tLoading: 'Loading image #%curr%...',
402
        mainClass: 'mfp-img-mobile',
403
        gallery: {
404
            enabled: true,
405
            navigateByImgClick: true,
406
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
407
        },
408
        image: {
409
            tError: 'The image could not be loaded.',
410
            titleSrc: function(item) {
411
                return item.el.data('title');
412
            }
413
        }
414
    });
415

    
416
    if($('#picker').length>0) {
417
        $('#picker').farbtastic('#color');
418
    };
419
    if($('#sb-search').length>0) {
420
        new UISearch( document.getElementById( 'sb-search' ) );
421
    }
422

    
423

    
424
    /* grid/list view for items-list */
425

    
426
    $('.view-type .list').click(function(e){
427
        e.preventDefault();
428
        $('.view-type .grid span').removeClass('current');
429
        $(this).find('span').addClass('current');
430
        $('.entities').removeClass('grid-view');
431
        $('.entities').addClass('list-view');
432
    });
433

    
434
     $('.view-type .grid').click(function(e){
435
        e.preventDefault();
436
        $('.view-type .list span').removeClass('current');
437
        $(this).find('span').addClass('current');
438
        $('.entities').addClass('grid-view');
439
        $('.entities').removeClass('list-view');
440
    });
441

    
442
     $('.lt-bar .select').click(function(e){
443
        $(this).find('span').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
444
        $(this).find('em').toggle();
445
        if ( $(this).find('span').hasClass('snf-checkbox-unchecked')){
446
            $('.list-view  li .check span').removeClass('snf-checkbox-checked');
447
            $('.list-view  li .check span').addClass('snf-checkbox-unchecked');
448
        } else {
449
            $('.list-view  li .check span').addClass('snf-checkbox-checked');
450
            $('.list-view  li .check span').removeClass('snf-checkbox-unchecked');
451
        }
452
        ui.entitiesActionsEnabled();
453
     });
454

    
455

    
456
    // set filter visible
457
    $('.filter-menu .filter').click(function(e) {
458
        e.preventDefault();
459
        $(this).parents('.filter-menu').toggleClass('current');
460
    });
461

    
462
    // temp function used to demonstrate the visual effect of the building state of vm
463
    $('[data-status="building"] .btn5.temp').click(function(e) {
464
        e.preventDefault();
465
        $(this).siblings('.container').find('.complete').toggleClass('build-progress');
466
    });
467

    
468
    $('[data-status="rebooting"] .btn5.temp').click(function(e) {
469
        e.preventDefault();
470
        $(this).siblings('.container').find('.snf-PC_fill').toggleClass('reboot-progress');
471
    })
472

    
473
    //temp function to preventDefault of links in modal
474
     $('.reveal-modal a:not(".close-reveal-modal, .generate-key-btn, .import-key-btn")').click(function(e){
475
        e.preventDefault();
476
        $('a.close-reveal-modal').trigger('click');
477
    });
478
})
479

    
480

    
481
$(window).resize(function(e){
482
    ui.setSidebarHeight();
483
    $('.scroll-pane').jScrollPane();
484
})