Statistics
| Branch: | Tag: | Revision:

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

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
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
    tt = $(radiobtn_link);
212
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
213
}
214

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

    
221

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

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

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

    
243

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

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

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

    
295

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

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

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

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

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

    
320
    })
321

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

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

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

    
345

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

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

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

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

    
385

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

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

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

    
410

    
411

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

    
423
})
424

    
425

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