Statistics
| Branch: | Tag: | Revision:

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

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

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

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

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

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