Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
142
// Hides the '/' and the subnet mask of an Ip_Address/subnet_mask group
143
function hideMask(name, hide) {
144
	if(hide) {
145
		$('[id^=' + name + ']').hide();
146
		$('[id^=' + name + ']').prev('span').hide();
147
		$('[id^=' + name + ']').parent('div').removeClass('input-group');
148
	} else {
149
		$('[id^=' + name + ']').show();
150
		$('[id^=' + name + ']').prev('span').show();
151
		$('[id^=' + name + ']').parent('div').addClass('input-group');
152
	}
153
}
154

    
155
// Toggle table row checkboxes and background colors on the pages that use sortable tables:
156
//	/usr/local/www/firewall_nat.php
157
//	/usr/local/www/firewall_nat_1to1.php
158
//	/usr/local/www/firewall_nat_out.php
159
//	/usr/local/www/firewall_rules.php
160
//	/usr/local/www/vpn_ipsec.php
161
// Striping of the tables is handled here, NOT with the Bootstrap table-striped class because it would
162
// get confused when rows are sorted or deleted.
163

    
164
function fr_toggle(id, prefix) {
165
	if (!prefix)
166
		prefix = 'fr';
167

    
168
	var checkbox = document.getElementById(prefix + 'c' + id);
169
	checkbox.checked = !checkbox.checked;
170
	fr_bgcolor(id, prefix);
171
}
172

    
173
// Change background color of selected row based on state of checkbox
174
function fr_bgcolor(id, prefix) {
175
	if (!prefix)
176
		prefix = 'fr';
177

    
178
	var row = $('#' + prefix + id);
179

    
180
	if ($('#' + prefix + 'c' + id).prop('checked') ) {
181
		row.addClass('active');
182
	} else {
183
		row.removeClass('active');
184
	}
185
}
186

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

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

    
194
function setMasks() {
195
	// Find all ipaddress masks and make dynamic based on address family of input
196
	$('span.pfIpMask + select').each(function (idx, select){
197
		var input = $(select).prevAll('input[type=text]');
198

    
199
		input.on('change', function(e){
200
			var isV6 = (input.val().indexOf(':') != -1), min = 0, max = 128;
201
			if (!isV6)
202
				max = 32;
203

    
204
			if (input.val() == "")
205
				return;
206

    
207
			while (select.options.length > max)
208
				select.remove(0);
209

    
210
			if (select.options.length < max) {
211
				for (var i=select.options.length; i<=max; i++)
212
					select.options.add(new Option(i, i), 0);
213
			}
214
		});
215

    
216
		// Fire immediately
217
		input.change();
218
	});
219
}
220

    
221
// Complicated function to move all help text associated with this input id to the same id
222
// on the row above. That way if you delete the last row, you don't lose the help
223
function moveHelpText(id) {
224

    
225
	$('#' + id).parent('div').parent('div').find('input, select, checkbox, button').each(function() {	 // For each <span></span>
226
		var fromId = this.id;
227
		var toId = decrStringInt(fromId);
228
		var helpSpan;
229

    
230

    
231
		if (!$(this).hasClass('pfIpMask') && !$(this).hasClass('btn') && ! fromId.startsWith("mask")) {
232
			if ($('#' + decrStringInt(fromId)).parent('div').hasClass('input-group')) {
233
				helpSpan = $('#' + fromId).parent('div').parent('div').find('span:last').clone();
234
			} else {
235
				helpSpan = $('#' + fromId).parent('div').find('span:last').clone();
236
			}
237

    
238
			if ($(helpSpan).hasClass('help-block')) {
239
				if ($('#' + decrStringInt(fromId)).parent('div').hasClass('input-group')) {
240
					$('#' + decrStringInt(fromId)).parent('div').after(helpSpan);
241
				} else {
242
					$('#' + decrStringInt(fromId)).after(helpSpan);
243
				}
244
			}
245
		}
246
	});
247
}
248

    
249
// Increment the number at the end of the string
250
function bumpStringInt( str )	{
251
  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
252

    
253
  if (data)
254
	newStr = data[ 1 ] + ( Number( data[ 2 ] ) + 1 ) + data[ 3 ];
255

    
256
  return newStr || str;
257
}
258

    
259
// Decrement the number at the end of the string
260
function decrStringInt( str )	{
261
  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
262

    
263
  if (data)
264
	newStr = data[ 1 ] + ( Number( data[ 2 ] ) - 1 ) + data[ 3 ];
265

    
266
  return newStr || str;
267
}
268

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

    
274
	$('.repeatable').each(function() {
275

    
276
		$(this).find('input').each(function() {
277
			$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
278
			$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
279
		});
280

    
281
		$(this).find('select').each(function() {
282
			$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
283
			$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
284
		});
285

    
286
		$(this).find('button').each(function() {
287
			$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
288
			$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
289
		});
290

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

    
293
		idx++;
294
	});
