root / snf-astakos-app / astakos / im / static / im / js / quotas.js @ d692d8f8
History | View | Annotate | Download (8.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 |
|
8 |
//$(this).appendTo('.visible');
|
9 |
$(this).show('slow'); |
10 |
|
11 |
$(this).find('input').first().focus(); |
12 |
console.info($(this).find('input').first()); |
13 |
|
14 |
|
15 |
} |
16 |
}); |
17 |
//egg = $('.visible').find('.'+id).find('input').first();
|
18 |
|
19 |
if ($('.quotas-form .with-info .with-errors input[type="text"]')){ |
20 |
$(this)[0].focus(); |
21 |
} |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
//setTimeout(function() { document.getElementById("city").focus(); }, 100);
|
27 |
|
28 |
} |
29 |
|
30 |
|
31 |
function bytesToSize2(bytes) { |
32 |
var sizes = [ 'n/a', 'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
33 |
var i = +Math.floor(Math.log(bytes) / Math.log(1024)); |
34 |
return (bytes / Math.pow(1024, i)).toFixed( 0 ) + sizes[ isNaN( bytes ) ? 0 : i+1 ]; |
35 |
} |
36 |
|
37 |
|
38 |
function goToByScroll(id){ |
39 |
$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow'); |
40 |
} |
41 |
|
42 |
$(document).ready(function() { |
43 |
|
44 |
|
45 |
|
46 |
// ugly fix to transfer data easily
|
47 |
$('.with-info input[name^="is_selected_"]').each(function() { |
48 |
$(this).parents('.form-row').hide(); |
49 |
}); |
50 |
|
51 |
$('.quotas-form ul li a').click(function(e){ |
52 |
|
53 |
// check the hidden input field
|
54 |
$(this).siblings('input[type="hidden"]').val("1"); |
55 |
|
56 |
// get the hidden input field without the proxy
|
57 |
// and check the python form field
|
58 |
hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_",""); |
59 |
$("input[name='"+hidden_name+"']").val("1"); |
60 |
|
61 |
// prevent extra actions if it is checked
|
62 |
if ( $(this).hasClass('selected')){ |
63 |
e.preventDefault(); |
64 |
} else {
|
65 |
|
66 |
// show the relevant fieldsets
|
67 |
group_form_show_resources($(this)); |
68 |
} |
69 |
}); |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
$('.quotas-form .group .delete').click(function(e){ |
77 |
|
78 |
e.preventDefault(); |
79 |
|
80 |
// clear form fields
|
81 |
$(this).siblings('fieldset').find('input').val(''); |
82 |
|
83 |
// clear errors
|
84 |
$(this).siblings('fieldset').find('.form-row').removeClass('with-errors'); |
85 |
|
86 |
// hide relevant fieldset
|
87 |
$(this).parents('.group').hide('slow', function() { |
88 |
//$(this).appendTo('.not-visible');
|
89 |
$(this).hide(); |
90 |
}); |
91 |
|
92 |
group_class = $(this).parents('.group').attr('class').replace('group ', ''); |
93 |
|
94 |
// unselect group icon
|
95 |
$('.quotas-form ul li a').each(function() { |
96 |
if($(this).attr('id')==group_class) { |
97 |
$(this).removeClass('selected'); |
98 |
$(this).siblings('input[type="hidden"]').val('0'); |
99 |
|
100 |
// get the hidden input field without the proxy
|
101 |
// and check the python form field
|
102 |
hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_",""); |
103 |
$("input[name='"+hidden_name+"']").val('0'); |
104 |
|
105 |
} |
106 |
}); |
107 |
|
108 |
// clear hidden fields
|
109 |
$(this).siblings('fieldset').find('input[type="text"]').each(function() { |
110 |
hidden_name = $(this).attr('name').replace("_proxy",""); |
111 |
hidden_input = $("input[name='"+hidden_name+"']"); |
112 |
hidden_input.val('');
|
113 |
}); |
114 |
|
115 |
|
116 |
}); |
117 |
|
118 |
|
119 |
// if you fill _proxy fields do stuff
|
120 |
$('.quotas-form .quota input[type="text"]').keyup(function () { |
121 |
|
122 |
if ( $('#icons span.info').hasClass('error-msg')){ |
123 |
$('#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.'); |
124 |
} |
125 |
|
126 |
// get value from input
|
127 |
var value = $(this).val(); |
128 |
|
129 |
//get input name without _proxy
|
130 |
hidden_name = $(this).attr('name').replace("_proxy",""); |
131 |
var hidden_input = $("input[name='"+hidden_name+"']"); |
132 |
|
133 |
if (value) {
|
134 |
|
135 |
// actions for humanize fields
|
136 |
if ($(this).hasClass('dehumanize')){ |
137 |
|
138 |
var flag = 0; |
139 |
|
140 |
// check if the value is not float
|
141 |
var num_float = parseFloat(value);
|
142 |
num_float= String(num_float); |
143 |
|
144 |
if (num_float.indexOf(".") == 1){ |
145 |
flag = 1 ;
|
146 |
msg="Please enter an integer";
|
147 |
} else {
|
148 |
var num = parseInt(value);
|
149 |
if ( num == '0' ) { |
150 |
flag = 1 ; msg="This value can not be zero. Try something like 10GB, 2MB etc" |
151 |
} else {
|
152 |
if ( value && !num ) { flag = 1 ; msg="Invalid format. Try something like 10GB, 2MB etc"} |
153 |
|
154 |
var bytes = num;
|
155 |
|
156 |
// remove any numbers and get suffix
|
157 |
var suffix = value.replace( num, ''); |
158 |
|
159 |
// validate suffix. 'i' renders it case insensitive
|
160 |
var suf = suffix.match( new RegExp('^(GB|KB|MB|TB|bytes|G|K|M|T|byte)$', 'i')); |
161 |
if (suf){
|
162 |
|
163 |
suf = suf[0].toLowerCase();
|
164 |
suf = suf.substr(0,1); |
165 |
|
166 |
// transform to bytes
|
167 |
switch (suf){
|
168 |
case 'b': |
169 |
bytes = num*Math.pow(1024,0); |
170 |
break;
|
171 |
case 'k': |
172 |
bytes = num*Math.pow(1024,1); |
173 |
break;
|
174 |
case 'm': |
175 |
bytes = num*Math.pow(1024,2); |
176 |
break;
|
177 |
case 'g': |
178 |
bytes = num*Math.pow(1024,3); |
179 |
break;
|
180 |
case 't': |
181 |
bytes = num*Math.pow(1024,4); |
182 |
break;
|
183 |
default:
|
184 |
bytes = num; |
185 |
} |
186 |
} else {
|
187 |
if (num) {
|
188 |
flag = 1;
|
189 |
msg ="You must specify correct units. Try something like 10GB, 2MB etc"
|
190 |
} |
191 |
|
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
if ( flag == '1' ){ |
197 |
$(this).parents('.form-row').addClass('with-errors'); |
198 |
$(this).parents('.form-row').find('.error-msg').html(msg); |
199 |
bytes = value; |
200 |
$(this).focus(); |
201 |
|
202 |
|
203 |
} else {
|
204 |
$(this).parents('.form-row').removeClass('with-errors'); |
205 |
} |
206 |
|
207 |
hidden_input.val(bytes); |
208 |
|
209 |
|
210 |
} |
211 |
|
212 |
// validation actions for int fields
|
213 |
else {
|
214 |
var is_int = value.match (new RegExp('^[1-9][0-9]*$')); |
215 |
if ( !is_int ){
|
216 |
$(this).parents('.form-row').find('.error-msg').html('Enter a positive integer'); |
217 |
$(this).parents('.form-row').addClass('with-errors'); |
218 |
|
219 |
} else {
|
220 |
if ( value == '0'){ |
221 |
$(this).parents('.form-row').find('.error-msg').html('Ensure this value is greater than or equal to 1'); |
222 |
$(this).parents('.form-row').addClass('with-errors'); |
223 |
}else {
|
224 |
$(this).parents('.form-row').removeClass('with-errors'); |
225 |
} |
226 |
|
227 |
|
228 |
} |
229 |
hidden_input.val(value); |
230 |
|
231 |
} |
232 |
|
233 |
} else {
|
234 |
; $(this).parents('.with-errors').removeClass('with-errors') |
235 |
hidden_input.removeAttr('value');
|
236 |
} |
237 |
$('#icons span.info').removeClass('error-msg'); |
238 |
|
239 |
}); |
240 |
|
241 |
|
242 |
// if hidden checkboxes are checked, the right group is selected
|
243 |
$('.with-info input[name^="is_selected_"]').each(function() { |
244 |
if ( ($(this).val()) == 1 ){ |
245 |
|
246 |
// get hidden input name
|
247 |
hidden_name = $(this).attr('name'); |
248 |
$("input[name='proxy_"+hidden_name+"']").val("1"); |
249 |
|
250 |
// pretend to check the ul li a
|
251 |
// show the relevant fieldsets
|
252 |
var mock_a = $("input[name='proxy_"+hidden_name+"']").siblings('a'); |
253 |
group_form_show_resources(mock_a); |
254 |
|
255 |
} |
256 |
}); |
257 |
|
258 |
|
259 |
|
260 |
$('.group input[name$="_uplimit_proxy"]').each(function() { |
261 |
if ($(this).val()){ |
262 |
|
263 |
// get value from input
|
264 |
var value = $(this).val(); |
265 |
|
266 |
|
267 |
// get hidden input name
|
268 |
hidden_name = $(this).attr('name'); |
269 |
hidden_field_name = hidden_name.replace("_proxy",""); |
270 |
$("input[name='"+hidden_field_name+"']").val(value); |
271 |
var field = $(this); |
272 |
|
273 |
|
274 |
if ( (field.hasClass('dehumanize')) && !($(this).parents('.form-row').hasClass('with-errors'))) { |
275 |
// for dehumanize fields transform bytes to KB, MB, etc
|
276 |
// unless there is an error
|
277 |
field.val(bytesToSize2(value)) |
278 |
} else {
|
279 |
// else just return the value
|
280 |
field.val(value); |
281 |
} |
282 |
|
283 |
var group_class = field.parents('div[class^="group"]').attr('class').replace('group ', ''); |
284 |
|
285 |
|
286 |
|
287 |
|
288 |
// select group icon
|
289 |
$('.quotas-form ul li a').each(function() { |
290 |
|
291 |
if($(this).attr('id') == group_class) { |
292 |
$(this).addClass('selected'); |
293 |
$(this).siblings('input[type="hidden"]').val("1"); |
294 |
|
295 |
// get the hidden input field without the proxy
|
296 |
// and check the python form field
|
297 |
hidden_name = $(this).siblings('input[type="hidden"]').attr('name').replace("proxy_",""); |
298 |
$("input[name='"+hidden_name+"']").val("1"); |
299 |
|
300 |
group_form_show_resources($(this)); |
301 |
|
302 |
} |
303 |
}); |
304 |
|
305 |
|
306 |
|
307 |
// if the field has class error, transfer error to the proxy fields
|
308 |
if ( $(this).parents('.form-row').hasClass('with-errors') ) { |
309 |
field.parents('.form-row').addClass('with-errors'); |
310 |
} |
311 |
|
312 |
|
313 |
} |
314 |
}); |
315 |
|
316 |
|
317 |
|
318 |
|
319 |
$('#group_create_form').submit(function(){ |
320 |
|
321 |
var flag = 0; |
322 |
$('.quotas-form .group input[type="text"]').each(function() { |
323 |
var value = $(this).val(); |
324 |
if (value){
|
325 |
flag = 1;
|
326 |
} |
327 |
}); |
328 |
|
329 |
console.info(flag); |
330 |
|
331 |
if (flag =='0') { |
332 |
$('#icons').focus(); |
333 |
$('#icons span.info').addClass('error-msg'); |
334 |
$('#icons span.info').find('span').html('You must fill in at least one resource'); |
335 |
goToByScroll("icons");
|
336 |
|
337 |
return false; |
338 |
|
339 |
} |
340 |
|
341 |
|
342 |
|
343 |
}); |
344 |
|
345 |
//goToByScroll("top");
|
346 |
$('.quotas-form .form-row.with-errors input[type="text"]').first().focus(); |
347 |
|
348 |
|
349 |
|
350 |
|
351 |
}); |