Conditions | 1 |
Paths | 4 |
Total Lines | 225 |
Code Lines | 161 |
Lines | 0 |
Ratio | 0 % |
Changes | 13 | ||
Bugs | 2 | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | 'use strict'; |
||
11 | ], function ( |
||
12 | Field, |
||
13 | _, |
||
14 | $, |
||
15 | fieldTemplate, |
||
16 | Routing, |
||
17 | initTable, |
||
18 | UserContext, |
||
19 | i18n |
||
20 | ) { |
||
21 | return Field.extend({ |
||
22 | fieldTemplate: _.template(fieldTemplate), |
||
23 | events: { |
||
24 | 'change .field-input:first .table-data': 'updateModel', |
||
25 | 'change .field-input:first .flagbit-table-values': 'updateJson', |
||
26 | 'click .field-input:first .flagbit-table-values .delete-row': 'deleteItem', |
||
27 | 'click .field-input:first .flagbit-table-attribute .item-add': 'addItem' |
||
28 | }, |
||
29 | columns: {}, |
||
30 | renderInput: function (context) { |
||
31 | return this.fieldTemplate(context); |
||
32 | }, |
||
33 | postRender: function() { |
||
34 | this.getColumnUrl().then(function (columnUrl) { |
||
35 | |||
36 | $.ajax( |
||
37 | { |
||
38 | async: true, |
||
39 | type: 'GET', |
||
40 | url: columnUrl, |
||
41 | success: function (response) { |
||
42 | if (response) { |
||
43 | this.columns = {}; |
||
44 | _.each(response, function (value) { |
||
45 | var column = this.convertBackendItem(value); |
||
46 | this.columns[column.id] = column; |
||
47 | }.bind(this)); |
||
48 | initTable.init(this.$('.flagbit-table-attribute'), this.columns); |
||
49 | // initialize dran & drop sorting |
||
50 | this.$('.flagbit-table-values tbody').sortable({ |
||
51 | handle: ".handle", |
||
52 | stop: function() { |
||
53 | this.updateJson(); |
||
54 | }.bind(this) |
||
55 | }); |
||
56 | } |
||
57 | }.bind(this) |
||
58 | } |
||
59 | ); |
||
60 | }.bind(this)); |
||
61 | }, |
||
62 | getColumnUrl: function () { |
||
63 | return $.Deferred().resolve( |
||
64 | Routing.generate( |
||
65 | 'pim_enrich_attributeoption_get', |
||
66 | { |
||
67 | identifier: this.attribute.code |
||
68 | } |
||
69 | ) |
||
70 | ).promise(); |
||
71 | }, |
||
72 | updateModel: function () { |
||
73 | var data = this.$('.field-input:first .table-data').val(); |
||
74 | |||
75 | this.setCurrentValue(data); |
||
76 | }, |
||
77 | updateJson: function () { |
||
78 | var rows = this.$('.flagbit-table-values tr.flagbit-table-row'); |
||
79 | |||
80 | var values = []; |
||
81 | var columns = this.columns; |
||
82 | _.each(rows, function(row) { |
||
83 | var fields = {}; |
||
84 | |||
85 | _.each($('td[data-code]', row), function(td) { |
||
86 | var id = $(td).data('code'); |
||
87 | fields[id] = columns[id].func.parseValue($(td)); |
||
88 | }); |
||
89 | |||
90 | values.push(fields); |
||
91 | }); |
||
92 | |||
93 | var valuesAsJson = JSON.stringify(values); |
||
94 | this.$('.field-input:first .table-data').val(valuesAsJson); |
||
95 | this.setCurrentValue(valuesAsJson); |
||
96 | }, |
||
97 | deleteItem: function (event) { |
||
98 | $(event.currentTarget).closest('tr').remove(); |
||
99 | this.updateJson(); |
||
100 | }, |
||
101 | addItem: function () { |
||
102 | this.$('table.flagbit-table-values').append(initTable.createEmptyRow(this.columns)); |
||
103 | }, |
||
104 | convertBackendItem: function (item) { |
||
105 | return { |
||
106 | id: item.code, |
||
107 | text: i18n.getLabel(item.labels, UserContext.get('catalogLocale'), item.code), |
||
108 | config: item.type_config, |
||
109 | type: item.type, |
||
110 | func: this.createColumnFunctions(item) |
||
111 | }; |
||
112 | }, |
||
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 | }); |
||
235 | } |
||
236 | ); |
||
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.