Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.8 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
}
37

    
38

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

    
68
ui.entitiesActionsInit = function(){
69

    
70
    // TODO: refactor this
71
    $('.entities a').click(function(){
72
        if ($(this).attr('data-reveal-id')) {
73
            $('.entities li .more').hide();
74
        }
75
    })
76

    
77
    if ($('.entities .items-list >li').length == 1) {
78
        $('.overlay-wrapper').addClass('no-vm');
79
    }
80
    $('.entities li .more').each(function(){
81
        var width = $(this).parent('li').outerWidth()  + 20;
82
        $(this).css('width', width);
83
    })
84
    $('.entities li .img').mouseenter(function(e) {
85
        if ($(this).closest('.container').siblings('.more').length>0) {
86
            $(this).closest('.container').stop(true, true).hide();
87
            $(this).closest('.container').siblings('.more').stop(true, true).fadeIn(600);//stop().slideToggle(600);
88
        }
89
    });
90
    $('.entities li .more').mouseleave(function(e) {
91
        var self = this;
92
        $(this).stop(true, true).fadeOut(200, function() {
93
            $(this).siblings('.container').stop(true,true).fadeIn('fast');
94
        });
95
    });
96
}
97

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

    
111
/*
112
* resetForm hides save and cancel buttons,
113
* text input and shows input-txt. resetForm does not alter
114
* input-txt content.
115
*/
116

    
117
    function resetForm(e, elem) {
118
        var el = elem.parents('.editable');
119
        el.find('input[type="text"]').hide();
120
        el.find('a.cancel, a.save').hide();
121
        el.find('a.edit, .input-txt').show();
122
    }
123

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

    
138
    }
139

    
140
/*
141
setValue sets input-txt value to the input value.
142
Makes sure that the input value is not empty.
143
TODO:
144
Ajax request to submit form
145
*/
146

    
147
    function setValue(elem) {
148
        var el = elem.parents('.editable');
149
        if( el.find('input[type="text"]').val() ) {
150
            el.find('.input-txt').html(el.find('input[type="text"]').val());
151
        }
152
    }
153

    
154

    
155
    $('.editable .edit').click(function(e){
156
        showForm(e, $(this));
157
    })
158

    
159
    $('.editable .cancel').click(function(e){
160
        e.stopPropagation();
161
        e.preventDefault();
162
        resetForm(e, $(this));
163
    })
164

    
165
    $('.editable .save').click(function(e){
166
        e.stopPropagation();
167
        e.preventDefault();
168
        setValue($(this));
169
        resetForm(e, $(this));
170

    
171
    })
172

    
173

    
174
    $('.editable input[type="text"]').click(function(e){
175
        e.stopPropagation();
176
    })
177

    
178
    $('.editable input[type="text"]').keyup(function(e){
179
        if(e.keyCode == 13) { 
180
            setValue($(this));
181
            resetForm(e, $(this));
182
            
183
        }
184
    
185
    })
186

    
187
    $('html').click(function(e) {
188
        resetForm(e, $('.editable a.cancel'));
189
    });
