Total Complexity | 63 |
Complexity/F | 1.5 |
Lines of Code | 387 |
Function Count | 42 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like +/main.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** global: wp, pollux, CodeMirror */ |
||
3 | pollux.dependency = {}; |
||
4 | pollux.editors = {}; |
||
5 | pollux.featured = {}; |
||
6 | pollux.metabox = {}; |
||
7 | pollux.tabs = {}; |
||
8 | |||
9 | /** |
||
10 | * @return bool |
||
11 | */ |
||
12 | pollux.classListAction = function( bool ) |
||
13 | { |
||
14 | return bool ? 'add' : 'remove'; |
||
15 | }; |
||
16 | |||
17 | /** |
||
18 | * @return void |
||
19 | */ |
||
20 | pollux.dependency.activate = function( el ) |
||
21 | { |
||
22 | pollux.dependency.updateButtonText( el, pollux.vars.l10n.pluginActivatingLabel ); |
||
23 | return pollux.dependency.ajax( 'pollux/dependency/activate', el ); |
||
24 | }; |
||
25 | |||
26 | /** |
||
27 | * @return void |
||
28 | */ |
||
29 | pollux.dependency.ajax = function( action, el ) |
||
30 | { |
||
31 | var args = pollux.dependency.getAjaxOptions( el ); |
||
32 | var options = { |
||
33 | success: args.success, |
||
34 | error: args.error, |
||
35 | }; |
||
36 | delete args.success; |
||
37 | delete args.error; |
||
38 | options.data = args; |
||
39 | wp.ajax.send( action, options ); |
||
40 | }; |
||
41 | |||
42 | /** |
||
43 | * @return object |
||
44 | */ |
||
45 | pollux.dependency.getAjaxOptions = function( el ) |
||
46 | { |
||
47 | return { |
||
48 | _ajax_nonce: wp.updates.ajaxNonce, |
||
49 | error: pollux.dependency.onError.bind( el ), |
||
50 | plugin: el.getAttribute( 'data-plugin' ), |
||
51 | slug: el.getAttribute( 'data-slug' ), |
||
52 | success: pollux.dependency.onSuccess.bind( el ), |
||
53 | }; |
||
54 | }; |
||
55 | |||
56 | /** |
||
57 | * @return void |
||
58 | */ |
||
59 | pollux.dependency.init = function() |
||
60 | { |
||
61 | pollux.dependency.buttons = document.querySelectorAll( '.pollux-notice a.button' ); |
||
62 | [].forEach.call( pollux.dependency.buttons, function( button ) { |
||
63 | button.addEventListener( 'click', pollux.dependency.onClick ); |
||
64 | }); |
||
65 | }; |
||
66 | |||
67 | /** |
||
68 | * @return void |
||
69 | */ |
||
70 | pollux.dependency.install = function( el, args ) |
||
71 | { |
||
72 | pollux.dependency.updateButtonText( el, wp.updates.l10n.pluginInstallingLabel ); |
||
73 | return wp.updates.ajax( 'install-plugin', args ); |
||
74 | }; |
||
75 | |||
76 | /** |
||
77 | * @return void |
||
78 | */ |
||
79 | pollux.dependency.onClick = function( ev ) |
||
80 | { |
||
81 | var action = this.href.match(/action=([^&]+)/); |
||
82 | if( action === null )return; |
||
1 ignored issue
–
show
|
|||
83 | action = action[1].split('-')[0]; |
||
84 | if( !pollux.dependency[action] )return; |
||
1 ignored issue
–
show
|
|||
85 | this.blur(); |
||
86 | ev.preventDefault(); |
||
87 | if( this.classList.contains( 'updating-message' ))return; |
||
1 ignored issue
–
show
|
|||
88 | pollux.dependency[action]( this, pollux.dependency.getAjaxOptions( this )); |
||
89 | }; |
||
90 | |||
91 | /** |
||
92 | * @return void |
||
93 | */ |
||
94 | pollux.dependency.onError = function( response ) |
||
95 | { |
||
96 | window.location = this.href; |
||
97 | }; |
||
98 | |||
99 | /** |
||
100 | * @return void |
||
101 | */ |
||
102 | pollux.dependency.onSuccess = function( response ) |
||
103 | { |
||
104 | var el = this; |
||
105 | if( response.update ) { |
||
106 | return pollux.dependency.ajax( 'pollux/dependency/updated', el ); |
||
107 | } |
||
108 | pollux.dependency.setUpdatedMessage( el ); |
||
109 | if( response.install ) { |
||
110 | el.innerHTML = wp.updates.l10n.pluginInstalledLabel.replace( '%s', response.pluginName ); |
||
111 | } |
||
112 | if( response.updated ) { |
||
113 | el.innerHTML = wp.updates.l10n.updatedLabel.replace( '%s', response.pluginName ); |
||
114 | } |
||
115 | if( response.activateUrl ) { |
||
116 | setTimeout( function() { |
||
117 | pollux.dependency.setActivateButton( el, response ); |
||
118 | }, 1000 ); |
||
119 | } |
||
120 | if( response.activate ) { |
||
121 | el.innerHTML = pollux.vars.l10n.pluginActivatedLabel.replace( '%s', response.pluginName ); |
||
122 | setTimeout( function() { |
||
123 | var notice = el.closest( '.pollux-notice' ); |
||
124 | if( pollux.dependency.buttons.length < 2 ) { |
||
125 | pollux.editors.all.forEach( function( editor, index ) { |
||
126 | pollux.editors.enable( index ); |
||
127 | }); |
||
128 | notice.parentNode.removeChild( notice ); |
||
129 | } |
||
130 | else { |
||
131 | var plugin = notice.querySelector( '.plugin-' + response.slug ); |
||
132 | plugin.parentNode.removeChild( plugin ); |
||
133 | el.parentNode.removeChild( el ); |
||
134 | } |
||
135 | }, 1000 ); |
||
136 | } |
||
137 | }; |
||
138 | |||
139 | /** |
||
140 | * @return void |
||
141 | */ |
||
142 | pollux.dependency.setActivateButton = function( el, response ) |
||
143 | { |
||
144 | el.classList.remove( 'updated-message' ); |
||
145 | el.classList.remove( 'button-disabled' ); |
||
146 | el.classList.add( 'button-primary' ); |
||
147 | el.href = response.activateUrl; |
||
148 | el.innerHTML = wp.updates.l10n.activatePluginLabel.replace( '%s', response.pluginName ); |
||
149 | }; |
||
150 | |||
151 | /** |
||
152 | * @return void |
||
153 | */ |
||
154 | pollux.dependency.setUpdatedMessage = function( el ) |
||
155 | { |
||
156 | el.classList.remove( 'updating-message' ); |
||
157 | el.classList.add( 'updated-message' ); |
||
158 | el.classList.add( 'button-disabled' ); |
||
159 | }; |
||
160 | |||
161 | /** |
||
162 | * @return void |
||
163 | */ |
||
164 | pollux.dependency.updateButtonText = function( el, text ) |
||
165 | { |
||
166 | var label = text.replace( '%s', el.getAttribute( 'data-name' )); |
||
167 | if( el.innerHTML !== label ) { |
||
168 | el.innerHTML = label; |
||
169 | el.classList.add( 'updating-message' ); |
||
170 | } |
||
171 | }; |
||
172 | |||
173 | /** |
||
174 | * @return void |
||
175 | */ |
||
176 | pollux.dependency.upgrade = function( el, args ) |
||
177 | { |
||
178 | pollux.dependency.updateButtonText( el, wp.updates.l10n.updatingLabel ); |
||
179 | return wp.updates.ajax( 'update-plugin', args ); |
||
180 | }; |
||
181 | |||
182 | /** |
||
183 | * @return void |
||
184 | */ |
||
185 | pollux.editors.disable = function( index ) |
||
186 | { |
||
187 | pollux.editors.all[index].setOption( 'theme', 'disabled' ); |
||
188 | pollux.editors.all[index].setOption( 'readOnly', 'nocursor' ); |
||
189 | pollux.editors.all[index].getTextArea().readOnly = true; |
||
190 | pollux.editors.all[index].refresh(); |
||
191 | }; |
||
192 | |||
193 | /** |
||
194 | * @return void |
||
195 | */ |
||
196 | pollux.editors.enable = function( index ) |
||
197 | { |
||
198 | pollux.editors.all[index].setOption( 'theme', 'pollux' ); |
||
199 | pollux.editors.all[index].setOption( 'readOnly', false ); |
||
200 | pollux.editors.all[index].getTextArea().readOnly = false; |
||
201 | pollux.editors.all[index].refresh(); |
||
202 | }; |
||
203 | |||
204 | /** |
||
205 | * @return void |
||
206 | */ |
||
207 | pollux.editors.init = function() |
||
208 | { |
||
209 | pollux.editors.all = []; |
||
210 | [].forEach.call( document.querySelectorAll( '.pollux-code' ), function( editor, index ) { |
||
211 | pollux.editors.all[index] = CodeMirror.fromTextArea( editor, { |
||
212 | gutters: ['CodeMirror-lint-markers'], |
||
213 | highlightSelectionMatches: { wordsOnly: true }, |
||
214 | lineNumbers: true, |
||
215 | lint: true, |
||
216 | mode: 'text/yaml', |
||
217 | showInvisibles: true, |
||
218 | showTrailingSpace: true, |
||
219 | styleActiveLine: true, |
||
220 | tabSize: 2, |
||
221 | theme: 'pollux', |
||
222 | viewportMargin: Infinity, |
||
223 | }); |
||
224 | pollux.editors.all[index].setOption( 'extraKeys', { |
||
225 | Tab: function( cm ) { |
||
226 | var spaces = Array( cm.getOption( 'indentUnit' ) + 1 ).join( ' ' ); |
||
227 | cm.replaceSelection( spaces ); |
||
228 | }, |
||
229 | }); |
||
230 | pollux.editors.all[index].display.wrapper.setAttribute( 'data-disabled', editor.getAttribute( 'data-disabled' )); |
||
231 | if( editor.readOnly ) { |
||
232 | pollux.editors.disable( index ); |
||
233 | } |
||
234 | }); |
||
235 | }; |
||
236 | |||
237 | /** |
||
238 | * @return void |
||
239 | */ |
||
240 | pollux.featured.init = function() |
||
241 | { |
||
242 | jQuery( '#postimagediv' ) |
||
243 | .on( 'click', '#pollux-set-featured', function( ev ) { |
||
244 | ev.preventDefault(); |
||
245 | wp.media.view.settings.post.featuredImageId = Math.round( jQuery( '#featured' ).val() ); |
||
246 | pollux.featured.frame = wp.media.featuredImage.frame; |
||
247 | pollux.featured.frame().open(); |
||
248 | }) |
||
249 | .on( 'click', '#pollux-remove-featured', function( ev ) { |
||
250 | ev.preventDefault(); |
||
251 | pollux.featured.set(-1); |
||
252 | }); |
||
253 | }; |
||
254 | |||
255 | /** |
||
256 | * @return void |
||
257 | */ |
||
258 | pollux.featured.select = function() |
||
259 | { |
||
260 | if( !wp.media.view.settings.post.featuredImageId )return; |
||
261 | var selection = this.get( 'selection' ).single(); |
||
262 | pollux.featured.set( selection ? selection.id : -1 ); |
||
263 | }; |
||
264 | |||
265 | /** |
||
266 | * @return void |
||
267 | */ |
||
268 | pollux.featured.set = function( id ) |
||
269 | { |
||
270 | wp.media.view.settings.post.featuredImageId = Math.round( id ); |
||
271 | wp.media.post( 'pollux/archives/featured/html', { |
||
272 | _wpnonce: document.querySelector( '#_wpnonce' ).value, |
||
273 | post_type: document.querySelector( '#archive-type' ).value, |
||
274 | thumbnail_id: id, |
||
275 | }).done( function( html ) { |
||
276 | document.querySelector( '#postimagediv > .inside' ).innerHTML = html; |
||
277 | }); |
||
278 | }; |
||
279 | |||
280 | /** |
||
281 | * @return bool |
||
282 | */ |
||
283 | pollux.metabox.hasValue = function( el ) |
||
284 | { |
||
285 | if( el.type === 'checkbox' ) { |
||
286 | return el.checked === true; |
||
287 | } |
||
288 | return el.value !== ''; |
||
289 | }; |
||
290 | |||
291 | /** |
||
292 | * @return void |
||
293 | */ |
||
294 | pollux.metabox.init = function() |
||
295 | { |
||
296 | var depends = document.querySelectorAll( '.rwmb-input [data-depends]' ); |
||
297 | [].forEach.call( depends, function( el ) { |
||
298 | var dependency = pollux.metabox.setVisibility( el ); |
||
299 | var event = dependency.type === 'checkbox' ? 'change' : 'keyup'; |
||
300 | dependency.addEventListener( event, function() { |
||
301 | pollux.metabox.setVisibility( el ); |
||
302 | }); |
||
303 | }); |
||
304 | }; |
||
305 | |||
306 | /** |
||
307 | * @return element |
||
308 | */ |
||
309 | pollux.metabox.setVisibility = function( el ) |
||
310 | { |
||
311 | var dependency = document.getElementById( el.getAttribute( 'data-depends' )); |
||
312 | var action = pollux.classListAction( !pollux.metabox.hasValue( dependency )); |
||
313 | el.closest( '.rwmb-field' ).classList[action]( 'hidden' ); |
||
314 | return dependency; |
||
315 | }; |
||
316 | |||
317 | /** |
||
318 | * @return void |
||
319 | */ |
||
320 | pollux.tabs.init = function() |
||
321 | { |
||
322 | pollux.tabs.active = document.querySelector( '#pollux-active-tab' ); |
||
323 | pollux.tabs.referrer = document.querySelector( 'input[name="_wp_http_referer"]' ); |
||
324 | pollux.tabs.tabs = document.querySelectorAll( '.pollux-tabs a' ); |
||
325 | pollux.tabs.views = document.querySelectorAll( '.pollux-config .form-table' ); |
||
326 | |||
327 | [].forEach.call( pollux.tabs.tabs, function( tab, index ) { |
||
328 | var active = location.hash ? tab.getAttribute( 'href' ).slice(1) === location.hash.slice(2) : index === 0; |
||
329 | if( active ) { |
||
330 | pollux.tabs.setTab( tab ); |
||
331 | } |
||
332 | tab.addEventListener( 'click', pollux.tabs.onClick ); |
||
333 | tab.addEventListener( 'touchend', pollux.tabs.onClick ); |
||
334 | }); |
||
335 | }; |
||
336 | |||
337 | /** |
||
338 | * @return void |
||
339 | */ |
||
340 | pollux.tabs.onClick = function( ev ) |
||
341 | { |
||
342 | ev.preventDefault(); |
||
343 | this.blur(); |
||
344 | pollux.tabs.setTab( this ); |
||
345 | location.hash = '!' + this.getAttribute( 'href' ).slice(1); |
||
346 | }; |
||
347 | |||
348 | /** |
||
349 | * @return void |
||
350 | */ |
||
351 | pollux.tabs.setReferrer = function( index ) |
||
352 | { |
||
353 | var referrerUrl = pollux.tabs.referrer.value.split('#')[0] + '#!' + pollux.tabs.views[index].id; |
||
354 | pollux.tabs.referrer.value = referrerUrl; |
||
355 | }; |
||
356 | |||
357 | /** |
||
358 | * @return void |
||
359 | */ |
||
360 | pollux.tabs.setTab = function( el ) |
||
361 | { |
||
362 | [].forEach.call( pollux.tabs.tabs, function( tab, index ) { |
||
363 | var action = pollux.classListAction( tab === el ); |
||
364 | if( action === 'add' ) { |
||
365 | pollux.tabs.active.value = pollux.tabs.views[index].id; |
||
366 | pollux.tabs.setReferrer( index ); |
||
367 | pollux.tabs.setView( index ); |
||
368 | } |
||
369 | tab.classList[action]( 'nav-tab-active' ); |
||
370 | }); |
||
371 | }; |
||
372 | |||
373 | /** |
||
374 | * @return void |
||
375 | */ |
||
376 | pollux.tabs.setView = function( idx ) |
||
377 | { |
||
378 | [].forEach.call( pollux.tabs.views, function( view, index ) { |
||
379 | var action = pollux.classListAction( index !== idx ); |
||
380 | view.classList[action]( 'ui-tabs-hide' ); |
||
381 | }); |
||
382 | }; |
||
383 | |||
384 | jQuery(function() { |
||
385 | for( var key in pollux ) { |
||
386 | if( !pollux[key].hasOwnProperty( 'init' ))continue; |
||
1 ignored issue
–
show
|
|||
387 | pollux[key].init(); |
||
388 | } |
||
389 | }); |
||
390 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.