Statistics
| Branch: | Tag: | Revision:

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

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

    
17
}
18

    
19

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

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

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

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

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