295
}
296

    
297
function delete_row(rowDelBtn) {
298
	var rowLabel;
299

    
300
	// If we are deleting row zero, we need to save/restore the label
301
	if (rowDelBtn == "deleterow0") {
302
		rowLabel = $('#' + rowDelBtn).parent('div').parent('div').find('label').text();
303
	}
304

    
305
	$('#' + rowDelBtn).parent('div').parent('div').remove();
306

    
307
	renumber();
308
	checkLastRow();
309

    
310
	if (rowDelBtn == "deleterow0") {
311
		$('#' + rowDelBtn).parent('div').parent('div').find('label').text(rowLabel);
312
	}
313
}
314

    
315
function checkLastRow() {
316
	if ($('.repeatable').length <= 1) {
317
		$('#deleterow0').hide();
318
	} else {
319
		$('[id^=deleterow]').show();
320
	}
321
}
322

    
323
function add_row() {
324
	// Find the last repeatable group
325
	var lastRepeatableGroup = $('.repeatable:last');
326

    
327
	// Clone it
328
	var newGroup = lastRepeatableGroup.clone();
329
	// Increment the suffix number for each input element in the new group
330
	$(newGroup).find('input').each(function() {
331
		$(this).prop("id", bumpStringInt(this.id));
332
		$(this).prop("name", bumpStringInt(this.name));
333
		if (!$(this).is('[id^=delete]'))
334
			$(this).val('');
335
	});
336

    
337
	// Increment the suffix number for the deleterow button element in the new group
338
	$(newGroup).find('[id^=deleterow]').each(function() {
339
		$(this).prop("id", bumpStringInt(this.id));
340
		$(this).prop("name", bumpStringInt(this.name));
341
	});
342

    
343
	// Do the same for selectors
344
	$(newGroup).find('select').each(function() {
345
		$(this).prop("id", bumpStringInt(this.id));
346
		$(this).prop("name", bumpStringInt(this.name));
347
		// If this selector lists mask bits, we need it to be reset to all 128 options
348
		// and no items selected, so that automatic v4/v6 selection still works
349
		if ($(this).is('[id^=address_subnet]')) {
350
			$(this).empty();
351
			for (idx=128; idx>0; idx--) {
352
				$(this).append($('<option>', {
353
					value: idx,
354
					text: idx
355
				}));
356
			}
357
		}
358
	});
359

    
360
	// And for "for" tags
361
//	$(newGroup).find('label').attr('for', bumpStringInt($(newGroup).find('label').attr('for')));
362

    
363
	$(newGroup).find('label').text(""); // Clear the label. We only want it on the very first row
364

    
365
	// Insert the updated/cloned row
366
	$(lastRepeatableGroup).after(newGroup);
367

    
368
	// Delete any help text from the group we have cloned
369
	$(lastRepeatableGroup).find('.help-block').each(function() {
370
		if ((typeof retainhelp) == "undefined")
371
			$(this).remove();
372
	});
373

    
374
	setMasks();
375

    
376
	checkLastRow();
377

    
378
	// Autocomplete
379
	if ( typeof addressarray !== 'undefined') {
380
		$('[id^=address]').each(function() {
381
			if (this.id.substring(0, 8) != "address_") {
382
				$(this).autocomplete({
383
					source: addressarray
384
				});
385
			}
386
		});
387
	}
388

    
389
	// Now that we are no longer cloning the event handlers, we need to remove and re-add after a new row
390
	// has been added to the table
391
	$('[id^=delete]').unbind();
392
	$('[id^=delete]').click(function(event) {
393
		if ($('.repeatable').length > 1) {
394
			if ((typeof retainhelp) == "undefined")
395
				moveHelpText(event.target.id);
396

    
397
			delete_row(event.target.id);
398
		} else {
399
			alert('The last row may not be deleted.');
400
		}
401
	});
402

    
403
}
404

    
405
// These are action buttons, not submit buttons
406
$('[id^=addrow]').prop('type','button');
407
$('[id^=delete]').prop('type','button');
408

    
409
// on click . .
410
$('[id^=addrow]').click(function() {
411
	add_row();
412
});
413

    
414
$('[id^=delete]').click(function(event) {
415
	if ($('.repeatable').length > 1) {
416
		if ((typeof retainhelp) == "undefined")
417
			moveHelpText(event.target.id);
418

    
419
		delete_row(event.target.id);
420
	} else {
421
		alert('The last row may not be deleted.');
422
	}
423
});
424

    
425
// "More information" handlers --------------------------------------------------------------------
426

    
427
// If there is an infoblock, automatically add an info icon that toggles its display
428

    
429
var sfx = 0;
430

    
431
$('.infoblock').each(function() {
432
	// If the block has the class "blockopen" it is initially open
433
	if (! $(this).hasClass("blockopen")) {
434
		$(this).hide();
435
	} else {
436
		$(this).removeClass("blockopen");
437
	}
438

    
439
	// Add the "i" icon before the infoblock, incrementing the icon id for each block (in case there are multiple infoblocks on a page)
440
	$(this).before('<i class="fa fa-info-circle icon-pointer" style="color: #337AB7; font-size:20px; margin-left: 10px; margin-bottom: 10px;" id="showinfo' + sfx.toString() + '" title="More information"></i>');
441
	$(this).removeClass("infoblock");
442
	$(this).addClass("infoblock" + sfx.toString());
443
	sfx++;
444
});
445

    
446
// Show the help on clicking the info icon
447
$('[id^="showinfo"]').click(function() {
448
	var id = $(this).attr("id");
449

    
450
	$('.' + "infoblock" + id.substr(8)).toggle();
451
	document.getSelection().removeAllRanges();		// Ensure the text is un-selected (Chrome browser quirk)
452
});
453
// ------------------------------------------------------------------------------------------------
454

    
455
// Put a dummy row into any empty table to keep IE happy
456
$('tbody').each(function(){
457
	$(this).html($.trim($(this).html()))
458
});
459

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

    
462
// Hide configuration button for panels without configuration
463
$('.container .panel-heading a.config').each(function (idx, el){
464
	var config = $(el).parents('.panel').children('.panel-footer');
465
	if (config.length == 1)
466
		$(el).removeClass('hidden');
467
});
468

    
469
// Initial state & toggle icons of collapsed panel
470
$('.container .panel-heading a[data-toggle="collapse"]').each(function (idx, el){
471
	var body = $(el).parents('.panel').children('.panel-body')
472
	var isOpen = body.hasClass('in');
473

    
474
	$(el).children('i').toggleClass('fa-plus-circle', !isOpen);
475
	$(el).children('i').toggleClass('fa-minus-circle', isOpen);
476

    
477
	body.on('shown.bs.collapse', function(){
478
		$(el).children('i').toggleClass('fa-minus-circle', true);
479
		$(el).children('i').toggleClass('fa-plus-circle', false);
480
	});
481

    
482
	body.on('hidden.bs.collapse', function(){
483
		$(el).children('i').toggleClass('fa-minus-circle', false);
484
		$(el).children('i').toggleClass('fa-plus-circle', true);
485
	});
486
});
487

    
488
// Separator bar stuff ------------------------------------------------------------------------
489

    
490
// Globals
491
gColor = 'bg-info';
492
newSeparator = false;
493
saving = false;
494
dirty = false;
495

    
496
$("#addsep").prop('type' ,'button');
497

    
498
$("#addsep").click(function() {
499
	if (newSeparator) {
500
		return(false);
501
	}
502

    
503
	gColor = 'bg-info';
504
	// Insert a temporary bar in which the user can enter some optional text
505
	sepcols = $( "#ruletable tr th" ).length - 2;
506

    
507
	$('#ruletable > tbody:last').append('<tr>' +
508
		'<td class="' + gColor + '" colspan="' + sepcols + '"><input id="newsep" placeholder="' + svbtnplaceholder + '" class="col-md-12" type="text" /></td>' +
509
		'<td class="' + gColor + '" colspan="2"><button class="btn btn-primary btn-sm" id="btnnewsep"><i class="fa fa-save icon-embed-btn"></i>' + svtxt + '</button>' +
510
		'<button class="btn btn-info btn-sm" id="btncncsep"><i class="fa fa-undo icon-embed-btn"></i>' + cncltxt + '</button>' +
511
		'&nbsp;&nbsp;&nbsp;&nbsp;' +
512
		'&nbsp;&nbsp;<a id="sepclrblue" value="bg-info"><i class="fa fa-circle text-info icon-pointer"></i></a>' +
513
		'&nbsp;&nbsp;<a id="sepclrred" value="bg-danger"><i class="fa fa-circle text-danger icon-pointer"></i></a>' +
514
		'&nbsp;&nbsp;<a id="sepclrgreen" value="bg-success"><i class="fa fa-circle text-success icon-pointer"></i></a>' +
515
		'&nbsp;&nbsp;<a id="sepclrorange" value="bg-warning"><i class="fa fa-circle text-warning icon-pointer"></i></button>' +
516
		'</td></tr>');
517

    
518
	$('#newsep').focus();
519
	newSeparator = true;
520

    
521
	$("#btnnewsep").prop('type' ,'button');
522

    
523
	// Watch escape and enter keys
524
	$('#newsep').keyup(function(e) {
525
		if (e.which == 27) {
526
			$('#btncncsep').trigger('click');
527
		}
528
	});
529

    
530
	$('#newsep').keypress(function(e) {
531
		if (e.which == 13) {
532
			$('#btnnewsep').trigger('click');
533
		}
534
	});
535

    
536
	handle_colors();
537

    
538
	// Replace the temporary separator bar with the final version containing the
539
	// user's text and a delete icon
540
	$("#btnnewsep").click(function() {
541
		var septext = escapeHtml($('#newsep').val());
542
		sepcols = $( "#ruletable tr th" ).length - 1;
543

    
544
		$(this).parents('tr').replaceWith('<tr class="ui-sortable-handle separator">' +
545
			'<td class="' + gColor + '" colspan="' + sepcols + '">' + '<span class="' + gColor + '">' + septext + '</span></td>' +
546
			'<td class="' + gColor + '"><a href="#"><i class="fa fa-trash sepdel"></i></a>' +
547
			'</td></tr>');
548

    
549
		$('#order-store').removeAttr('disabled');
550
		newSeparator = false;
551
		dirty = true;
552
	});
553

    
554
	// Cancel button
555
	$('#btncncsep').click(function(e) {
556
		e.preventDefault();
557
		$(this).parents('tr').remove();
558
		newSeparator = false;
559
	});
560
});
561

    
562
// Delete a separator row
563
$(function(){
564
	$('table').on('click','tr a .sepdel',function(e){
565
		e.preventDefault();
566
		$(this).parents('tr').remove();
567
		$('#order-store').removeAttr('disabled');
568
		dirty = true;
569
	});
570
});
571

    
572
// Compose an input array containing the row #, color and text for each separator
573
function save_separators() {
574
	var row = 0;
575
	var sepinput;
576
	var sepnum = 0;
577

    
578
	$('#ruletable > tbody > tr').each(function() {
579
		if ($(this).hasClass('separator')) {
580
			seprow = $(this).next('tr').attr("id");
581
			if (seprow == undefined) {
582
				seprow = "fr" + row;
583
			}
584

    
585
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][row]" value="' + seprow + '"></input>';
586
			$('form').append(sepinput);
587
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][text]" value="' + escapeHtml($(this).find('td').text()) + '"></input>';
588
			$('form').append(sepinput);
589
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][color]" value="' + $(this).find('td').prop('class') + '"></input>';
590
			$('form').append(sepinput);
591
			sepinput = '<input type="hidden" name="separator[' + sepnum + '][if]" value="' + iface + '"></input>';
592
			$('form').append(sepinput);
593
			sepnum++;
594
		} else {
595
			if ($(this).parent('tbody').hasClass('user-entries')) {
596
				row++;
597
			}
598
		}
