Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.5 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
//create vm
230
ui.pickFlavor = function(flavorSelection) {
231
        ui.select_flavor = 1;
232
        console.log(flavorSelection);
233
        var classes = $(flavorSelection).attr('class').split(" ");
234
        // the second class is: 'small_flavor' or 'medium_flavor' or 'large_flavor'
235
        
236
        $(flavorSelection).parent('li').siblings('li').find('a.chosen_flavor').removeClass('chosen_flavor');
237
        $(flavorSelection).addClass('chosen_flavor');
238
        $('.select-flavor').find('dl.cpus span.current, dl.ram span.current, dl.disk span.current').removeClass('current');
239
        $('.select-flavor').find('.'+classes[1]).addClass('current');
240

    
241
}
242

    
243

    
244
ui.pickResources = function(resource) {
245
        $(resource).parents('dl').find('span').removeClass('current');
246
        $(resource).addClass('current');
247
        if(ui.select_flavor == 1){
248
            if(!$(resource).parents('dl').hasClass('storage')){
249
            $('.lt-sidebar').find('a.chosen_flavor').removeClass('chosen_flavor');
250
            select_flavor = 0;
251
            }
252
        }
253
    }
254

    
255

    
256
ui.netOptions = function(option) {
257
    var checkbox = $(option).find('.snf-checkbox-checked, .snf-checkbox-unchecked');
258
    var list = $(option).closest('ul');
259
    
260
    ui.checkAction(checkbox); //allazw to checkbox p pataw
261
    if(list.hasClass('subnet_options')){
262
        checkedBefore = $(option).closest('li').siblings('li').find('span.snf-checkbox-checked');
263
        if($(checkedBefore).closest('li').find('a').hasClass('manual'))
264
        {
265
            $(checkedBefore).closest('li').find('.manual_sub').hide();
266
        }
267
        ui.checkAction(checkedBefore); //allazw ta alla checkboxes
268
        
269
        if($(option).hasClass('manual')) {
270

    
271
            if($(checkbox).hasClass('snf-checkbox-unchecked')) {
272
                $(option).closest('span').find('.manual_sub').hide();
273
            }
274
            else {
275
                $(option).closest('span').find('.manual_sub').show();
276
            }
277
        }
278
    }
279
    else if($(option).closest('li').hasClass('dhcp_option')) {
280
        if($(checkbox).hasClass('snf-checkbox-unchecked')) {
281
            $('.network_options').find('.subnet_options').hide();
282
        }
283
        else {
284
            $('.network_options').find('.subnet_options').show();
285
        }
286
    }
287
}
288
    
289

    
290
$(document).ready(function(){
291

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

    
298

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

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

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

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

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

    
323
    })
324

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

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

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

    
348

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

    
367

    
368
    // create vm
369
    // choose resources one by one
370
    ui.select_flavor =0;
371
    $('.select-flavor dl span').click(function(e){
372
        e.preventDefault();
373
        ui.pickResources(this);
374
        
375
        
376
    });
377

    
378
    // create vm
379
    // if a predefined flavor has been selected from the user, it highlights the proper resources 
380
    $('.lt-sidebar li a.flavor_selection').click(function(e){
381
        e.preventDefault();
382
        ui.pickFlavor(this);        
383
    });
384

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

    
399
})
400

    
401

    
402
$(window).resize(function(e){
403
    ui.setSidebarHeight();
404
    $('.scroll-pane').jScrollPane();
405
})