Project

General

Profile

Download (9.61 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * pfSense.js
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2004-2016 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
				// Remove all options
135
				$(select).find('option').remove().end();
136

    
137
				// Then re-install the required ones
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

    
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
		// Fire immediately
150
		input.change();
151
	});
152

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

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

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

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

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

    
180
	// Add toggle-all when there are multiple checkboxes
181
	$('.control-label + .checkbox.multi').each(function() {
182
		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>');
183

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

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

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

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

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

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

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

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

    
218
	  originalLeave.call(this, obj);
219

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
294
		return this;
295
	}
296

    
297
	var old = $.fn.disable;
298

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

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

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

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