Conditions | 8 |
Paths | 5 |
Total Lines | 121 |
Code Lines | 85 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 1 | Features | 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 | 'use strict'; |
||
113 | createColumnFunctions: function(item) { |
||
114 | // TODO Move this mapping to Symfony tags for every type to make this extendable |
||
115 | var fieldTemplate; |
||
116 | var parser = function (td) { |
||
117 | return $('input', td).val(); |
||
118 | }; |
||
119 | var init = function (td, column, value) { |
||
120 | }; |
||
121 | var formTypeValue = function (value) { |
||
122 | if (typeof value === 'undefined' || null === value) { |
||
123 | return ''; |
||
124 | } |
||
125 | |||
126 | return value.toString(); |
||
127 | }; |
||
128 | |||
129 | switch (item.type) { |
||
130 | case "text": |
||
131 | fieldTemplate = "<input data-type='<%= column.type %>' type='text' name='<%= column.id %>' class='<%= column.id %> AknTextField' value='<%= _.escape(column.func.formTypeValue(value)) %>' />"; |
||
132 | break; |
||
133 | case "number": |
||
134 | fieldTemplate = "<input data-type='<%= column.type %>' type='number' name='<%= column.id %>' class='<%= column.id %> AknTextField' value='<%= _.escape(column.func.formTypeValue(value)) %>' step='<%= \'is_decimal\' in column.config && true === column.config.is_decimal ? 0.1 : 1 %>' />"; |
||
135 | if ('is_decimal' in item.type_config && item.type_config.is_decimal === true) { |
||
136 | parser = function (td) { |
||
137 | return parseFloat($('input', td).val()); |
||
138 | }; |
||
139 | } else { |
||
140 | parser = function (td) { |
||
141 | return parseInt($('input', td).val()); |
||
142 | }; |
||
143 | } |
||
144 | break; |
||
145 | case "select": |
||
146 | fieldTemplate = "<input data-type='<%= column.type %>' type='text' name='<%= column.id %>' class='<%= column.id %>' value='<%= _.escape(column.func.formTypeValue(value)) %>' />"; |
||
147 | |||
148 | parser = function (td) { |
||
149 | var option = $('input', td).select2('data'); |
||
150 | return option.id; |
||
151 | }; |
||
152 | |||
153 | init = function (td, column, value) { |
||
154 | var select2Config = { |
||
155 | placeholder: ' ' |
||
156 | }; |
||
157 | if ('options' in column.config) { |
||
158 | var options = []; |
||
159 | _.each(column.config.options, function(option, key) { |
||
160 | options.push({ id: key, text: option }); |
||
161 | }); |
||
162 | select2Config.data = options; |
||
163 | } |
||
164 | |||
165 | var select2 = $('input', td).select2(select2Config); |
||
166 | select2.on('select2-close', function () { |
||
167 | this.updateJson(); |
||
168 | }.bind(this)); |
||
169 | }.bind(this); |
||
170 | break; |
||
171 | case "select_from_url": |
||
172 | fieldTemplate = "<input data-type='<%= column.type %>' type='text' name='<%= column.id %>' class='<%= column.id %>' value='<%= _.escape(column.func.formTypeValue(value)) %>' />"; |
||
173 | |||
174 | parser = function (td) { |
||
175 | var option = $('input', td).select2('data'); |
||
176 | return option.id; |
||
177 | }; |
||
178 | |||
179 | init = function (td, column, value) { |
||
180 | var select2Config = { |
||
181 | placeholder: ' ' |
||
182 | }; |
||
183 | if ('options_url' in column.config) { |
||
184 | select2Config.ajax = { |
||
185 | url: column.config.options_url, |
||
186 | cache: true, |
||
187 | minimumInputLength: 0, |
||
188 | dataType: 'json', |
||
189 | quietMillis: 1000, |
||
190 | results: function (data) { |
||
191 | return data; |
||
192 | }, |
||
193 | data: function (term) { |
||
194 | return { |
||
195 | q: term |
||
196 | }; |
||
197 | } |
||
198 | }; |
||
199 | // initSelection needs to be cleaned up in the future without forcing a whole API |
||
200 | select2Config.initSelection = function(element, callback) { |
||
201 | var option = $(element).val(); |
||
202 | |||
203 | if (option !== '') { |
||
204 | $.ajax(column.config.options_url, { |
||
205 | dataType: "json", |
||
206 | cache: true |
||
207 | }).done(function (data) { |
||
208 | var selected = _.find(data.results, function (row) { |
||
209 | return row.id === option; |
||
210 | }); |
||
211 | callback(selected); |
||
212 | }); |
||
213 | } |
||
214 | }; |
||
215 | } |
||
216 | |||
217 | var select2 = $('input', td).select2(select2Config); |
||
218 | select2.on('select2-close', function () { |
||
219 | this.updateJson(); |
||
220 | }.bind(this)); |
||
221 | }.bind(this); |
||
222 | break; |
||
223 | default: |
||
224 | throw "Unknown type '"+item.type+"'"; |
||
225 | } |
||
226 | |||
227 | return { |
||
228 | renderField: _.template(fieldTemplate), // renders the template of the field |
||
229 | parseValue: parser, // parses the value into the proper type for the json result |
||
230 | init: init, // an optional function that allows to initialize third party plugins |
||
231 | formTypeValue: formTypeValue // changes the value when it is put to the form field(s) |
||
232 | }; |
||
233 | } |
||
234 | }); |
||
237 |
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.