Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.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
/* 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/stopped ( for running/stopped 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('.container').find('.running').length;
50
    var stopped = $('.snf-checkbox-checked').parents('.container').find('.stopped').length;
51
    $('.header .main-actions li:not(.permanent) a').removeClass('active');  
52
    if ( (running*stopped) > 0 ){          
53
         $('.main-actions li.both a').addClass('active');
54
         $('.main-actions li.single a').removeClass('active');
55
    } else {
56
        if (running > 0) {
57
            $('.main-actions li.both a').addClass('active');
58
            $('.main-actions li.running a').addClass('active');
59
        } else if (stopped>0) {
60
            $('.main-actions li.both a').addClass('active');
61
            $('.main-actions li.stopped a').addClass('active');
62
        }
63
        if ( all > 1 ) {
64
            $('.main-actions li.single a').removeClass('active');
65
        }
66
    }
67
}
68

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

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

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

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

    
109
    }
110

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

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

    
125

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

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

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

    
142
    })
143

    
144

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

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

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

    
162
}
163

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

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

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

    
191

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

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

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

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

    
208

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

    
223
            });
224
}
225

    
226
$(document).ready(function(){
227

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

    
239
    $('.entities a').click(function(){
240
        if ($(this).attr('data-reveal-id')) {
241
            $('.entities li .more').hide();
242
        }
243
    });
244

    
245
    if ($('.entities .items-list >li').length == 1) {
246
        $('.overlay-wrapper').addClass('no-vm');
247
    };
248
    $('.entities li .more').each(function(){
249
        var width = $(this).parent('li').outerWidth()  + 20;
250
        $(this).css('width', width);
251
    });
252

    
253
    $('.items-list li .img-wrap').on("mouseenter", function(e) {
254
        var that = this;
255
        if ($(this).parents('.entities').hasClass('grid-view')) {
256
            if ($(that).parent('.container').siblings('.more').length>0) {
257
                $(that).parent('.container').fadeOut(50,'easeInCirc');
258
                $(that).parent('.container').siblings('.more').fadeIn(150,'easeInCirc');
259
            }
260
        }
261
    });
262
    $('.entities li .more').mouseleave(function(e) {
263
        $(this).fadeOut(50, function() {
264
            $(this).siblings('.container').fadeIn(50);
265
        });
266
    });
267
    $('.grid-view .items-list > li').mouseleave(function(e){
268
        var that = this;
269
        setTimeout(function(){
270
            $(that).find('.more').fadeOut(200, function() {
271
                $(this).siblings('.container').fadeIn('fast');
272
            });
273
        },50)
274
    });
275

    
276
    ui.closeDiv($('.info .close'), '.info');
277
    ui.editable();
278
    ui.overlay();
279
    ui.colorPickerVisible = 0;
280

    
281
    $("a.disabled").each(function() {
282
        $(this).removeAttr('href');
283
    });
284
    
285
    $("a.disabled").click(function(e) {
286
        e.preventDefault();
287
    });
288

    
289
    // checkbox: basic reaction on click (checked, unchecked)
290
    // see wizard
291
    $('.check').click(function(e){
292
        e.preventDefault();
293
        e.stopPropagation();
294
        ui.changeCheckboxState(this);
295
    });
296

    
297

    
298
    $('.with-checkbox').click(function(e){
299
        e.preventDefault();
300
        e.stopPropagation();
301
        var checkbox = self.find('.check');
302
        ui.changeCheckboxState(checkbox);
303
    });
304

    
305
    $('.radio_btn').click(function(e) {
306
        e.preventDefault();
307
         var state = $(this).find('span');
308
         if(state.hasClass('snf-radio-unchecked')) {
309
            ui.checkOneRadioButton(this);
310
            ui.changeRadiobuttonState(this);
311
        }
312
    })
313

    
314
    $('.new-btn a.current').click(function(e){
315
        e.preventDefault();
316
    })
317

    
318
    $('.main-actions li a').click(function(e){
319
        if (!($(this).hasClass('active'))) {
320
            e.preventDefault();
321
        }
322
    })
323
    $('.scroll-pane').jScrollPane();
324

    
325
    // $('.main .items-list .title em').each(function(){
326
    //     $(this).html( ui.trimChars($(this).html(), 20) );
327

    
328
    // })
329

    
330
    $('.main-actions li a').click(function(e){
331
        if (!($(this).hasClass('active'))) {
332
            e.preventDefault();
333
        }
334
    })
335
    $('.overlay-area').children('.close').click(function(e){
336
        e.preventDefault();
337
        e.stopPropagation();
338
        $(this).parents('.overlay-area').hide();
339
        $(this).parents('.overlay-area').find($('.overlay-div')).hide();
340
        $('body').removeClass('with-overlay');
341
    })
342

    
343
    $('.browse-files').click(function(e){
344
        e.preventDefault();
345
    })
346

    
347
    Dropzone.options.filesDropzone = {
348
        dictDefaultMessage:'',
349
        clickable: '.browse-files',
350
        previewsContainer: '.dropzone-files',
351
        createImageThumbnails: false,
352
        dictRemoveFile: "snf-Remove file",
353
    };
354

    
355

    
356
    $('.main .files').magnificPopup({
357
        delegate: 'a.show.image',
358
        type: 'image',
359
        tLoading: 'Loading image #%curr%...',
360
        mainClass: 'mfp-img-mobile',
361
        gallery: {
362
            enabled: true,
363
            navigateByImgClick: true,
364
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
365
        },
366
        image: {
367
            tError: 'The image could not be loaded.',
368
            titleSrc: function(item) {
369
                return item.el.data('title');
370
            }
371
        }
372
    });
373

    
374
    if($('#picker').length>0) {
375
        $('#picker').farbtastic('#color');
376
    };
377
    if($('#sb-search').length>0) {
378
        new UISearch( document.getElementById( 'sb-search' ) );
379
    }
380

    
381

    
382
    /* grid/list view for items-list */
383

    
384
    $('.view-type .list').click(function(e){
385
        e.preventDefault();
386
        $('.view-type .grid span').removeClass('current');
387
        $(this).find('span').addClass('current');
388
        $('.entities').removeClass('grid-view');
389
        $('.entities').addClass('list-view');
390
    });
391

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

    
400
     $('.lt-bar .select').click(function(e){
401
        $(this).find('span').toggleClass('snf-checkbox-checked snf-checkbox-unchecked');
402
        $(this).find('em').toggle();
403
        if ( $(this).find('span').hasClass('snf-checkbox-unchecked')){
404
            $('.list-view  li .check span').removeClass('snf-checkbox-checked');
405
            $('.list-view  li .check span').addClass('snf-checkbox-unchecked');
406
        } else {
407
            $('.list-view  li .check span').addClass('snf-checkbox-checked');
408
            $('.list-view  li .check span').removeClass('snf-checkbox-unchecked');
409
        }
410
     });
411

    
412
    // set filter visible
413
    $('.filter-menu .filter').click(function(e) {
414
        e.preventDefault();
415
        $(this).parents('.filter-menu').toggleClass('current');
416
    });
417

    
418
    // temp function used to demonstrate the visual effect of the building state of vm
419
    $('.btn5.temp').click(function(e) {
420
        e.preventDefault();
421
        $(this).siblings('.container').find('.complete').toggleClass('build-progress');
422
    })
423
})
424

    
425

    
426
$(window).resize(function(e){
427
    ui.setSidebarHeight();
428
    $('.scroll-pane').jScrollPane();
429
})