Project

General

Profile

Download (13.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/* ====================================================================
2
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
3
 *
4
 *	Redistribution and use in source and binary forms, with or without modification,
5
 *	are permitted provided that the following conditions are met:
6
 *
7
 *	1. Redistributions of source code must retain the above copyright notice,
8
 *		this list of conditions and the following disclaimer.
9
 *
10
 *	2. Redistributions in binary form must reproduce the above copyright
11
 *		notice, this list of conditions and the following disclaimer in
12
 *		the documentation and/or other materials provided with the
13
 *		distribution.
14
 *
15
 *	3. All advertising materials mentioning features or use of this software
16
 *		must display the following acknowledgment:
17
 *		"This product includes software developed by the pfSense Project
18
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
19
 *
20
 *	4. The names "pfSense" and "pfSense Project" must not be used to
21
 *		 endorse or promote products derived from this software without
22
 *		 prior written permission. For written permission, please contact
23
 *		 coreteam@pfsense.org.
24
 *
25
 *	5. Products derived from this software may not be called "pfSense"
26
 *		nor may "pfSense" appear in their names without prior written
27
 *		permission of the Electric Sheep Fencing, LLC.
28
 *
29
 *	6. Redistributions of any form whatsoever must retain the following
30
 *		acknowledgment:
31
 *
32
 *	"This product includes software developed by the pfSense Project
33
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
34
 *
35
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
36
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
39
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
47
 *
48
 *	====================================================================
49
 *
50
 */
51

    
52
// These helper functions are used on many/most UI pages to hide/show/disable/enable form elements where required
53

    
54
// Hides the <div> in which the specified input element lives so that the input, its label and help text are hidden
55
function hideInput(id, hide) {
56
	if(hide)
57
		$('#' + id).parent().parent('div').addClass('hidden');
58
	else
59
		$('#' + id).parent().parent('div').removeClass('hidden');
60
}
61

    
62
// Hides the <div> in which the specified group input element lives so that the input,
63
// its label and help text are hidden
64
function hideGroupInput(id, hide) {
65
	if(hide)
66
		$('#' + id).parent('div').addClass('hidden');
67
	else
68
		$('#' + id).parent('div').removeClass('hidden');
69
}
70

    
71
// Hides the <div> in which the specified checkbox lives so that the checkbox, its label and help text are hidden
72
function hideCheckbox(id, hide) {
73
	if(hide)
74
		$('#' + id).parent().parent().parent('div').addClass('hidden');
75
	else
76
		$('#' + id).parent().parent().parent('div').removeClass('hidden');
77
}
78

    
79
// Disables the specified input element
80
function disableInput(id, disable) {
81
	$('#' + id).prop("disabled", disable);
82
}
83

    
84
// Hides all elements of the specified class. This will usually be a section
85
function hideClass(s_class, hide) {
86
	if(hide)
87
		$('.' + s_class).hide();
88
	else
89
		$('.' + s_class).show();
90
}
91

    
92
// Hides all elements of the specified class assigned to a group. This will usually be a group
93
function hideGroupClass(s_class, hide) {
94
	if(hide)
95
		$('.' + s_class).parent().parent().parent().hide();
96
	else
97
		$('.' + s_class).parent().parent().parent().show();
98
}
99

    
100
function hideSelect(id, hide) {
101
	if(hide)
102
		$('#' + id).parent('div').parent('div').addClass('hidden');
103
	else
104
		$('#' + id).parent('div').parent('div').removeClass('hidden');
105
}
106

    
107
function hideMultiCheckbox(id, hide) {
108
	if(hide)
109
		$("[name=" + id + "]").parent().addClass('hidden');
110
	else
111
		$("[name=" + id + "]").parent().removeClass('hidden');
112
}
113

    
114
// Hides the <div> in which the specified IP address element lives so that the input, its label and help text are hidden
115
function hideIpAddress(id, hide) {
116
	if(hide)
117
		$('#' + id).parent().parent().parent('div').addClass('hidden');
118
	else
119
		$('#' + id).parent().parent().parent('div').removeClass('hidden');
120
}
121

    
122
// Hides all elements of the specified class belonging to a multiselect.
123
function hideMultiClass(s_class, hide) {
124
	if(hide)
125
		$('.' + s_class).parent().parent().hide();
126
	else
127
		$('.' + s_class).parent().parent().show();
128
}
129

    
130
// Hides div whose label contains the specified text. (Good for StaticText)
131
function hideLabel(text, hide) {
132

    
133
	var element = $('label:contains(' + text + ')');
134

    
135
	if(hide)
136
		element.parent('div').addClass('hidden');
137
	else
138
		element.parent('div').removeClass('hidden');
139
}
140

    
141
// Toggle table row checkboxes and background colors on the pages that use sortable tables:
142
//	/usr/local/www/firewall_nat.php
143
//	/usr/local/www/firewall_nat_1to1.php
144
//	/usr/local/www/firewall_nat_out.php
145
//	/usr/local/www/firewall_rules.php
146
//	/usr/local/www/vpn_ipsec.php
147
// Striping of the tables is handled here, NOT with the Bootstrap table-striped class because it would
148
// get confused when rows are sorted or deleted.
149

    
150
function fr_toggle(id, prefix) {
151
	if (!prefix)
152
		prefix = 'fr';
153

    
154
	var checkbox = document.getElementById(prefix + 'c' + id);
155
	checkbox.checked = !checkbox.checked;
156
	fr_bgcolor(id, prefix);
157
}
158

    
159
// Change background color of selected row based on state of checkbox
160
function fr_bgcolor(id, prefix) {
161
	if (!prefix)
162
		prefix = 'fr';
163

    
164
	var row = $('#' + prefix + id);
165

    
166
	if ($('#' + prefix + 'c' + id).prop('checked') ) {
167
		row.addClass('active');
168
	} else {
169
		row.removeClass('active');
170
	}
171
}
172

    
173
// The following functions are used by Form_Groups assigned a class of "repeatable" and provide the ability
174
// to add/delete rows of sequentially numbered elements, their labels and their help text
175
// See firewall_aliases_edit.php for an example
176

    
177
// NOTE: retainhelp is a global var that defined prevents any help text from being deleted as lines are inserted.
178
// IOW it causes every row to have help text, not just the last row
179

    
180
function setMasks() {
181
	// Find all ipaddress masks and make dynamic based on address family of input
182
	$('span.pfIpMask + select').each(function (idx, select){
183
		var input = $(select).prevAll('input[type=text]');
184

    
185
		input.on('change', function(e){
186
			var isV6 = (input.val().indexOf(':') != -1), min = 0, max = 128;
187
			if (!isV6)
188
				max = 32;
189

    
190
			if (input.val() == "")
191
				return;
192

    
193
			while (select.options.length > max)
194
				select.remove(0);
195

    
196
			if (select.options.length < max)
197
			{
198
				for (var i=select.options.length; i<=max; i++)
199
					select.options.add(new Option(i, i), 0);
200
			}
201
		});
202

    
203
		// Fire immediately
204
		input.change();
205
	});
206
}
207

    
208
// Complicated function to move all help text associated with this input id to the same id
209
// on the row above. That way if you delete the last row, you don't lose the help
210
function moveHelpText(id) {
211
	$('#' + id).parent('div').parent('div').find('input').each(function() {	 // For each <span></span>
212
		var fromId = this.id;
213
		var toId = decrStringInt(fromId);
214
		var helpSpan;
215

    
216
		if(!$(this).hasClass('pfIpMask') && !$(this).hasClass('btn')) {
217
			if($('#' + decrStringInt(fromId)).parent('div').hasClass('input-group')) {
218
				helpSpan = $('#' + fromId).parent('div').parent('div').find('span:last').clone();
219
			} else {
220
				helpSpan = $('#' + fromId).parent('div').find('span:last').clone();
221
			}
222
			if($(helpSpan).hasClass('help-block')) {
223
				if($('#' + decrStringInt(fromId)).parent('div').hasClass('input-group')) {
224
					$('#' + decrStringInt(fromId)).parent('div').after(helpSpan);
225
				}
226
				else {
227
					$('#' + decrStringInt(fromId)).after(helpSpan);
228
				}
229
			}
230
		}
231
	});
232
}
233

    
234
// Increment the number at the end of the string
235
function bumpStringInt( str )	{
236
  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
237

    
238
  if( data )
239
	newStr = data[ 1 ] + ( Number( data[ 2 ] ) + 1 ) + data[ 3 ];
240

    
241
  return newStr || str;
242
}
243

    
244
// Decrement the number at the end of the string
245
function decrStringInt( str )	{
246
  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
247

    
248
  if( data )
249
	newStr = data[ 1 ] + ( Number( data[ 2 ] ) - 1 ) + data[ 3 ];
250

    
251
  return newStr || str;
252
}
253

    
254
// Called after a delete so that there are no gaps in the numbering. Most of the time the config system doesn't care about
255
// gaps, but I do :)
256
function renumber() {
257
	var idx = 0;
258

    
259
	$('.repeatable').each(function() {
260

    
261
		$(this).find('input').each(function() {
262
			$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
263
			$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
264
		});
265

    
266
		$(this).find('select').each(function() {
267
			$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
268
			$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
269
		});
270

    
271
		$(this).find('label').attr('for', $(this).find('label').attr('for').replace(/\d+$/, "") + idx);
272

    
273
		idx++;
274
	});
275
}
276

    
277
function delete_row(row) {
278
	$('#' + row).parent('div').parent('div').remove();
279
	renumber();
280
	checkLastRow();
281
}
282

    
283
function checkLastRow() {
284
	if($('.repeatable').length <= 1) {
285
		$('#deleterow0').hide();
286
	} else {
287
		$('[id^=deleterow]').show();
288
	}
289
}
290

    
291
function add_row() {
292
	// Find the last repeatable group
293
	var lastRepeatableGroup = $('.repeatable:last');
294

    
295
	// Clone it
296
	var newGroup = lastRepeatableGroup.clone();
297
	// Increment the suffix number for each input element in the new group
298
	$(newGroup).find('input').each(function() {
299
		$(this).prop("id", bumpStringInt(this.id));
300
		$(this).prop("name", bumpStringInt(this.name));
301
		if(!$(this).is('[id^=delete]'))
302
			$(this).val('');
303
	});
304

    
305
	// Do the same for selectors
306
	$(newGroup).find('select').each(function() {
307
		$(this).prop("id", bumpStringInt(this.id));
308
		$(this).prop("name", bumpStringInt(this.name));
309
		// If this selector lists mask bits, we need it to be reset to all 128 options
310
		// and no items selected, so that automatic v4/v6 selection still works
311
		if($(this).is('[id^=address_subnet]')) {
312
			$(this).empty();
313
			for(idx=128; idx>0; idx--) {
314
				$(this).append($('<option>', {
315
					value: idx,
316
					text: idx
317
				}));
318
			}
319
		}
320
	});
321

    
322
	// And for "for" tags
323
	$(newGroup).find('label').attr('for', bumpStringInt($(newGroup).find('label').attr('for')));
324
	$(newGroup).find('label').text(""); // Clear the label. We only want it on the very first row
325

    
326
	// Insert the updated/cloned row
327
	$(lastRepeatableGroup).after(newGroup);
328

    
329
	// Delete any help text from the group we have cloned
330
	$(lastRepeatableGroup).find('.help-block').each(function() {
331
		if((typeof retainhelp) == "undefined")
332
			$(this).remove();
333
	});
334

    
335
	setMasks();
336

    
337
	checkLastRow();
338

    
339
	// Autocomplete
340
	if ( typeof addressarray !== 'undefined') {
341
		$('[id^=address]').each(function() {
342
			if(this.id.substring(0, 8) != "address_") {
343
				$(this).autocomplete({
344
					source: addressarray
345
				});
346
			}
347
		});
348
	}
349

    
350
	// Now that we are no longer cloning the event handlers, we need to remove and re-add after a new row
351
	// has been added to the table
352
	$('[id^=delete]').unbind();
353
	$('[id^=delete]').click(function(event) {
354
		if($('.repeatable').length > 1) {
355
			if((typeof retainhelp) == "undefined")
356
				moveHelpText(event.target.id);
357

    
358
			delete_row(event.target.id);
359
		}
360
		else
361
			alert('You may not delete the last row!');
362
	});
363

    
364
}
365

    
366
// These are action buttons, not submit buttons
367
$('[id^=addrow]').prop('type','button');
368
$('[id^=delete]').prop('type','button');
369

    
370
// on click . .
371
$('[id^=addrow]').click(function() {
372
	add_row();
373
});
374

    
375
$('[id^=delete]').click(function(event) {
376
	if($('.repeatable').length > 1) {
377
		if((typeof retainhelp) == "undefined")
378
			moveHelpText(event.target.id);
379

    
380
		delete_row(event.target.id);
381
	}
382
	else
383
		alert('You may not delete the last row!');
384
});
385

    
386
// "More information" handlers
387

    
388
// If there is an infoblock, automatically add an info icon that toggles its display
389
if($('#infoblock').length != 0) {
390
	$('#infoblock').before('<i class="fa fa-info-circle icon-pointer" style="color: #337AB7;; font-size:20px; margin-left: 10px; margin-bottom: 10px;" id="showinfo" title="More information"></i>');
391

    
392
	// and remove the 'X' button from the last text box (Which we assume to be the infoblock)
393
	$('.close :last').remove();
394
}
395

    
396
// Hide information on page load
397
$('#infoblock').hide();
398

    
399
// Show the help on clicking the info icon
400
$('#showinfo').click(function() {
401
	$('#infoblock').toggle();
402
});
403

    
404
// Put a dummy row into any empty table to keep IE happy
405
$('tbody').each(function(){
406
	$(this).html($.trim($(this).html()))
407
});
408

    
409
$('tbody:empty').html("<tr><td></td></tr>");
410

    
411
// Hide configuration button for panels without configuration
412
$('.container .panel-heading a.config').each(function (idx, el){
413
	var config = $(el).parents('.panel').children('.panel-footer');
414
	if (config.length == 1)
415
		$(el).removeClass('hidden');
416
});
417

    
418
// Initial state & toggle icons of collapsed panel
419
$('.container .panel-heading a[data-toggle="collapse"]').each(function (idx, el){
420
	var body = $(el).parents('.panel').children('.panel-body')
421
	var isOpen = body.hasClass('in');
422

    
423
	$(el).children('i').toggleClass('fa-plus-circle', !isOpen);
424
	$(el).children('i').toggleClass('fa-minus-circle', isOpen);
425

    
426
	body.on('shown.bs.collapse', function(){
427
		$(el).children('i').toggleClass('fa-minus-circle', true);
428
		$(el).children('i').toggleClass('fa-plus-circle', false);
429

    
430
		if($(el).closest('a').attr('id') != 'widgets-available') {
431
			updateWidgets();
432
		}
433
	});
434

    
435
	body.on('hidden.bs.collapse', function(){
436
		$(el).children('i').toggleClass('fa-minus-circle', false);
437
		$(el).children('i').toggleClass('fa-plus-circle', true);
438

    
439
		if($(el).closest('a').attr('id') != 'widgets-available') {
440
			updateWidgets();
441
		}
442
	});
443
});
(4-4/4)