| | 1 | /* global tinymce, switchEditors */ |
| | 2 | /* eslint consistent-this: [ "error", "control" ] */ |
| | 3 | wp.textWidgets = ( function( $ ) { |
| | 4 | 'use strict'; |
| | 5 | |
| | 6 | var component = {}; |
| | 7 | |
| | 8 | /** |
| | 9 | * Text widget control. |
| | 10 | * |
| | 11 | * @class TextWidgetControl |
| | 12 | * @constructor |
| | 13 | * @abstract |
| | 14 | */ |
| | 15 | component.TextWidgetControl = Backbone.View.extend({ |
| | 16 | |
| | 17 | /** |
| | 18 | * View events. |
| | 19 | * |
| | 20 | * @type {Object} |
| | 21 | */ |
| | 22 | events: {}, |
| | 23 | |
| | 24 | /** |
| | 25 | * Initialize. |
| | 26 | * |
| | 27 | * @param {Object} options - Options. |
| | 28 | * @param {Backbone.Model} options.model - Model. |
| | 29 | * @param {jQuery} options.el - Control container element. |
| | 30 | * @returns {void} |
| | 31 | */ |
| | 32 | initialize: function initialize( options ) { |
| | 33 | var control = this; |
| | 34 | |
| | 35 | if ( ! options.el ) { |
| | 36 | throw new Error( 'Missing options.el' ); |
| | 37 | } |
| | 38 | |
| | 39 | Backbone.View.prototype.initialize.call( control, options ); |
| | 40 | |
| | 41 | /* |
| | 42 | * Create a container element for the widget control fields. |
| | 43 | * This is inserted into the DOM immediately before the the .widget-content |
| | 44 | * element because the contents of this element are essentially "managed" |
| | 45 | * by PHP, where each widget update cause the entire element to be emptied |
| | 46 | * and replaced with the rendered output of WP_Widget::form() which is |
| | 47 | * sent back in Ajax request made to save/update the widget instance. |
| | 48 | * To prevent a "flash of replaced DOM elements and re-initialized JS |
| | 49 | * components", the JS template is rendered outside of the normal form |
| | 50 | * container. |
| | 51 | */ |
| | 52 | control.fieldContainer = $( '<div class="text-widget-fields"></div>' ); |
| | 53 | control.fieldContainer.html( wp.template( 'widget-text-control-fields' ) ); |
| | 54 | control.widgetContentContainer = control.$el.find( '.widget-content:first' ); |
| | 55 | control.widgetContentContainer.before( control.fieldContainer ); |
| | 56 | |
| | 57 | control.fields = { |
| | 58 | title: control.fieldContainer.find( '.title' ), |
| | 59 | text: control.fieldContainer.find( '.text' ) |
| | 60 | }; |
| | 61 | |
| | 62 | // Sync input fields to hidden sync fields which actually get sent to the server. |
| | 63 | _.each( control.fields, function( fieldInput, fieldName ) { |
| | 64 | fieldInput.on( 'input change', function updateSyncField() { |
| | 65 | var syncInput = control.widgetContentContainer.find( 'input[type=hidden].' + fieldName ); |
| | 66 | if ( syncInput.val() !== $( this ).val() ) { |
| | 67 | syncInput.val( $( this ).val() ); |
| | 68 | syncInput.trigger( 'change' ); |
| | 69 | } |
| | 70 | }); |
| | 71 | |
| | 72 | // Note that syncInput cannot be re-used because it will be destroyed with each widget-updated event. |
| | 73 | fieldInput.val( control.widgetContentContainer.find( 'input[type=hidden].' + fieldName ).val() ); |
| | 74 | }); |
| | 75 | }, |
| | 76 | |
| | 77 | /** |
| | 78 | * Update input fields from the sync fields. |
| | 79 | * |
| | 80 | * This function is called at the widget-updated and widget-synced events. |
| | 81 | * A field will only be updated if it is not currently focused, to avoid |
| | 82 | * overwriting content that the user is entering. |
| | 83 | * |
| | 84 | * @returns {void} |
| | 85 | */ |
| | 86 | updateFields: function updateFields() { |
| | 87 | var control = this, syncInput; |
| | 88 | |
| | 89 | if ( ! control.fields.title.is( document.activeElement ) ) { |
| | 90 | syncInput = control.widgetContentContainer.find( 'input[type=hidden].title' ); |
| | 91 | control.fields.title.val( syncInput.val() ); |
| | 92 | } |
| | 93 | |
| | 94 | syncInput = control.widgetContentContainer.find( 'input[type=hidden].text' ); |
| | 95 | if ( control.fields.text.is( ':visible' ) ) { |
| | 96 | if ( ! control.fields.text.is( document.activeElement ) ) { |
| | 97 | control.fields.text.val( syncInput.val() ); |
| | 98 | } |
| | 99 | } else if ( control.editor && ! control.editorFocused && syncInput.val() !== control.fields.text.val() ) { |
| | 100 | control.editor.setContent( wp.editor.autop( syncInput.val() ) ); |
| | 101 | } |
| | 102 | }, |
| | 103 | |
| | 104 | /** |
| | 105 | * Initialize editor. |
| | 106 | * |
| | 107 | * @returns {void} |
| | 108 | */ |
| | 109 | initializeEditor: function initializeEditor() { |
| | 110 | var control = this, changeDebounceDelay = 1000, iframeKeepAliveInterval = 1000, id, textarea, restoreTextMode = false; |
| | 111 | textarea = control.fields.text; |
| | 112 | id = textarea.attr( 'id' ); |
| | 113 | |
| | 114 | /** |
| | 115 | * Build (or re-build) the visual editor. |
| | 116 | * |
| | 117 | * @returns {void} |
| | 118 | */ |
| | 119 | function buildEditor() { |
| | 120 | var editor, triggerChangeIfDirty, onInit; |
| | 121 | |
| | 122 | // Destroy any existing editor so that it can be re-initialized after a widget-updated event. |
| | 123 | if ( tinymce.get( id ) ) { |
| | 124 | restoreTextMode = tinymce.get( id ).isHidden(); |
| | 125 | wp.editor.remove( id ); |
| | 126 | } |
| | 127 | |
| | 128 | wp.editor.initialize( id, { |
| | 129 | tinymce: { |
| | 130 | wpautop: true |
| | 131 | }, |
| | 132 | quicktags: true |
| | 133 | } ); |
| | 134 | |
| | 135 | editor = window.tinymce.get( id ); |
| | 136 | if ( ! editor ) { |
| | 137 | throw new Error( 'Failed to initialize editor' ); |
| | 138 | } |
| | 139 | onInit = function() { |
| | 140 | watchForDestroyedBody( control.$el.find( 'iframe' )[0] ); |
| | 141 | |
| | 142 | // If a prior mce instance was replaced, and it was in text mode, toggle to text mode. |
| | 143 | if ( restoreTextMode ) { |
| | 144 | switchEditors.go( id, 'toggle' ); |
| | 145 | } |
| | 146 | }; |
| | 147 | |
| | 148 | if ( editor.initialized ) { |
| | 149 | onInit(); |
| | 150 | } else { |
| | 151 | editor.on( 'init', onInit ); |
| | 152 | } |
| | 153 | |
| | 154 | control.editorFocused = false; |
| | 155 | triggerChangeIfDirty = function() { |
| | 156 | if ( editor.isDirty() ) { |
| | 157 | editor.save(); |
| | 158 | textarea.trigger( 'change' ); |
| | 159 | } |
| | 160 | }; |
| | 161 | editor.on( 'focus', function() { |
| | 162 | control.editorFocused = true; |
| | 163 | } ); |
| | 164 | editor.on( 'NodeChange', _.debounce( triggerChangeIfDirty, changeDebounceDelay ) ); |
| | 165 | editor.on( 'blur', function() { |
| | 166 | control.editorFocused = false; |
| | 167 | triggerChangeIfDirty(); |
| | 168 | } ); |
| | 169 | |
| | 170 | control.editor = editor; |
| | 171 | } |
| | 172 | |
| | 173 | /** |
| | 174 | * Watch an iframe for the destruction of its TinyMCE contenteditable contents. |
| | 175 | * |
| | 176 | * @todo There may be a better way to listen for an iframe being destroyed. |
| | 177 | * @param {HTMLIFrameElement} iframe - TinyMCE iframe. |
| | 178 | * @returns {void} |
| | 179 | */ |
| | 180 | function watchForDestroyedBody( iframe ) { |
| | 181 | var timeoutId = setInterval( function() { |
| | 182 | if ( ! iframe.contentWindow || iframe.contentWindow.document.body.id ) { |
| | 183 | return; |
| | 184 | } |
| | 185 | clearInterval( timeoutId ); |
| | 186 | buildEditor(); |
| | 187 | }, iframeKeepAliveInterval ); |
| | 188 | } |
| | 189 | |
| | 190 | buildEditor(); |
| | 191 | } |
| | 192 | }); |
| | 193 | |
| | 194 | /** |
| | 195 | * Mapping of widget ID to instances of TextWidgetControl subclasses. |
| | 196 | * |
| | 197 | * @type {Object.<string, wp.textWidgets.TextWidgetControl>} |
| | 198 | */ |
| | 199 | component.widgetControls = {}; |
| | 200 | |
| | 201 | /** |
| | 202 | * Handle widget being added or initialized for the first time at the widget-added event. |
| | 203 | * |
| | 204 | * @param {jQuery.Event} event - Event. |
| | 205 | * @param {jQuery} widgetContainer - Widget container element. |
| | 206 | * @returns {void} |
| | 207 | */ |
| | 208 | component.handleWidgetAdded = function handleWidgetAdded( event, widgetContainer ) { |
| | 209 | var widgetForm, idBase, widgetControl, widgetId, animatedCheckDelay = 50, widgetInside, renderWhenAnimationDone; |
| | 210 | widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' ); // Note: '.form' appears in the customizer, whereas 'form' on the widgets admin screen. |
| | 211 | |
| | 212 | idBase = widgetForm.find( '> .id_base' ).val(); |
| | 213 | if ( 'text' !== idBase ) { |
| | 214 | return; |
| | 215 | } |
| | 216 | |
| | 217 | // Prevent initializing already-added widgets. |
| | 218 | widgetId = widgetForm.find( '> .widget-id' ).val(); |
| | 219 | if ( component.widgetControls[ widgetId ] ) { |
| | 220 | return; |
| | 221 | } |
| | 222 | |
| | 223 | widgetControl = new component.TextWidgetControl({ |
| | 224 | el: widgetContainer |
| | 225 | }); |
| | 226 | |
| | 227 | component.widgetControls[ widgetId ] = widgetControl; |
| | 228 | |
| | 229 | /* |
| | 230 | * Render the widget once the widget parent's container finishes animating, |
| | 231 | * as the widget-added event fires with a slideDown of the container. |
| | 232 | * This ensures that the textarea is visible and an iframe can be embedded |
| | 233 | * with TinyMCE being able to set contenteditable on it. |
| | 234 | */ |
| | 235 | widgetInside = widgetContainer.parent(); |
| | 236 | renderWhenAnimationDone = function() { |
| | 237 | if ( widgetInside.is( ':animated' ) ) { |
| | 238 | setTimeout( renderWhenAnimationDone, animatedCheckDelay ); |
| | 239 | } else { |
| | 240 | widgetControl.initializeEditor(); |
| | 241 | } |
| | 242 | }; |
| | 243 | renderWhenAnimationDone(); |
| | 244 | }; |
| | 245 | |
| | 246 | /** |
| | 247 | * Sync widget instance data sanitized from server back onto widget model. |
| | 248 | * |
| | 249 | * This gets called via the 'widget-updated' event when saving a widget from |
| | 250 | * the widgets admin screen and also via the 'widget-synced' event when making |
| | 251 | * a change to a widget in the customizer. |
| | 252 | * |
| | 253 | * @param {jQuery.Event} event - Event. |
| | 254 | * @param {jQuery} widgetContainer - Widget container element. |
| | 255 | * @returns {void} |
| | 256 | */ |
| | 257 | component.handleWidgetUpdated = function handleWidgetUpdated( event, widgetContainer ) { |
| | 258 | var widgetForm, widgetId, widgetControl, idBase; |
| | 259 | widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' ); |
| | 260 | |
| | 261 | idBase = widgetForm.find( '> .id_base' ).val(); |
| | 262 | if ( 'text' !== idBase ) { |
| | 263 | return; |
| | 264 | } |
| | 265 | |
| | 266 | widgetId = widgetForm.find( '> .widget-id' ).val(); |
| | 267 | widgetControl = component.widgetControls[ widgetId ]; |
| | 268 | if ( ! widgetControl ) { |
| | 269 | return; |
| | 270 | } |
| | 271 | |
| | 272 | widgetControl.updateFields(); |
| | 273 | }; |
| | 274 | |
| | 275 | /** |
| | 276 | * Initialize functionality. |
| | 277 | * |
| | 278 | * This function exists to prevent the JS file from having to boot itself. |
| | 279 | * When WordPress enqueues this script, it should have an inline script |
| | 280 | * attached which calls wp.textWidgets.init(). |
| | 281 | * |
| | 282 | * @returns {void} |
| | 283 | */ |
| | 284 | component.init = function init() { |
| | 285 | var $document = $( document ); |
| | 286 | $document.on( 'widget-added', component.handleWidgetAdded ); |
| | 287 | $document.on( 'widget-synced widget-updated', component.handleWidgetUpdated ); |
| | 288 | |
| | 289 | /* |
| | 290 | * Manually trigger widget-added events for media widgets on the admin |
| | 291 | * screen once they are expanded. The widget-added event is not triggered |
| | 292 | * for each pre-existing widget on the widgets admin screen like it is |
| | 293 | * on the customizer. Likewise, the customizer only triggers widget-added |
| | 294 | * when the widget is expanded to just-in-time construct the widget form |
| | 295 | * when it is actually going to be displayed. So the following implements |
| | 296 | * the same for the widgets admin screen, to invoke the widget-added |
| | 297 | * handler when a pre-existing media widget is expanded. |
| | 298 | */ |
| | 299 | $( function initializeExistingWidgetContainers() { |
| | 300 | var widgetContainers; |
| | 301 | if ( 'widgets' !== window.pagenow ) { |
| | 302 | return; |
| | 303 | } |
| | 304 | widgetContainers = $( '.widgets-holder-wrap:not(#available-widgets)' ).find( 'div.widget' ); |
| | 305 | widgetContainers.one( 'click.toggle-widget-expanded', function toggleWidgetExpanded() { |
| | 306 | var widgetContainer = $( this ); |
| | 307 | component.handleWidgetAdded( new jQuery.Event( 'widget-added' ), widgetContainer ); |
| | 308 | }); |
| | 309 | }); |
| | 310 | }; |
| | 311 | |
| | 312 | return component; |
| | 313 | })( jQuery ); |