Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.3 kB)

1
/*
2
* Various functions that will be used throughout all templates
3
* are inside ui Object
4
*/
5

    
6
ui = {};
7

    
8
/* when closeEl el is clicked, its parent with class divToCloseClass slidesUp */
9
ui.closeDiv = function(closeEl, divToCloseClass) {
10
    closeEl.click(function(e){
11
        e.preventDefault();
12
        $(this).parents(divToCloseClass).slideUp('slow');
13
    });
14
}
15

    
16

    
17
ui.trimChars = function( str, chars) {
18
    if ( str.length>chars){
19
        return $.trim(str).substring(0, chars)+ "...";
20
    } else {
21
        return str;
22
    }
23
}
24

    
25
/* sets lt-sidebar height. Useful for jscrollpane scrollbar */
26
ui.setSidebarHeight = function(){
27
    var WindowHeight = $(window).height();
28
    var h1= WindowHeight - $('.header').outerHeight();
29
    var h2= $('.main').outerHeight();
30
    $('.lt-sidebar').height((h2>h1) ? h2 : h1);
31
}
32

    
33

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

    
63
ui.entitiesActionsInit = function(){
64

    
65
    $('.entities li .container').mouseleave(
66
        function(e){
67
            $(this).find('.snf-checkbox-unchecked').parents('.check').removeClass('active');
68
         }
69
    );
70

    
71
    $('.entities .container .check').click(function(e){
72
        e.preventDefault();
73
        var checkbox = $(this).find('.snf-checkbox-unchecked, .snf-checkbox-checked');
74
        checkbox.toggleClass('snf-checkbox-unchecked');
75
        checkbox.toggleClass('snf-checkbox-checked');
76
        
77
        if(checkbox.hasClass('snf-checkbox-checked')){
78
            $(this).parents('.container').addClass('set-bg');
79
            $(this).addClass('active');
80
        }
81
        else{
82
            $(this).parents('.container').removeClass('set-bg');
83
        }
84
        ui.entitiesActionsEnabled();
85
    })
86
   
87
}
88

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

    
102
/*
103
* resetForm hides save and cancel buttons,
104
* text input and shows input-txt. resetForm does not alter
105
* input-txt content.
106
*/
107

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

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

    
129
    }
130

    
131
/*
132
setValue sets input-txt value to the input value.
133
Makes sure that the input value is not empty.
134
TODO:
135
Ajax request to submit form
136
*/
137

    
138
    function setValue(elem) {
139
        var el = elem.parents('.editable');
140
        if( el.find('input[type="text"]').val() ) {
141
            el.find('.input-txt').html(el.find('input[type="text"]').val());
142
        }
143
    }
144

    
145

    
146
    $('.editable .edit').click(function(e){
147
        showForm(e, $(this));
148
    })
149

    
150
    $('.editable .cancel').click(function(e){
151
        e.stopPropagation();
152
        e.preventDefault();
153
        resetForm(e, $(this));
154
    })
155

    
156
    $('.editable .save').click(function(e){
157
        e.stopPropagation();
158
        e.preventDefault();
159
        setValue($(this));
160
        resetForm(e, $(this));
161

    
162
    })
163

    
164

    
165
    $('.editable input[type="text"]').click(function(e){
166
        e.stopPropagation();
167
    })
168

    
169
    $('.editable input[type="text"]').keyup(function(e){
170
        if(e.keyCode == 13) { 
171
            setValue($(this));
172
            resetForm(e, $(this));
173
            
174
        }
175
    
176
    })
177

    
178
    $('html').click(function(e) {
179
        resetForm(e, $('.editable a.cancel'));
180
    });
