Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.8 kB)

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

    
6
ui = {};
7
/*
8
* ui.wizards get populated in vm-wizard.js
9
* here is the declaration only
10
*/
11
ui.wizard = {};
12

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

    
21

    
22
ui.trimChars = function( str, chars) {
23
    if ( str.length>chars){
24
        return $.trim(str).substring(0, chars)+ "...";
25
    } else {
26
        return str;
27
    }
28
}
29

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

    
38

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

    
68
ui.entitiesActionsInit = function(){
69

    
70
    // $('.entities li .container').mouseleave(
71
    //     function(e){
72
    //         $(this).find('.snf-checkbox-unchecked').parents('.check').removeClass('active');
73
    //      }
74
    // );
75

    
76
    // $('.entities .container .check').click(function(e){
77
        // e.preventDefault();
78
        // var checkbox = $(this).find('.snf-checkbox-unchecked, .snf-checkbox-checked');
79
        // checkbox.toggleClass('snf-checkbox-unchecked');
80
        // checkbox.toggleClass('snf-checkbox-checked');
81
        
82
        // if(checkbox.hasClass('snf-checkbox-checked')){
83
        //     $(this).parents('.container').addClass('set-bg');
84
        //     $(this).addClass('active');
85
        // }
86
        // else{
87
        //     $(this).parents('.container').removeClass('set-bg');
88
        // }
89
        // ui.entitiesActionsEnabled();
90
    // })
91
    $('.entities li.test1 .container .img-wrap').mouseenter(function(e) {
92
        $(this).closest('.container').hide();
93
        $(this).closest('.container').siblings('.overlap_options').fadeIn('slow');//stop().slideToggle(600);
94
    });
95
    $('.entities li.test1 .overlap_options').mouseleave(function(e) {
96
        var self = this;
97
        $(this).fadeOut('slow', function(){//stop().slideToggle(600, function(e) {
98
             $(self).siblings('.container').fadeIn('fast');
99
         });
100
    });
101

    
102
    $('.entities li.test2 .container .img-wrap').mouseenter(function(e) {
103
        $(this).closest('.container').hide();
104
        $(this).closest('.container').siblings('.overlap_options').stop().slideToggle(600);
105
    });
106
    $('.entities li.test2 .overlap_options').mouseleave(function(e) {
107
        var self = this;
108
        $(this).stop().slideToggle(600, function(e) {
109
             $(self).siblings('.container').fadeIn();
110
        });
111
    });
112
}
113

    
114
/*
115
* In order for the editable value functionality to work, the html markup
116
* should be:
117
    <div class="editable">
118
        <span class="input-txt">editable value</span>
119
        <input type="text">
120
        <a href="#" class="edit">edit</a>
121
        <a href="#" class="save">save</a>
122
        <a href="#" class="cancel">cancel</a>
123
    </div>
124
*/
125
ui.editable = function(){
126

    
127
/*
128
* resetForm hides save and cancel buttons,
129
* text input and shows input-txt. resetForm does not alter
130
* input-txt content.
131
*/
132

    
133
    function resetForm(e, elem) {
134
        var el = elem.parents('.editable');
135
        el.find('input[type="text"]').hide();
136
        el.find('a.cancel, a.save').hide();
137
        el.find('a.edit, .input-txt').show();
138
    }
139

    
140
/* 
141
* showForm hides input-txt, shows save and cancel buttons and
142
* sets input value to input-txt content.
143
*/
144
    function showForm(e,elem) {
145
        e.stopPropagation(); 
146
        e.preventDefault();
147
        var el = elem.parents('.editable'); 
148
        el.find('input[type="text"]').val(el.find('.input-txt').html());
149
        el.find('input[type="text"]').show();
150
        el.find('a.cancel, a.save').show();
151
        elem.hide();
152
        el.find('.input-txt').hide();
153

    
154
    }
155

    
156
/*
157
setValue sets input-txt value to the input value.
158
Makes sure that the input value is not empty.
159
TODO:
160
Ajax request to submit form
161
*/
162

    
163
    function setValue(elem) {
164
        var el = elem.parents('.editable');
165
        if( el.find('input[type="text"]').val() ) {
166
            el.find('.input-txt').html(el.find('input[type="text"]').val());
167
        }
168
    }
169

    
170

    
171
    $('.editable .edit').click(function(e){
172
        showForm(e, $(this));
173
    })
174

    
175
    $('.editable .cancel').click(function(e){
176
        e.stopPropagation();
177
        e.preventDefault();
178
        resetForm(e, $(this));
179
    })
180

    
181
    $('.editable .save').click(function(e){
182
        e.stopPropagation();
183
        e.preventDefault();
184
        setValue($(this));
185
        resetForm(e, $(this));
186

    
187
    })
188

    
189

    
190
    $('.editable input[type="text"]').click(function(e){
191
        e.stopPropagation();
192
    })
193

    
194
    $('.editable input[type="text"]').keyup(function(e){
195
        if(e.keyCode == 13) { 
196
            setValue($(this));
197
            resetForm(e, $(this));
198
            
199
        }
200
    
201
    })
202

    
203
    $('html').click(function(e) {
204
        resetForm(e, $('.editable a.cancel'));
205
    });
206

    
207
}
208

    
209
/* TODO: better overlay functionality */
210
ui.overlay = function() {
211
    $('[data-overlay-id]').click(function(e){
212
        e.preventDefault();
213
        var el = $(this);
214
        // main-actions a need to be active to trigger overlay
215
        if ( (el.parents('.main-actions').find('li a.active').length == 0) && (el.parents('.main-actions').length > 0) ) {
216
            return false;
217
        }
218
        var id = el.data('overlay-id');
219

    
220
        $('.overlay-area').fadeIn(100);
221
        $('body').addClass('with-overlay');
222
        $(id).fadeIn('slow');
223
        console.log(id);
224
        if (id=='#network-wizard') {
225
            $(id).find('input').first().focus();
226
            return false;
227
        }
228
        $(id).find('a').first().focus();
229
    });
230
}
231

    
232
// when user moves a vm or network icon (list view)
233
ui.placementByUser = function() {
234
    if($('.sortable').length != 0) {
235
        $( ".sortable" ).sortable({
236
            items: "> li:not(:last)",
237
            stop: function(event, ui) {
238
                $.map($(this).find('li'), function(el) {
239
                            return $(el).attr('data-order', $(el).index());
240
                        });
241
            }
242
        });
243

    
244
        $( ".sortable" ).disableSelection(); //i think unnecessary
245
    }
246
}
247

    
248
function goToByScroll(id){
249
      // Remove "link" from the ID
250
    id = id.replace("link", "");
251
      // Scroll
252
    $('html,body').animate({
253
        scrollTop: $("#"+id).offset().top},
254
        'slow');
255
}
256

    
257

    
258
/*
259
* functions concerning checkboxes and radiobuttons links
260
*/
261
ui.changeCheckboxState =function(checkbox_link) {
262
     $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
263
}
264

    
265
ui.changeRadiobuttonState = function(radiobtn_link) {
266
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
267
}
268

    
269
ui.checkOneRadioButton = function(radiobtn_link) {
270
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
271
}
272

    
273

    
274
// toggle expand arrrow  and corresponding area
275
// todo: one function for all the areas we reveal
276
ui.expandDownArea = function(arrow_link, area) {
277
    var arrow_link = $(arrow_link);
278
    var area = $(area);
279
            arrow_link.find('span.snf-arrow-up, span.snf-arrow-down').toggleClass('snf-arrow-up snf-arrow-down');
280
            // $('.wizard-content').removeAttr('style');
281
            area.stop().slideToggle(600, function() {
282
                if (area.is(':visible').length != 0) {
283
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').removeClass('snf-arrow-down').addClass('snf-arrow-up');
284
                } else {
285
                    arrow_link.find('.snf-arrow-down .snf-arrow-up').addClass('snf-arrow-down');
286
                }
287

    
288
            });
289
}
290

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

    
293
    ui.setSidebarHeight();
