Revision bdc1c103 snf-astakos-app/astakos/im/static/im/js/quotas.js

b/snf-astakos-app/astakos/im/static/im/js/quotas.js
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

  
1 22
$(document).ready(function() {
2 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
    
3 31
	$('.quotas-form ul li a').click(function(e){
4
		$(this).siblings('input[type="hidden"]').val('1');
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		 
5 42
		if ( $(this).hasClass('selected')){
6 43
			e.preventDefault();
7
		}
8
		if ( !$(this).hasClass('selected')){
9
			$(this).addClass('selected');
10
			var id = $(this).attr('id');
11
			$('.quotas-form .group').each(function() {
12
				if( $(this).hasClass(id) ) {
13
					$(this).appendTo('.foo');
14
					$(this).show('slow');
15
					$(this).find('input')[0].focus()
16
				}
17
			});
44
		} else {
45
			
46
			// show the relevant fieldsets
47
			group_form_show_resources($(this));
18 48
		}   
19 49
	});
20 50
	
51
	 
52
	
53
	
54
	 
55
	
21 56
	$('.quotas-form .group .delete').click(function(e){
57
		
22 58
		e.preventDefault(); 
59
		
60
		// clear form fields
23 61
		$(this).siblings('fieldset').find('input').val('');
24 62
		
25
		$(this).siblings('fieldset').find('.checkbox-widget.unlimited').addClass('checked');
26
		$(this).siblings('fieldset').find('.checkbox-widget.limited').removeClass('checked');
27
		$(this).siblings('fieldset').find('input[type="checkbox"].limited').removeAttr('checked');  
28
		$(this).siblings('fieldset').find('input[type="checkbox"].unlimited').attr('checked','checked');  
29
		$(this).siblings('fieldset').find('.double-checks input[type="text"]').hide();
63
		// clear errors
64
		$(this).siblings('fieldset').find('.form-row').removeClass('with-errors');
65
		 
66
		// hide relevant fieldset 
30 67
		$(this).parents('.group').hide('slow', function() {
31 68
		    $(this).appendTo('.not-foo');	
32 69
		});
33
		groupClass = $(this).parents('.group').attr('class').replace('group ', '');
70
		
71
		group_class = $(this).parents('.group').attr('class').replace('group ', '');
72
		
73
		// unselect group icon
34 74
		$('.quotas-form ul li a').each(function() {
35
			if($(this).attr('id')==groupClass) {
75
			if($(this).attr('id')==group_class) {
36 76
				$(this).removeClass('selected');
37
				$(this).siblings('input[type="hidden"]').val('0');
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
				
38 84
			}
39 85
		}); 
40
		 
41
		 
42
	});
43
	 
44
		
45
	$('.quotas-form input.limited').bind("changed", function(e){
46
		console.log($(this).attr('checked'));
47
		if ($(this).attr('checked')){
48
			$(this).siblings('input[type="text"]').toggle();
49
			$(this).siblings('input[type="text"]').focus();
50
		 	parentdiv = $(this).parents('.form-row').prev('.form-row');
51
		 	parentdiv.find('input[type="checkbox"].unlimited').removeAttr('checked');  
52
		 	parentdiv.find('.checkbox-widget').removeClass('checked');
53
		}
54 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
		 
55 94
		 
56 95
	});
57
	
58
	$('.quotas-form input.unlimited').bind("changed", function(e){
59
		parentdiv = $(this).parents('.form-row').next('.form-row');
60
		if (parentdiv.find('.checkbox-widget').hasClass('checked')){
61
			parentdiv.find('.checkbox-widget').removeClass('checked');
62
			parentdiv.find('input[type="checkbox"].limited').removeAttr('checked');  
63
			parentdiv.find('input[type="text"]').val('');	
64
			parentdiv.find('input[type="text"]').hide();	
65
		}
66
		
67
		
68
		
69
	});
70

  
71
    
72
	
73
	 
74
	 
96
	 	 
97
	// if you fill _proxy fields do stuff 
75 98
	$('.quotas-form .quota input[type="text"]').change(function () {
76 99
	 	
77 100
	 	
......
83 106
	 	hidden_name = $(this).attr('name').replace("_proxy","");
84 107
	 	var hidden_input = $("input[name='"+hidden_name+"']");
85 108
	 	
86
	 	
87
	 	if ($(this).hasClass('dehumanize')){
88
	 		
89
	 		var flag = 0;
90
	 		
91
	 		// replace , with .  and get number 
92
		 	value = value.replace(",",".");
93
		 	var num = parseFloat(value);
94
		 	
95
		 	if ( !num ) { flag = 1}
96
		 	
97
		 	var bytes = num;
98
	 		
99
	 		 // get suffix. 'i' renders it case insensitive
100
		 	var suf = value.match( new RegExp('GB|KB|MB|TB|bytes', 'i'));
101
		 	if (suf){
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;
102 122
		 		
103
		 		suf = suf[0].toLowerCase(); 
104
		 	
105
			 	// transform to bytes
106
			 	switch (suf){
107
			 		case 'bytes': 
108
			 		  bytes = num*Math.pow(1024,0);
109
			 		  break;
110
			 		case 'byte': 
111
			 		  bytes = num*Math.pow(1024,0);
112
			 		  break;
113
			 		case 'kb':
114
			 		  bytes = num*Math.pow(1024,1);
115
			 		  break;
116
			 		case 'mb':
117
			 		  bytes = num*Math.pow(1024,2);
118
			 		  break;
119
			 		case 'gb':
120
			 		  bytes = num*Math.pow(1024,3);
121
			 		  break;
122
			 		case 'tb':
123
			 		  bytes = num*Math.pow(1024,4);
124
			 		  break;    
125
			 		default:
126
			 		  bytes = num; 
127
		 		}
128
		 	} else {
129
		 		 flag = 1;
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
			 	
130 169
		 	}
131
		 	
132
		 	if ( flag == '1' ){ 
133
		 		$(this).parents('.form-row').addClass('with-errors');
134
		 		return;
135
		 		 
136
		 	} else {
137
		 		$(this).parents('.form-row').removeClass('with-errors');
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
	
138 191
		 	}
139
		 	
140
		 	machine_value = bytes;
141
		 	hidden_input.val(bytes);
142
		 	
192
	 	
143 193
	 	} else {
144

  
145
	 		var is_int = value.match (new RegExp('^[0-9]*$'));
146
	 		if ( !is_int ){ 
147
	 			$(this).parents('.form-row').find('.error-msg').html('Enter a whole number');
148
		 		$(this).parents('.form-row').addClass('with-errors');
149
		 		 
150
		 	} else {
151
		 		$(this).parents('.form-row').removeClass('with-errors');
152
		 		hidden_input.val(value);
153
		 	}
154

  
194
	 		hidden_input.removeAttr('value');
155 195
	 	}
156 196
	 	
157
	 	
158
	 	
159 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
	}); 
160 274
	
161 275
});

Also available in: Unified diff