Statistics
| Branch: | Tag: | Revision:

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

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
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

    
203

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

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

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

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

    
223

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

    
236
        $( ".sortable" ).disableSelection(); //i think unnecessary
237
    }
238
}
239

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

    
245

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

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

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

    
297

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

    
303
    
304
    if ($('.overlay').length >0 ){
305
        $('body').addClass('with-overlay');
306
    }
307

    
308
    $('.new-btn a.current').click(function(e){
309
        e.preventDefault();
310
    })
311

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

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

    
322
    })
323

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

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

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

    
347

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

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

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

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

    
387

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

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

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

    
412

    
413

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

    
425
})
426

    
427

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