599
	});
600
}
601

    
602
function reindex_rules(section) {
603
	var row = 0;
604

    
605
	section.find('tr').each(function() {
606
		if (this.id) {
607
			$(this).attr("id", "fr" + row);
608
			$(this).attr("onclick", "fr_toggle(" + row + ")")
609
			$(this).find('input:checkbox:first').each(function() {
610
				$(this).attr("id", "frc" + row);
611
				$(this).attr("onclick", "fr_toggle(" + row + ")");
612
			});
613

    
614
			row++;
615
		}
616
	});
617
}
618

    
619
function handle_colors() {
620
	$('[id^=sepclr]').prop("type", "button");
621

    
622
	$('[id^=sepclr]').click(function () {
623
		var color =	 $(this).attr('value');
624
		// Clear all the color classes
625
		$(this).parent('td').prop('class', '');
626
		$(this).parent('td').prev('td').prop('class', '');
627
		// Install our new color class
628
		$(this).parent('td').addClass(color);
629
		$(this).parent('td').prev('td').addClass(color);
630
		// Set the global color
631
		gColor = color;
632
	});
633
}
634

    
635
//JS equivalent to PHP htmlspecialchars()
636
function escapeHtml(text) {
637
	var map = {
638
		'&': '&amp;',
639
		'<': '&lt;',
640
		'>': '&gt;',
641
		'"': '&quot;',
642
		"'": '&#039;'
643
	};
644

    
645
	return text.replace(/[&<>"']/g, function(m) { return map[m]; });
646
}
647
// --------------------------------------------------------------------------------------------
648

    
649
// Select every option in the specified multiselect
650
function AllServers(id, selectAll) {
651
   for (i = 0; i < id.length; i++)	   {
652
	   id.eq(i).prop('selected', selectAll);
653
   }
654
}
655

    
656
// Move all selected options from one multiselect to another
657
function moveOptions(From, To)	{
658
	var len = From.length;
659
	var option;
660

    
661
	if (len > 0) {
662
		for (i=0; i<len; i++) {
663
			if (From.eq(i).is(':selected')) {
664
				option = From.eq(i).val();
665
				value  = From.eq(i).text();
666
				To.append(new Option(value, option));
667
				From.eq(i).remove();
668
			}
669
		}
670
	}
671
}
672

    
673

    
674
// ------------- Service start/stop/restart functions.
675
// If a start/stop/restart button is clicked, parse the button name and make a POST via AJAX
676
$('[id*=restartservice-], [id*=stopservice-], [id*=startservice-]').click(function(event) {
677
	var args = this.id.split('-');
678
	var action, name, mode_zone, id;
679

    
680
	if (args[0] == "openvpn") {
681
		action = args[1];
682
		name = args[0];
683
		mode_zone = args[2];
684
		id = args[3];
685
	} else if (args[0] == "cpativeportal") {
686
		action = args[1];
687
		name = args[0];
688
		mode_zone = args[2];
689
		id = args[3];
690
	} else {
691
		action = args[0];
692
		name = args[1];
693
	}
694

    
695
	$(this).children('i').removeClass().addClass('fa fa-cog fa-spin text-success');
696
	this.blur();
697

    
698
	ajaxRequest = $.ajax(
699
		{
700
			url: "/status_services.php",
701
			type: "post",
702
			data: {
703
				ajax: 		"ajax",
704
				mode: 		action,
705
				service: 	name,
706
				vpnmode: 	mode_zone,
707
				zone: 		mode_zone,
708
				id: 		id
709
			}
710
		}
711
	);
712

    
713
	// Once the AJAX call has returned, refresh the page to show the new service
714
	ajaxRequest.done(function (response, textStatus, jqXHR) {
715
		location.reload(true);
716
	});
717
});
(2-2/3)