Project

General

Profile

Download (9.68 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * pfSense.js
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
6
 * All rights reserved.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

    
21
/*
22
 * This file should only contain functions that will be used on more than 2 pages
23
 */
24

    
25
$(function() {
26
	// Attach collapsable behaviour to select options
27
	(function()
28
	{
29
		var selects = $('select[data-toggle="collapse"]');
30

    
31
		selects.on('change', function(){
32
			var options = $(this).find('option');
33
			var selectedValue = $(this).find(':selected').val();
34

    
35
			options.each(function(){
36
				if ($(this).val() == selectedValue)
37
					return;
38

    
39
				targets = $('.toggle-'+ $(this).val() +'.in:not(.toggle-'+ selectedValue +')');
40

    
41
				// Hide related collapsables which are visible (.in)
42
				targets.collapse('hide');
43

    
44
				// Disable all invisible inputs
45
				targets.find(':input').prop('disabled', true);
46
			});
47

    
48
			$('.toggle-' + selectedValue).collapse('show').find(':input').prop('disabled', false);
49
		});
50

    
51
		// Trigger change to open currently selected item
52
		selects.trigger('change');
53
	})();
54

    
55

    
56
	// Add +/- buttons to certain Groups; to allow adding multiple entries
57
	// This time making the buttons col-2 wide so they can fit on the same line as the
58
	// rest of the group (providing the total width of the group is col-8 or less)
59
	(function()
60
	{
61
		var groups = $('div.form-group.user-duplication-horiz');
62
		var controlsContainer = $('<div class="col-sm-2"></div>');
63
		var plus = $('<a class="btn btn-sm btn-success"><i class="fa fa-plus icon-embed-btn"></i>Add</a>');
64
		var minus = $('<a class="btn btn-sm btn-warning"><i class="fa fa-trash icon-embed-btn"></i>Delete</a>');
65

    
66
		minus.on('click', function(){
67
			$(this).parents('div.form-group').remove();
68
		});
69

    
70
		plus.on('click', function(){
71
			var group = $(this).parents('div.form-group');
72

    
73
			var clone = group.clone(true);
74
			clone.find('*').val('');
75
			clone.appendTo(group.parent());
76
		});
77

    
78
		groups.each(function(idx, group){
79
			var controlsClone = controlsContainer.clone(true).appendTo(group);
80
			minus.clone(true).appendTo(controlsClone);
81

    
82
			if (group == group.parentNode.lastElementChild)
83
				plus.clone(true).appendTo(controlsClone);
84
		});
85
	})();
86

    
87
	// Add +/- buttons to certain Groups; to allow adding multiple entries
88
	(function()
89
	{
90
		var groups = $('div.form-group.user-duplication');
91
		var controlsContainer = $('<div class="col-sm-10 col-sm-offset-2 controls"></div>');
92
		var plus = $('<a class="btn btn-xs btn-success"><i class="fa fa-plus icon-embed-btn"></i>Add</a>');
93
		var minus = $('<a class="btn btn-xs btn-warning"><i class="fa fa-trash icon-embed-btn"></i>Delete</a>');
94

    
95
		minus.on('click', function(){
96
			$(this).parents('div.form-group').remove();
97
		});
98

    
99
		plus.on('click', function(){
100
			var group = $(this).parents('div.form-group');
101

    
102
			var clone = group.clone(true);
103
			clone.find('*').removeAttr('value');
104
			clone.appendTo(group.parent());
105
		});
106

    
107
		groups.each(function(idx, group){
108
			var controlsClone = controlsContainer.clone(true).appendTo(group);
109
			minus.clone(true).appendTo(controlsClone);
110

    
111
			if (group == group.parentNode.lastElementChild)
112
				plus.clone(true).appendTo(controlsClone);
113
		});
114
	})();
115

    
116
	// Automatically change IpAddress mask selectors to 128/32 options for IPv6/IPv4 addresses
117
	$('span.pfIpMask + select').each(function (idx, select){
118
		var input = $(select).prevAll('input[type=text]');
119

    
120
		input.on('change', function(e){
121
			var isV6 = (input.val().indexOf(':') != -1), min = 0, max = 128;
122

    
123
			if (!isV6)
124
				max = 32;
125

    
126
			if (input.val() == "") {
127
				return;
128
			}
129

    
130
			var attr = $(select).attr('disabled');
131

    
132
			// Don't do anything if the mask selector is disabled
133
			if (typeof attr === typeof undefined || attr === false) {
134
				// Eat all of the options with a value greater than max. We don't want them to be available
135
				while (select.options[0].value > max)
136
					select.remove(0);
137

    
138
				if (select.options.length < max) {
139
					for (var i=select.options.length; i<=max; i++)
140
						select.options.add(new Option(i, i), 0);
141

    
142
					if (isV6) {
143
						// Make sure index 0 is selected otherwise it will stay in "32" for V6
144
						select.options.selectedIndex = "0";
145
					}
146
				}
147
			}
148
		});
149

    
150
		// Fire immediately
151
		input.change();
152
	});
153

    
154
	// Add confirm to all btn-danger buttons and fa-trash icons
155
	// Use element title in the confirmation message, or if not available
156
	// the element value
157
	$('.btn-danger, .fa-trash').on('click', function(e){
158
		if (!($(this).hasClass('no-confirm')) && !($(this).hasClass('icon-embed-btn'))) {
159
			// Anchors using the automatic get2post system (pfSenseHelpers.js) perform the confirmation dialog
160
			// in those functions
161
			var attr = $(this).attr('usepost');
162
			if (typeof attr === typeof undefined || attr === false) {
163
				var msg = $.trim(this.textContent).toLowerCase();
164

    
165
				if (!msg)
166
					var msg = $.trim(this.value).toLowerCase();
167

    
168
				var q = 'Are you sure you wish to '+ msg +'?';
169

    
170
				if ($(this).attr('title') != undefined)
171
					q = 'Are you sure you wish to '+ $(this).attr('title').toLowerCase() + '?';
172

    
173
				if (!confirm(q)) {
174
					e.preventDefault();
175
					e.stopPropagation();	// Don't leave ancestor(s) selected.
176
				}
177
			}
178
		}
179
	});
180

    
181
	// Add toggle-all when there are multiple checkboxes
182
	$('.control-label + .checkbox.multi').each(function() {
183
		var a = $('<a name="btntoggleall" class="btn btn-xs btn-info"><i class="fa fa-check-square-o icon-embed-btn"></i>Toggle All</a>');
184

    
185
		a.on('click', function() {
186
			var wrap = $(this).parents('.form-group').find('.checkbox.multi'),
187
				all = wrap.find('input[type=checkbox]'),
188
				checked = wrap.find('input[type=checkbox]:checked');
189

    
190
			all.prop('checked', (all.length != checked.length));
191
		});
192

    
193
		a.appendTo($(this));
194
	});
195

    
196
	// The need to NOT hide the advanced options if the elements therein are not set to the system
197
	// default values makes it better to handle advanced option hiding in each PHP file so this is being
198
	// disabled for now by changing the class name it acts on to "auto-advanced"
199

    
200
	// Hide advanced inputs by default
201
	if ($('.auto-advanced').length > 0)
202
	{
203
		var advButt = $('<a id="toggle-advanced" class="btn btn-default">toggle advanced options</a>');
204
		advButt.on('click', function() {
205
			$('.advanced').parents('.form-group').collapse('toggle');
206
		});
207

    
208
		advButt.insertAfter($('#save'));
209

    
210
		$('.auto-advanced').parents('.form-group').collapse({toggle: true});
211
	}
212

    
213
	var originalLeave = $.fn.popover.Constructor.prototype.leave;
214
	$.fn.popover.Constructor.prototype.leave = function(obj){
215
	  var self = obj instanceof this.constructor ?
216
	    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
217
	  var container, timeout;
218

    
219
	  originalLeave.call(this, obj);
220

    
221
	  if (self.$tip && self.$tip.length) {
222
	    container = self.$tip;
223
	    timeout = self.timeout;
224
	    container.one('mouseenter', function(){
225
	      //We entered the actual popover - call off the dogs
226
	      clearTimeout(timeout);
227
	      //Let's monitor popover content instead
228
	      container.one('mouseleave', function(){
229
	        $.fn.popover.Constructor.prototype.leave.call(self, self);
230
	      });
231
	    })
232
	  }
233
	};
234

    
235
	// Enable popovers globally
236
	$('[data-toggle="popover"]').popover({ delay: {show: 50, hide: 400} });
237

    
238
	// Force correct initial state for toggleable checkboxes
239
	$('input[type=checkbox][data-toggle="collapse"]:not(:checked)').each(function() {
240
		$( $(this).data('target') ).addClass('collapse');
241
	});
242

    
243
	$('input[type=checkbox][data-toggle="disable"]:not(:checked)').each(function() {
244
		$( $(this).data('target') ).prop('disabled', true);
245
	});
246

    
247
	$('.table-rowdblclickedit>tbody>tr').dblclick(function () {
248
		$(this).find(".fa-pencil")[0].click();
249
	});
250

    
251
	// Focus first input
252
	$(':input:enabled:visible:first').focus();
253

    
254
	$(".resizable").each(function() {
255
		$(this).css('height', 80).resizable({minHeight: 80, minWidth: 200}).parent().css('padding-bottom', 0);
256
		$(this).css('height', 78);
257
	});
258

    
259
	// Run in-page defined events
260
	while (func = window.events.shift())
261
		func();
262
});
263

    
264
// Implement data-toggle=disable
265
// Source: https://github.com/synergic-cz/synergic-ui/blob/master/src/js/disable.js
266
;(function($, window, document) {
267
	'use strict';
268

    
269
	var Disable = function($element) {
270
		this.$element = $element;
271
	};
272

    
273
	Disable.prototype.toggle = function() {
274
		this.$element.prop('disabled', !this.$element.prop('disabled'));
275
	};
276

    
277
	function Plugin(options) {
278
		$(document).trigger('toggle.sui.disable');
279

    
280
		this.each(function() {
281
			var $this = $(this);
282
			var data = $this.data('sui.disable');
283

    
284
			if (!data) {
285
				$this.data('sui.disable', (data = new Disable($this)));
286
			}
287

    
288
			if (options === 'toggle') {
289
				data.toggle();
290
			}
291
		});
292

    
293
		$(document).trigger('toggled.sui.disable');
294

    
295
		return this;
296
	}
297

    
298
	var old = $.fn.disable;
299

    
300
	$.fn.disable = Plugin;
301
	$.fn.disable.Constructor = Disable;
302

    
303
	$.fn.disable.noConflict = function() {
304
		$.fn.disable = old;
305
		return this;
306
	};
307

    
308
	(function(Plugin, $, window) {
309
		$(window).load(function() {
310
			var $controls = $('[data-toggle=disable]');
311

    
312
			$controls.each(function() {
313
				var $this = $(this);
314
				var eventType = $this.data('disable-event');
315
				if (!eventType) {
316
					eventType = 'change';
317
				}
318
				$this.on(eventType + '.sui.disable.data-api', function() {
319
					Plugin.call($($this.data('target')), 'toggle');
320
				});
321
			});
322
		});
323
	}(Plugin, $, window, document));
324
}(jQuery, window, document));
(1-1/4)