Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.7 kB)

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

    
6
ui = {};
7
ui.wizard = {};
8

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

    
17

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

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

    
34

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

    
64
ui.entitiesActionsInit = function(){
65

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

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

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

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

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

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

    
130
    }
131

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

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

    
146

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

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

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

    
163
    })
164

    
165

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

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

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

    
183
}
184

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

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

    
201

    
202
ui.change_checkbox_state =function(checkbox_link) {
203
     $(checkbox_link).find('span.snf-checkbox-unchecked, span.snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
204
}
205

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

    
210
ui.check_one_radiobtn = function(radiobtn_link) {
211
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
212
}
213

    
214
ui.expand_arrow = function(arrow_link) {
215
    var arrow = arrow_link.find('span.snf-arrow-up, span.snf-arrow-down');
216
    arrow.toggleClass('snf-arrow-up snf-arrow-down');
217
        
218
}
219

    
220

    
221
// when user moves a vm or network icon (list view)
222
ui.placementByUser = function() {
223
    if($('.sortable').length != 0) {
224
        $( ".sortable" ).sortable({
225
            items: "> li:not(:last)",
226
            stop: function(event, ui) {
227
                $.map($(this).find('li'), function(el) {
228
                            return $(el).attr('data-order', $(el).index());
229
                        });
230
            }
231
        });
232

    
233
        $( ".sortable" ).disableSelection(); //i think unnecessary
234
    }
235
}
236

    
237
ui.pickResources = function(resource) {
238
    $('.flavor .with-flavor a:not(.'+resource+')').removeClass('current');
239
    $('.flavor .with-flavor a.'+resource+'').addClass('current');
240
}
241

    
242

    
243
// ui.netOptions = function(option) {
244
//     var checkbox = $(option).find('.snf-checkbox-checked, .snf-checkbox-unchecked, .snf-radio-checked, .snf-radio-unchecked');
245
//     var list = $(option).closest('ul');
246
    
247
//     ui.checkAction(checkbox); //allazw to checkbox p pataw
248
//     if(list.hasClass('subnet_options')){
249
//         checkedBefore = $(option).closest('li').siblings('li').find('span.snf-radio-checked');
250
//         if($(checkedBefore).closest('li').find('a').hasClass('manual'))
251
//         {
252
//             $(checkedBefore).closest('li').find('.manual_sub').hide();
253
//         }
254
//         ui.checkAction(checkedBefore); //allazw ta alla checkboxes
255
        
256
//         if($(option).hasClass('manual')) {
257

    
258
//             if($(checkbox).hasClass('snf-checkbox-unchecked')) {
259
//                 $(option).closest('span').find('.manual_sub').hide();
260
//             }
261
//             else {
262
//                 $(option).closest('span').find('.manual_sub').show();
263
//             }
264
//         }
265
//     }
266
//     else if($(option).closest('li').hasClass('dhcp_option')) {
267
//         if($(checkbox).hasClass('snf-checkbox-unchecked')) {
268
//             $('.network_options').find('.subnet_options').hide();
269
//             $('.network_options').find('.sub_title').hide();
270
//         }
271
//         else {
272
//             $('.network_options').find('.sub_title').show();
273
//             $('.network_options').find('.subnet_options').show();
274
//         }
275
//     }
276
// }
277
    
278
function goToByScroll(id){
279
      // Remove "link" from the ID
280
    id = id.replace("link", "");
281
      // Scroll
282
    $('html,body').animate({
283
        scrollTop: $("#"+id).offset().top},
284
        'slow');
285
}
286

    
287

    
288
$(document).ready(function(){
289

    
290
    ui.setSidebarHeight();
291
    ui.closeDiv($('.info .close'), '.info');
292
    ui.entitiesActionsInit();
293
    ui.editable();
294
    ui.overlay();
295
    ui.colorPickerVisible = 0;
296

    
297
    $('.select-os li').click(function(e){
298
        $('.select-os li').removeClass('selected');
299
        $(this).addClass('selected');
300
    })
301

    
302
    
303
    if ($('.overlay').length >0 ){
304
        $('body').addClass('with-overlay');
305
    }
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 .close').click(function(e){
329
        e.preventDefault();
330
        $(this).parents('.overlay-area').hide();
331
        $(this).parents('.overlay-area').find($('.overlay-div')).hide();
332
    })
333

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

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

    
346

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

    
365
    // vm wizard pick flavor
366
    $('.wizard .sub-menu a[data-size]').on( "click", function(e) {
367
        e.preventDefault();
368
        $(this).parents('.sub-menu').find('a').removeClass('current');
369
        $(this).addClass('current');
370
        ui.pickResources($(this).data('size')); 
371
    });
372

    
373
    $('.wizard .flavor .options a').click(function(e){
374
        e.preventDefault();
375
        $('.wizard .sub-menu a[data-size]').removeClass('current');
376
        $(this).parents('.options').find('a').removeClass('current');
377
        $(this).addClass('current');
378
    })
379

    
380
    $('.wizard .os > li').click(function(e){
381
        e.preventDefault();
382
        $('.wizard .os >li').removeClass('current');
383
        $(this).addClass('current');
384
    })
385

    
386

    
387
    // create network
388
    // checkbox: basic reaction on click (checked, unchecked)
389
    $('.network_options .check').click(function(e){
390
        e.preventDefault();
391
      //  ui.netOptions(this);
392
    })
393
  
394
    ui.placementByUser();
395
    $('.os .btn-col a').click( function(e){
396
        e.preventDefault();
397
        e.stopPropagation();
398
        $(this).toggleClass('current');
399
        $(this).parents('li').find('.details').stop().slideToggle();
400
    })
401

    
402
    $('.advanced-conf-options .checkbox').click(function(e){
403
        console.log($(this).find('span'));
404
        $(this).find('h3').next('span').toggleClass('snf-checkbox-unchecked snf-checkbox-checked ');
405
    })
406

    
407
    $('.advanced-conf-options .has-more').click(function(e){
408
        $(this).next('.more').slideToggle();
409
    })
410

    
411

    
412

    
413
    if($('#picker').length>0) { $('#picker').farbtastic('#color'); }
414
    $('.show-add-tag').click(function(e){
415
        e.preventDefault();
416
        $(this).parents('.tags-area').find('.snf-color-picker').slideDown();
417
        goToByScroll('hide-add-tag');
418
        ui.colorPickerVisible =1;
419
    })
420
    $('.hide-add-tag').click(function(e){
421
        e.preventDefault();
422
        $(this).parents('.snf-color-picker').slideUp('400', function(){
423
            ui.colorPickerVisible = 0;
424
        });
425

    
426
    })
427

    
428
})
429

    
430

    
431
$(window).resize(function(e){
432
    ui.setSidebarHeight();
433
    $('.scroll-pane').jScrollPane();
434
})