Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.6 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
    return  (bytes / Math.pow(1024, i)).toFixed( 0 ) + sizes[ isNaN( bytes ) ? 0 : i+1 ];
47
}
48

    
49

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

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

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

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

    
348
                        return false;
349
                        
350
                }
351
                 
352
                
353
                if ($('.not-visible .group .with-errors').length >0 ){
354
                        //$('.not-visible .group .with-errors').first().find('input[type="text"]').focus();
355
                         
356
                        return false;
357
                }
358
                         
359
                  
360
                 
361
        });
362

    
363
        //goToByScroll("top");
364
        $('.quotas-form .form-row.with-errors input[type="text"]').first().focus();
365
        
366
        // change error colors in quotas forms
367
        $('.quotas-form .quota input[type="text"]').focusout(function() {
368
          $(this).parents('.with-errors').addClass('strong-error');
369
           
370
        });
371
        $('.quotas-form .quota input[type="text"]').focusin(function() {
372
          $(this).parents('.with-errors').removeClass('strong-error');
373
           
374
        });
375
        
376
        
377
});