Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.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
   
92
}
93

    
94
/*
95
* In order for the editable value functionality to work, the html markup
96
* should be:
97
    <div class="editable">
98
        <span class="input-txt">editable value</span>
99
        <input type="text">
100
        <a href="#" class="edit">edit</a>
101
        <a href="#" class="save">save</a>
102
        <a href="#" class="cancel">cancel</a>
103
    </div>
104
*/
105
ui.editable = function(){
106

    
107
/*
108
* resetForm hides save and cancel buttons,
109
* text input and shows input-txt. resetForm does not alter
110
* input-txt content.
111
*/
112

    
113
    function resetForm(e, elem) {
114
        var el = elem.parents('.editable');
115
        el.find('input[type="text"]').hide();
116
        el.find('a.cancel, a.save').hide();
117
        el.find('a.edit, .input-txt').show();
118
    }
119

    
120
/* 
121
* showForm hides input-txt, shows save and cancel buttons and
122
* sets input value to input-txt content.
123
*/
124
    function showForm(e,elem) {
125
        e.stopPropagation(); 
126
        e.preventDefault();
127
        var el = elem.parents('.editable'); 
128
        el.find('input[type="text"]').val(el.find('.input-txt').html());
129
        el.find('input[type="text"]').show();
130
        el.find('a.cancel, a.save').show();
131
        elem.hide();
132
        el.find('.input-txt').hide();
133

    
134
    }
135

    
136
/*
137
setValue sets input-txt value to the input value.
138
Makes sure that the input value is not empty.
139
TODO:
140
Ajax request to submit form
141
*/
142

    
143
    function setValue(elem) {
144
        var el = elem.parents('.editable');
145
        if( el.find('input[type="text"]').val() ) {
146
            el.find('.input-txt').html(el.find('input[type="text"]').val());
147
        }
148
    }
149

    
150

    
151
    $('.editable .edit').click(function(e){
152
        showForm(e, $(this));
153
    })
154

    
155
    $('.editable .cancel').click(function(e){
156
        e.stopPropagation();
157
        e.preventDefault();
158
        resetForm(e, $(this));
159
    })
160

    
161
    $('.editable .save').click(function(e){
162
        e.stopPropagation();
163
        e.preventDefault();
164
        setValue($(this));
165
        resetForm(e, $(this));
166

    
167
    })
168

    
169

    
170
    $('.editable input[type="text"]').click(function(e){
171
        e.stopPropagation();
172
    })
173

    
174
    $('.editable input[type="text"]').keyup(function(e){
175
        if(e.keyCode == 13) { 
176
            setValue($(this));
177
            resetForm(e, $(this));
178
            
179
        }
180
    
181
    })
182

    
183
    $('html').click(function(e) {
184
        resetForm(e, $('.editable a.cancel'));
185
    });
186

    
187
}
188

    
189
/* TODO: better overlay functionality */
190
ui.overlay = function() {
191
    $('[data-overlay-id]').click(function(e){
192
        e.preventDefault();
193
        var el = $(this);
194
        // main-actions a need to be active to trigger overlay
195
        if ( (el.parents('.main-actions').find('li a.active').length == 0) && (el.parents('.main-actions').length > 0) ) {
196
            return false;
197
        }
198
        var id = el.data('overlay-id');
199

    
200
        $('.overlay-area').show();
201
        $(id).slideDown('slow');
202

    
203
        $(id).find('a').first().focus();
204
    });
205
}
206

    
207
// when user moves a vm or network icon (list view)
208
ui.placementByUser = function() {
209
    if($('.sortable').length != 0) {
210
        $( ".sortable" ).sortable({
211
            items: "> li:not(:last)",
212
            stop: function(event, ui) {
213
                $.map($(this).find('li'), function(el) {
214
                            return $(el).attr('data-order', $(el).index());
215
                        });
216
            }
217
        });
218

    
219
        $( ".sortable" ).disableSelection(); //i think unnecessary
220
    }
221
}
222

    
223
ui.pickResources = function(resource) {
224
    $('.flavor .with-flavor a:not(.'+resource+')').removeClass('current');
225
    $('.flavor .with-flavor a.'+resource+'').addClass('current');
226
}
227

    
228
function goToByScroll(id){
229
      // Remove "link" from the ID
230
    id = id.replace("link", "");
231
      // Scroll
232
    $('html,body').animate({
233
        scrollTop: $("#"+id).offset().top},
234
        'slow');
235
}
236

    
237

    
238
/*
239
* functions concerning checkboxes and radiobuttons links
240
*/
241
ui.changeCheckboxState =function(checkbox_link) {
242
     $(checkbox_link).find('.snf-checkbox-unchecked, .snf-checkbox-checked').toggleClass('snf-checkbox-unchecked snf-checkbox-checked');
243
}
244

    
245
ui.changeRadiobuttonState = function(radiobtn_link) {
246
    $(radiobtn_link).find('span.snf-radio-unchecked, span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
247
}
248

    
249
ui.checkOneRadioButton = function(radiobtn_link) {
250
    $(radiobtn_link).closest('ul').find('span.snf-radio-checked').toggleClass('snf-radio-unchecked snf-radio-checked');
251
}
252
/* toggle expand arrrow */
253
ui.expandArrow = function(arrow_link) {
254
    var arrow = arrow_link.find('span.snf-arrow-up, span.snf-arrow-down');
255
    arrow.toggleClass('snf-arrow-up snf-arrow-down');
256
}
257

    
258

    
259

    
260
$(document).ready(function(){
261

    
262
    ui.setSidebarHeight();
263
    ui.closeDiv($('.info .close'), '.info');
264
    ui.editable();
265
    ui.overlay();
266
    ui.colorPickerVisible = 0;
267

    
268
    // checkbox: basic reaction on click (checked, unchecked)
269
    $('.check').click(function(e){
270
        e.preventDefault();
271
        e.stopPropagation();
272
        console.log('check this', this);
273
        console.log('check $this', $(this));
274
        ui.changeCheckboxState(this);
275
    });
276

    
277
    ui.entitiesActionsInit();
278
    
279
    if ($('.overlay').length >0 ){
280
        $('body').addClass('with-overlay');
281
    }
282

    
283
    $('.new-btn a.current').click(function(e){
284
        e.preventDefault();
285
    })
286

    
287
    $('.main-actions li a').click(function(e){
288
        if (!($(this).hasClass('active'))) {
289
            e.preventDefault();
290
        }
291
    })
292
    $('.scroll-pane').jScrollPane();
293

    
294
    $('.main .items-list .title em').each(function(){
295
        $(this).html( ui.trimChars($(this).html(), 20) );
296

    
297
    })
298

    
299
    $('.main-actions li a').click(function(e){
300
        if (!($(this).hasClass('active'))) {
301
            e.preventDefault();
302
        }
303
    })
304
    $('.overlay-area').children('.close').click(function(e){
305
        e.preventDefault();
306
        e.stopPropagation();
307
        $(this).parents('.overlay-area').hide();
308
        $(this).parents('.overlay-area').find($('.overlay-div')).hide();
309
    })
310

    
311
    $('.browse-files').click(function(e){
312
        e.preventDefault();
313
    })
314

    
315
    Dropzone.options.filesDropzone = {
316
        dictDefaultMessage:'',
317
        clickable: '.browse-files',
318
        previewsContainer: '.dropzone-files',
319
        createImageThumbnails: false,
320
        dictRemoveFile: "snf-Remove file",
321
    };
322

    
323

    
324
    $('.main .files').magnificPopup({
325
        delegate: 'a.show.image',
326
        type: 'image',
327
        tLoading: 'Loading image #%curr%...',
328
        mainClass: 'mfp-img-mobile',
329
        gallery: {
330
            enabled: true,
331
            navigateByImgClick: true,
332
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
333
        },
334
        image: {
335
            tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
336
            titleSrc: function(item) {
337
                return item.el.data('title');
338
            }
339
        }
340
    });
341

    
342
    ui.placementByUser();
343

    
344
    if($('#picker').length>0) {
345
        $('#picker').farbtastic('#color');
346
    };
347
})
348

    
349

    
350
$(window).resize(function(e){
351
    ui.setSidebarHeight();
352
    $('.scroll-pane').jScrollPane();
353
})