Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.9 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('.foo');
8
                        $(this).show('slow');
9
                        $(this).find('input')[0].focus()
10
                }
11
        });
12
        
13
}
14

    
15

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

    
22
$(document).ready(function() {
23

    
24
        
25
        
26
        // ugly fix to transfer data easily 
27
        $('.with-info input[name^="is_selected_"]').each(function() {
28
                $(this).parents('.form-row').hide();
29
        });
30
    
31
        $('.quotas-form ul li a').click(function(e){
32
                
33
                // check the hidden input field
34
                $(this).siblings('input[type="hidden"]').attr('checked','checked');
35
                
36
                // get the hidden input field without the proxy
37
                // and check the python form field
38
                 hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
39
                 $("input[name='"+hidden_name+"']").attr('checked','checked');  
40
                
41
                 // prevent extra actions if it is checked                 
42
                if ( $(this).hasClass('selected')){
43
                        e.preventDefault();
44
                } else {
45
                        
46
                        // show the relevant fieldsets
47
                        group_form_show_resources($(this));
48
                }   
49
        });
50
        
51
         
52
        
53
        
54
         
55
        
56
        $('.quotas-form .group .delete').click(function(e){
57
                
58
                e.preventDefault(); 
59
                
60
                // clear form fields
61
                $(this).siblings('fieldset').find('input').val('');
62
                
63
                // clear errors
64
                $(this).siblings('fieldset').find('.form-row').removeClass('with-errors');
65
                 
66
                // hide relevant fieldset 
67
                $(this).parents('.group').hide('slow', function() {
68
                    $(this).appendTo('.not-foo');        
69
                });
70
                
71
                group_class = $(this).parents('.group').attr('class').replace('group ', '');
72
                
73
                // unselect group icon
74
                $('.quotas-form ul li a').each(function() {
75
                        if($(this).attr('id')==group_class) {
76
                                $(this).removeClass('selected');
77
                                $(this).siblings('input[type="hidden"]').removeAttr('checked');
78
                                
79
                                // get the hidden input field without the proxy
80
                                // and check the python form field
81
                                 hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
82
                                 $("input[name='"+hidden_name+"']").removeAttr('checked');  
83
                                
84
                        }
85
                }); 
86
                
87
                // clear hidden fields
88
                $(this).siblings('fieldset').find('input[type="text"]').each(function() {
89
                        hidden_name = $(this).attr('name').replace("_proxy","");
90
                         hidden_input = $("input[name='"+hidden_name+"']");
91
                         hidden_input.val('');
92
                });
93
                 
94
                 
95
        });
96
                  
97
        // if you fill _proxy fields do stuff 
98
        $('.quotas-form .quota input[type="text"]').change(function () {
99
                 
100
                 
101
                  
102
                 // get value from input
103
                 var value = $(this).val();
104
                 
105
                 //get input name without _proxy
106
                 hidden_name = $(this).attr('name').replace("_proxy","");
107
                 var hidden_input = $("input[name='"+hidden_name+"']");
108
                 
109
                 if (value) {
110
                         // actions for humanize fields
111
                         if ($(this).hasClass('dehumanize')){
112
                                 
113
                                 var flag = 0;
114
                                 
115
                                 // replace , with .  and get number 
116
                                 value = value.replace(",",".");
117
                                 var num = parseFloat(value);
118
                                 
119
                                 if ( value && !num ) { flag = 1}
120
                                 
121
                                 var bytes = num;
122
                                 
123
                                  // get suffix. 'i' renders it case insensitive
124
                                 var suf = value.match( new RegExp('GB|KB|MB|TB|bytes|G|K|M|T|byte', 'i'));
125
                                 if (suf){
126
                                         
127
                                         suf = suf[0].toLowerCase(); 
128
                                         suf = suf.substr(0,1);
129
                                 
130
                                         // transform to bytes
131
                                         switch (suf){
132
                                                 case 'b': 
133
                                                   bytes = num*Math.pow(1024,0);
134
                                                   break;
135
                                                 case 'k':
136
                                                   bytes = num*Math.pow(1024,1);
137
                                                   break;
138
                                                 case 'm':
139
                                                   bytes = num*Math.pow(1024,2);
140
                                                   break;
141
                                                 case 'g':
142
                                                   bytes = num*Math.pow(1024,3);
143
                                                   break;
144
                                                 case 't':
145
                                                   bytes = num*Math.pow(1024,4);
146
                                                   break;    
147
                                                 default:
148
                                                   bytes = num; 
149
                                         }
150
                                 } else {
151
                                         if (value) {
152
                                          flag = 1;
153
                                         }
154
                                          
155
                                 }
156
                                 
157
                                 if ( flag == '1' ){ 
158
                                         $(this).parents('.form-row').addClass('with-errors');
159
                                         bytes = value;
160
                                         $(this).focus();
161
                                         
162
                                          
163
                                 } else {
164
                                         $(this).parents('.form-row').removeClass('with-errors');
165
                                 }
166
                                 
167
                                 hidden_input.val(bytes);
168
                                 
169
                         }
170
                          
171
                         // actions for int fields
172
                         else {
173
        
174
                                 var is_int = value.match (new RegExp('^[0-9]*$'));
175
                                 if ( !is_int ){ 
176
                                         $(this).parents('.form-row').find('.error-msg').html('Enter a whole number');
177
                                         $(this).parents('.form-row').addClass('with-errors');
178
                                          
179
                                 } else {
180
                                         if ( value == '0'){
181
                                                 $(this).parents('.form-row').find('.error-msg').html('Ensure this value is greater than or equal to 1');
182
                                                 $(this).parents('.form-row').addClass('with-errors');
183
                                         }else {
184
                                                 $(this).parents('.form-row').removeClass('with-errors');
185
                                         }
186
                                         
187
                                         
188
                                 }
189
                                 hidden_input.val(value);
190
        
191
                         }
192
                 
193
                 } else {
194
                         hidden_input.removeAttr('value');
195
                 }
196
                 
197
         });
198
         
199
        
200
        // if hidden checkboxes are checked, the right group is selected 
201
        $('.with-info input[name^="is_selected_"]').each(function() {
202
                if ($(this).attr('checked')){
203
                        
204
                        // get hidden input name
205
                        hidden_name = $(this).attr('name');
206
                        $("input[name='proxy_"+hidden_name+"']").attr('checked','checked'); 
207
                        
208
                        // pretend to check the ul li a
209
                        // show the relevant fieldsets
210
                        var mock_a = $("input[name='proxy_"+hidden_name+"']").siblings('a');
211
                        group_form_show_resources(mock_a);
212
                         
213
                }
214
        }); 
215
        
216
        
217
        
218
        // if input_uplimit fields are filled,
219
        // fill the _uplimit_proxy ones
220
         
221
        $('.with-info input[name$="_uplimit"]').each(function() {
222
                if ($(this).val()){
223
                        
224
                        // get value from input
225
                         var value = $(this).val();
226
                        
227
                        
228
                        // get hidden input name
229
                        hidden_name = $(this).attr('name');
230
                        var field = $("input[name='"+hidden_name+"_proxy']"); 
231
                        
232
                        
233
                        if ( (field.hasClass('dehumanize')) && !($(this).parents('.form-row').hasClass('with-errors'))) {
234
                                // for dehumanize fields transform bytes to KB, MB, etc
235
                                // unless there is an error
236
                                field.val(bytesToSize2(value))
237
                        } else {
238
                                // else just return the value
239
                                field.val(value);        
240
                        }
241
                        
242
                        var group_class = field.parents('div[class^="group"]').attr('class').replace('group ', '');
243
                        
244
                         
245
                         
246
                        
247
                        // select group icon
248
                        $('.quotas-form ul li a').each(function() {
249
                                
250
                                if($(this).attr('id') == group_class) {
251
                                        $(this).addClass('selected');
252
                                        $(this).siblings('input[type="hidden"]').attr('checked', 'checked');
253
                                        
254
                                        // get the hidden input field without the proxy
255
                                        // and check the python form field
256
                                         hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_","");
257
                                         $("input[name='"+hidden_name+"']").attr('checked', 'checked');  
258
                                         
259
                                         group_form_show_resources($(this));
260
                                        
261
                                }
262
                        }); 
263
                        
264
                
265
                        
266
                        // if the field has class error, transfer error to the proxy fields
267
                        if ( $(this).parents('.form-row').hasClass('with-errors') ) {
268
                                field.parents('.form-row').addClass('with-errors');
269
                        }
270
                        
271
                         
272
                }
273
        }); 
274
        
275
});