Conditions | 1 |
Paths | 2 |
Total Lines | 205 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | ;(function ($, window, document, undefined) { |
||
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'; |
||
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>'); |
||
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); |
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.