Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / static / im / js / quotas.js @ 595dde70

History | View | Annotate | Download (10 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')[0].focus()
11
                }
12
        });
13
        if ($('.quotas-form .with-info .with-errors input[type="text"]')){
14
                $(this)[0].focus();        
15
        }
16

    
17
}
18

    
19

    
20
function bytesToSize2(bytes) {
21
    var sizes = [ 'n/a', 'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
22
    var i = +Math.floor(Math.log(bytes) / Math.log(1024));
23
    return  (bytes / Math.pow(1024, i)).toFixed( 0 ) + sizes[ isNaN( bytes ) ? 0 : i+1 ];
24
}
25

    
26
$(document).ready(function() {
27

    
28
        
29
        
30
        // ugly fix to transfer data easily 
31
        $('.with-info input[name^="is_selected_"]').each(function() {
32
                $(this).parents('.form-row').hide();
33
        });
34
    
35
        $('.quotas-form ul li a').click(function(e){
36
                
37
                // check the hidden input field
38
                $(this).siblings('input[type="hidden"]').val("1");
39
                
40
                // get the hidden input field without the proxy
41
                // and check the python form field
42
                 hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
43
                 $("input[name='"+hidden_name+"']").val("1");  
44
                
45
                 // prevent extra actions if it is checked                 
46
                if ( $(this).hasClass('selected')){
47
                        e.preventDefault();
48
                } else {
49
                        
50
                        // show the relevant fieldsets
51
                        group_form_show_resources($(this));
52
                }   
53
        });
54
        
55
         
56
        
57
        
58
         
59
        
60
        $('.quotas-form .group .delete').click(function(e){
61
                
62
                e.preventDefault(); 
63
                
64
                // clear form fields
65
                $(this).siblings('fieldset').find('input').val('');
66
                
67
                // clear errors
68
                $(this).siblings('fieldset').find('.form-row').removeClass('with-errors');
69
                 
70
                // hide relevant fieldset 
71
                $(this).parents('.group').hide('slow', function() {
72
                    $(this).appendTo('.not-visible');        
73
                });
74
                
75
                group_class = $(this).parents('.group').attr('class').replace('group ', '');
76
                
77
                // unselect group icon
78
                $('.quotas-form ul li a').each(function() {
79
                        if($(this).attr('id')==group_class) {
80
                                $(this).removeClass('selected');
81
                                $(this).siblings('input[type="hidden"]').val('0');
82
                                
83
                                // get the hidden input field without the proxy
84
                                // and check the python form field
85
                                 hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
86
                                 $("input[name='"+hidden_name+"']").val('0');  
87
                                
88
                        }
89
                }); 
90
                
91
                // clear hidden fields
92
                $(this).siblings('fieldset').find('input[type="text"]').each(function() {
93
                        hidden_name = $(this).attr('name').replace("_proxy","");
94
                         hidden_input = $("input[name='"+hidden_name+"']");
95
                         hidden_input.val('');
96
                });
97
                 
98
                 
99
        });
100
                  
101
        // if you fill _proxy fields do stuff 
102
        $('.quotas-form .quota input[type="text"]').change(function () {
103
                 
104
                 if ( $('#icons span.info').hasClass('error-msg')){
105
                        $('#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.');
106
                 }
107
                  
108
                 // get value from input
109
                 var value = $(this).val();
110
                 
111
                 //get input name without _proxy
112
                 hidden_name = $(this).attr('name').replace("_proxy","");
113
                 var hidden_input = $("input[name='"+hidden_name+"']");
114
                 
115
                 if (value) {
116
                         // actions for humanize fields
117
                         if ($(this).hasClass('dehumanize')){
118
                                 
119
                                 var flag = 0;
120

    
121
                                // check if the value is not float
122
                                 var num_float = parseFloat(value);
123
                                 num_float= String(num_float);
124

    
125
                                 if (num_float.indexOf(".") == 1){
126
                                         flag = 1 ; 
127
                                         msg="Please enter an integer";
128
                                 } else {
129
                                         var num = parseInt(value);
130
                                        if ( num == '0' ) { 
131
                                                flag = 1 ; msg="zero"
132
                                        } else {
133
                                                if ( value && !num ) { flag = 1 ; msg="Invalid format. Try something like 10GB, 2MB etc"}
134
                                         
135
                                                 var bytes = num;
136
                                                 
137
                                                // remove any numbers and get suffix                                 
138
                                                 var suffix = value.replace( num, '');
139
                
140
                                                  // validate suffix. 'i' renders it case insensitive
141
                                                 var suf = suffix.match( new RegExp('^(GB|KB|MB|TB|bytes|G|K|M|T|byte)$', 'i'));
142
                                                 if (suf){
143
                                                         
144
                                                         suf = suf[0].toLowerCase(); 
145
                                                         suf = suf.substr(0,1);
146
                                                 
147
                                                         // transform to bytes
148
                                                         switch (suf){
149
                                                                 case 'b': 
150
                                                                   bytes = num*Math.pow(1024,0);
151
                                                                   break;
152
                                                                 case 'k':
153
                                                                   bytes = num*Math.pow(1024,1);
154
                                                                   break;
155
                                                                 case 'm':
156
                                                                   bytes = num*Math.pow(1024,2);
157
                                                                   break;
158
                                                                 case 'g':
159
                                                                   bytes = num*Math.pow(1024,3);
160
                                                                   break;
161
                                                                 case 't':
162
                                                                   bytes = num*Math.pow(1024,4);
163
                                                                   break;    
164
                                                                 default:
165
                                                                   bytes = num; 
166
                                                         }
167
                                                 } else {
168
                                                         if (num) {
169
                                                                  flag = 1;
170
                                                                  msg ="You must specify correct units" 
171
                                                         }  
172
                                                          
173
                                                 }
174
                                        }
175
                                         
176
                                         
177
                                         
178
                                 }
179
                                 
180
                                  
181
                                 
182
                                 
183
                                 if ( flag == '1' ){ 
184
                                         $(this).parents('.form-row').addClass('with-errors');
185
                                         $(this).parents('.form-row').find('.error-msg').html(msg);
186
                                         bytes = value;
187
                                         $(this).focus();
188
                                         
189
                                          
190
                                 } else {
191
                                         $(this).parents('.form-row').removeClass('with-errors');
192
                                 }
193
                                 
194
                                 hidden_input.val(bytes);
195
                                 
196
                                 
197
                         }
198
                          
199
                         // validation actions for int fields
200
                         else {
201
        
202
                                 var is_int = value.match (new RegExp('^[0-9]*$'));
203
                                 if ( !is_int ){ 
204
                                         $(this).parents('.form-row').find('.error-msg').html('Enter a positive integer');
205
                                         $(this).parents('.form-row').addClass('with-errors');
206
                                          
207
                                 } else {
208
                                         if ( value == '0'){
209
                                                 $(this).parents('.form-row').find('.error-msg').html('Ensure this value is greater than or equal to 1');
210
                                                 $(this).parents('.form-row').addClass('with-errors');
211
                                         }else {
212
                                                 $(this).parents('.form-row').removeClass('with-errors');
213
                                         }
214
                                         
215
                                         
216
                                 }
217
                                 hidden_input.val(value);
218
        
219
                         }
220
                 
221
                 } else {
222
                         hidden_input.removeAttr('value');
223
                 }
224
                 $('#icons span.info').removeClass('error-msg');
225
                 
226
         });
227
         
228
        
229
        // if hidden checkboxes are checked, the right group is selected 
230
        $('.with-info input[name^="is_selected_"]').each(function() {
231
                if ( ($(this).val()) == 1 ){
232
                        
233
                        // get hidden input name
234
                        hidden_name = $(this).attr('name');
235
                        $("input[name='proxy_"+hidden_name+"']").val("1"); 
236
                        
237
                        // pretend to check the ul li a
238
                        // show the relevant fieldsets
239
                        var mock_a = $("input[name='proxy_"+hidden_name+"']").siblings('a');
240
                        group_form_show_resources(mock_a);
241
                         
242
                }
243
        }); 
244
        
245
        
246
        /*
247
        // if input_uplimit fields are filled,
248
        // fill the _uplimit_proxy ones
249
         
250
        $('.with-info input[name$="_uplimit"]').each(function() {
251
                if ($(this).val()){
252
                        
253
                        // get value from input
254
                         var value = $(this).val();
255
                        
256
                        
257
                        // get hidden input name
258
                        hidden_name = $(this).attr('name');
259
                        var field = $("input[name='"+hidden_name+"_proxy']"); 
260
                        
261
                        
262
                        if ( (field.hasClass('dehumanize')) && !($(this).parents('.form-row').hasClass('with-errors'))) {
263
                                // for dehumanize fields transform bytes to KB, MB, etc
264
                                // unless there is an error
265
                                field.val(bytesToSize2(value))
266
                        } else {
267
                                // else just return the value
268
                                field.val(value);        
269
                        }
270
                        
271
                        var group_class = field.parents('div[class^="group"]').attr('class').replace('group ', '');
272
                        
273
                         
274
                         
275
                        
276
                        // select group icon
277
                        $('.quotas-form ul li a').each(function() {
278
                                
279
                                if($(this).attr('id') == group_class) {
280
                                        $(this).addClass('selected');
281
                                        $(this).siblings('input[type="hidden"]').attr('checked', 'checked');
282
                                        
283
                                        // get the hidden input field without the proxy
284
                                        // and check the python form field
285
                                         hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
286
                                         $("input[name='"+hidden_name+"']").attr('checked', 'checked');  
287
                                         
288
                                         group_form_show_resources($(this));
289
                                        
290
                                }
291
                        }); 
292
                        
293
                
294
                        
295
                        // if the field has class error, transfer error to the proxy fields
296
                        if ( $(this).parents('.form-row').hasClass('with-errors') ) {
297
                                field.parents('.form-row').addClass('with-errors');
298
                        }
299
                        
300
                         
301
                }
302
        });*/
303
        // if input_uplimit fields are filled,
304
        // fill the _uplimit_proxy ones
305
         
306
        $('.group input[name$="_uplimit_proxy"]').each(function() {
307
                if ($(this).val()){
308
                        
309
                        // get value from input
310
                         var value = $(this).val();
311
                        
312
                        
313
                        // get hidden input name
314
                        hidden_name = $(this).attr('name');
315
                        hidden_field_name = hidden_name.replace("_proxy","");
316
                        $("input[name='"+hidden_field_name+"']").val(value);
317
                        var field = $(this); 
318
                        
319
                        
320
                        if ( (field.hasClass('dehumanize')) && !($(this).parents('.form-row').hasClass('with-errors'))) {
321
                                // for dehumanize fields transform bytes to KB, MB, etc
322
                                // unless there is an error
323
                                field.val(bytesToSize2(value))
324
                        } else {
325
                                // else just return the value
326
                                field.val(value);        
327
                        }
328
                        
329
                        var group_class = field.parents('div[class^="group"]').attr('class').replace('group ', '');
330
                        
331
                         
332
                         
333
                        
334
                        // select group icon
335
                        $('.quotas-form ul li a').each(function() {
336
                                
337
                                if($(this).attr('id') == group_class) {
338
                                        $(this).addClass('selected');
339
                                        $(this).siblings('input[type="hidden"]').val("1");
340
                                        
341
                                        // get the hidden input field without the proxy
342
                                        // and check the python form field
343
                                         hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
344
                                         $("input[name='"+hidden_name+"']").val("1");  
345
                                         
346
                                         group_form_show_resources($(this));
347
                                        
348
                                }
349
                        }); 
350
                        
351
                
352
                        
353
                        // if the field has class error, transfer error to the proxy fields
354
                        if ( $(this).parents('.form-row').hasClass('with-errors') ) {
355
                                field.parents('.form-row').addClass('with-errors');
356
                        }
357
                        
358
                         
359
                }
360
        });  
361
        
362
        // todo den doulevei
363
        $('#group_create_form').submit(function(){
364
                if ($('.quotas-form .group .form-row.with-errors').length>0 ){
365
                        return false;
366
                }
367
                var flag = 0;
368
                $('.quotas-form .group input[type="text"]').each(function() {
369
                        // get value from input
370
                         var value = $(this).val();
371
                        if (value){
372
                                flag =1;
373
                        }
374
                });
375
                if (flag =='0') {
376
                        $('#icons span.info').addClass('error-msg');
377
                        $('#icons span.info').find('span').html('You must fill in at least one resource');
378
                        return false;
379
                        
380
                }
381
        });
382

    
383

    
384
        
385
        
386
        
387
});