PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.6.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.6.6
3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 All 195 releases
convertkit / resources / backend / js / modal.js

modal.js in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.6.6, at resources/backend/js/modal.js

168 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 document.addEventListener(
11 'DOMContentLoaded',
12 function () {
13
14 // Cancel.
15 document.body.addEventListener(
16 'click',
17 function ( e ) {
18
19 // Check if a cancel button was clicked.
20 if ( e.target.matches( '#convertkit-modal-body div.mce-cancel button, #convertkit-modal-body div.mce-cancel button *, #convertkit-quicktags-modal .media-toolbar .media-toolbar-secondary button.cancel' ) ) {
21 // TinyMCE.
22 if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) {
23 tinymce.activeEditor.windowManager.close();
24 return;
25 }
26
27 // Text Editor.
28 // Close the QuickTags modal.
29 convertKitQuickTagsModal.close();
30 }
31 }
32 );
33
34 // Insert.
35 document.body.addEventListener(
36 'click',
37 function ( e ) {
38
39 // Check if an insert button was clicked.
40 if ( e.target.matches( '#convertkit-modal-body div.mce-insert button, #convertkit-modal-body div.mce-insert button *, #convertkit-quicktags-modal .media-toolbar .media-toolbar-primary button.button-primary' ) ) {
41 // Prevent default action.
42 e.preventDefault();
43
44 // Get containing form.
45 const form = document.querySelector( 'form.convertkit-tinymce-popup' );
46
47 // Build Shortcode.
48 let shortcode = '[' + form.querySelector( 'input[name="shortcode"]' ).value;
49 const shortcodeClose = form.querySelector( 'input[name="close_shortcode"]' ).value === '1';
50
51 // Iterate through form fields.
52 form.querySelectorAll( 'input, select' ).forEach(
53 function ( element ) {
54 // Skip if no data-shortcode attribute.
55 if ( ! element.dataset.shortcode ) {
56 return;
57 }
58
59 let val = '';
60
61 // If this is a color picker, #000000 will be submitted by the browser as the value
62 // even if no value / color was selected.
63 // Check the data-value attribute instead.
64 switch ( element.type ) {
65 case 'color':
66 val = element.dataset.value;
67 break;
68 default:
69 val = element.value;
70 break;
71 }
72
73 // Skip if the value is empty.
74 if ( ! val ) {
75 return;
76 }
77 if ( val.length === 0 ) {
78 return;
79 }
80
81 // Get shortcode attribute.
82 const key = element.dataset.shortcode;
83 const trim = element.dataset.trim !== '0';
84
85 // Skip if the shortcode is empty.
86 if ( ! key.length ) {
87 return;
88 }
89
90 // Trim the value, unless the shortcode attribute disables string trimming.
91 if ( trim ) {
92 val = val.trim();
93 }
94
95 // Append attribute and value to shortcode string.
96 shortcode += ' ' + key.trim() + '="' + val + '"';
97 }
98 );
99
100 // Close Shortcode.
101 shortcode += ']';
102
103 // If the shortcode includes a closing element, append it now.
104 if ( shortcodeClose ) {
105 shortcode += '[/' + form.querySelector( 'input[name="shortcode"]' ).value + ']';
106 }
107
108 // Depending on the editor type, insert the shortcode.
109 switch ( form.querySelector( 'input[name="editor_type"]' ).value ) {
110 case 'tinymce':
111 // Sanity check that a Visual editor exists and is active.
112 if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) {
113 // Insert into editor.
114 tinyMCE.activeEditor.execCommand( 'mceReplaceContent', false, shortcode );
115
116 // Close modal.
117 tinyMCE.activeEditor.windowManager.close();
118 }
119 break;
120
121 case 'quicktags':
122 // Insert into editor.
123 QTags.insertContent( shortcode );
124
125 // Close modal.
126 convertKitQuickTagsModal.close();
127 break;
128 }
129 }
130 }
131 );
132
133 }
134 );
135
136 // QuickTags: Setup Backbone Modal and Template.
137 if ( typeof wp !== 'undefined' && typeof wp.media !== 'undefined' ) {
138
139 // Declared globally, as used in this file and quicktags.js.
140 var convertKitQuickTagsModal = new wp.media.view.Modal(
141 {
142 controller: { trigger: function () {} },
143 className: 'convertkit-quicktags-modal'
144 }
145 );
146 const convertKitQuickTagsModalContent = wp.Backbone.View.extend(
147 {
148 template: wp.template( 'convertkit-quicktags-modal' )
149 }
150 );
151 convertKitQuickTagsModal.content( new convertKitQuickTagsModalContent() );
152
153 /**
154 * Resets the content of the convertKitQuickTagsModal when closing.
155 *
156 * If this isn't performed, switching from Text to Visual Editor for the same shortcode results
157 * code picking up data from the QuickTags modal, not the TinyMCE one, due to this 'stale'
158 * modal remaining in the DOM, resulting in e.g. the tabbed UI not loading correctly.
159 */
160 convertKitQuickTagsModal.on(
161 'close',
162 function ( e ) {
163 this.content( new convertKitQuickTagsModalContent() );
164 }
165 );
166
167 }
168