| 1 |
/** |
| 2 |
* Inserts shortcodes into TinyMCE and QuickTag instances when the TinyMCE |
| 3 |
* modal's Insert or Cancel buttons are clicked. |
| 4 |
* |
| 5 |
* For QuickTags, sets up a Backbone modal to look similar to the TinyMCE |
| 6 |
* modal. |
| 7 |
* |
| 8 |
* @package WPZincDashboardWidget |
| 9 |
* @author WP Zinc |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Handles the Insert and Cancel events on TinyMCE and QuickTag Modals |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
jQuery( document ).ready( |
| 18 |
function ( $ ) { |
| 19 |
|
| 20 |
// Cancel. |
| 21 |
$( 'body' ).on( |
| 22 |
'click', |
| 23 |
'#wpzinc-tinymce-modal div.mce-cancel button, .wpzinc-backbone-modal .media-frame-toolbar .media-toolbar button.cancel', |
| 24 |
function ( e ) { |
| 25 |
|
| 26 |
// TinyMCE. |
| 27 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 28 |
tinymce.activeEditor.windowManager.close(); |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
// Text Editor. |
| 33 |
wpZincModal.close(); |
| 34 |
|
| 35 |
// Reset the non-TinyMCE modal content. |
| 36 |
// If we don't do this, switching from Text to Visual Editor for the same shortcode results |
| 37 |
// code picking up data from the QuickTags modal, not the TinyMCE one. |
| 38 |
if ( typeof wpZincModal !== 'undefined' ) { |
| 39 |
wpZincModal.content( new wpZincModalContent() ); |
| 40 |
} |
| 41 |
|
| 42 |
} |
| 43 |
); |
| 44 |
|
| 45 |
// Insert. |
| 46 |
$( 'body' ).on( |
| 47 |
'click', |
| 48 |
'#wpzinc-tinymce-modal div.mce-insert button, .wpzinc-backbone-modal .media-frame-toolbar .media-toolbar button.insert', |
| 49 |
function ( e ) { |
| 50 |
|
| 51 |
// Prevent default action. |
| 52 |
e.preventDefault(); |
| 53 |
|
| 54 |
// Get containing form. |
| 55 |
var form = $( 'form.wpzinc-tinymce-popup' ); |
| 56 |
|
| 57 |
// Build Shortcode. |
| 58 |
var shortcode = '[' + $( 'input[name="shortcode"]', $( form ) ).val(), |
| 59 |
shortcodeEnclosedContent = false; |
| 60 |
|
| 61 |
$( 'input, select, textarea', $( form ) ).each( |
| 62 |
function ( i ) { |
| 63 |
// Skip if no data-shortcode attribute. |
| 64 |
if ( typeof $( this ).data( 'shortcode' ) === 'undefined' ) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
// Skip if the value is empty. |
| 69 |
if ( ! $( this ).val() ) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
if ( $( this ).val().length == 0 ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
// Get shortcode attribute. |
| 77 |
var key = $( this ).data( 'shortcode' ), |
| 78 |
trim = ( $( this ).data( 'trim' ) == '0' ? false : true ), |
| 79 |
val = $( this ).val(); |
| 80 |
|
| 81 |
// Skip if the shortcode is empty. |
| 82 |
if ( ! key.length ) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
// If the shortcode attribute is within curly braces, the shortcode attribute |
| 87 |
// is the value of another field. |
| 88 |
if ( key.search( '}' ) > -1 && key.search( '{' ) > -1 ) { |
| 89 |
// Remove curly braces. |
| 90 |
key = key.replace( |
| 91 |
/{|}/gi, |
| 92 |
function ( x ) { |
| 93 |
return ''; |
| 94 |
} |
| 95 |
); |
| 96 |
|
| 97 |
// Get value of input/select, which will form our attribute. |
| 98 |
key = $( key, $( this ).parent().parent() ).val(); |
| 99 |
} |
| 100 |
|
| 101 |
// If a prepend is specified, prepend the key with it now. |
| 102 |
if ( typeof ( $( this ).data( 'shortcode-prepend' ) ) !== 'undefined' ) { |
| 103 |
key = $( this ).data( 'shortcode-prepend' ) + key; |
| 104 |
} |
| 105 |
|
| 106 |
// If the value is an array (i.e. from selectize), implode it now. |
| 107 |
if ( Array.isArray( val ) ) { |
| 108 |
val = val.join( ',' ); |
| 109 |
} |
| 110 |
|
| 111 |
// Trim the value, unless the shortcode attribute disables string trimming. |
| 112 |
if ( trim ) { |
| 113 |
val = val.trim(); |
| 114 |
} |
| 115 |
|
| 116 |
// Skip if the shortcode attribute will be enclosed in the shortcode content. |
| 117 |
if ( $( this ).data( 'enclose' ) == '1' ) { |
| 118 |
shortcodeEnclosedContent = val; |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
// Append attribute and value to shortcode string. |
| 123 |
shortcode += ' ' + key.trim() + '="' + val + '"'; |
| 124 |
} |
| 125 |
); |
| 126 |
|
| 127 |
// Close Shortcode. |
| 128 |
shortcode += ']'; |
| 129 |
|
| 130 |
// If the shortcode includes enclosed content, append it now and close the shortcode. |
| 131 |
if ( shortcodeEnclosedContent ) { |
| 132 |
shortcode += shortcodeEnclosedContent + '[/' + $( 'input[name="shortcode"]', $( form ) ).val() + ']'; |
| 133 |
} |
| 134 |
|
| 135 |
// Depending on the editor type, insert the shortcode. |
| 136 |
let editor_type = $( 'input[name="editor_type"]', $( form ) ).val(); |
| 137 |
switch ( editor_type ) { |
| 138 |
case 'tinymce': |
| 139 |
// Sanity check that a Visual editor exists and is active. |
| 140 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 141 |
// Insert into editor. |
| 142 |
tinyMCE.activeEditor.execCommand( 'mceReplaceContent', false, shortcode ); |
| 143 |
|
| 144 |
// Close modal. |
| 145 |
tinyMCE.activeEditor.windowManager.close(); |
| 146 |
} |
| 147 |
break; |
| 148 |
|
| 149 |
case 'quicktags': |
| 150 |
// Insert into editor. |
| 151 |
QTags.insertContent( shortcode ); |
| 152 |
|
| 153 |
// Close modal. |
| 154 |
wpZincModal.close(); |
| 155 |
|
| 156 |
// Reset the modal content. |
| 157 |
// If we don't do this, switching from Text to Visual Editor for the same shortcode results |
| 158 |
// code picking up data from the QuickTags modal, not the TinyMCE one. |
| 159 |
if ( typeof wpZincModal !== 'undefined' ) { |
| 160 |
wpZincModal.content( new wpZincModalContent() ); |
| 161 |
} |
| 162 |
break; |
| 163 |
|
| 164 |
default: |
| 165 |
// Insert into selector. |
| 166 |
$( editor_type ).val( shortcode ); |
| 167 |
|
| 168 |
// Close modal. |
| 169 |
wpZincModal.close(); |
| 170 |
|
| 171 |
// Reset the modal content. |
| 172 |
// If we don't do this, switching from Text to Visual Editor for the same shortcode results |
| 173 |
// code picking up data from the QuickTags modal, not the TinyMCE one. |
| 174 |
if ( typeof wpZincModal !== 'undefined' ) { |
| 175 |
wpZincModal.content( new wpZincModalContent() ); |
| 176 |
} |
| 177 |
break; |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
); |
| 182 |
|
| 183 |
} |
| 184 |
); |
| 185 |
|
| 186 |
// QuickTags: Setup Backbone Modal and Template. |
| 187 |
if ( typeof wp !== 'undefined' && typeof wp.media !== 'undefined' ) { |
| 188 |
var wpZincModal = new wp.media.view.Modal( |
| 189 |
{ |
| 190 |
controller: { trigger: function () {} }, |
| 191 |
className: 'wpzinc-backbone-modal' |
| 192 |
} |
| 193 |
); |
| 194 |
var wpZincModalContent = wp.Backbone.View.extend( |
| 195 |
{ |
| 196 |
template: wp.template( 'wpzinc-modal' ) |
| 197 |
} |
| 198 |
); |
| 199 |
wpZincModal.content( new wpZincModalContent() ); |
| 200 |
} |
| 201 |
|