Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.7 kB)

1
ui = {};
2

    
3

    
4
ui.closeDiv = function(closeEl, divToCloseClass) {
5
    closeEl.click(function(e){
6
        e.preventDefault();
7
        $(this).parents(divToCloseClass).slideUp('slow');
8
    });
9
}
10

    
11

    
12

    
13
ui.trimChars = function( str, chars) {
14
    if ( str.length>chars){
15
        return jQuery.trim(str).substring(0, chars).split(" ").slice(0, -1).join(" ") + "...";
16
    } else {
17
        return str;
18
    }
19
}
20

    
21
// set lt-sidebar height 
22
ui.setSidebarHeight = function(){
23
    var WindowHeight = $(window).height();
24
    var h1= WindowHeight - $('.header').outerHeight();
25
    var h2= $('.main.details').outerHeight();
26
    
27
    if (h2>h1) {
28
        var ltSidebarHeight = h2;
29
    } else {
30
        var ltSidebarHeight = h1;
31
    }
32
    $('.lt-sidebar').height(ltSidebarHeight);
33
}
34

    
35
ui.cntCheckbox = function(){ 
36
    var all = $('.snf-checkbox-checked').length;
37
    var running = $('.snf-checkbox-checked').parents('.container').find('.running').length;
38
    var stopped = $('.snf-checkbox-checked').parents('.container').find('.stopped').length;
39
    $('.header .main-actions li:not(.permanent) a').removeClass('active');  
40
    if ( (running*stopped) > 0 ){          
41
         $('.header .main-actions li.both a').addClass('active');   
42
         $('.header .main-actions li.single a').removeClass('active');   
43
    } else {
44
        if (running > 0) {
45
            $('.header .main-actions li.both a').addClass('active');   
46
            $('.header .main-actions li.running a').addClass('active');   
47
        } else if (stopped>0) {
48
            $('.header .main-actions li.both a').addClass('active');   
49
            $('.header .main-actions li.stopped a').addClass('active');   
50

    
51
        } else {
52

    
53
        }
54
        if ( all > 1 ) {
55
            $('.header .main-actions li.single a').removeClass('active');   
56
        }
57

    
58
    }
59
}
60

    
61
ui.setCheckedVMBgColor = function(){
62

    
63
    if ($('.check span').length >0) {
64
        $('.more_checkbox .checkbox-checked').parents('.container').addClass('set-bg');
65
    } else {
66
        $('.more_checkbox').parents('.container').removeClass('set-bg');    
67
    }
68

    
69
}
70

    
71

    
72
ui.entitiesActionsInit = function(){
73

    
74
    // if VM is stopped hide connect option 
75
    $('.vms .container .stopped').parents('.container').find('.options .connect').hide();
76
    
77
    $('.entities li .container').mouseenter(
78
      function (e) {
79
        $(this).find('.options, .check').stop(true, true).addClass('active');
80
        $(this).find('.options').stop(true, true).fadeIn(500); 
81
        $(this).stop(true, true).addClass('set-border');
82
      }
83
    );
84

    
85
    $('.entities li .container').mouseleave(
86
        function(e){
87
            $(this).stop(true, true).removeClass('set-border');
88
            $(this).find('.options').stop(true, true).fadeOut(200); 
89
            $(this).find('.snf-checkbox-unchecked').parents('.check').stop(true, true).removeClass('active');
90
         }
91
    );
92

    
93
    $('.entities .container .check').click(function(e){
94
        e.preventDefault();
95

    
96
        var checkbox = $(this).find('.snf-checkbox-unchecked, .snf-checkbox-checked');
97

    
98
        checkbox.toggleClass('snf-checkbox-unchecked');
99
        checkbox.toggleClass('snf-checkbox-checked');
100
        
101
        if(checkbox.hasClass('snf-checkbox-checked')){
102
            $(this).parents('.container').addClass('set-bg');
103
            $(this).addClass('active');
104
        }
105
        else{
106
            $(this).parents('.container').removeClass('set-bg');
107
            
108
      
109
        }
110

    
111
        
112

    
113
        ui.cntCheckbox();
114
    })
115
    $('.entities li .details').mouseenter(function (e) {
116
        $(this).parents('.container').find('.info-box').show();
117
    }) 
118
      
119
    $('.entities li .details').mouseleave(function (e) {
120
        $(this).parents('.container').find('.info-box').hide();
121
    })   
122
    $('.entities li .container').mouseleave(
123
        function(e){
124
            $(this).stop(true, true).removeClass('set-border');
125
            $(this).find('.options').stop(true, true).fadeOut(200); 
126
            $(this).find('.img').stop(true, true).fadeIn('slow');
127
            $(this).find('.snf-checkbox-unchecked').parents('.check').stop(true, true).removeClass('active');
128
            $(this).find('.visible-info em').stop(true, true).each(function(){
129
                $(this).animate({
130
                 left: 0,
131
                }, 300);        
132
            }) 
133
         }
134
    );
135
   
136
}
137

    
138
ui.editable = function(){
139

    
140
/*
141
resetForm hides save and cancel buttons, 
142
text input and shows input-txt. resetForm does not alter 
143
input-txt content.
144
*/
145

    
146
    function resetForm(e, elem) {
147
        var el = elem.parents('.editable');
148
        el.find('input[type="text"]').hide();
149
        el.find('a.cancel, a.save').hide();
150
        el.find('a.edit').show();
151
        el.find('.input-txt').show();
152
    }
153

    
154
/* 
155
showForm hides input-txt, shows save and cancel buttons and
156
set input value to input-txt content.
157
*/
158
    function showForm(e,elem) {
159
        e.stopPropagation(); 
160
        e.preventDefault();
161
        var el = elem.parents('.editable'); 
162
        el.find('input[type="text"]').val(el.find('.input-txt').html());
163
        el.find('input[type="text"]').show();
164
        el.find('a.cancel, a.save').show();
165
        elem.hide();
166
        el.find('.input-txt').hide();
167

    
168
    }
169

    
170
/*
171
setValue sets input-txt value to the input value.
172
Makes sure that the input value is not empty.
173
TODO:
174
Ajax request to submit form
175
*/
176

    
177
    function setValue(elem) {
178
        var el = elem.parents('.editable');
179
        if( el.find('input[type="text"]').val() ) {
180
            el.find('.input-txt').html(el.find('input[type="text"]').val());
181
        }
182
    }
183

    
184

    
185
    $('.editable .edit').click(function(e){
186
        showForm(e, $(this));
187
    })
188

    
189
    $('.editable .cancel').click(function(e){
190
        e.stopPropagation();
191
        e.preventDefault();
192
        resetForm(e, $(this));
193
    })
194

    
195
    $('.editable .save').click(function(e){
196
        e.stopPropagation();
197
        e.preventDefault();
198
        setValue($(this));
199
        resetForm(e, $(this));
200

    
201
    })
202

    
203

    
204
    $('.editable input[type="text"]').click(function(e){
205
        e.stopPropagation();
206
    })
207

    
208
    $('.editable input[type="text"]').keyup(function(e){
209
        if(e.keyCode == 13) { 
210
            setValue($(this));
211
            resetForm(e, $(this));
212
            
213
        }
214
    
215
    })
216

    
217
    $('html').click(function(e) {
218
        resetForm(e, $('.editable a.cancel'));
219
    });
220

    
221
}
222

    
223
ui.overlay = function() {
224
    $('[data-overlay-id]').click(function(e){
225
        e.preventDefault();
226
        var el = $(this);
227

    
228
        // main-actions a need to be active to trigger overlay
229
        if ( (el.parents('.main-actions').find('li a.active').length == 0) && (el.parents('.main-actions').length > 0) ) {
230
            return false;
231
        }
232
        var id = el.data('overlay-id');
233
        $('.overlay-area').show();
234
        $(id).slideDown('slow');
235

    
236

    
237
    });
238

    
239

    
240
}
241

    
242
//permits only one checkbox to be checked in a ul
243
ui.checkAction = function(checkbox) {
244
        var otherChecked = checkbox.closest('li').siblings('li').find('span.snf-checkbox-checked').length;
245
        if(otherChecked!=0){
246
            checkbox.toggleClass('snf-checkbox-checked');
247
            checkbox.toggleClass('snf-checkbox-unchecked');
248
        }
249
        
250
}
251

    
252

    
253
// when user moves a vm or network icon (list view)
254
ui.placementByUser = function() {
255
    if($('.sortable').length != 0) {
256
        $( ".sortable" ).sortable({
257
            items: "> li:not(:last)",
258
            stop: function(event, ui) {
259
                $.map($(this).find('li'), function(el) {
260
                            return $(el).attr('data-order', $(el).index());
261
                        });
262
            }
263
        });
264

    
265
        $( ".sortable" ).disableSelection(); //i think unnecessary
266
    }
267
}
268

    
269
//create vm
270
ui.pickFlavor = function(flavorSelection) {
271
        ui.select_flavor = 1;
272
        console.log(flavorSelection);
273
        var classes = $(flavorSelection).attr('class').split(" ");
274
        // the second class is: 'small_flavor' or 'medium_flavor' or 'large_flavor'
275
        
276
        $(flavorSelection).parent('li').siblings('li').find('a.chosen_flavor').removeClass('chosen_flavor');
277
        $(flavorSelection).addClass('chosen_flavor');
278
        $('.select-flavor').find('dl.cpus span.current, dl.ram span.current, dl.disk span.current').removeClass('current');
279
        $('.select-flavor').find('.'+classes[1]).addClass('current');
280

    
281
}
282

    
283

    
284
ui.pickResources = function(resource) {
285
        $(resource).parents('dl').find('span').removeClass('current');
286
        $(resource).addClass('current');
287
        if(ui.select_flavor == 1){
288
            if(!$(resource).parents('dl').hasClass('storage')){
289
            $('.lt-sidebar').find('a.chosen_flavor').removeClass('chosen_flavor');
290
            select_flavor = 0;
291
            }
292
        }
293
    }
