Conditions | 1 |
Paths | 4 |
Total Lines | 321 |
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 | /** |
||
7 | ( function(window, document, $, wysiwyg, undefined ) { |
||
|
|||
8 | 'use strict'; |
||
9 | |||
10 | // Private variables |
||
11 | var toBeDestroyed = []; |
||
12 | var toBeInitialized = []; |
||
13 | var all = wysiwyg.all = {}; |
||
14 | |||
15 | // Private functions |
||
16 | |||
17 | /** |
||
18 | * Initializes any editors that weren't initialized because they didn't exist yet. |
||
19 | * |
||
20 | * @since 2.2.3 |
||
21 | * |
||
22 | * @return {void} |
||
23 | */ |
||
24 | function delayedInit() { |
||
25 | |||
26 | // Don't initialize until they've all been destroyed. |
||
27 | if ( 0 === toBeDestroyed.length ) { |
||
28 | toBeInitialized.forEach( function ( toInit ) { |
||
29 | toBeInitialized.splice( toBeInitialized.indexOf( toInit ), 1 ); |
||
30 | wysiwyg.init.apply( wysiwyg, toInit ); |
||
31 | } ); |
||
32 | } else { |
||
33 | window.setTimeout( delayedInit, 100 ); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Destroys any editors that weren't destroyed because they didn't exist yet. |
||
39 | * |
||
40 | * @since 2.2.3 |
||
41 | * |
||
42 | * @return {void} |
||
43 | */ |
||
44 | function delayedDestroy() { |
||
45 | toBeDestroyed.forEach( function( id ) { |
||
46 | toBeDestroyed.splice( toBeDestroyed.indexOf( id ), 1 ); |
||
47 | wysiwyg.destroy( id ); |
||
48 | } ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Gets the option data for a group (and initializes that data if it doesn't exist). |
||
53 | * |
||
54 | * @since 2.2.3 |
||
55 | * |
||
56 | * @param {object} data The group/field data. |
||
57 | * |
||
58 | * @return {object} Options data object for a group. |
||
59 | */ |
||
60 | function getGroupData( data ) { |
||
61 | var groupid = data.groupid; |
||
62 | var fieldid = data.fieldid; |
||
63 | |||
64 | if ( ! all[ groupid ] || ! all[ groupid ][ fieldid ] ) { |
||
65 | all[ groupid ] = all[ groupid ] || {}; |
||
66 | all[ groupid ][ fieldid ] = { |
||
67 | template : wp.template( 'cmb2-wysiwyg-' + groupid + '-' + fieldid ), |
||
68 | defaults : { |
||
69 | |||
70 | // Get the data from the template-wysiwyg initiation. |
||
71 | mce : $.extend( {}, tinyMCEPreInit.mceInit[ 'cmb2_i_' + groupid + fieldid ] ), |
||
72 | qt : $.extend( {}, tinyMCEPreInit.qtInit[ 'cmb2_i_' + groupid + fieldid ] ) |
||
73 | } |
||
74 | }; |
||
75 | // This is the template-wysiwyg data, and we do not want that to be initiated. |
||
76 | delete tinyMCEPreInit.mceInit[ 'cmb2_i_' + groupid + fieldid ]; |
||
77 | delete tinyMCEPreInit.qtInit[ 'cmb2_i_' + groupid + fieldid ]; |
||
78 | } |
||
79 | |||
80 | return all[ groupid ][ fieldid ]; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Initiates the tinyMCEPreInit options for a wysiwyg editor instance. |
||
85 | * |
||
86 | * @since 2.2.3 |
||
87 | * |
||
88 | * @param {object} options Options data object for the wysiwyg editor instance. |
||
89 | * |
||
90 | * @return {void} |
||
91 | */ |
||
92 | function initOptions( options ) { |
||
93 | var nameRegex = new RegExp( 'cmb2_n_' + options.groupid + options.fieldid, 'g' ); |
||
94 | var idRegex = new RegExp( 'cmb2_i_' + options.groupid + options.fieldid, 'g' ); |
||
95 | var prop, newSettings, newQTS; |
||
96 | |||
97 | // If no settings for this field. Clone from placeholder. |
||
98 | if ( 'undefined' === typeof( tinyMCEPreInit.mceInit[ options.id ] ) ) { |
||
99 | newSettings = $.extend( {}, options.defaults.mce ); |
||
100 | for ( prop in newSettings ) { |
||
101 | if ( 'string' === typeof( newSettings[ prop ] ) ) { |
||
102 | newSettings[ prop ] = newSettings[ prop ] |
||
103 | .replace( idRegex, options.id ) |
||
104 | .replace( nameRegex, options.name ); |
||
105 | } |
||
106 | } |
||
107 | tinyMCEPreInit.mceInit[ options.id ] = newSettings; |
||
108 | } |
||
109 | |||
110 | // If no Quicktag settings for this field. Clone from placeholder. |
||
111 | if ( 'undefined' === typeof( tinyMCEPreInit.qtInit[ options.id ] ) ) { |
||
112 | newQTS = $.extend( {}, options.defaults.qt ); |
||
113 | for ( prop in newQTS ) { |
||
114 | if ( 'string' === typeof( newQTS[ prop ] ) ) { |
||
115 | newQTS[ prop ] = newQTS[ prop ] |
||
116 | .replace( idRegex, options.id ) |
||
117 | .replace( nameRegex, options.name ); |
||
118 | } |
||
119 | } |
||
120 | tinyMCEPreInit.qtInit[ options.id ] = newQTS; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Initializes all group wysiwyg editors. Hooked to cmb_init. |
||
126 | * |
||
127 | * @since 2.2.3 |
||
128 | * |
||
129 | * @return {void} |
||
130 | */ |
||
131 | wysiwyg.initAll = function() { |
||
132 | var $this,data,initiated; |
||
133 | |||
134 | $( '.cmb2-wysiwyg-placeholder' ).each( function() { |
||
135 | $this = $( this ); |
||
136 | data = $this.data(); |
||
137 | |||
138 | if ( data.groupid ) { |
||
139 | |||
140 | data.id = $this.attr( 'id' ); |
||
141 | data.name = $this.attr( 'name' ); |
||
142 | data.value = $this.val(); |
||
143 | |||
144 | wysiwyg.init( $this, data, false ); |
||
145 | initiated = true; |
||
146 | } |
||
147 | } ); |
||
148 | |||
149 | if ( true === initiated ) { |
||
150 | window.QTags._buttonsInit(); |
||
151 | |||
152 | // Hook in our event callbacks. |
||
153 | $( document ) |
||
154 | .on( 'cmb2_add_row', wysiwyg.addRow ) |
||
155 | .on( 'cmb2_remove_group_row_start', wysiwyg.destroyRowEditors ) |
||
156 | .on( 'cmb2_shift_rows_start', wysiwyg.shiftStart ) |
||
157 | .on( 'cmb2_shift_rows_complete', wysiwyg.shiftComplete ); |
||
158 | } |
||
159 | }; |
||
160 | |||
161 | /** |
||
162 | * Initiates wysiwyg editors in a new group row. Hooked to cmb2_add_row. |
||
163 | * |
||
164 | * @since 2.2.3 |
||
165 | * |
||
166 | * @param {object} evt A jQuery-normalized event object. |
||
167 | * @param {object} $row A jQuery dom element object for the group row. |
||
168 | * |
||
169 | * @return {void} |
||
170 | */ |
||
171 | wysiwyg.addRow = function( evt, $row ) { |
||
172 | wysiwyg.initRow( $row ); |
||
173 | }; |
||
174 | |||
175 | /** |
||
176 | * Destroys wysiwyg editors in a group row when that row is removed. Hooked to cmb2_remove_group_row_start. |
||
177 | * |
||
178 | * @since 2.2.3 |
||
179 | * |
||
180 | * @param {object} evt A jQuery-normalized event object. |
||
181 | * @param {object} $btn A jQuery dom element object for the remove-row button. |
||
182 | * |
||
183 | * @return {void} |
||
184 | */ |
||
185 | wysiwyg.destroyRowEditors = function( evt, $btn ) { |
||
186 | wysiwyg.destroy( $btn.parents( '.cmb-repeatable-grouping' ).find( '.wp-editor-area' ).attr( 'id' ) ); |
||
187 | }; |
||
188 | |||
189 | /** |
||
190 | * When a row-shift starts, we need to destroy the wysiwyg editors for the group-rows being shuffled. |
||
191 | * |
||
192 | * @since 2.2.3 |
||
193 | * |
||
194 | * @param {object} evt A jQuery-normalized event object. |
||
195 | * @param {object} $btn A jQuery dom element object for the remove-row button. |
||
196 | * @param {object} $from A jQuery dom element object for the row being shifted from. |
||
197 | * @param {object} $to A jQuery dom element object for the row being shifted to. |
||
198 | * |
||
199 | * @return {void} |
||
200 | */ |
||
201 | wysiwyg.shiftStart = function( evt, $btn, $from, $to ) { |
||
202 | $from.add( $to ).find( '.wp-editor-wrap textarea' ).each( function() { |
||
203 | wysiwyg.destroy( $( this ).attr( 'id' ) ); |
||
204 | } ); |
||
205 | }; |
||
206 | |||
207 | /** |
||
208 | * When a row-shift completes, we need to re-init the wysiwyg editors for the group-rows being shuffled. |
||
209 | * |
||
210 | * @since 2.2.3 |
||
211 | * |
||
212 | * @param {object} evt A jQuery-normalized event object. |
||
213 | * @param {object} $btn A jQuery dom element object for the remove-row button. |
||
214 | * @param {object} $from A jQuery dom element object for the row being shifted from. |
||
215 | * @param {object} $to A jQuery dom element object for the row being shifted to. |
||
216 | * |
||
217 | * @return {void} |
||
218 | */ |
||
219 | wysiwyg.shiftComplete = function( evt, $btn, $from, $to ) { |
||
220 | $from.add( $to ).each( function() { |
||
221 | wysiwyg.initRow( $( this ) ); |
||
222 | } ); |
||
223 | }; |
||
224 | |||
225 | /** |
||
226 | * Initializes editors for a new CMB row. |
||
227 | * |
||
228 | * @since 2.2.3 |
||
229 | * |
||
230 | * @param {object} $row A jQuery dom element object for the group row. |
||
231 | * |
||
232 | * @return {void} |
||
233 | */ |
||
234 | wysiwyg.initRow = function( $row ) { |
||
235 | var $toReplace, data; |
||
236 | |||
237 | $row.find( '.cmb2-wysiwyg-inner-wrap' ).each( function() { |
||
238 | $toReplace = $( this ); |
||
239 | data = $toReplace.data(); |
||
240 | |||
241 | data.iterator = $row.data( 'iterator' ); |
||
242 | data.fieldid = data.id; |
||
243 | data.id = data.groupid + '_' + data.iterator + '_' + data.fieldid; |
||
244 | data.name = data.groupid + '[' + data.iterator + '][' + data.fieldid + ']'; |
||
245 | data.value = $toReplace.find( '.wp-editor-area' ).length ? $toReplace.find( '.wp-editor-area' ).val() : ''; |
||
246 | |||
247 | // The destroys might not have happened yet. Don't init until they have. |
||
248 | if ( 0 === toBeDestroyed.length ) { |
||
249 | |||
250 | wysiwyg.init( $toReplace, data ); |
||
251 | |||
252 | } else { |
||
253 | toBeInitialized.push( [$toReplace, data] ); |
||
254 | window.setTimeout( delayedInit, 100 ); |
||
255 | } |
||
256 | } ); |
||
257 | |||
258 | }; |
||
259 | |||
260 | /** |
||
261 | * Initiates a wysiwyg editor instance and replaces the passed dom element w/ the editor html. |
||
262 | * |
||
263 | * @since 2.2.3 |
||
264 | * |
||
265 | * @param {object} $toReplace A jQuery dom element which will be replaced with the wysiwyg editor. |
||
266 | * @param {object} data Data used to initate the editor. |
||
267 | * @param {bool} buttonsInit Whether to run QTags._buttonsInit() |
||
268 | * |
||
269 | * @return {void} |
||
270 | */ |
||
271 | wysiwyg.init = function( $toReplace, data, buttonsInit ) { |
||
272 | if ( ! data.groupid ) { |
||
273 | return false; |
||
274 | } |
||
275 | |||
276 | $.extend( data, getGroupData( data ) ); |
||
277 | |||
278 | initOptions( data ); |
||
279 | |||
280 | $toReplace.replaceWith( data.template( data ) ); |
||
281 | |||
282 | window.tinyMCE.init( tinyMCEPreInit.mceInit[ data.id ] ); |
||
283 | window.quicktags( tinyMCEPreInit.qtInit[ data.id ] ); |
||
284 | |||
285 | $( document.getElementById( data.id ) ).parents( '.wp-editor-wrap' ).removeClass( 'html-active' ).addClass( 'tmce-active' ); |
||
286 | |||
287 | if ( false !== buttonsInit ) { |
||
288 | window.QTags._buttonsInit(); |
||
289 | } |
||
290 | |||
291 | }; |
||
292 | |||
293 | /** |
||
294 | * Destroys a wysiwyg editor instance. |
||
295 | * |
||
296 | * @since 2.2.3 |
||
297 | * |
||
298 | * @param {string} id Editor id. |
||
299 | * |
||
300 | * @return {void} |
||
301 | */ |
||
302 | wysiwyg.destroy = function( id ) { |
||
303 | |||
304 | // The editor might not be initialized yet. But we need to destroy it once it is. |
||
305 | var editor = tinyMCE.get( id ); |
||
306 | |||
307 | if ( editor !== null && typeof( editor ) !== 'undefined' ) { |
||
308 | editor.destroy(); |
||
309 | |||
310 | if ( 'undefined' === typeof( tinyMCEPreInit.mceInit[ id ] ) ) { |
||
311 | delete tinyMCEPreInit.mceInit[ id ]; |
||
312 | } |
||
313 | |||
314 | if ( 'undefined' === typeof( tinyMCEPreInit.qtInit[ id ] ) ) { |
||
315 | delete tinyMCEPreInit.qtInit[ id ]; |
||
316 | } |
||
317 | |||
318 | } else if ( -1 === toBeDestroyed.indexOf( id ) ) { |
||
319 | toBeDestroyed.push( id ); |
||
320 | window.setTimeout( delayedDestroy, 100 ); |
||
321 | } |
||
322 | }; |
||
323 | |||
324 | // Hook in our event callbacks. |
||
325 | $( document ).on( 'cmb_init', wysiwyg.initAll ); |
||
326 | |||
327 | } )( window, document, jQuery, window.CMB2.wysiwyg ); |
||
328 |
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.