Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:
1 ac24dc24 Renato Botelho
/*
2
 * pfSense.js
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
6
 * Copyright (c) 2013-2016 Electric Sheep Fencing
7 a68f7a3d Luiz Otavio O Souza
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13 ac24dc24 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21 ac24dc24 Renato Botelho
 */
22
23 dc58b7b3 Sjon Hortensius
/*
24 a41b03d0 Sjon Hortensius
 * This file should only contain functions that will be used on more than 2 pages
25 dc58b7b3 Sjon Hortensius
 */
26
27 41ea4cf3 Sjon Hortensius
$(function() {
28 b2e6dd64 Sjon Hortensius
	// Attach collapsable behaviour to select options
29 85033097 Sjon Hortensius
	(function()
30 c39f7276 Sander van Leeuwen
	{
31
		var selects = $('select[data-toggle="collapse"]');
32
33 b2e6dd64 Sjon Hortensius
		selects.on('change', function(){
34 c39f7276 Sander van Leeuwen
			var options = $(this).find('option');
35
			var selectedValue = $(this).find(':selected').val();
36
37 b2e6dd64 Sjon Hortensius
			options.each(function(){
38 c39f7276 Sander van Leeuwen
				if ($(this).val() == selectedValue)
39
					return;
40
41 50ea0588 Sjon Hortensius
				targets = $('.toggle-'+ $(this).val() +'.in:not(.toggle-'+ selectedValue +')');
42
43
				// Hide related collapsables which are visible (.in)
44
				targets.collapse('hide');
45 b2e6dd64 Sjon Hortensius
46
				// Disable all invisible inputs
47 50ea0588 Sjon Hortensius
				targets.find(':input').prop('disabled', true);
48 c39f7276 Sander van Leeuwen
			});
49
50 50ea0588 Sjon Hortensius
			$('.toggle-' + selectedValue).collapse('show').find(':input').prop('disabled', false);
51 c39f7276 Sander van Leeuwen
		});
52
53
		// Trigger change to open currently selected item
54
		selects.trigger('change');
55 85033097 Sjon Hortensius
	})();
56 a8a4a7e3 Sjon Hortensius
57 3bbf56d7 Stephen Beaver
58
	// Add +/- buttons to certain Groups; to allow adding multiple entries
59
	// This time making the buttons col-2 wide so they can fit on the same line as the
60
	// rest of the group (providing the total width of the group is col-8 or less)
61
	(function()
62
	{
63
		var groups = $('div.form-group.user-duplication-horiz');
64
		var controlsContainer = $('<div class="col-sm-2"></div>');
65 e0cb987c Marcos Mendoza
		var plus = $('<a class="btn btn-sm btn-success"><i class="fa-solid fa-plus icon-embed-btn"></i>Add</a>');
66 c1d304b3 Marcos Mendoza
		var minus = $('<a class="btn btn-sm btn-warning"><i class="fa-solid fa-trash-can icon-embed-btn"></i>Delete</a>');
67 3bbf56d7 Stephen Beaver
68
		minus.on('click', function(){
69
			$(this).parents('div.form-group').remove();
70
		});
71
72
		plus.on('click', function(){
73
			var group = $(this).parents('div.form-group');
74
75
			var clone = group.clone(true);
76 7c540a5c Stephen Beaver
			clone.find('*').val('');
77 3bbf56d7 Stephen Beaver
			clone.appendTo(group.parent());
78
		});
79
80
		groups.each(function(idx, group){
81
			var controlsClone = controlsContainer.clone(true).appendTo(group);
82
			minus.clone(true).appendTo(controlsClone);
83
84
			if (group == group.parentNode.lastElementChild)
85
				plus.clone(true).appendTo(controlsClone);
86
		});
87
	})();
88
89 b2e6dd64 Sjon Hortensius
	// Add +/- buttons to certain Groups; to allow adding multiple entries
90 85033097 Sjon Hortensius
	(function()
91 b2e6dd64 Sjon Hortensius
	{
92
		var groups = $('div.form-group.user-duplication');
93 8c10899b Sander van Leeuwen
		var controlsContainer = $('<div class="col-sm-10 col-sm-offset-2 controls"></div>');
94 e0cb987c Marcos Mendoza
		var plus = $('<a class="btn btn-xs btn-success"><i class="fa-solid fa-plus icon-embed-btn"></i>Add</a>');
95 c1d304b3 Marcos Mendoza
		var minus = $('<a class="btn btn-xs btn-warning"><i class="fa-solid fa-trash-can icon-embed-btn"></i>Delete</a>');
96 b2e6dd64 Sjon Hortensius
97
		minus.on('click', function(){
98 8c10899b Sander van Leeuwen
			$(this).parents('div.form-group').remove();
99 b2e6dd64 Sjon Hortensius
		});
100
101
		plus.on('click', function(){
102 8c10899b Sander van Leeuwen
			var group = $(this).parents('div.form-group');
103 b2e6dd64 Sjon Hortensius
104
			var clone = group.clone(true);
105 5a957b9a Phil Davis
			clone.find('*').removeAttr('value');
106 b2e6dd64 Sjon Hortensius
			clone.appendTo(group.parent());
107
		});
108
109
		groups.each(function(idx, group){
110 8c10899b Sander van Leeuwen
			var controlsClone = controlsContainer.clone(true).appendTo(group);
111
			minus.clone(true).appendTo(controlsClone);
112 b2e6dd64 Sjon Hortensius
113
			if (group == group.parentNode.lastElementChild)
114 8c10899b Sander van Leeuwen
				plus.clone(true).appendTo(controlsClone);
115 0ba2494e Sjon Hortensius
		});
116 85033097 Sjon Hortensius
	})();
117 0ba2494e Sjon Hortensius
118 22dbacd0 PiBa-NL
	// Add +/- buttons to certain Groups; to allow adding multiple entries
119
	(function()
120
	{
121
		var groups = $('div.form-listitem.user-duplication');
122
		var fg = $('<div class="form-group"></div>');
123
		var controlsContainer = $('<div class="col-sm-10 col-sm-offset-2 controls"></div>');
124 e0cb987c Marcos Mendoza
		var plus = $('<a class="btn btn-xs btn-success"><i class="fa-solid fa-plus icon-embed-btn"></i>Add</a>');
125 c1d304b3 Marcos Mendoza
		var minus = $('<a class="btn btn-xs btn-warning"><i class="fa-solid fa-trash-can icon-embed-btn"></i>Delete</a>');
126 22dbacd0 PiBa-NL
127
		minus.on('click', function(){
128
			var groups = $('div.form-listitem.user-duplication');
129
			if (groups.length > 1) {
130
				$(this).parents('div.form-listitem').remove();
131
			}
132
		});
133
134
		plus.on('click', function(){
135
			var group = $(this).parents('div.form-listitem');
136
			var clone = group.clone(true);
137
			bump_input_id(clone);
138
			clone.appendTo(group.parent());
139
		});
140
141
		groups.each(function(idx, group){
142
			var fgClone = fg.clone(true).appendTo(group);
143
			var controlsClone = controlsContainer.clone(true).appendTo(fgClone);
144
			minus.clone(true).appendTo(controlsClone);
145
			plus.clone(true).appendTo(controlsClone);
146
		});
147
	})();
148 179377b0 robjarsen
149 2d911f2c Phil Davis
	// Automatically change IpAddress mask selectors to 128/32 options for IPv6/IPv4 addresses
150 85033097 Sjon Hortensius
	$('span.pfIpMask + select').each(function (idx, select){
151
		var input = $(select).prevAll('input[type=text]');
152 5a957b9a Phil Davis
153 85033097 Sjon Hortensius
		input.on('change', function(e){
154
			var isV6 = (input.val().indexOf(':') != -1), min = 0, max = 128;
155 8150bd51 Steve Beaver
156 85033097 Sjon Hortensius
			if (!isV6)
157
				max = 32;
158 0ba2494e Sjon Hortensius
159 8150bd51 Steve Beaver
			if (input.val() == "") {
160 85033097 Sjon Hortensius
				return;
161 8150bd51 Steve Beaver
			}
162
163
			var attr = $(select).attr('disabled');
164
165 a7c47d85 Steve Beaver
			// Don't do anything if the mask selector is disabled
166 8150bd51 Steve Beaver
			if (typeof attr === typeof undefined || attr === false) {
167 a7c47d85 Steve Beaver
				// Eat all of the options with a value greater than max. We don't want them to be available
168
				while (select.options[0].value > max)
169
					select.remove(0);
170 0ba2494e Sjon Hortensius
171 8150bd51 Steve Beaver
				if (select.options.length < max) {
172 a7c47d85 Steve Beaver
					for (var i=select.options.length; i<=max; i++)
173 8150bd51 Steve Beaver
						select.options.add(new Option(i, i), 0);
174 db28039e Steve Beaver
175
					if (isV6) {
176
						// Make sure index 0 is selected otherwise it will stay in "32" for V6
177
						select.options.selectedIndex = "0";
178
					}
179 8150bd51 Steve Beaver
				}
180 85033097 Sjon Hortensius
			}
181 0ba2494e Sjon Hortensius
		});
182 85033097 Sjon Hortensius
183
		// Fire immediately
184
		input.change();
185
	});
186 5a957b9a Phil Davis
187 c4704b9d Marcos Mendoza
	// Add confirm to all btn-danger buttons and fa-trash-can icons
188 7c540a5c Stephen Beaver
	// Use element title in the confirmation message, or if not available
189
	// the element value
190 c4704b9d Marcos Mendoza
	$('.btn-danger, .fa-trash-can').on('click', function(e){
191 fab3c245 Steve Beaver
		if (!($(this).hasClass('no-confirm')) && !($(this).hasClass('icon-embed-btn'))) {
192 e04daed0 Steve Beaver
			// Anchors using the automatic get2post system (pfSenseHelpers.js) perform the confirmation dialog
193
			// in those functions
194
			var attr = $(this).attr('usepost');
195
			if (typeof attr === typeof undefined || attr === false) {
196
				var msg = $.trim(this.textContent).toLowerCase();
197 5a957b9a Phil Davis
198 e04daed0 Steve Beaver
				if (!msg)
199
					var msg = $.trim(this.value).toLowerCase();
200 1c306cd0 Sjon Hortensius
201 e04daed0 Steve Beaver
				var q = 'Are you sure you wish to '+ msg +'?';
202 1c306cd0 Sjon Hortensius
203 e04daed0 Steve Beaver
				if ($(this).attr('title') != undefined)
204
					q = 'Are you sure you wish to '+ $(this).attr('title').toLowerCase() + '?';
205 33f0b0d5 Stephen Beaver
206 e04daed0 Steve Beaver
				if (!confirm(q)) {
207
					e.preventDefault();
208
					e.stopPropagation();	// Don't leave ancestor(s) selected.
209
				}
210 82d4c81b NOYB
			}
211 33f0b0d5 Stephen Beaver
		}
212 45d6ada5 Sjon Hortensius
	});
213
214 5e7a1d46 Stephen Beaver
	// Add toggle-all when there are multiple checkboxes
215 f0a108f2 Sander van Leeuwen
	$('.control-label + .checkbox.multi').each(function() {
216 c1d304b3 Marcos Mendoza
		var a = $('<a name="btntoggleall" class="btn btn-xs btn-info"><i class="fa-regular fa-square-check icon-embed-btn"></i>Toggle All</a>');
217 5a957b9a Phil Davis
218 5e7a1d46 Stephen Beaver
		a.on('click', function() {
219
			var wrap = $(this).parents('.form-group').find('.checkbox.multi'),
220
				all = wrap.find('input[type=checkbox]'),
221
				checked = wrap.find('input[type=checkbox]:checked');
222 5a957b9a Phil Davis
223 5e7a1d46 Stephen Beaver
			all.prop('checked', (all.length != checked.length));
224
		});
225 5a957b9a Phil Davis
226 a1aa91de Steve Beaver
		if ( ! $(this).parent().hasClass("notoggleall")) {
227
			a.appendTo($(this));
228
		}
229 f0a108f2 Sander van Leeuwen
	});
230 85033097 Sjon Hortensius
231 03b53522 Stephen Beaver
	// The need to NOT hide the advanced options if the elements therein are not set to the system
232
	// default values makes it better to handle advanced option hiding in each PHP file so this is being
233 2d911f2c Phil Davis
	// disabled for now by changing the class name it acts on to "auto-advanced"
234 03b53522 Stephen Beaver
235 08d1762e Sjon Hortensius
	// Hide advanced inputs by default
236 03b53522 Stephen Beaver
	if ($('.auto-advanced').length > 0)
237 08d1762e Sjon Hortensius
	{
238 321a7e89 Stephen Beaver
		var advButt = $('<a id="toggle-advanced" class="btn btn-default">toggle advanced options</a>');
239 08d1762e Sjon Hortensius
		advButt.on('click', function() {
240
			$('.advanced').parents('.form-group').collapse('toggle');
241
		});
242
243
		advButt.insertAfter($('#save'));
244
245 03b53522 Stephen Beaver
		$('.auto-advanced').parents('.form-group').collapse({toggle: true});
246 08d1762e Sjon Hortensius
	}
247
248 76bd03bb Jared Dillard
	var originalLeave = $.fn.popover.Constructor.prototype.leave;
249
	$.fn.popover.Constructor.prototype.leave = function(obj){
250
	  var self = obj instanceof this.constructor ?
251
	    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
252
	  var container, timeout;
253
254
	  originalLeave.call(this, obj);
255
256 9d3e8723 Phil Davis
	  if (self.$tip && self.$tip.length) {
257 76bd03bb Jared Dillard
	    container = self.$tip;
258
	    timeout = self.timeout;
259
	    container.one('mouseenter', function(){
260 c0367cb8 jim-p
	      //We entered the actual popover - call off the dogs
261 76bd03bb Jared Dillard
	      clearTimeout(timeout);
262
	      //Let's monitor popover content instead
263
	      container.one('mouseleave', function(){
264
	        $.fn.popover.Constructor.prototype.leave.call(self, self);
265
	      });
266
	    })
267
	  }
268
	};
269
270 11a71857 Steve Beaver
	// Bootstrap 3.4.1 sanitizes the contents of popovers even when data-html is specified
271
	// Add table tags to the list of elements permitted by the sanitizer
272
	var defaultWhiteList = $.fn.tooltip.Constructor.DEFAULTS.whiteList
273
274
	defaultWhiteList.table = []
275
	defaultWhiteList.thead = []
276 4e9467ae Steve Beaver
	defaultWhiteList.tr = ["class"]
277 11a71857 Steve Beaver
	defaultWhiteList.th = ["style"]
278
	defaultWhiteList.tbody = []
279
	defaultWhiteList.td = ["style"]
280
281 a8a4a7e3 Sjon Hortensius
	// Enable popovers globally
282 76bd03bb Jared Dillard
	$('[data-toggle="popover"]').popover({ delay: {show: 50, hide: 400} });
283 a8a4a7e3 Sjon Hortensius
284 3e607d0e Sjon Hortensius
	// Force correct initial state for toggleable checkboxes
285 36a0cfe8 Sander van Leeuwen
	$('input[type=checkbox][data-toggle="collapse"]:not(:checked)').each(function() {
286
		$( $(this).data('target') ).addClass('collapse');
287 3e607d0e Sjon Hortensius
	});
288 0619c9db Stephen Beaver
289 08d1762e Sjon Hortensius
	$('input[type=checkbox][data-toggle="disable"]:not(:checked)').each(function() {
290
		$( $(this).data('target') ).prop('disabled', true);
291
	});
292 3e607d0e Sjon Hortensius
293 54691fc6 PiBa-NL
	$('.table-rowdblclickedit>tbody>tr').dblclick(function () {
294
		$(this).find(".fa-pencil")[0].click();
295
	});
296 fab3c245 Steve Beaver
297 45d6ada5 Sjon Hortensius
	// Focus first input
298
	$(':input:enabled:visible:first').focus();
299
300 57625777 PiBa-NL
	$(".resizable").each(function() {
301
		$(this).css('height', 80).resizable({minHeight: 80, minWidth: 200}).parent().css('padding-bottom', 0);
302
		$(this).css('height', 78);
303
	});
304 fab3c245 Steve Beaver
305 85033097 Sjon Hortensius
	// Run in-page defined events
306
	while (func = window.events.shift())
307
		func();
308 08d1762e Sjon Hortensius
});
309
310
// Implement data-toggle=disable
311 8fa5038b Marcos Mendoza
// Source: https://github.com/visionappscz/bootstrap-ui/blob/master/src/js/disable.js
312 08d1762e Sjon Hortensius
;(function($, window, document) {
313
	'use strict';
314
315
	var Disable = function($element) {
316
		this.$element = $element;
317
	};
318
319
	Disable.prototype.toggle = function() {
320
		this.$element.prop('disabled', !this.$element.prop('disabled'));
321
	};
322
323
	function Plugin(options) {
324
		$(document).trigger('toggle.sui.disable');
325
326
		this.each(function() {
327
			var $this = $(this);
328
			var data = $this.data('sui.disable');
329
330
			if (!data) {
331
				$this.data('sui.disable', (data = new Disable($this)));
332
			}
333
334
			if (options === 'toggle') {
335
				data.toggle();
336
			}
337
		});
338
339
		$(document).trigger('toggled.sui.disable');
340
341
		return this;
342
	}
343
344
	var old = $.fn.disable;
345
346
	$.fn.disable = Plugin;
347
	$.fn.disable.Constructor = Disable;
348
349
	$.fn.disable.noConflict = function() {
350
		$.fn.disable = old;
351
		return this;
352
	};
353
354
	(function(Plugin, $, window) {
355 df4262d0 Steve Beaver
		$(window).on("load", function() {
356 08d1762e Sjon Hortensius
			var $controls = $('[data-toggle=disable]');
357
358
			$controls.each(function() {
359
				var $this = $(this);
360
				var eventType = $this.data('disable-event');
361
				if (!eventType) {
362
					eventType = 'change';
363
				}
364
				$this.on(eventType + '.sui.disable.data-api', function() {
365
					Plugin.call($($this.data('target')), 'toggle');
366
				});
367
			});
368
		});
369
	}(Plugin, $, window, document));
370 586a79d5 heper
}(jQuery, window, document));