181

    
182
}
183

    
184
/* TODO: better overlay functionality */
185
ui.overlay = function() {
186
    $('[data-overlay-id]').click(function(e){
187
        e.preventDefault();
188
        var el = $(this);
189
        // main-actions a need to be active to trigger overlay
190
        if ( (el.parents('.main-actions').find('li a.active').length == 0) && (el.parents('.main-actions').length > 0) ) {
191
            return false;
192
        }
193
        var id = el.data('overlay-id');
194

    
195
        $('.overlay-area').show();
196
        $(id).slideDown('slow');
197
    });
198

    
199

    
200
}
201

    
202
//permits only one checkbox to be checked in a ul
203
ui.checkAction = function(checkbox) {
204
        var otherChecked = checkbox.closest('li').siblings('li').find('span.snf-checkbox-checked').length;
205
        if(otherChecked!=0){
206
            checkbox.toggleClass('snf-checkbox-checked');
207
            checkbox.toggleClass('snf-checkbox-unchecked');
208
        }
209
        
210
}
211

    
212

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

    
225
        $( ".sortable" ).disableSelection(); //i think unnecessary
226
    }
227
}
228

    
229
ui.pickResources = function(resource) {
230
    $('.flavor .with-flavor a:not(.'+resource+')').removeClass('current');
231
    $('.flavor .with-flavor a.'+resource+'').addClass('current');
232
}
233

    
234

    
235
ui.netOptions = function(option) {
236
    var checkbox = $(option).find('.snf-checkbox-checked, .snf-checkbox-unchecked');
237
    var list = $(option).closest('ul');
238
    
239
    ui.checkAction(checkbox); //allazw to checkbox p pataw
240
    if(list.hasClass('subnet_options')){
241
        checkedBefore = $(option).closest('li').siblings('li').find('span.snf-checkbox-checked');
242
        if($(checkedBefore).closest('li').find('a').hasClass('manual'))
243
        {
244
            $(checkedBefore).closest('li').find('.manual_sub').hide();
245
        }
246
        ui.checkAction(checkedBefore); //allazw ta alla checkboxes
247
        
248
        if($(option).hasClass('manual')) {
249

    
250
            if($(checkbox).hasClass('snf-checkbox-unchecked')) {
251
                $(option).closest('span').find('.manual_sub').hide();
252
            }
253
            else {
254
                $(option).closest('span').find('.manual_sub').show();
255
            }
256
        }
257
    }
258
    else if($(option).closest('li').hasClass('dhcp_option')) {
259
        if($(checkbox).hasClass('snf-checkbox-unchecked')) {
260
            $('.network_options').find('.subnet_options').hide();
261
        }
262
        else {
263
            $('.network_options').find('.subnet_options').show();
264
        }
265
    }
266
}
267
    
