GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 699b70...879176 )
by Chris
13:23
created

vendor/cmb2/js/cmb2-wysiwyg.js   B

Complexity

Total Complexity 39
Complexity/F 2.05

Size

Lines of Code 324
Function Count 19

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 64
dl 0
loc 324
rs 8.2857
c 0
b 0
f 0
wmc 39
mnd 3
bc 38
fnc 19
bpm 2
cpm 2.0526
noi 9

13 Functions

Rating   Name   Duplication   Size   Complexity  
B wysiwyg.destroy 0 21 6
A cmb2-wysiwyg.js ➔ delayedInit 0 12 2
A wysiwyg.init 0 21 3
B 0 321 1
C cmb2-wysiwyg.js ➔ initOptions 0 31 7
A cmb2-wysiwyg.js ➔ delayedDestroy 0 6 1
B wysiwyg.initRow 0 25 1
B wysiwyg.initAll 0 29 2
A wysiwyg.destroyRowEditors 0 3 1
A wysiwyg.shiftComplete 0 5 1
A wysiwyg.addRow 0 3 1
A cmb2-wysiwyg.js ➔ getGroupData 0 22 3
A wysiwyg.shiftStart 0 5 1
1
/**
2
 * Used for WYSIWYG logic
3
 */
4
window.CMB2 = window.CMB2 || {};
5
window.CMB2.wysiwyg = window.CMB2.wysiwyg || {};
6
7
( function(window, document, $, wysiwyg, undefined ) {
0 ignored issues
show
Unused Code introduced by
The parameter undefined is not used and could be removed.

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.

Loading history...
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 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 ),
0 ignored issues
show
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
68
				defaults : {
69
70
					// Get the data from the template-wysiwyg initiation.
71
					mce : $.extend( {}, tinyMCEPreInit.mceInit[ 'cmb2_i_' + groupid + fieldid ] ),
0 ignored issues
show
Bug introduced by
The variable tinyMCEPreInit seems to be never declared. If this is a global, consider adding a /** global: tinyMCEPreInit */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 ] ) ) {
0 ignored issues
show
Bug introduced by
The variable tinyMCEPreInit seems to be never declared. If this is a global, consider adding a /** global: tinyMCEPreInit */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 ] );
0 ignored issues
show
Bug introduced by
The variable tinyMCEPreInit seems to be never declared. If this is a global, consider adding a /** global: tinyMCEPreInit */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if false !== buttonsInit is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
288
			window.QTags._buttonsInit();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The variable tinyMCE seems to be never declared. If this is a global, consider adding a /** global: tinyMCE */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
306
307
		if ( editor !== null && typeof( editor ) !== 'undefined' ) {
308
			editor.destroy();
309
310
			if ( 'undefined' === typeof( tinyMCEPreInit.mceInit[ id ] ) ) {
0 ignored issues
show
Bug introduced by
The variable tinyMCEPreInit seems to be never declared. If this is a global, consider adding a /** global: tinyMCEPreInit */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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