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
123
	// Use element title in the confirmation message, or if not available
124
	// the element value
125
	$('.btn-danger').on('click', function(e){
126
		var msg = $.trim(this.textContent);
127
		
128
		if(!msg)
129
			var msg = $.trim(this.value).toLowerCase();
130
			
131
		var q = 'Are you sure you wish to '+ msg +'?';
132

    
133
		if ($(this).attr('title') != undefined)
134
			q = $(this).attr('title')+'?';
135

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

    
140
	// Add toggle-all when there are multiple checkboxes and none of them are radio buttons
141
	$('.control-label + .checkbox.multi').each(function() {
142
		var a = $('<a name="btntoggleall" class="btn btn-xs btn-default">toggle all</a>');
143
		
144
		if(($(this).html().indexOf("type=\"radio\"") == -1)) {
145
			a.on('click', function() {
146
				var wrap = $(this).parents('.form-group').find('.checkbox.multi'),
147
					all = wrap.find('input[type=checkbox]'),
148
					checked = wrap.find('input[type=checkbox]:checked');
149
	
150
				all.prop('checked', (all.length != checked.length));
151
			});
152
	
153
			a.appendTo($(this));
154
		}
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
	$('input[type=checkbox][data-toggle="disable"]:not(:checked)').each(function() {
182
		$( $(this).data('target') ).prop('disabled', true);
183
	});
184

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

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

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

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

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

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

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

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

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

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

    
224
		return this;
225
	}
226

    
227
	var old = $.fn.disable;
228

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

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

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

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