Conditions | 1 |
Paths | 1 |
Total Lines | 204 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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 | define( |
||
6 | function(JsonGeneratorObserver, __) { |
||
7 | |||
8 | /** |
||
9 | * @class |
||
10 | * @param {Boolean} $editable |
||
11 | * @param {HTMLElement} $container |
||
12 | */ |
||
13 | var JsonGeneratorRendererSelect = function($editable, $container) { |
||
14 | |||
15 | /** |
||
16 | * @protected |
||
17 | * @type {HTMLTableElement} |
||
18 | */ |
||
19 | var $table = null; |
||
20 | /** |
||
21 | * @public |
||
22 | * @type {JsonGeneratorObserver} |
||
23 | */ |
||
24 | this.observer = new JsonGeneratorObserver(); |
||
25 | |||
26 | /** |
||
27 | * @public |
||
28 | * @param {Object} $data |
||
29 | */ |
||
30 | this.render = function($data) { |
||
31 | |||
32 | var $label = document.createElement('label'); |
||
33 | $label.innerText = __('flagbit_attribute_table_simpleselect_options_label'); |
||
34 | $label.className = 'select-options-config-label'; |
||
35 | $container.appendChild($label); |
||
36 | |||
37 | // needed! |
||
38 | this.getTable(); |
||
39 | |||
40 | for(var $key in $data.options) { |
||
41 | if($data.options.hasOwnProperty($key)) { |
||
42 | var $value = $data.options[$key]; |
||
43 | |||
44 | var $keyCol = createTableColumn($key); |
||
45 | var $valCol = createTableColumn($value); |
||
46 | var $row = document.createElement('tr'); |
||
47 | |||
48 | $row.appendChild($keyCol); |
||
49 | $row.appendChild($valCol); |
||
50 | if($editable) { |
||
51 | $row.appendChild(createDeleteButton($row)); |
||
52 | } |
||
53 | this.getTable().appendChild($row); |
||
54 | } |
||
55 | } |
||
56 | }; |
||
57 | |||
58 | |||
59 | /** |
||
60 | * @public |
||
61 | * @returns {Object} |
||
62 | */ |
||
63 | this.read = function() { |
||
64 | |||
65 | var $data = {options: {}}; |
||
66 | |||
67 | var $collection = this.getTable().querySelectorAll('tr'); |
||
68 | for(var $i in $collection) { |
||
69 | if($collection.hasOwnProperty($i)) { |
||
70 | var $tr = $collection[$i]; |
||
71 | $data.options[$tr.querySelectorAll('td input')[0].value] = $tr.querySelectorAll('td input')[1].value; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return $data; |
||
76 | }; |
||
77 | |||
78 | /** |
||
79 | * @public |
||
80 | * @returns {HTMLTableSectionElement} |
||
81 | */ |
||
82 | this.getTable = function() { |
||
83 | |||
84 | if($table === null) { |
||
85 | $table = document.createElement('table'); |
||
86 | $table.className = 'AknGrid AknGrid--unclickable select-options-table'; |
||
87 | |||
88 | var $thead = document.createElement('thead'); |
||
89 | var $thRow = document.createElement('tr'); |
||
90 | $thRow.className = 'AknGrid-bodyRow'; |
||
91 | var $thCl1 = document.createElement('th'); |
||
92 | $thCl1.className = 'AknGrid-headerCell'; |
||
93 | $thCl1.innerText = __('flagbit.table_attribute.simpleselect.key.label'); |
||
94 | var $thCl2 = document.createElement('th'); |
||
95 | $thCl2.className = 'AknGrid-headerCell'; |
||
96 | $thCl2.innerText = __('flagbit.table_attribute.simpleselect.value.label'); |
||
97 | |||
98 | $thRow.appendChild($thCl1); |
||
99 | $thRow.appendChild($thCl2); |
||
100 | $thead.appendChild($thRow); |
||
101 | $table.appendChild($thead); |
||
102 | |||
103 | var $tbody = document.createElement('tbody'); |
||
104 | $table.appendChild($tbody); |
||
105 | |||
106 | if($editable) { |
||
107 | var $tfoot = document.createElement('tfoot'); |
||
108 | var $tfRow = document.createElement('tr'); |
||
109 | var $tfCol = document.createElement('td'); |
||
110 | $tfCol.className = 'AknGrid-bodyCell field-cell'; |
||
111 | var $tfBut = document.createElement('button'); |
||
112 | |||
113 | $tfBut.innerText = __('label.attribute_option.add_option'); |
||
114 | $tfBut.type = 'button'; |
||
115 | $tfBut.className = 'btn AknButton AknButton--small pull-right'; |
||
116 | $tfBut.addEventListener('click', addRow); |
||
117 | $tfCol.colSpan = 3; |
||
118 | $tfCol.appendChild($tfBut); |
||
119 | $tfRow.appendChild($tfCol); |
||
120 | $tfoot.appendChild($tfRow); |
||
121 | $table.appendChild($tfoot); |
||
122 | } |
||
123 | |||
124 | $container.appendChild($table); |
||
125 | } |
||
126 | |||
127 | return $table.querySelector('tbody'); |
||
128 | }; |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @protected |
||
133 | */ |
||
134 | var addRow = function() { |
||
135 | |||
136 | var $row = document.createElement('tr'); |
||
137 | $row.appendChild(createTableColumn('')); |
||
138 | $row.appendChild(createTableColumn('')); |
||
139 | if($editable) { |
||
140 | $row.appendChild(createDeleteButton($row)); |
||
141 | } |
||
142 | |||
143 | this.getTable().appendChild($row); |
||
144 | |||
145 | notify(); |
||
146 | }.bind(this); |
||
147 | |||
148 | |||
149 | /** |
||
150 | * @protected |
||
151 | * @param {String} $text |
||
152 | */ |
||
153 | var createTableColumn = function($text) { |
||
154 | |||
155 | var $column = document.createElement('td'); |
||
156 | |||
157 | if($editable) { |
||
158 | var $input = document.createElement('input'); |
||
159 | $input.type = 'text'; |
||
160 | $input.value = $text; |
||
161 | $input.className = 'AknTextField'; |
||
162 | observeChanges($input); |
||
163 | $column.appendChild($input); |
||
164 | } |
||
165 | else { |
||
166 | $column.innerText = $text; |
||
167 | } |
||
168 | |||
169 | return $column; |
||
170 | }.bind(this); |
||
|
|||
171 | |||
172 | |||
173 | /** |
||
174 | * @protected |
||
175 | * @param {HTMLTableRowElement} $row |
||
176 | */ |
||
177 | var createDeleteButton = function($row) { |
||
178 | var $col = createTableColumn(); |
||
179 | $col.innerHTML = '<span class="btn btn-small AknButton AknButton--small"><i class="icon-remove"></i></span>'; |
||
180 | $col.querySelector('span').addEventListener('click', function() { |
||
181 | $row.parentNode.removeChild($row); |
||
182 | notify(); |
||
183 | }); |
||
184 | |||
185 | return $col; |
||
186 | }.bind(this); |
||
187 | |||
188 | |||
189 | /** |
||
190 | * @protected |
||
191 | * @param {HTMLInputElement} $input |
||
192 | */ |
||
193 | var observeChanges = function($input) { |
||
194 | $input.addEventListener('keyup', notify); |
||
195 | $input.addEventListener('blur', notify); |
||
196 | }.bind(this); |
||
197 | |||
198 | |||
199 | /** |
||
200 | * @protected |
||
201 | */ |
||
202 | var notify = function() { |
||
203 | |||
204 | this.observer.notify('update'); |
||
205 | }.bind(this); |
||
206 | }; |
||
207 | |||
208 | return JsonGeneratorRendererSelect; |
||
209 | } |
||
210 | ); |
||
211 |