190

    
191
}
192

    
193
/* TODO: better overlay functionality */
194
ui.overlay = function() {
195
    $('[data-overlay-id]').click(function(e){
196
        e.preventDefault();
197
        var el = $(this);
198
        // main-actions a need to be active to trigger overlay
199
        if ( (el.parents('.main-actions').find('li a.active').length == 0) && (el.parents('.main-actions').length > 0) ) {
200
            return false;
201
        }
202
        var id = el.data('overlay-id');
203

    
204
        $('.overlay-area').fadeIn(100);
205
        $('body').addClass('with-overlay');
206
        $(id).fadeIn('slow');
207
        console.log(id);
208
        if (id=='#network-wizard') {
209
            $(id).find('input').first().focus();
210
            return false;
211
        }
212
        $(id).find('a').first().focus();
213
    });
214
}
215

    
216
function goToByScroll(id){
217
      // Remove "link" from the ID
218
    id = id.replace("link", "");
219
      // Scroll
220
    $('html,body').animate({
221
        scrollTop: $("#"+id).offset().top},
222
        'slow');
223
}
224

    
225

    
226
/*
227
* functions concerning checkboxes and radiobuttons links
228
*/
229
ui.changeCheckboxState =function(checkbox_link) {
230
     $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
231
}
232

    
233
ui.changeRadiobuttonState = function(radiobtn_link) {
234
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
235
}
236

    
237
ui.checkOneRadioButton = function(radiobtn_link) {
238
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
239
}
240

    
241

    
242
// toggle expand arrrow  and corresponding area
243
// todo: one function for all the areas we reveal
244
ui.expandDownArea = function(arrow_link, area) {
245
    var arrow_link = $(arrow_link);
246
    var area = $(area);
247
            arrow_link.find('span.snf-arrow-up, span.snf-arrow-down').toggleClass('snf-arrow-up snf-arrow-down');
248
            // $('.wizard-content').removeAttr('style');
249
            area.stop().slideToggle(600, function() {
250
                if (area.is(':visible')) {
251
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
252
                } else {
253
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
254
                }
255

    
256
            });
257
}
258

    
259
$(document).ready(function(){
260

    
261

    
262
    $('#hd-search .hd-icon-search').click(function(e){
263
        $(this).parents('.hd-search').toggleClass('hd-open');
264
    })
265

    
266
    ui.setSidebarHeight();
267
    ui.closeDiv($('.info .close'), '.info');
268
    ui.editable();
269
    ui.overlay();
270
    ui.colorPickerVisible = 0;
271

    
272
    $("a.disabled").each(function() {
273
        $(this).removeAttr('href');
274
    });
275
    
276
    $("a.disabled").click(function(e) {
277
        e.preventDefault();
278
    });
279

    
280
    // checkbox: basic reaction on click (checked, unchecked)
281
    // see wizard
282
    $('.check').click(function(e){
283
        e.preventDefault();
284
        e.stopPropagation();
285
        ui.changeCheckboxState(this);
286
    });
287

    
288

    
289
    $('.with-checkbox').click(function(e){
290
        e.preventDefault();
291
        e.stopPropagation();
292
        var checkbox = self.find('.check');
293
        ui.changeCheckboxState(checkbox);
294
    });
295

    
296
    $('.radio_btn').click(function(e) {
297
        e.preventDefault();
298
         var state = $(this).find('span');
299
         if(state.hasClass('snf-radio-unchecked')) {
300
            ui.checkOneRadioButton(this);
301
            ui.changeRadiobuttonState(this);
302
        }
303
    })
304

    
305
    ui.entitiesActionsInit();
306
    
307
    $('.new-btn a.current').click(function(e){
308
        e.preventDefault();
309
    })
310

    
311
    $('.main-actions li a').click(function(e){
312
        if (!($(this).hasClass('active'))) {
313
            e.preventDefault();
314
        }
315
    })
316
    $('.scroll-pane').jScrollPane();
317

    
318
    // $('.main .items-list .title em').each(function(){
319
    //     $(this).html( ui.trimChars($(this).html(), 20) );
320

    
321
    // })
322

    
323
    $('.main-actions li a').click(function(e){
324
        if (!($(this).hasClass('active'))) {
325
            e.preventDefault();
326
        }
327
    })
328
    $('.overlay-area').children('.close').click(function(e){
329
        e.preventDefault();
330
        e.stopPropagation();
331
        $(this).parents('.overlay-area').hide();
332
        $(this).parents('.overlay-area').find($('.overlay-div')).hide();
333
        $('body').removeClass('with-overlay');
334
    })
335

    
336
    $('.browse-files').click(function(e){
337
        e.preventDefault();
338
    })
339

    
340
    Dropzone.options.filesDropzone = {
341
        dictDefaultMessage:'',
342
        clickable: '.browse-files',
343
        previewsContainer: '.dropzone-files',
344
        createImageThumbnails: false,
345
        dictRemoveFile: "snf-Remove file",
346
    };
347

    
348

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

    
367
    ui.placementByUser();
368

    
369
    if($('#picker').length>0) {
370
        $('#picker').farbtastic('#color');
371
    };
372
    if($('#sb-search').length>0) {
373
        new UISearch( document.getElementById( 'sb-search' ) );
374
    }
375

    
376

    
377
    /* grid/list view for items-list */
378

    
379
    $('.view-type .list').click(function(e){
380
        e.preventDefault();
381
        $('.view-type .grid span').removeClass('current');
382
        $(this).find('span').addClass('current');
383
        $('.items-list').removeClass('small-block-grid-2 large-block-grid-3');
384
        $('.items-list').addClass('list-view');
385
    });
386

    
387
     $('.view-type .grid').click(function(e){
388
        e.preventDefault();
389
        $('.view-type .list span').removeClass('current');
390
        $(this).find('span').addClass('current');
391
        $('.items-list').addClass('small-block-grid-2 large-block-grid-3');
392
        $('.items-list').removeClass('list-view');
393
    });
394

    
395
    // set filter visible
396
    $('.filter-menu .filter').click(function(e) {
397
        e.preventDefault();
398
        $(this).parents('.filter-menu').toggleClass('current');
399
    });
400

    
401
    // temp function used to demonstrate the visual effect of the building state of vm
402
    $('.btn5.temp').click(function(e) {
403
        e.preventDefault();
404
        $(this).siblings('.container').find('.complete').toggleClass('build-progress');
405
    })
406
})
407

    
408

    
409
$(window).resize(function(e){
410
    ui.setSidebarHeight();
411
    $('.scroll-pane').jScrollPane();
412
})