Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.4 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 bytesToSize2(bytes) {
30
    var sizes = [ 'n/a', 'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
31
    var i = +Math.floor(Math.log(bytes) / Math.log(1024));
32
    return  (bytes / Math.pow(1024, i)).toFixed( 0 ) + sizes[ isNaN( bytes ) ? 0 : i+1 ];
33
}
34

    
35

    
36
function goToByScroll(id){
37
        $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
38
}
39
        
40
$(document).ready(function() {
41

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

    
138
                                // check if the value is not float
139
                                 var num_float = parseFloat(value);
140
                                 num_float= String(num_float);
141

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

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

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