Project

General

Profile

Download (7.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * This file should only contain functions that will be used on more than 2 pages
3
 */
4

    
5
$(function() {
6
	// Attach collapsable behaviour to select options
7
	(function()
8
	{
9
		var selects = $('select[data-toggle="collapse"]');
10

    
11
		selects.on('change', function(){
12
			var options = $(this).find('option');
13
			var selectedValue = $(this).find(':selected').val();
14

    
15
			options.each(function(){
16
				if ($(this).val() == selectedValue)
17
					return;
18

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

    
21
				// Hide related collapsables which are visible (.in)
22
				targets.collapse('hide');
23

    
24
				// Disable all invisible inputs
25
				targets.find(':input').prop('disabled', true);
26
			});
27

    
28
			$('.toggle-' + selectedValue).collapse('show').find(':input').prop('disabled', false);
29
		});
30

    
31
		// Trigger change to open currently selected item
32
		selects.trigger('change');
33
	})();
34

    
35

    
36
	// Add +/- buttons to certain Groups; to allow adding multiple entries
37
	// This time making the buttons col-2 wide so they can fit on the same line as the
38
	// rest of the group (providing the total width of the group is col-8 or less)
39
	(function()
40
	{
41
		var groups = $('div.form-group.user-duplication-horiz');
42
		var controlsContainer = $('<div class="col-sm-2"></div>');
43
		var plus = $('<a class="btn btn-sm btn-success">Add</a>');
44
		var minus = $('<a class="btn btn-sm btn-warning">Delete</a>');
45

    
46
		minus.on('click', function(){
47
			$(this).parents('div.form-group').remove();
48
		});
49

    
50
		plus.on('click', function(){
51
			var group = $(this).parents('div.form-group');
52

    
53
			var clone = group.clone(true);
54
			clone.find('*').val('');
55
			clone.appendTo(group.parent());
56
		});
57

    
58
		groups.each(function(idx, group){
59
			var controlsClone = controlsContainer.clone(true).appendTo(group);
60
			minus.clone(true).appendTo(controlsClone);
61

    
62
			if (group == group.parentNode.lastElementChild)
63
				plus.clone(true).appendTo(controlsClone);
64
		});
65
	})();
66

    
67
	// Add +/- buttons to certain Groups; to allow adding multiple entries
68
	(function()
69
	{
70
		var groups = $('div.form-group.user-duplication');
71
		var controlsContainer = $('<div class="col-sm-10 col-sm-offset-2 controls"></div>');
72
		var plus = $('<a class="btn btn-xs btn-success">Add</a>');
73
		var minus = $('<a class="btn btn-xs btn-warning">Delete</a>');
74

    
75
		minus.on('click', function(){
76
			$(this).parents('div.form-group').remove();
77
		});
78

    
79
		plus.on('click', function(){
80
			var group = $(this).parents('div.form-group');
81

    
82
			var clone = group.clone(true);
83
			clone.find('*').removeAttr('value');
84
			clone.appendTo(group.parent());
85
		});
86

    
87
		groups.each(function(idx, group){
88
			var controlsClone = controlsContainer.clone(true).appendTo(group);
89
			minus.clone(true).appendTo(controlsClone);
90

    
91
			if (group == group.parentNode.lastElementChild)
92
				plus.clone(true).appendTo(controlsClone);
93
		});
94
	})();
95

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

    
100
		input.on('change', function(e){
101
			var isV6 = (input.val().indexOf(':') != -1), min = 0, max = 128;
102
			if (!isV6)
103
				max = 32;
104

    
105
			if (input.val() == "")
106
				return;
107

    
108
			// Eat all of the options with a value greater than max. We don't want them to be available
109
			while (select.options[0].value > max)
110
				select.remove(0);
111

    
112
			if (select.options.length < max) {
113
				for (var i=select.options.length; i<=max; i++)
114
					select.options.add(new Option(i, i), 0);
115
			}
116
		});
117

    
118
		// Fire immediately
119
		input.change();
120
	});
121

    
122
	// Add confirm to all btn-danger buttons and fa-trash icons
123
	// Use element title in the confirmation message, or if not available
124
	// the element value
125
	$('.btn-danger, .fa-trash').on('click', function(e){
126
		if(!($(this).hasClass('no-confirm'))) {
127
			var msg = $.trim(this.textContent);
128

    
129
			if(!msg)
130
				var msg = $.trim(this.value).toLowerCase();
131

    
132
			var q = 'Are you sure you wish to '+ msg +'?';
133

    
134
			if ($(this).attr('title') != undefined)
135
				q = 'Are you sure you wish to '+ $(this).attr('title').toLowerCase() + '?';
136

    
137
			if (!confirm(q))
138
				e.preventDefault();
139
		}
140
	});
141

    
142
	// Add toggle-all when there are multiple checkboxes
143
	$('.control-label + .checkbox.multi').each(function() {
144
		var a = $('<a name="btntoggleall" class="btn btn-xs btn-default">toggle all</a>');
145

    
146
		a.on('click', function() {
147
			var wrap = $(this).parents('.form-group').find('.checkbox.multi'),
148
				all = wrap.find('input[type=checkbox]'),
149
				checked = wrap.find('input[type=checkbox]:checked');
150

    
151
			all.prop('checked', (all.length != checked.length));
152
		});
153

    
154
		a.appendTo($(this));
155
	});
156

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

    
161
	// Hide advanced inputs by default
162
	if ($('.auto-advanced').length > 0)
163
	{
164
		var advButt = $('<a id="toggle-advanced" class="btn btn-default">toggle advanced options</a>');
165
		advButt.on('click', function() {
166
			$('.advanced').parents('.form-group').collapse('toggle');
167
		});
168

    
169
		advButt.insertAfter($('#save'));
170

    
171
		$('.auto-advanced').parents('.form-group').collapse({toggle: true});
172
	}
173

    
174
	// Enable popovers globally
175
	$('[data-toggle="popover"]').popover();
176

    
177
	// Force correct initial state for toggleable checkboxes
178
	$('input[type=checkbox][data-toggle="collapse"]:not(:checked)').each(function() {
179
		$( $(this).data('target') ).addClass('collapse');
180
	});
181

    
182
	$('input[type=checkbox][data-toggle="disable"]:not(:checked)').each(function() {
183
		$( $(this).data('target') ).prop('disabled', true);
184
	});
185

    
186
	// Focus first input
187
	$(':input:enabled:visible:first').focus();
188

    
189
	// Run in-page defined events
190
	while (func = window.events.shift())
191
		func();
192
});
193

    
194
// Implement data-toggle=disable
195
// Source: https://github.com/synergic-cz/synergic-ui/blob/master/src/js/disable.js
196
;(function($, window, document) {
197
	'use strict';
198

    
199
	var Disable = function($element) {
200
		this.$element = $element;
201
	};
202

    
203
	Disable.prototype.toggle = function() {
204
		this.$element.prop('disabled', !this.$element.prop('disabled'));
205
	};
206

    
207
	function Plugin(options) {
208
		$(document).trigger('toggle.sui.disable');
209

    
210
		this.each(function() {
211
			var $this = $(this);
212
			var data = $this.data('sui.disable');
213

    
214
			if (!data) {
215
				$this.data('sui.disable', (data = new Disable($this)));
216
			}
217

    
218
			if (options === 'toggle') {
219
				data.toggle();
220
			}
221
		});
222

    
223
		$(document).trigger('toggled.sui.disable');
224

    
225
		return this;
226
	}
227

    
228
	var old = $.fn.disable;
229

    
230
	$.fn.disable = Plugin;
231
	$.fn.disable.Constructor = Disable;
232

    
233
	$.fn.disable.noConflict = function() {
234
		$.fn.disable = old;
235
		return this;
236
	};
237

    
238
	(function(Plugin, $, window) {
239
		$(window).load(function() {
240
			var $controls = $('[data-toggle=disable]');
241

    
242
			$controls.each(function() {
243
				var $this = $(this);
244
				var eventType = $this.data('disable-event');
245
				if (!eventType) {
246
					eventType = 'change';
247
				}
248
				$this.on(eventType + '.sui.disable.data-api', function() {
249
					Plugin.call($($this.data('target')), 'toggle');
250
				});
251
			});
252
		});
253
	}(Plugin, $, window, document));
254
}(jQuery, window, document));
(3-3/4)