Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / static / im / js / quotas.js @ 116e778a

History | View | Annotate | Download (8.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')[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
                  
102
        // if you fill _proxy fields do stuff 
103
        $('.quotas-form .quota input[type="text"]').keyup(function () {
104
                 
105
                 if ( $('#icons span.info').hasClass('error-msg')){
106
                        $('#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.');
107
                 }
108
                  
109
                 // get value from input
110
                 var value = $(this).val();
111
                  
112
                 //get input name without _proxy
113
                 hidden_name = $(this).attr('name').replace("_proxy","");
114
                 var hidden_input = $("input[name='"+hidden_name+"']");
115
                 
116
                 if (value) {
117
                          
118
                         // actions for humanize fields
119
                         if ($(this).hasClass('dehumanize')){
120
                                 
121
                                 var flag = 0;
122

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

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

    
322
                        return false;
323
                        
324
                }
325
                if ($('.quotas-form .group .form-row.with-errors').length>0 ){
326
                        $('.quotas-form .group .form-row.with-errors input[type="text"]')[0].focus();
327
                        return false;
328
                        
329
                } 
330
                                 
331
                  
332
                 
333
        });
334

    
335

    
336
        
337
        
338
        
339
});