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