Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.1 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
    $('.entities li.test1 .container .img').mouseenter(function(e) {
71
        $(this).closest('.container').stop(true, true).hide();
72
        $(this).closest('.container').siblings('.overlap_options').stop(true, true).fadeIn(500);//stop().slideToggle(600);
73
    });
74
    $('.entities li.test1 .overlap_options').mouseleave(function(e) {
75
        var self = this;
76
        $(this).stop(true, true).fadeOut(200, function() {
77
            $(this).siblings('.container').stop(true,true).fadeIn('fast');
78
        });
79
    });
80

    
81
    $('.entities li.test2 .container .img-wrap').mouseenter(function(e) {
82
        $(this).closest('.container').hide();
83
        $(this).closest('.container').siblings('.overlap_options').stop().slideToggle(600);
84
    });
85
    $('.entities li.test2 .overlap_options').mouseleave(function(e) {
86
        var self = this;
87
        $(this).stop().slideToggle(600, function(e) {
88
             $(self).siblings('.container').fadeIn();
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
        // main-actions a need to be active to trigger overlay
194
        if ( (el.parents('.main-actions').find('li a.active').length == 0) && (el.parents('.main-actions').length > 0) ) {
195
            return false;
196
        }
197
        var id = el.data('overlay-id');
198

    
199
        $('.overlay-area').fadeIn(100);
200
        $('body').addClass('with-overlay');
201
        $(id).fadeIn('slow');
202
        console.log(id);
203
        if (id=='#network-wizard') {
204
            $(id).find('input').first().focus();
205
            return false;
206
        }
207
        $(id).find('a').first().focus();
208
    });
209
}
210

    
211
// when user moves a vm or network icon (list view)
212
ui.placementByUser = function() {
213
    if($('.sortable').length != 0) {
214
        $( ".sortable" ).sortable({
215
            items: "> li:not(:last)",
216
            stop: function(event, ui) {
217
                $.map($(this).find('li'), function(el) {
218
                            return $(el).attr('data-order', $(el).index());
219
                        });
220
            }
221
        });
222

    
223
        $( ".sortable" ).disableSelection(); //i think unnecessary
224
    }
225
}
226

    
227
function goToByScroll(id){
228
      // Remove "link" from the ID
229
    id = id.replace("link", "");
230
      // Scroll
231
    $('html,body').animate({
232
        scrollTop: $("#"+id).offset().top},
233
        'slow');
234
}
235

    
236

    
237
/*
238
* functions concerning checkboxes and radiobuttons links
239
*/
240
ui.changeCheckboxState =function(checkbox_link) {
241
     $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
242
}
243

    
244
ui.changeRadiobuttonState = function(radiobtn_link) {
245
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
246
}
247

    
248
ui.checkOneRadioButton = function(radiobtn_link) {
249
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
250
}
251

    
252

    
253
// toggle expand arrrow  and corresponding area
254
// todo: one function for all the areas we reveal
255
ui.expandDownArea = function(arrow_link, area) {
256
    var arrow_link = $(arrow_link);
257
    var area = $(area);
258
            arrow_link.find('span.snf-arrow-up, span.snf-arrow-down').toggleClass('snf-arrow-up snf-arrow-down');
259
            // $('.wizard-content').removeAttr('style');
260
            area.stop().slideToggle(600, function() {
261
                if (area.is(':visible').length != 0) {
262
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
263
                } else {
264
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
265
                }
266

    
267
            });
268
}
269

    
270
$(document).ready(function(){
271

    
272
    ui.setSidebarHeight();
273
    ui.closeDiv($('.info .close'), '.info');
274
    ui.editable();
275
    ui.overlay();
276
    ui.colorPickerVisible = 0;
277

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

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

    
294

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

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

    
311
    ui.entitiesActionsInit();
312
    
313
    $('.new-btn a.current').click(function(e){
314
        e.preventDefault();
315
    })
316

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

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

    
327
    })
328

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

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

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

    
354

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

    
373
    ui.placementByUser();
374

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

    
382

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

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

    
393
     $('.view-type .grid').click(function(e){
394
        e.preventDefault();
395
        $('.view-type .list span').removeClass('current');
396
        $(this).find('span').addClass('current');
397
        $('.items-list').addClass('small-block-grid-2 large-block-grid-3');
398
        $('.items-list').removeClass('list-view');
399
    });
400

    
401
    // set filter visible
402
    $('.filter-menu .filter').click(function(e) {
403
        e.preventDefault();
404
        var self = this;
405
        $(self).parents('.filter-menu').toggleClass('current');
406
        // $(self).siblings('.options').stop().slideToggle(400, function() {
407
        //     if($(self).s)
408
        // });
409
    })
410
})
411

    
412

    
413
$(window).resize(function(e){
414
    ui.setSidebarHeight();
415
    $('.scroll-pane').jScrollPane();
416
})