294

    
295

    
296
ui.netOptions = function(option) {
297
    var checkbox = $(option).find('.snf-checkbox-checked, .snf-checkbox-unchecked');
298
    var list = $(option).closest('ul');
299
    
300
    ui.checkAction(checkbox); //allazw to checkbox p pataw
301
    if(list.hasClass('subnet_options')){
302
        checkedBefore = $(option).closest('li').siblings('li').find('span.snf-checkbox-checked');
303
        if($(checkedBefore).closest('li').find('a').hasClass('manual'))
304
        {
305
            $(checkedBefore).closest('li').find('.manual_sub').hide();
306
        }
307
        ui.checkAction(checkedBefore); //allazw ta alla checkboxes
308
        
309
        if($(option).hasClass('manual')) {
310

    
311
            if($(checkbox).hasClass('snf-checkbox-unchecked')) {
312
                $(option).closest('span').find('.manual_sub').hide();
313
            }
314
            else {
315
                $(option).closest('span').find('.manual_sub').show();
316
            }
317
        }
318
    }
319
    else if($(option).closest('li').hasClass('dhcp_option')) {
320
        if($(checkbox).hasClass('snf-checkbox-unchecked')) {
321
            $('.network_options').find('.subnet_options').hide();
322
        }
323
        else {
324
            $('.network_options').find('.subnet_options').show();
325
        }
326
    }
327
}
328
    
