Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / static / im / js / quotas.js @ 9efd0075

History | View | Annotate | Download (9.7 kB)

1
function group_form_show_resources(el){
2
        
3
        el.addClass('selected');
4
        var id = el.attr('id');
5
        $('.quotas-form .group').each(function() {
6
                if( $(this).hasClass(id) ) {
7
                         
8
                        //$(this).appendTo('.visible');
9
                        $(this).show('slow');                 
10
                        $(this).find('input').first().focus();
11
 
12
                 
13
                }
14
        });
15
         
16
         
17
        if ($('.quotas-form .with-info .with-errors input[type="text"]')){
18
                $('.quotas-form .with-info .with-errors input[type="text"]').first().focus();        
19
        }
20
        
21
        
22
         
23
        
24
        //setTimeout(function() { document.getElementById("city").focus(); }, 100);
25

    
26
}
27

    
28

    
29
function group_form_toggle_resources(el){
30
        
31

    
32
        var id = el.attr('id');
33
        $('.quotas-form .group').each(function() {
34
                if( $(this).hasClass(id) ) {
35
                         
36
                        //$(this).appendTo('.visible');
37
                        $(this).toggle('slow');                  
38
                }
39
        });
40
}
41

    
42

    
43
function bytesToSize2(bytes) {
44
    var sizes = [ 'n/a', 'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
45
    var i = +Math.floor(Math.log(bytes) / Math.log(1024));
46
    if (!isFinite(i)) { return 0 + 'KB'}
47
    return  (bytes / Math.pow(1024, i)).toFixed( 0 ) + sizes[ isNaN( bytes ) ? 0 : i+1 ];
48
}
49

    
50

    
51
function goToByScroll(id){
52
        $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
53
}
54
        
55
$(document).ready(function() {
56

    
57
        
58
         
59
        // ugly fix to transfer data easily 
60
        $('.with-info input[name^="is_selected_"]').each(function() {
61
                $(this).parents('.form-row').hide();
62
        });
63
    
64
        $('.quotas-form ul li a').click(function(e){
65
                
66
                // check the hidden input field
67
                $(this).siblings('input[type="hidden"]').val("1");
68
                
69
                // get the hidden input field without the proxy
70
                // and check the python form field
71
                 hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
72
                 $("input[name='"+hidden_name+"']").val("1");  
73
                
74
                 // prevent extra actions if it is checked                 
75
                if ( $(this).hasClass('selected')){
76
                         e.preventDefault();
77
                        //group_form_toggle_resources($(this));
78
                } else {
79
                        
80
                        // show the relevant fieldsets
81
                        group_form_show_resources($(this));
82
                }   
83
        });
84
        
85
         
86
        
87
        
88
         
89
        
90
        $('.quotas-form .group .delete').click(function(e){
91
                
92
                e.preventDefault(); 
93
                
94
                // clear form fields
95
                $(this).siblings('fieldset').find('input').val('');
96
                
97
                // clear errors
98
                $(this).siblings('fieldset').find('.form-row').removeClass('with-errors');
99
                 
100
                // hide relevant fieldset 
101
                $(this).parents('.group').hide('slow', function() {
102
                    //$(this).appendTo('.not-visible');
103
                    $(this).hide();        
104
                });
105
                
106
                group_class = $(this).parents('.group').attr('class').replace('group ', '');
107
                
108
                // unselect group icon
109
                $('.quotas-form ul li a').each(function() {
110
                        if($(this).attr('id')==group_class) {
111
                                $(this).removeClass('selected');
112
                                $(this).siblings('input[type="hidden"]').val('0');
113
                                
114
                                // get the hidden input field without the proxy
115
                                // and check the python form field
116
                                 hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
117
                                 $("input[name='"+hidden_name+"']").val('0');  
118
                                
119
                        }
120
                }); 
121
                
122
                // clear hidden fields
123
                $(this).siblings('fieldset').find('input[type="text"]').each(function() {
124
                        hidden_name = $(this).attr('name').replace("_proxy","");
125
                         hidden_input = $("input[name='"+hidden_name+"']");
126
                         hidden_input.val('');
127
                });
128
                 
129
                 
130
        });
131
          
132
                  
133
        // if you fill _proxy fields do stuff 
134
        $('.quotas-form .quota input[type="text"]').keyup(function () {
135
                 
136
                 if ( $('#icons span.info').hasClass('error-msg')){
137
                        $('#icons span.info').find('span').html('Here you add resources to your Project. Each resource you specify here, will be granted to *EACH* user of this Project. So the total resources will be: <Total number of members> * <amount_of_resource> for each resource.');
138
                 }
139
                  
140
                 // get value from input
141
                 var value = $(this).val();
142
                  
143
                 //get input name without _proxy
144
                 hidden_name = $(this).attr('name').replace("_proxy","");
145
                 var hidden_input = $("input[name='"+hidden_name+"']");
146
                 
147
                 if (value) {
148
                          
149
                         // actions for humanize fields
150
                         if ($(this).hasClass('dehumanize')){
151
                                 
152
                                 var flag = 0;
153

    
154
                                // check if the value is not float
155
                                 var num_float = parseFloat(value);
156
                                 num_float= String(num_float);
157

    
158
                                 if (num_float.indexOf(".") == 1){
159
                                         flag = 1 ; 
160
                                         msg="Please enter an integer";
161
                                 } else {
162
                                         var num = parseInt(value);
163
                                        if ( num == '0' ) { 
164
                                                flag = 1 ; msg="This value can not be zero. Try something like 10GB, 2MB etc"
165
                                        } else {
166
                                                if ( value && !num ) { flag = 1 ; msg="Invalid format. Try something like 10GB, 2MB etc"}
167
                                         
168
                                                 var bytes = num;
169
                                                 
170
                                                // remove any numbers and get suffix                                 
171
                                                 var suffix = value.replace( num, '');
172
                
173
                                                  // validate suffix. 'i' renders it case insensitive
174
                                                 var suf = suffix.match( new RegExp('^(GB|KB|MB|TB|bytes|G|K|M|T|byte)$', 'i'));
175
                                                 if (suf){
176
                                                         
177
                                                         suf = suf[0].toLowerCase(); 
178
                                                         suf = suf.substr(0,1);
179
                                                 
180
                                                         // transform to bytes
181
                                                         switch (suf){
182
                                                                 case 'b': 
183
                                                                   bytes = num*Math.pow(1024,0);
184
                                                                   break;
185
                                                                 case 'k':
186
                                                                   bytes = num*Math.pow(1024,1);
187
                                                                   break;
188
                                                                 case 'm':
189
                                                                   bytes = num*Math.pow(1024,2);
190
                                                                   break;
191
                                                                 case 'g':
192
                                                                   bytes = num*Math.pow(1024,3);
193
                                                                   break;
194
                                                                 case 't':
195
                                                                   bytes = num*Math.pow(1024,4);
196
                                                                   break;    
197
                                                                 default:
198
                                                                   bytes = num; 
199
                                                         }
200
                                                 } else {
201
                                                         if (num) {
202
                                                                  flag = 1;
203
                                                                  msg ="You must specify correct units. Try something like 10GB, 2MB etc" 
204
                                                         }  
205
                                                          
206
                                                 }
207
                                        }                                         
208
                                 } 
209
                                 
210
                                 if ( flag == '1' ){ 
211
                                         $(this).parents('.form-row').addClass('with-errors');
212
                                         $(this).parents('.form-row').find('.error-msg').html(msg);
213
                                         bytes = value;
214
                                         $(this).focus();
215
                                         
216
                                          
217
                                 } else {
218
                                         $(this).parents('.form-row').removeClass('with-errors');
219
                                 }
220
                                 
221
                                 hidden_input.val(bytes);
222
                                 
223
                                 
224
                         }
225
                          
226
                         // validation actions for int fields
227
                         else {
228
                                 var is_int = value.match (new RegExp('^[1-9][0-9]*$'));
229
                                 if ( !is_int ){ 
230
                                         $(this).parents('.form-row').find('.error-msg').html('Enter a positive integer');
231
                                         $(this).parents('.form-row').addClass('with-errors');
232
                                          
233
                                 } else {
234
                                         if ( value == '0'){
235
                                                 $(this).parents('.form-row').find('.error-msg').html('Ensure this value is greater than or equal to 1');
236
                                                 $(this).parents('.form-row').addClass('with-errors');
237
                                         }else {
238
                                                 $(this).parents('.form-row').removeClass('with-errors');
239
                                         }
240
                                         
241
                                         
242
                                 }
243
                                 hidden_input.val(value);
244
        
245
                         }
246
                 
247
                 } else {
248
        ;                 $(this).parents('.with-errors').removeClass('with-errors')
249
                         hidden_input.removeAttr('value');
250
                 }
251
                 $('#icons span.info').removeClass('error-msg');
252
                 
253
         });
254
         
255
        
256
        // if hidden checkboxes are checked, the right group is selected 
257
        $('.with-info input[name^="is_selected_"]').each(function() {
258
                if ( ($(this).val()) == 1 ){
259
                        
260
                        // get hidden input name
261
                        hidden_name = $(this).attr('name');
262
                        $("input[name='proxy_"+hidden_name+"']").val("1"); 
263
                        
264
                        // pretend to check the ul li a
265
                        // show the relevant fieldsets
266
                        var mock_a = $("input[name='proxy_"+hidden_name+"']").siblings('a');
267
                        group_form_show_resources(mock_a);
268
                         
269
                }
270
        }); 
271
        
272
 
273
         
274
        $('.group input[name$="_m_uplimit_proxy"], .group input[name$="_p_uplimit_proxy"]').each(function() {
275
                if ($(this).val()){
276
                        // get value from input
277
                         var value = $(this).val();
278
                        
279
                        // get hidden input name
280
                        hidden_name = $(this).attr('name');
281
                        hidden_field_name = hidden_name.replace("_proxy", "");
282
                        $("input[name='"+hidden_field_name+"']").val(value);
283
                        var field = $(this); 
284
                        
285
                        if ( (field.hasClass('dehumanize')) && !($(this).parents('.form-row').hasClass('with-errors'))) {
286
                                // for dehumanize fields transform bytes to KB, MB, etc
287
                                // unless there is an error
288
                                field.val(bytesToSize2(value) || 0)
289
                        } else {
290
                                // else just return the value
291
                                field.val(value);        
292
                        }
293
                        var group_class = field.parents('div[class^="group"]').attr('class').replace('group ', '');
294
            
295
                        // select group icon
296
                        $('.quotas-form ul li a').each(function() {
297
                                
298
                                if($(this).attr('id') == group_class) {
299
                                        $(this).addClass('selected');
300
                                        $(this).siblings('input[type="hidden"]').val("1");
301
                                        
302
                                        // get the hidden input field without the proxy
303
                                        // and check the python form field
304
                                         hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
305
                                         $("input[name='"+hidden_name+"']").val("1");  
306
                                         
307
                                         group_form_show_resources($(this));
308
                                }
309
                        }); 
310
                        
311
                        // if the field has class error, transfer error to the proxy fields
312
                        if ( $(this).parents('.form-row').hasClass('with-errors') ) {
313
                                field.parents('.form-row').addClass('with-errors');
314
                        }
315
                }
316
        });  
317
        
318
        
319
        
320
        
321
        $('#group_create_form').submit(function(){
322
                var flag = 0;                 
323
                $('.quotas-form .group input[type="text"]').each(function() {
324
                         var value = $(this).val();
325
                        if (value){
326
                                flag = 1;
327
                        }
328
                });
329
                
330
                console.info(flag);
331
                
332
                if (flag =='0') {
333
                        $('#icons').focus();
334
                        $('#icons span.info').addClass('error-msg');
335
                        $('#icons span.info').find('span').html('You must fill in at least one resource');
336
                        goToByScroll("icons");
337

    
338
                        return false;
339
                        
340
                }
341
                 
342
                
343
                if ($('.not-visible .group .with-errors').length >0 ){
344
                        //$('.not-visible .group .with-errors').first().find('input[type="text"]').focus();
345
                         
346
                        return false;
347
                }
348
                         
349
                  
350
                 
351
        });
352

    
353
        //goToByScroll("top");
354
        $('.quotas-form .form-row.with-errors input[type="text"]').first().focus();
355
        
356
        // change error colors in quotas forms
357
        $('.quotas-form .quota input[type="text"]').focusout(function() {
358
          $(this).parents('.with-errors').addClass('strong-error');
359
           
360
        });
361
        $('.quotas-form .quota input[type="text"]').focusin(function() {
362
          $(this).parents('.with-errors').removeClass('strong-error');
363
           
364
        });
365
        
366
        
367
});