294
    ui.closeDiv($('.info .close'), '.info');
295
    ui.editable();
296
    ui.overlay();
297
    ui.colorPickerVisible = 0;
298

    
299
    $("a.disabled").each(function() {
300
        $(this).removeAttr('href');
301
    });
302
    
303
    $("a.disabled").click(function(e) {
304
        e.preventDefault();
305
    });
306

    
307
    // checkbox: basic reaction on click (checked, unchecked)
308
    // see wizard
309
    $('.check').click(function(e){
310
        e.preventDefault();
311
        e.stopPropagation();
312
        ui.changeCheckboxState(this);
313
    });
314

    
315

    
316
    $('.with-checkbox').click(function(e){
317
        e.preventDefault();
318
        e.stopPropagation();
319
        var checkbox = self.find('.check');
320
        ui.changeCheckboxState(checkbox);
321
    });
322

    
323
    $('.radio_btn').click(function(e) {
324
        e.preventDefault();
325
         var state = $(this).find('span');
326
         if(state.hasClass('snf-radio-unchecked')) {
327
            ui.checkOneRadioButton(this);
328
            ui.changeRadiobuttonState(this);
329
        }
330
    })
331

    
332
    ui.entitiesActionsInit();
333
    
334
    $('.new-btn a.current').click(function(e){
335
        e.preventDefault();
336
    })
