| 1 |
/** |
| 2 |
* Handles the Insert and Cancel events on TinyMCE and QuickTag Modals |
| 3 |
* |
| 4 |
* @since 1.9.6 |
| 5 |
* |
| 6 |
* @package ConvertKit |
| 7 |
* @author ConvertKit |
| 8 |
*/ |
| 9 |
|
| 10 |
jQuery( document ).ready( |
| 11 |
function( $ ) { |
| 12 |
|
| 13 |
// Cancel. |
| 14 |
$( 'body' ).on( |
| 15 |
'click', |
| 16 |
'form.convertkit-tinymce-popup button.close', |
| 17 |
function( e ) { |
| 18 |
|
| 19 |
// TinyMCE. |
| 20 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 21 |
tinymce.activeEditor.windowManager.close(); |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// Text Editor. |
| 26 |
convertKitQuickTagsModal.close(); |
| 27 |
|
| 28 |
} |
| 29 |
); |
| 30 |
|
| 31 |
// Insert. |
| 32 |
$( 'body' ).on( |
| 33 |
'click', |
| 34 |
'form.convertkit-tinymce-popup div.buttons input[type=button]', |
| 35 |
function( e ) { |
| 36 |
|
| 37 |
// Prevent default action. |
| 38 |
e.preventDefault(); |
| 39 |
|
| 40 |
// Get containing form. |
| 41 |
var form = $( this ).closest( 'form.convertkit-tinymce-popup' ); |
| 42 |
|
| 43 |
// Build Shortcode. |
| 44 |
var shortcode = '[' + $( 'input[name="shortcode"]', $( form ) ).val(), |
| 45 |
shortcodeClose = ( $( 'input[name="close_shortcode"]', $( form ) ).val() == '1' ? true : false ); |
| 46 |
|
| 47 |
$( 'input, select', $( form ) ).each( |
| 48 |
function( i ) { |
| 49 |
// Skip if no data-shortcode attribute. |
| 50 |
if ( typeof $( this ).data( 'shortcode' ) === 'undefined' ) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
// Skip if the value is empty. |
| 55 |
if ( ! $( this ).val() ) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
if ( $( this ).val().length == 0 ) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
// Get shortcode attribute. |
| 63 |
var key = $( this ).data( 'shortcode' ), |
| 64 |
trim = ( $( this ).data( 'trim' ) == '0' ? false : true ), |
| 65 |
val = $( this ).val(); |
| 66 |
|
| 67 |
// Skip if the shortcode is empty. |
| 68 |
if ( ! key.length ) { |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
// Trim the value, unless the shortcode attribute disables string trimming. |
| 73 |
if ( trim ) { |
| 74 |
val = val.trim(); |
| 75 |
} |
| 76 |
|
| 77 |
// Append attribute and value to shortcode string. |
| 78 |
shortcode += ' ' + key.trim() + '="' + val + '"'; |
| 79 |
} |
| 80 |
); |
| 81 |
|
| 82 |
// Close Shortcode. |
| 83 |
shortcode += ']'; |
| 84 |
|
| 85 |
// If the shortcode includes a closing element, append it now. |
| 86 |
if ( shortcodeClose ) { |
| 87 |
shortcode += '[/' + $( 'input[name="shortcode"]', $( form ) ).val() + ']'; |
| 88 |
} |
| 89 |
|
| 90 |
// Depending on the editor type, insert the shortcode. |
| 91 |
switch ( $( 'input[name="editor_type"]', $( form ) ).val() ) { |
| 92 |
case 'tinymce': |
| 93 |
// Sanity check that a Visual editor exists and is active. |
| 94 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 95 |
// Insert into editor. |
| 96 |
tinyMCE.activeEditor.execCommand( 'mceReplaceContent', false, shortcode ); |
| 97 |
|
| 98 |
// Close modal. |
| 99 |
tinyMCE.activeEditor.windowManager.close(); |
| 100 |
} |
| 101 |
break; |
| 102 |
|
| 103 |
case 'quicktags': |
| 104 |
// Insert into editor. |
| 105 |
QTags.insertContent( shortcode ); |
| 106 |
|
| 107 |
// Close modal. |
| 108 |
convertKitQuickTagsModal.close(); |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
); |
| 113 |
|
| 114 |
} |
| 115 |
); |
| 116 |
|
| 117 |
// QuickTags: Setup Backbone Modal and Template. |
| 118 |
if ( typeof wp !== 'undefined' && typeof wp.media !== 'undefined' ) { |
| 119 |
var convertKitQuickTagsModal = new wp.media.view.Modal( |
| 120 |
{ |
| 121 |
controller: { trigger: function() {} }, |
| 122 |
className: 'convertkit-quicktags-modal' |
| 123 |
} |
| 124 |
); |
| 125 |
var convertKitQuickTagsModalContent = wp.Backbone.View.extend( |
| 126 |
{ |
| 127 |
template: wp.template( 'convertkit-quicktags-modal' ) |
| 128 |
} |
| 129 |
); |
| 130 |
convertKitQuickTagsModal.content( new convertKitQuickTagsModalContent() ); |
| 131 |
} |
| 132 |
|