Passed
Branch develop (893f38)
by Hans Erik
06:54
created

Plugin.init   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
nc 3
nop 0
1
;(function ($, window, document, undefined) {
0 ignored issues
show
Unused Code introduced by
The parameter undefined is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter window is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter document is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
2
3
	// Create the defaults once
4
	var pluginName = "redgrid";
5
6
	var defaults = {
7
		ajaxRefreshUrl     : '',
8
		formSelector       : '#adminForm',
9
		orderColSelector   : '.js-order-col',
10
		orderDirSelector   : '.js-order-dir',
11
		orderFieldSelector : '.js-order-field',
12
		directionFieldSelector : '.js-direction-field',
13
		ordering : '',
14
		direction : 'ASC'
15
	};
16
17
	// The actual plugin constructor
18
	function Plugin(element, options) {
19
		this.element = element;
20
		this.options = $.extend({}, defaults, options);
21
		this._defaults = defaults;
22
23
		// Initialise selectors
24
		this.theForm        = this.element;
25
		this.orderCols      = $(this.options.orderColSelector);
26
		this.orderDir       = $(this.options.orderDirSelector);
27
		this.orderField     = $(this.options.orderFieldSelector);
28
		this.directionField = $(this.options.directionFieldSelector);
29
		this.ordering       = this.options.ordering;
30
		this.direction      = this.options.direction;
31
32
		// Selector values
33
		this._name = pluginName;
34
35
		this.init();
36
	}
37
38
	Plugin.prototype = {
39
		init: function () {
40
41
			var main = this;
42
43
			// Check/create ordering field
44
			this.createOrderingField();
45
46
			// Check/create direction field
47
			this.createDirectionField();
48
49
			this.orderCols.click(function() {
50
51
				// Order to set
52
				var newOrderCol = $(this).attr('data-order');
53
				var newDirection = 'ASC';
0 ignored issues
show
Unused Code introduced by
The variable newDirection seems to be never used. Consider removing it.
Loading history...
54
55
				// The data-order attrib is required
56
				if (newOrderCol.length)
57
				{
58
					if (newOrderCol !== main.orderCol)
59
					{
60
						// Update the order field
61
						main.updateFieldValue(main.orderField, newOrderCol);
62
						main.updateFieldValue(main.directionField, 'ASC');
63
					}
64
					else
65
					{
66
						main.toggleDirection();
67
					}
68
69
					main.theForm.submit();
70
				}
71
72
			});
73
		},
74
		toggleDirection: function () {
75
76
			var newDirection = 'ASC';
77
78
			if (this.direction.toUpperCase() == 'ASC')
79
			{
80
				newDirection = 'DESC';
81
			}
82
83
			this.updateFieldValue(this.directionField, newDirection);
84
		},
85
		createOrderingField: function () {
86
87
			var main = this;
88
89
			if (!this.orderField.length)
90
			{
91
				this.orderField = $('<input>').attr({
92
				    'type': 'hidden',
93
				    'id': 'js-order-field',
94
				    'class': 'js-order-field',
95
				    'name': 'filter_order',
96
				    'value': this.ordering
97
				});
98
99
				this.orderField.appendTo(this.theForm);
100
			}
101
102
			// Add missing columns to the order select
103
			if (this.orderField.is('select'))
104
			{
105
				this.orderCols.each(function(){
106
					var value = $(this).attr('data-order');
107
					var name = $(this).attr('data-name');
108
109
					if (value.length)
110
					{
111
						var option = main.findOption(main.orderField, value);
112
113
						if (!option.length)
114
						{
115
							var option = $('<option>');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable option already seems to be declared on line 111. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
116
							option.text(name).val(value);
117
118
							// If it is the active option select it
119
							if ($(this).hasClass('active'))
120
							{
121
								option.attr('selected', 'selected');
122
							}
123
124
							// Append the option an repopulate the chosen field
125
							main.orderField.append(option);
126
						}
127
					}
128
129
				});
130
131
				// Reorder options alphabetically ?
132
				//this.orderField.html($("option", this.orderField).sort(function(a, b) {
133
		            //return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
134
		        //}));
135
136
				this.orderField.trigger('liszt:updated');
137
			}
138
139
			this.orderCol  = this.orderField.val();
140
		},
141
		createDirectionField: function () {
142
143
			if (!this.directionField.length)
144
			{
145
				this.directionField = $('<input>').attr({
146
				    'type': 'hidden',
147
				    'id': 'js-direction-field',
148
				    'class': 'js-direction-field',
149
				    'name': 'filter_order_Dir',
150
				    'value': this.direction
151
				});
152
153
				this.directionField.appendTo(this.theForm);
154
			}
155
156
			this.direction = this.directionField.val();
157
		},
158
		updateFieldValue: function (field, newValue) {
159
160
			var type = field.attr('type');
161
162
			if (type === 'hidden' || type === 'text')
163
			{
164
				field.attr('value', newValue);
165
			}
166
			else if (field.is('select'))
167
			{
168
				// Select the option result
169
				var desiredOption = field.find('option').filter(function () { return $(this).val() == newValue; });
170
171
				if (desiredOption.length)
172
				{
173
					desiredOption.attr('selected', 'selected');
174
				}
175
				// If the option does not exist create it on the fly
176
				else
177
				{
178
					var option = $('<option>');
179
					option.text(newValue).val(newValue);
180
					option.attr('selected','selected');
181
182
					// Append the option an repopulate the chosen field
183
					field.append(option);
184
				}
185
				// Trigger the chosen update
186
				field.trigger('liszt:updated');
187
188
			}
189
		},
190
		findOption: function(select, value) {
191
			return select.find('option').filter(function () { return $(this).val() == value; });
192
		}
193
	};
194
195
	// A really lightweight plugin wrapper around the constructor,
196
	// preventing against multiple instantiations
197
	$.fn[pluginName] = function (options) {
198
		return this.each(function () {
199
			if (!$.data(this, "plugin_" + pluginName)) {
200
				$.data(this, "plugin_" + pluginName, new Plugin(this, options));
201
			}
202
		});
203
	};
204
205
})(jQuery, window, document);