Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.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
                        $(this).appendTo('.visible');
8
                        $(this).show('slow');
9
                        $(this).find('input')[0].focus()
10
                }
11
        });
12
        if ($('.quotas-form .with-info .with-errors input[type="text"]')){
13
                $(this)[0].focus();        
14
        }
15

    
16
}
17

    
18

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

    
25
$(document).ready(function() {
26

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

    
118
                                // check if the value is not float
119
                                 var num_float = parseFloat(value);
120
                                 num_float= String(num_float);
121

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