root / snf-astakos-app / astakos / im / static / im / js / quotas.js @ 2934e509
History | View | Annotate | Download (8.7 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"]').val("1"); |
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+"']").val("1"); |
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"]').val('0'); |
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+"']").val('0'); |
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 |
|
102 |
// if you fill _proxy fields do stuff
|
103 |
$('.quotas-form .quota input[type="text"]').change(function () { |
104 |
|
105 |
if ( $('#icons span.info').hasClass('error-msg')){ |
106 |
$('#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.'); |
107 |
} |
108 |
|
109 |
// get value from input
|
110 |
var value = $(this).val(); |
111 |
|
112 |
//get input name without _proxy
|
113 |
hidden_name = $(this).attr('name').replace("_proxy",""); |
114 |
var hidden_input = $("input[name='"+hidden_name+"']"); |
115 |
|
116 |
if (value) {
|
117 |
|
118 |
// actions for humanize fields
|
119 |
if ($(this).hasClass('dehumanize')){ |
120 |
|
121 |
var flag = 0; |
122 |
|
123 |
// check if the value is not float
|
124 |
var num_float = parseFloat(value);
|
125 |
num_float= String(num_float); |
126 |
|
127 |
if (num_float.indexOf(".") == 1){ |
128 |
flag = 1 ;
|
129 |
msg="Please enter an integer";
|
130 |
} else {
|
131 |
var num = parseInt(value);
|
132 |
if ( num == '0' ) { |
133 |
flag = 1 ; msg="zero" |
134 |
} else {
|
135 |
if ( value && !num ) { flag = 1 ; msg="Invalid format. Try something like 10GB, 2MB etc"} |
136 |
|
137 |
var bytes = num;
|
138 |
|
139 |
// remove any numbers and get suffix
|
140 |
var suffix = value.replace( num, ''); |
141 |
|
142 |
// validate suffix. 'i' renders it case insensitive
|
143 |
var suf = suffix.match( new RegExp('^(GB|KB|MB|TB|bytes|G|K|M|T|byte)$', 'i')); |
144 |
if (suf){
|
145 |
|
146 |
suf = suf[0].toLowerCase();
|
147 |
suf = suf.substr(0,1); |
148 |
|
149 |
// transform to bytes
|
150 |
switch (suf){
|
151 |
case 'b': |
152 |
bytes = num*Math.pow(1024,0); |
153 |
break;
|
154 |
case 'k': |
155 |
bytes = num*Math.pow(1024,1); |
156 |
break;
|
157 |
case 'm': |
158 |
bytes = num*Math.pow(1024,2); |
159 |
break;
|
160 |
case 'g': |
161 |
bytes = num*Math.pow(1024,3); |
162 |
break;
|
163 |
case 't': |
164 |
bytes = num*Math.pow(1024,4); |
165 |
break;
|
166 |
default:
|
167 |
bytes = num; |
168 |
} |
169 |
} else {
|
170 |
if (num) {
|
171 |
flag = 1;
|
172 |
msg ="You must specify correct units"
|
173 |
} |
174 |
|
175 |
} |
176 |
} |
177 |
} |
178 |
|
179 |
if ( flag == '1' ){ |
180 |
$(this).parents('.form-row').addClass('with-errors'); |
181 |
$(this).parents('.form-row').find('.error-msg').html(msg); |
182 |
bytes = value; |
183 |
$(this).focus(); |
184 |
|
185 |
|
186 |
} else {
|
187 |
$(this).parents('.form-row').removeClass('with-errors'); |
188 |
} |
189 |
|
190 |
hidden_input.val(bytes); |
191 |
|
192 |
|
193 |
} |
194 |
|
195 |
// validation actions for int fields
|
196 |
else {
|
197 |
|
198 |
var is_int = value.match (new RegExp('^[0-9]*$')); |
199 |
if ( !is_int ){
|
200 |
$(this).parents('.form-row').find('.error-msg').html('Enter a positive integer'); |
201 |
$(this).parents('.form-row').addClass('with-errors'); |
202 |
|
203 |
} else {
|
204 |
if ( value == '0'){ |
205 |
$(this).parents('.form-row').find('.error-msg').html('Ensure this value is greater than or equal to 1'); |
206 |
$(this).parents('.form-row').addClass('with-errors'); |
207 |
}else {
|
208 |
$(this).parents('.form-row').removeClass('with-errors'); |
209 |
} |
210 |
|
211 |
|
212 |
} |
213 |
hidden_input.val(value); |
214 |
|
215 |
} |
216 |
|
217 |
} else {
|
218 |
; $(this).parents('.with-errors').removeClass('with-errors') |
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).val()) == 1 ){ |
229 |
|
230 |
// get hidden input name
|
231 |
hidden_name = $(this).attr('name'); |
232 |
$("input[name='proxy_"+hidden_name+"']").val("1"); |
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 |
$('.group input[name$="_uplimit_proxy"]').each(function() { |
245 |
if ($(this).val()){ |
246 |
|
247 |
// get value from input
|
248 |
var value = $(this).val(); |
249 |
|
250 |
|
251 |
// get hidden input name
|
252 |
hidden_name = $(this).attr('name'); |
253 |
hidden_field_name = hidden_name.replace("_proxy",""); |
254 |
$("input[name='"+hidden_field_name+"']").val(value); |
255 |
var field = $(this); |
256 |
|
257 |
|
258 |
if ( (field.hasClass('dehumanize')) && !($(this).parents('.form-row').hasClass('with-errors'))) { |
259 |
// for dehumanize fields transform bytes to KB, MB, etc
|
260 |
// unless there is an error
|
261 |
field.val(bytesToSize2(value)) |
262 |
} else {
|
263 |
// else just return the value
|
264 |
field.val(value); |
265 |
} |
266 |
|
267 |
var group_class = field.parents('div[class^="group"]').attr('class').replace('group ', ''); |
268 |
|
269 |
|
270 |
|
271 |
|
272 |
// select group icon
|
273 |
$('.quotas-form ul li a').each(function() { |
274 |
|
275 |
if($(this).attr('id') == group_class) { |
276 |
$(this).addClass('selected'); |
277 |
$(this).siblings('input[type="hidden"]').val("1"); |
278 |
|
279 |
// get the hidden input field without the proxy
|
280 |
// and check the python form field
|
281 |
hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_",""); |
282 |
$("input[name='"+hidden_name+"']").val("1"); |
283 |
|
284 |
group_form_show_resources($(this)); |
285 |
|
286 |
} |
287 |
}); |
288 |
|
289 |
|
290 |
|
291 |
// if the field has class error, transfer error to the proxy fields
|
292 |
if ( $(this).parents('.form-row').hasClass('with-errors') ) { |
293 |
field.parents('.form-row').addClass('with-errors'); |
294 |
} |
295 |
|
296 |
|
297 |
} |
298 |
}); |
299 |
|
300 |
function goToByScroll(id){ |
301 |
$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow'); |
302 |
} |
303 |
|
304 |
|
305 |
$('#group_create_form').submit(function(){ |
306 |
|
307 |
var flag = 0; |
308 |
$('.quotas-form .group input[type="text"]').each(function() { |
309 |
var value = $(this).val(); |
310 |
if (value){
|
311 |
flag = 1;
|
312 |
} |
313 |
}); |
314 |
|
315 |
console.info(flag); |
316 |
|
317 |
if (flag =='0') { |
318 |
$('#icons').focus(); |
319 |
$('#icons span.info').addClass('error-msg'); |
320 |
$('#icons span.info').find('span').html('You must fill in at least one resource'); |
321 |
goToByScroll("icons");
|
322 |
|
323 |
return false; |
324 |
|
325 |
} |
326 |
if ($('.quotas-form .group .form-row.with-errors').length>0 ){ |
327 |
$('.quotas-form .group .form-row.with-errors input[type="text"]')[0].focus(); |
328 |
return false; |
329 |
|
330 |
} |
331 |
|
332 |
|
333 |
|
334 |
}); |
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
}); |