Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (13.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 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
/*
71
* In order for the editable value functionality to work, the html markup
72
* should be:
73
    <div class="editable">
74
        <span class="input-txt">editable value</span>
75
        <input type="text">
76
        <a href="#" class="edit">edit</a>
77
        <a href="#" class="save">save</a>
78
        <a href="#" class="cancel">cancel</a>
79
    </div>
80
*/
81
ui.editable = function(){
82

    
83
/*
84
* resetForm hides save and cancel buttons,
85
* text input and shows input-txt. resetForm does not alter
86
* input-txt content.
87
*/
88

    
89
    function resetForm(e, elem) {
90
        var el = elem.parents('.editable');
91
        el.find('input[type="text"]').hide();
92
        el.find('a.cancel, a.save').hide();
93
        el.find('a.edit, .input-txt').show();
94
    }
95

    
96
/* 
97
* showForm hides input-txt, shows save and cancel buttons and
98
* sets input value to input-txt content.
99
*/
100
    function showForm(e,elem) {
101
        e.stopPropagation(); 
102
        e.preventDefault();
103
        var el = elem.parents('.editable'); 
104
        el.find('input[type="text"]').val(el.find('.input-txt').html());
105
        el.find('input[type="text"]').show();
106
        el.find('a.cancel, a.save').show();
107
        elem.hide();
108
        el.find('.input-txt').hide();
109

    
110
    }
111

    
112
/*
113
setValue sets input-txt value to the input value.
114
Makes sure that the input value is not empty.
115
TODO:
116
Ajax request to submit form
117
*/
118

    
119
    function setValue(elem) {
120
        var el = elem.parents('.editable');
121
        if( el.find('input[type="text"]').val() ) {
122
            el.find('.input-txt').html(el.find('input[type="text"]').val());
123
        }
124
    }
125

    
126

    
127
    $('.editable .edit').click(function(e){
128
        showForm(e, $(this));
129
    })
130

    
131
    $('.editable .cancel').click(function(e){
132
        e.stopPropagation();
133
        e.preventDefault();
134
        resetForm(e, $(this));
135
    })
136

    
137
    $('.editable .save').click(function(e){
138
        e.stopPropagation();
139
        e.preventDefault();
140
        setValue($(this));
141
        resetForm(e, $(this));
142

    
143
    })
144

    
145

    
146
    $('.editable input[type="text"]').click(function(e){
147
        e.stopPropagation();
148
    })
149

    
150
    $('.editable input[type="text"]').keyup(function(e){
151
        if(e.keyCode == 13) { 
152
            setValue($(this));
153
            resetForm(e, $(this));
154
            
155
        }
156
    
157
    })
158

    
159
    $('html').click(function(e) {
160
        resetForm(e, $('.editable a.cancel'));
161
    });
162

    
163
}
164

    
165
/* TODO: better overlay functionality */
166
ui.overlay = function() {
167
    $('[data-overlay-id]').click(function(e){
168
        e.preventDefault();
169
        var el = $(this);
170
        var id = '#'+el.data('overlay-id');
171

    
172
        $('.overlay-area-custom').fadeIn(100);
173
        $('body').addClass('with-overlay');
174
        $(id).fadeIn('slow');
175
        if (id=='#network-wizard') {
176
            $(id).find('input').first().focus();
177
            return false;
178
        }
179
        $(id).find('a').first().focus();
180
    });
181
}
182

    
183
function goToByScroll(id){
184
      // Remove "link" from the ID
185
    id = id.replace("link", "");
186
      // Scroll
187
    $('html,body').animate({
188
        scrollTop: $("#"+id).offset().top},
189
        'slow');
190
}
191

    
192

    
193
/*
194
* functions concerning checkboxes and radiobuttons links
195
*/
196

    
197
ui.changeCheckboxState =function(checkbox_link) {
198
    $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
199
    ui.entitiesActionsEnabled();
200
}
201

    
202
ui.changeRadiobuttonState = function(radiobtn_link) {
203
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
204
}
205

    
206
ui.checkOneRadioButton = function(radiobtn_link) {
207
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
208
}
209

    
210

    
211
// toggle expand arrrow  and corresponding area
212
// todo: one function for all the areas we reveal
213
ui.expandDownArea = function(arrow_link, area) {
214
    var arrow_link = $(arrow_link);
215
    var area = $(area);
216
            arrow_link.find('span.snf-arrow-up, span.snf-arrow-down').toggleClass('snf-arrow-up snf-arrow-down');
217
            // $('.wizard-content').removeAttr('style');
218
            area.stop().slideToggle(600, function() {
219
                if (area.is(':visible')) {
220
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
221
                } else {
222
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
223
                }
224

    
225
            });
226
}
227

    
228
$(document).ready(function(){
229

    
230
    ui.setSidebarHeight();
231
    $('#hd-search .hd-icon-search').click(function(e){
232
        var that = this;
233
        $(this).parents('.hd-search').toggleClass('hd-open');
234
        if ($(this).parents('.hd-search').hasClass('hd-open')) {
235
            $(that).parents('.hd-search').find('input[type="search"]').focus();
236
        } else {
237
            $(that).parents('.hd-search').find('input[type="search"]').val('');
238
        }
239
    })
240

    
241
    $('.header .login').mouseenter(function(e){
242
        $(this).find('ul').stop(true, true).slideDown(200);
243
    });
244
    $('.header .login').mouseleave(function(e){
245
        $(this).find('ul').stop(true, true).slideUp(200);
246
    });
247

    
248
    $('.entities a').click(function(){
249
        if ($(this).attr('data-reveal-id')) {
250
            $('.entities li .more').hide();
251
        }
252
    });
253

    
254
    $('.lt-actions a:not(.active)').click(function(e){
255
        e.preventDefault();
256
    })
257

    
258
    if ($('.entities .items-list >li').length == 1) {
259
        $('.overlay-wrapper').addClass('no-vm');
260
    };
261
    $('.entities li .more').each(function(){
262
        var width = $(this).parent('li').outerWidth()  + 20;
263
        $(this).css('width', width);
264
    });
265

    
266
    $('.items-list li .img-wrap').on("mouseenter", function(e) {
267
        var that = this;
268
        if ($(this).parents('.entities').hasClass('grid-view')) {
269
            if ($(that).parent('.container').siblings('.more').length>0) {
270
                $(that).parent('.container').fadeOut(50,'easeInCirc');
271
                $(that).parent('.container').siblings('.more').fadeIn(150,'easeInCirc');
272
            }
273
        }
274
    });
275
    $('.entities li .more').mouseleave(function(e) {
276
        $(this).fadeOut(50, function() {
277
            $(this).siblings('.container').fadeIn(50);
278
        });
279
    });
280
    $('.grid-view .items-list > li').mouseleave(function(e){
281
        var that = this;
282
        setTimeout(function(){
283
            $(that).find('.more').fadeOut(200, function() {
284
                $(this).siblings('.container').fadeIn('fast');
285
            });
286
        },50)
287
    });
288

    
289
    ui.closeDiv($('.info .close'), '.info');
290
    ui.editable();
291
    ui.overlay();
292
    ui.colorPickerVisible = 0;
293

    
294
    $("a.disabled").each(function() {
295
        $(this).removeAttr('href');
296
    });
297
    
298
    $("a.disabled").click(function(e) {
299
        e.preventDefault();
300
    });
301

    
302
    // checkbox: basic reaction on click (checked, unchecked)
303
    // see wizard
304
    $('.check').click(function(e){
305
        e.preventDefault();
306
        e.stopPropagation();
307
        ui.changeCheckboxState(this);
308
    });
309

    
310

    
311
    $('.with-checkbox').click(function(e){
312
        e.preventDefault();
313
        e.stopPropagation();
314
        var checkbox = self.find('.check');
315
        ui.changeCheckboxState(checkbox);
316
    });
317

    
318
    $('.radio_btn').click(function(e) {
319
        e.preventDefault();
320
         var state = $(this).find('span');
321
         if(state.hasClass('snf-radio-unchecked')) {
322
            ui.checkOneRadioButton(this);
323
            ui.changeRadiobuttonState(this);
324
        }
325
    })
326

    
327
    $('.main-actions li a').click(function(e){
328
        if (!($(this).hasClass('active'))) {
329
            e.preventDefault();
330
        }
331
    })
332
    $('.scroll-pane').jScrollPane();
333

    
334
    // $('.main .items-list .title em').each(function(){
335
    //     $(this).html( ui.trimChars($(this).html(), 20) );
336

    
337
    // })
338

    
339
    $('.main-actions li a').click(function(e){
340
        if (!($(this).hasClass('active'))) {
341
            e.preventDefault();
342
        }
343
    })
344
    $('.overlay-area-custom').children('.close').click(function(e){
345
        e.preventDefault();
346
        e.stopPropagation();
347
        console.log('blah')
348
        $(this).parents('.overlay-area-custom').hide();
349
        $(this).parents('.overlay-area-custom').find($('.overlay-div')).hide();
350
        $('body').removeClass('with-overlay');
351
    })
352

    
353
    $('.browse-files').click(function(e){
354
        e.preventDefault();
355
    })
356

    
357
    Dropzone.options.filesDropzone = {
358
        dictDefaultMessage:'',
359
        clickable: '.browse-files',
360
        previewsContainer: '.dropzone-files',
361
        createImageThumbnails: false,
362
        dictRemoveFile: "snf-Remove file",
363
    };
364

    
365

    
366
    $('.main .files').magnificPopup({
367
        delegate: 'a.show.image',
368
        type: 'image',
369
        tLoading: 'Loading image #%curr%...',
370
        mainClass: 'mfp-img-mobile',
371
        gallery: {
372
            enabled: true,
373
            navigateByImgClick: true,
374
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
375
        },
376
        image: {
377
            tError: 'The image could not be loaded.',
378
            titleSrc: function(item) {
379
                return item.el.data('title');
380
            }
381
        }
382
    });
383

    
384
    if($('#picker').length>0) {
385
        $('#picker').farbtastic('#color');
386
    };
387
    if($('#sb-search').length>0) {
388
        new UISearch( document.getElementById( 'sb-search' ) );
389
    }
390

    
391

    
392
    /* grid/list view for items-list */
393

    
394
    $('.view-type .list').click(function(e){
395
        e.preventDefault();
396
        $('.view-type .grid span').removeClass('current');
397
        $(this).find('span').addClass('current');
398
        $('.entities').removeClass('grid-view');
399
        $('.entities').addClass('list-view');
400
    });
401

    
402
     $('.view-type .grid').click(function(e){
403
        e.preventDefault();
404
        $('.view-type .list span').removeClass('current');
405
        $(this).find('span').addClass('current');
406
        $('.entities').addClass('grid-view');
407
        $('.entities').removeClass('list-view');
408
    });
409

    
410
     $('.lt-bar .select').click(function(e){
411
        $(this).find('span').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
412
        $(this).find('em').toggle();
413
        if ( $(this).find('span').hasClass('snf-checkbox-unchecked')){
414
            $('.list-view  li .check span').removeClass('snf-checkbox-checked');
415
            $('.list-view  li .check span').addClass('snf-checkbox-unchecked');
416
        } else {
417
            $('.list-view  li .check span').addClass('snf-checkbox-checked');
418
            $('.list-view  li .check span').removeClass('snf-checkbox-unchecked');
419
        }
420
        ui.entitiesActionsEnabled();
421
     });
422

    
423
    // set filter visible
424
    $('.filter-menu .filter').click(function(e) {
425
        e.preventDefault();
426
        $(this).parents('.filter-menu').toggleClass('current');
427
    });
428

    
429
    // temp function used to demonstrate the visual effect of the building state of vm
430
    $('[data-status="building"] .btn5.temp').click(function(e) {
431
        e.preventDefault();
432
        $(this).siblings('.container').find('.complete').toggleClass('build-progress');
433
    });
434

    
435
    $('[data-status="rebooting"] .btn5.temp').click(function(e) {
436
        e.preventDefault();
437
        $(this).siblings('.container').find('.snf-PC_fill').toggleClass('reboot-progress');
438
    })
439

    
440
    //temp function to preventDefault of links in modal
441
    $('.reveal-modal a:not(".close-reveal-modal")').click(function(e){
442
        e.preventDefault();
443
        $('a.close-reveal-modal').trigger('click');
444
    });
445
})
446

    
447

    
448
$(window).resize(function(e){
449
    ui.setSidebarHeight();
450
    $('.scroll-pane').jScrollPane();
451
})