337

    
338
    $('.main-actions li a').click(function(e){
339
        if (!($(this).hasClass('active'))) {
340
            e.preventDefault();
341
        }
342
    })
343
    $('.scroll-pane').jScrollPane();
344

    
345
    $('.main .items-list .title em').each(function(){
346
        $(this).html( ui.trimChars($(this).html(), 20) );
347

    
348
    })
349

    
350
    $('.main-actions li a').click(function(e){
351
        if (!($(this).hasClass('active'))) {
352
            e.preventDefault();
353
        }
354
    })
355
    $('.overlay-area').children('.close').click(function(e){
356
        e.preventDefault();
357
        e.stopPropagation();
358
        $(this).parents('.overlay-area').hide();
359
        $(this).parents('.overlay-area').find($('.overlay-div')).hide();
360
        $('body').removeClass('with-overlay');
361
    })
362

    
363
    $('.browse-files').click(function(e){
364
        e.preventDefault();
365
    })
366

    
367
    Dropzone.options.filesDropzone = {
368
        dictDefaultMessage:'',
369
        clickable: '.browse-files',
370
        previewsContainer: '.dropzone-files',
371
        createImageThumbnails: false,
372
        dictRemoveFile: "snf-Remove file",
373
    };
374

    
375

    
376
    $('.main .files').magnificPopup({
377
        delegate: 'a.show.image',
378
        type: 'image',
379
        tLoading: 'Loading image #%curr%...',
380
        mainClass: 'mfp-img-mobile',
381
        gallery: {
382
            enabled: true,
383
            navigateByImgClick: true,
384
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
385
        },
386
        image: {
387
            tError: 'The image could not be loaded.',
388
            titleSrc: function(item) {
389
                return item.el.data('title');
390
            }
391
        }
392
    });
393

    
394
    ui.placementByUser();
395

    
396
    if($('#picker').length>0) {
397
        $('#picker').farbtastic('#color');
398
    };
399
    if($('#sb-search').length>0) {
400
        new UISearch( document.getElementById( 'sb-search' ) );
401
    }
402

    
403

    
404
    /* grid/list view for items-list */
405

    
406
    $('.view-type .list').click(function(e){
407
        e.preventDefault();
408
        $('.view-type .grid span').removeClass('current');
409
        $(this).find('span').addClass('current');
410
        $('.items-list').removeClass('small-block-grid-2 large-block-grid-3');
411
        $('.items-list').addClass('list-view');
412
    });
413

    
414
     $('.view-type .grid').click(function(e){
415
        e.preventDefault();
416
        $('.view-type .list span').removeClass('current');
417
        $(this).find('span').addClass('current');
418
        $('.items-list').addClass('small-block-grid-2 large-block-grid-3');
419
        $('.items-list').removeClass('list-view');
420
    });
421

    
422
    // set filter visible
423
    $('.filter-menu .filter').click(function(e) {
424
        e.preventDefault();
425
        var self = this;
426
        $(self).parents('.filter-menu').toggleClass('current');
427
        // $(self).siblings('.options').stop().slideToggle(400, function() {
428
        //     if($(self).s)
429
        // });
430
    })
431
})
432

    
433

    
434
$(window).resize(function(e){
435
    ui.setSidebarHeight();
436
    $('.scroll-pane').jScrollPane();
437
})