Statistics
| Branch: | Tag: | Revision:

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

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

    
20
}
21

    
22

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

    
29

    
30
function goToByScroll(id){
31
        $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
32
}
33
        
34
$(document).ready(function() {
35

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

    
131
                                // check if the value is not float
132
                                 var num_float = parseFloat(value);
133
                                 num_float= String(num_float);
134

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

    
328
                        return false;
329
                        
330
                }
331
                         
332
                  
333
                 
334
        });
335

    
336
        goToByScroll("top");
337
        $('.quotas-form .form-row.with-errors input[type="text"]').first().focus();
338
        
339
        
340
        
341
        
342
});