329

    
330
$(document).ready(function(){
331

    
332

    
333
    ui.closeDiv($('.info .close'), '.info');
334
    ui.closeDiv($('.dummy-navigation .close'), '.dummy-navigation');
335

    
336
    $('.dummy-navigation .our').click(function(e){
337
        e.preventDefault();
338
        $('.ours.'+$(this).data('our')).toggle();
339
        $(this).toggleClass('open');
340
    });
341

    
342
    ui.setSidebarHeight();
343
    $('.select-os li').click(function(e){
344
        $('.select-os li').removeClass('selected');
345
        $(this).addClass('selected');
346
    })
347

    
348
    
349
    if ($('.overlay').length >0 ){
350
        $('body').addClass('with-overlay');
351
    }
352

    
353
    $('.new-btn a.current').click(function(e){
354
        e.preventDefault();
355
    })
356

    
357
    ui.entitiesActionsInit();
358
    ui.editable();
359
    ui.overlay();
360

    
361
    $('.main-actions li a').click(function(e){
362
        if (!($(this).hasClass('active'))) {
363
            e.preventDefault();
364
        }
365
    })
366
    $('.scroll-pane').jScrollPane();
367

    
368

    
369

    
370

    
371

    
372
    $('.main .items-list .title em').each(function(){
373
        $(this).html( ui.trimChars($(this).html(), 22) );
374

    
375
    })
376

    
377
    $('.main-actions li a').click(function(e){
378
        if (!($(this).hasClass('active'))) {
379
            e.preventDefault();
380
        }
381
    })
382
    $('.overlay-area .close').click(function(e){
383
        e.preventDefault();
384
        $(this).parents('.overlay-area').hide();
385
        $(this).parents('.overlay-area').find($('[data-overlay-id]')).hide();
386
    })
387

    
388
    $('.browse-files').click(function(e){
389
        e.preventDefault();
390
    })
391

    
392
    Dropzone.options.filesDropzone = {
393
        dictDefaultMessage:'',
394
        clickable: '.browse-files',
395
        previewsContainer: '.dropzone-files',
396
        createImageThumbnails: false,
397
        dictRemoveFile: "snf-Remove file",
398
    };
399

    
400

    
401
    $('.main .files').magnificPopup({
402
        delegate: 'a.show.image',
403
        type: 'image',
404
        tLoading: 'Loading image #%curr%...',
405
        mainClass: 'mfp-img-mobile',
406
        gallery: {
407
            enabled: true,
408
            navigateByImgClick: true,
409
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
410
        },
411
        image: {
412
            tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
413
            titleSrc: function(item) {
414
                return item.el.data('title');
415
            }
416
        }
417
    });
418

    
419

    
420
    // create vm
421
    // choose resources one by one
422
    ui.select_flavor =0;
423
    $('.select-flavor dl span').click(function(e){
424
        e.preventDefault();
425
        ui.pickResources(this);
426
        
427
        
428
    });
429

    
430
    // create vm
431
    // if a predefined flavor has been selected from the user, it highlights the proper resources 
432
    $('.lt-sidebar li a.flavor_selection').click(function(e){
433
        e.preventDefault();
434
        ui.pickFlavor(this);        
435
    });
436

    
437
    // create network
438
    // checkbox: basic reaction on click (checked, unchecked)
439
    $('.network_options .check').click(function(e){
440
        e.preventDefault();
441
        ui.netOptions(this);
442
    })
443
  
444
    ui.placementByUser();
445
   
446

    
447
})
448

    
449

    
450
$(window).resize(function(e){
451
    ui.setSidebarHeight();
452
    $('.scroll-pane').jScrollPane();
453
})