268
function goToByScroll(id){
269
      // Remove "link" from the ID
270
    id = id.replace("link", "");
271
      // Scroll
272
    $('html,body').animate({
273
        scrollTop: $("#"+id).offset().top},
274
        'slow');
275
}
276
$(document).ready(function(){
277

    
278
    ui.setSidebarHeight();
279
    ui.closeDiv($('.info .close'), '.info');
280
    ui.entitiesActionsInit();
281
    ui.editable();
282
    ui.overlay();
283

    
284

    
285
    $('.select-os li').click(function(e){
286
        $('.select-os li').removeClass('selected');
287
        $(this).addClass('selected');
288
    })
289

    
290
    
291
    if ($('.overlay').length >0 ){
292
        $('body').addClass('with-overlay');
293
    }
294

    
295
    $('.new-btn a.current').click(function(e){
296
        e.preventDefault();
297
    })
298

    
299
    $('.main-actions li a').click(function(e){
300
        if (!($(this).hasClass('active'))) {
301
            e.preventDefault();
302
        }
303
    })
304
    $('.scroll-pane').jScrollPane();
305

    
306
    $('.main .items-list .title em').each(function(){
307
        $(this).html( ui.trimChars($(this).html(), 20) );
308

    
309
    })
310

    
311
    $('.main-actions li a').click(function(e){
312
        if (!($(this).hasClass('active'))) {
313
            e.preventDefault();
314
        }
315
    })
316
    $('.overlay-area .close').click(function(e){
317
        e.preventDefault();
318
        $(this).parents('.overlay-area').hide();
319
        $(this).parents('.overlay-area').find($('.overlay-div')).hide();
320
    })
321

    
322
    $('.browse-files').click(function(e){
323
        e.preventDefault();
324
    })
325

    
326
    Dropzone.options.filesDropzone = {
327
        dictDefaultMessage:'',
328
        clickable: '.browse-files',
329
        previewsContainer: '.dropzone-files',
330
        createImageThumbnails: false,
331
        dictRemoveFile: "snf-Remove file",
332
    };
333

    
334

    
335
    $('.main .files').magnificPopup({
336
        delegate: 'a.show.image',
337
        type: 'image',
338
        tLoading: 'Loading image #%curr%...',
339
        mainClass: 'mfp-img-mobile',
340
        gallery: {
341
            enabled: true,
342
            navigateByImgClick: true,
343
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
344
        },
345
        image: {
346
            tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
347
            titleSrc: function(item) {
348
                return item.el.data('title');
349
            }
350
        }
351
    });
352

    
353
    // vm wizard pick flavor
354
    $('.wizard .sub-menu a[data-size]').on( "click", function(e) {
355
        e.preventDefault();
356
        $(this).parents('.sub-menu').find('a').removeClass('current');
357
        $(this).addClass('current');
358
        ui.pickResources($(this).data('size')); 
359
    });
360

    
361
    $('.wizard .flavor .options a').click(function(e){
362
        e.preventDefault();
363
        $('.wizard .sub-menu a[data-size]').removeClass('current');
364
        $(this).parents('.options').find('a').removeClass('current');
365
        $(this).addClass('current');
366
    })
367

    
368
    $('.wizard .os > li').click(function(e){
369
        e.preventDefault();
370
        $('.wizard .os >li').removeClass('current');
371
        $(this).addClass('current');
372
    })
373

    
374

    
375
    // create network
376
    // checkbox: basic reaction on click (checked, unchecked)
377
    $('.network_options .check').click(function(e){
378
        e.preventDefault();
379
        ui.netOptions(this);
380
    })
381
  
382
    ui.placementByUser();
383
    $('.os .btn-col a').click( function(e){
384
        e.preventDefault();
385
        e.stopPropagation();
386
        $(this).toggleClass('current');
387
        $(this).parents('li').find('.details').slideToggle();
388
    })
389

    
390
    $('.advanced-conf-options .checkbox').click(function(e){
391
        console.log($(this).find('span'));
392
        $(this).find('h3').next('span').toggleClass('snf-checkbox-unchecked snf-checkbox-checked ');
393
    })
394

    
395
    $('.advanced-conf-options .has-more').click(function(e){
396
        $(this).next('.more').slideToggle();
397
    })
398

    
399
    $('.adv-main .expand-link').click( function(e){
400
        e.preventDefault();
401
        var link = $(this);
402
        link.toggleClass('current');
403
        var arrow = link.find('span.snf-arrow-up, span.snf-arrow-down');
404
        arrow.toggleClass('snf-arrow-up');
405
        arrow.toggleClass('snf-arrow-down');
406
        link.parents('div.advanced-conf-step').find('.advanced-conf-options').slideToggle();
407
    })
408

    
409
    $('#picker').farbtastic('#color');
410
    $('.show-add-tag').click(function(e){
411
        e.preventDefault();
412
        $(this).parents('.tags-area').find('.snf-color-picker').slideDown();
413
        goToByScroll('hide-add-tag');
414
    })
415
    $('.hide-add-tag').click(function(e){
416
        e.preventDefault();
417
        $(this).parents('.snf-color-picker').slideUp();
418
    })
419

    
420
})
421

    
422

    
423
$(window).resize(function(e){
424
    ui.setSidebarHeight();
425
    $('.scroll-pane').jScrollPane();
426
})