X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/af50974463450c98503e763a7836a50e260461a9..b22765f41bf0b2021b9beb9120ee0ac91fa89292:/wp-includes/js/mce-view.js diff --git a/wp-includes/js/mce-view.js b/wp-includes/js/mce-view.js index da1ce6ae..3d2616d7 100644 --- a/wp-includes/js/mce-view.js +++ b/wp-includes/js/mce-view.js @@ -1,267 +1,34 @@ /* global tinymce */ -/** + +/* + * The TinyMCE view API. + * * Note: this API is "experimental" meaning that it will probably change * in the next few releases based on feedback from 3.9.0. * If you decide to use it, please follow the development closely. + * + * Diagram + * + * |- registered view constructor (type) + * | |- view instance (unique text) + * | | |- editor 1 + * | | | |- view node + * | | | |- view node + * | | | |- ... + * | | |- editor 2 + * | | | |- ... + * | |- view instance + * | | |- ... + * |- registered view + * | |- ... */ - -// Ensure the global `wp` object exists. -window.wp = window.wp || {}; - -( function( $ ) { +( function( window, wp, shortcode, $ ) { 'use strict'; var views = {}, - instances = {}, - media = wp.media, - mediaWindows = [], - windowIdx = 0, - waitInterval = 50, - viewOptions = ['encodedText']; - - // Create the `wp.mce` object if necessary. - wp.mce = wp.mce || {}; - - /** - * wp.mce.View - * - * A Backbone-like View constructor intended for use when rendering a TinyMCE View. The main difference is - * that the TinyMCE View is not tied to a particular DOM node. - * - * @param {Object} [options={}] - */ - wp.mce.View = function( options ) { - options = options || {}; - this.type = options.type; - _.extend( this, _.pick( options, viewOptions ) ); - this.initialize.apply( this, arguments ); - }; - - _.extend( wp.mce.View.prototype, { - initialize: function() {}, - getHtml: function() { - return ''; - }, - loadingPlaceholder: function() { - return '' + - '
' + - '
' + - '
' + - '
'; - }, - render: function( force ) { - if ( force || ! this.rendered() ) { - this.unbind(); - - this.setContent( - '

\u00a0

' + - '
' + - '
' + - ( _.isFunction( views[ this.type ].edit ) ? '
' : '' ) + - '
' + - '
' + - '
' + - ( this.getHtml() || this.loadingPlaceholder() ) + - '
' + - ( this.overlay ? '
' : '' ) + - '
' + - '

\u00a0

', - 'wrap' - ); - - $( this ).trigger( 'ready' ); - - this.rendered( true ); - } - }, - unbind: function() {}, - getEditors: function( callback ) { - var editors = []; - - _.each( tinymce.editors, function( editor ) { - if ( editor.plugins.wpview ) { - if ( callback ) { - callback( editor ); - } - - editors.push( editor ); - } - }, this ); - - return editors; - }, - getNodes: function( callback ) { - var nodes = [], - self = this; - - this.getEditors( function( editor ) { - $( editor.getBody() ) - .find( '[data-wpview-text="' + self.encodedText + '"]' ) - .each( function ( i, node ) { - if ( callback ) { - callback( editor, node, $( node ).find( '.wpview-content' ).get( 0 ) ); - } - - nodes.push( node ); - } ); - } ); - - return nodes; - }, - setContent: function( html, option ) { - this.getNodes( function ( editor, node, content ) { - var el = ( option === 'wrap' || option === 'replace' ) ? node : content, - insert = html; - - if ( _.isString( insert ) ) { - insert = editor.dom.createFragment( insert ); - } - - if ( option === 'replace' ) { - editor.dom.replace( insert, el ); - } else { - el.innerHTML = ''; - el.appendChild( insert ); - } - } ); - }, - /* jshint scripturl: true */ - setIframes: function ( head, body ) { - var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, - importStyles = this.type === 'video' || this.type === 'audio' || this.type === 'playlist'; - - if ( head || body.indexOf( ' Visual. - setTimeout( function() { - iframe = dom.add( content, 'iframe', { - src: tinymce.Env.ie ? 'javascript:""' : '', - frameBorder: '0', - allowTransparency: 'true', - scrolling: 'no', - 'class': 'wpview-sandbox', - style: { - width: '100%', - display: 'block' - } - } ); - - iframeDoc = iframe.contentWindow.document; - - iframeDoc.open(); - iframeDoc.write( - '' + - '' + - '' + - '' + - head + - styles + - '' + - '' + - '' + - body + - '' + - '' - ); - iframeDoc.close(); - - resize = function() { - // Make sure the iframe still exists. - iframe.contentWindow && $( iframe ).height( $( iframeDoc.body ).height() ); - }; - - if ( MutationObserver ) { - new MutationObserver( _.debounce( function() { - resize(); - }, 100 ) ) - .observe( iframeDoc.body, { - attributes: true, - childList: true, - subtree: true - } ); - } else { - for ( i = 1; i < 6; i++ ) { - setTimeout( resize, i * 700 ); - } - } - - if ( importStyles ) { - editor.on( 'wp-body-class-change', function() { - iframeDoc.body.className = editor.getBody().className; - }); - } - }, waitInterval ); - }); - } else { - this.setContent( body ); - } - }, - setError: function( message, dashicon ) { - this.setContent( - '
' + - '
' + - '

' + message + '

' + - '
' - ); - }, - rendered: function( value ) { - var notRendered; - - this.getNodes( function( editor, node ) { - if ( value != null ) { - $( node ).data( 'rendered', value === true ); - } else { - notRendered = notRendered || ! $( node ).data( 'rendered' ); - } - } ); - - return ! notRendered; - } - } ); - - // take advantage of the Backbone extend method - wp.mce.View.extend = Backbone.View.extend; + wp.mce = wp.mce || {}; /** * wp.mce.views @@ -273,67 +40,38 @@ window.wp = window.wp || {}; wp.mce.views = { /** - * wp.mce.views.register( type, view ) - * - * Registers a new TinyMCE view. - * - * @param type - * @param constructor + * Registers a new view type. * + * @param {String} type The view type. + * @param {Object} extend An object to extend wp.mce.View.prototype with. */ - register: function( type, constructor ) { - var defaultConstructor = { - type: type, - View: {}, - toView: function( content ) { - var match = wp.shortcode.next( this.type, content ); - - if ( ! match ) { - return; - } - - return { - index: match.index, - content: match.content, - options: { - shortcode: match.shortcode - } - }; - } - }; - - constructor = _.defaults( constructor, defaultConstructor ); - constructor.View = wp.mce.View.extend( constructor.View ); - - views[ type ] = constructor; + register: function( type, extend ) { + views[ type ] = wp.mce.View.extend( _.extend( extend, { type: type } ) ); }, /** - * wp.mce.views.get( id ) - * - * Returns a TinyMCE view constructor. + * Unregisters a view type. * - * @param type + * @param {String} type The view type. */ - get: function( type ) { - return views[ type ]; + unregister: function( type ) { + delete views[ type ]; }, /** - * wp.mce.views.unregister( type ) + * Returns the settings of a view type. * - * Unregisters a TinyMCE view. + * @param {String} type The view type. * - * @param type + * @return {Function} The view constructor. */ - unregister: function( type ) { - delete views[ type ]; + get: function( type ) { + return views[ type ]; }, /** - * wp.mce.views.unbind( editor ) - * - * The editor DOM is being rebuilt, run cleanup. + * Unbinds all view nodes. + * Runs before removing all view nodes from the DOM. */ unbind: function() { _.each( instances, function( instance ) { @@ -342,24 +80,26 @@ window.wp = window.wp || {}; }, /** - * toViews( content ) - * Scans a `content` string for each view's pattern, replacing any - * matches with wrapper elements, and creates a new instance for - * every match, which triggers the related data to be fetched. + * Scans a given string for each view's pattern, + * replacing any matches with markers, + * and creates a new instance for every match. + * + * @param {String} content The string to scan. * - * @param content + * @return {String} The string with markers. */ - toViews: function( content ) { + setMarkers: function( content ) { var pieces = [ { content: content } ], - current; + self = this, + instance, current; - _.each( views, function( view, viewType ) { + _.each( views, function( view, type ) { current = pieces.slice(); pieces = []; _.each( current, function( piece ) { var remaining = piece.content, - result; + result, text; // Ignore processed pieces, but retain their location. if ( piece.processed ) { @@ -369,110 +109,102 @@ window.wp = window.wp || {}; // Iterate through the string progressively matching views // and slicing the string as we go. - while ( remaining && (result = view.toView( remaining )) ) { + while ( remaining && ( result = view.prototype.match( remaining ) ) ) { // Any text before the match becomes an unprocessed piece. if ( result.index ) { - pieces.push({ content: remaining.substring( 0, result.index ) }); + pieces.push( { content: remaining.substring( 0, result.index ) } ); } + instance = self.createInstance( type, result.content, result.options ); + text = instance.loader ? '.' : instance.text; + // Add the processed piece for the match. - pieces.push({ - content: wp.mce.views.toView( viewType, result.content, result.options ), + pieces.push( { + content: instance.ignore ? text : '

' + text + '

', processed: true - }); + } ); // Update the remaining content. remaining = remaining.slice( result.index + result.content.length ); } - // There are no additional matches. If any content remains, - // add it as an unprocessed piece. + // There are no additional matches. + // If any content remains, add it as an unprocessed piece. if ( remaining ) { - pieces.push({ content: remaining }); + pieces.push( { content: remaining } ); } - }); - }); + } ); + } ); - return _.pluck( pieces, 'content' ).join(''); + content = _.pluck( pieces, 'content' ).join( '' ); + return content.replace( /

\s*

' ); }, /** - * Create a placeholder for a particular view type + * Create a view instance. * - * @param viewType - * @param text - * @param options + * @param {String} type The view type. + * @param {String} text The textual representation of the view. + * @param {Object} options Options. + * @param {Boolean} force Recreate the instance. Optional. * + * @return {wp.mce.View} The view instance. */ - toView: function( viewType, text, options ) { - var view = wp.mce.views.get( viewType ), - encodedText = window.encodeURIComponent( text ), - instance, viewOptions; + createInstance: function( type, text, options, force ) { + var View = this.get( type ), + encodedText, + instance; + text = tinymce.DOM.decode( text ); - if ( ! view ) { - return text; - } + if ( ! force ) { + instance = this.getInstance( text ); - if ( ! wp.mce.views.getInstance( encodedText ) ) { - viewOptions = options; - viewOptions.type = viewType; - viewOptions.encodedText = encodedText; - instance = new view.View( viewOptions ); - instances[ encodedText ] = instance; + if ( instance ) { + return instance; + } } - return wp.html.string({ - tag: 'div', + encodedText = encodeURIComponent( text ); - attrs: { - 'class': 'wpview-wrap', - 'data-wpview-text': encodedText, - 'data-wpview-type': viewType - }, + options = _.extend( options || {}, { + text: text, + encodedText: encodedText + } ); - content: '\u00a0' - }); + return instances[ encodedText ] = new View( options ); }, /** - * Refresh views after an update is made + * Get a view instance. + * + * @param {(String|HTMLElement)} object The textual representation of the view or the view node. * - * @param view {object} being refreshed - * @param text {string} textual representation of the view - * @param force {Boolean} whether to force rendering - */ - refreshView: function( view, text, force ) { - var encodedText = window.encodeURIComponent( text ), - viewOptions, - result, instance; - - instance = wp.mce.views.getInstance( encodedText ); - - if ( ! instance ) { - result = view.toView( text ); - viewOptions = result.options; - viewOptions.type = view.type; - viewOptions.encodedText = encodedText; - instance = new view.View( viewOptions ); - instances[ encodedText ] = instance; + * @return {wp.mce.View} The view instance or undefined. + */ + getInstance: function( object ) { + if ( typeof object === 'string' ) { + return instances[ encodeURIComponent( object ) ]; } - instance.render( force ); + return instances[ $( object ).attr( 'data-wpview-text' ) ]; }, - getInstance: function( encodedText ) { - return instances[ encodedText ]; + /** + * Given a view node, get the view's text. + * + * @param {HTMLElement} node The view node. + * + * @return {String} The textual representation of the view. + */ + getText: function( node ) { + return decodeURIComponent( $( node ).attr( 'data-wpview-text' ) || '' ); }, /** - * render( scope ) + * Renders all view nodes that are not yet rendered. * - * Renders any view instances inside a DOM node `scope`. - * - * View instances are detected by the presence of wrapper elements. - * To generate wrapper elements, pass your content through - * `wp.mce.view.toViews( content )`. + * @param {Boolean} force Rerender all view nodes. */ render: function( force ) { _.each( instances, function( instance ) { @@ -480,409 +212,714 @@ window.wp = window.wp || {}; } ); }, - edit: function( node ) { - var viewType = $( node ).data('wpview-type'), - view = wp.mce.views.get( viewType ); + /** + * Update the text of a given view node. + * + * @param {String} text The new text. + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to update. + * @param {Boolean} force Recreate the instance. Optional. + */ + update: function( text, editor, node, force ) { + var instance = this.getInstance( node ); + + if ( instance ) { + instance.update( text, editor, node, force ); + } + }, + + /** + * Renders any editing interface based on the view type. + * + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to edit. + */ + edit: function( editor, node ) { + var instance = this.getInstance( node ); + + if ( instance && instance.edit ) { + instance.edit( instance.text, function( text, force ) { + instance.update( text, editor, node, force ); + } ); + } + }, + + /** + * Remove a given view node from the DOM. + * + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to remove. + */ + remove: function( editor, node ) { + var instance = this.getInstance( node ); - if ( view ) { - view.edit( node ); + if ( instance ) { + instance.remove( editor, node ); } } }; - wp.mce.views.register( 'gallery', { - View: { - template: media.template( 'editor-gallery' ), + /** + * A Backbone-like View constructor intended for use when rendering a TinyMCE View. + * The main difference is that the TinyMCE View is not tied to a particular DOM node. + * + * @param {Object} options Options. + */ + wp.mce.View = function( options ) { + _.extend( this, options ); + this.initialize(); + }; - // The fallback post ID to use as a parent for galleries that don't - // specify the `ids` or `include` parameters. - // - // Uses the hidden input on the edit posts page by default. - postID: $('#post_ID').val(), + wp.mce.View.extend = Backbone.View.extend; - initialize: function( options ) { - this.shortcode = options.shortcode; - this.fetch(); - }, + _.extend( wp.mce.View.prototype, { - fetch: function() { - var self = this; + /** + * The content. + * + * @type {*} + */ + content: null, - this.attachments = wp.media.gallery.attachments( this.shortcode, this.postID ); - this.dfd = this.attachments.more().done( function() { - self.render( true ); - } ); - }, + /** + * Whether or not to display a loader. + * + * @type {Boolean} + */ + loader: true, - getHtml: function() { - var attrs = this.shortcode.attrs.named, - attachments = false, - options; + /** + * Runs after the view instance is created. + */ + initialize: function() {}, - // Don't render errors while still fetching attachments - if ( this.dfd && 'pending' === this.dfd.state() && ! this.attachments.length ) { - return ''; - } + /** + * Retuns the content to render in the view node. + * + * @return {*} + */ + getContent: function() { + return this.content; + }, - if ( this.attachments.length ) { - attachments = this.attachments.toJSON(); - - _.each( attachments, function( attachment ) { - if ( attachment.sizes ) { - if ( attrs.size && attachment.sizes[ attrs.size ] ) { - attachment.thumbnail = attachment.sizes[ attrs.size ]; - } else if ( attachment.sizes.thumbnail ) { - attachment.thumbnail = attachment.sizes.thumbnail; - } else if ( attachment.sizes.full ) { - attachment.thumbnail = attachment.sizes.full; - } - } - } ); - } + /** + * Renders all view nodes tied to this view instance that are not yet rendered. + * + * @param {String} content The content to render. Optional. + * @param {Boolean} force Rerender all view nodes tied to this view instance. Optional. + */ + render: function( content, force ) { + if ( content != null ) { + this.content = content; + } - options = { - attachments: attachments, - columns: attrs.columns ? parseInt( attrs.columns, 10 ) : wp.media.galleryDefaults.columns - }; + content = this.getContent(); - return this.template( options ); + // If there's nothing to render an no loader needs to be shown, stop. + if ( ! this.loader && ! content ) { + return; + } + + // We're about to rerender all views of this instance, so unbind rendered views. + force && this.unbind(); + + // Replace any left over markers. + this.replaceMarkers(); + + if ( content ) { + this.setContent( content, function( editor, node ) { + $( node ).data( 'rendered', true ); + this.bindNode.call( this, editor, node ); + }, force ? null : false ); + } else { + this.setLoader(); } }, - edit: function( node ) { - var gallery = wp.media.gallery, - self = this, - frame, data; + /** + * Binds a given node after its content is added to the DOM. + */ + bindNode: function() {}, - data = window.decodeURIComponent( $( node ).attr('data-wpview-text') ); - frame = gallery.edit( data ); + /** + * Unbinds a given node before its content is removed from the DOM. + */ + unbindNode: function() {}, - frame.state('gallery-edit').on( 'update', function( selection ) { - var shortcode = gallery.shortcode( selection ).string(), force; - $( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) ); - force = ( data !== shortcode ); - wp.mce.views.refreshView( self, shortcode, force ); - }); + /** + * Unbinds all view nodes tied to this view instance. + * Runs before their content is removed from the DOM. + */ + unbind: function() { + this.getNodes( function( editor, node ) { + this.unbindNode.call( this, editor, node ); + }, true ); + }, - frame.on( 'close', function() { - frame.detach(); - }); - } - } ); + /** + * Gets all the TinyMCE editor instances that support views. + * + * @param {Function} callback A callback. + */ + getEditors: function( callback ) { + _.each( tinymce.editors, function( editor ) { + if ( editor.plugins.wpview ) { + callback.call( this, editor ); + } + }, this ); + }, - /** - * These are base methods that are shared by the audio and video shortcode's MCE controller. - * - * @mixin - */ - wp.mce.av = { - View: { - overlay: true, + /** + * Gets all view nodes tied to this view instance. + * + * @param {Function} callback A callback. + * @param {Boolean} rendered Get (un)rendered view nodes. Optional. + */ + getNodes: function( callback, rendered ) { + this.getEditors( function( editor ) { + var self = this; - action: 'parse-media-shortcode', + $( editor.getBody() ) + .find( '[data-wpview-text="' + self.encodedText + '"]' ) + .filter( function() { + var data; + + if ( rendered == null ) { + return true; + } + + data = $( this ).data( 'rendered' ) === true; + + return rendered ? data : ! data; + } ) + .each( function() { + callback.call( self, editor, this, this /* back compat */ ); + } ); + } ); + }, - initialize: function( options ) { + /** + * Gets all marker nodes tied to this view instance. + * + * @param {Function} callback A callback. + */ + getMarkers: function( callback ) { + this.getEditors( function( editor ) { var self = this; - this.shortcode = options.shortcode; + $( editor.getBody() ) + .find( '[data-wpview-marker="' + this.encodedText + '"]' ) + .each( function() { + callback.call( self, editor, this ); + } ); + } ); + }, + + /** + * Replaces all marker nodes tied to this view instance. + */ + replaceMarkers: function() { + this.getMarkers( function( editor, node ) { + var $viewNode; + + if ( ! this.loader && $( node ).text() !== this.text ) { + editor.dom.setAttrib( node, 'data-wpview-marker', null ); + return; + } + + $viewNode = editor.$( + '

' + ); + + editor.$( node ).replaceWith( $viewNode ); + } ); + }, - _.bindAll( this, 'setIframes', 'setNodes', 'fetch', 'stopPlayers' ); - $( this ).on( 'ready', this.setNodes ); + /** + * Removes all marker nodes tied to this view instance. + */ + removeMarkers: function() { + this.getMarkers( function( editor, node ) { + editor.dom.setAttrib( node, 'data-wpview-marker', null ); + } ); + }, - $( document ).on( 'media:edit', this.stopPlayers ); + /** + * Sets the content for all view nodes tied to this view instance. + * + * @param {*} content The content to set. + * @param {Function} callback A callback. Optional. + * @param {Boolean} rendered Only set for (un)rendered nodes. Optional. + */ + setContent: function( content, callback, rendered ) { + if ( _.isObject( content ) && content.body.indexOf( ' Visual. + setTimeout( function() { + var iframe, iframeWin, iframeDoc, MutationObserver, observer, i, block; - callback = _.bind( function () { - this.pauseOtherWindows( win ); - }, this ); + editor.undoManager.transact( function() { + node.innerHTML = ''; - if ( ! _.isEmpty( win.mejs.MediaPluginBridge.pluginMediaElements ) ) { - _.each( win.mejs.MediaPluginBridge.pluginMediaElements, function ( mediaElement ) { - mediaElement.addEventListener( 'play', callback ); + iframe = dom.add( node, 'iframe', { + /* jshint scripturl: true */ + src: tinymce.Env.ie ? 'javascript:""' : '', + frameBorder: '0', + allowTransparency: 'true', + scrolling: 'no', + 'class': 'wpview-sandbox', + style: { + width: '100%', + display: 'block' + }, + height: self.iframeHeight } ); - } - _.each( win.mejs.players, function ( player ) { - $( player.node ).on( 'play', callback ); - }, this ); - }, this ); - }, - - listenToSandboxes: function () { - _.each( this.getNodes(), function ( node ) { - var win, iframe = $( '.wpview-sandbox', node ).get( 0 ); - if ( iframe && ( win = iframe.contentWindow ) ) { - $( win ).load( _.bind( this.iframeLoaded( win ), this ) ); - } - }, this ); - }, + dom.add( node, 'span', { 'class': 'mce-shim' } ); + dom.add( node, 'span', { 'class': 'wpview-end' } ); + } ); - deferredListen: function () { - window.setTimeout( _.bind( this.listenToSandboxes, this ), this.getNodes().length * waitInterval ); - }, + // Bail if the iframe node is not attached to the DOM. + // Happens when the view is dragged in the editor. + // There is a browser restriction when iframes are moved in the DOM. They get emptied. + // The iframe will be rerendered after dropping the view node at the new location. + if ( ! iframe.contentWindow ) { + return; + } - setNodes: function () { - if ( this.parsed ) { - this.setIframes( this.parsed.head, this.parsed.body ); - this.deferredListen(); - } else { - this.fail(); - } - }, + iframeWin = iframe.contentWindow; + iframeDoc = iframeWin.document; + iframeDoc.open(); + + iframeDoc.write( + '' + + '' + + '' + + '' + + head + + styles + + '' + + '' + + '' + + body + + '' + + '' + ); + + iframeDoc.close(); + + function resize() { + var $iframe; + + if ( block ) { + return; + } - fetch: function () { - var self = this; + // Make sure the iframe still exists. + if ( iframe.contentWindow ) { + $iframe = $( iframe ); + self.iframeHeight = $( iframeDoc.body ).height(); - wp.ajax.send( this.action, { - data: { - post_ID: $( '#post_ID' ).val() || 0, - type: this.shortcode.tag, - shortcode: this.shortcode.string() - } - } ) - .done( function( response ) { - if ( response ) { - self.parsed = response; - self.setIframes( response.head, response.body ); - self.deferredListen(); - } else { - self.fail( true ); + if ( $iframe.height() !== self.iframeHeight ) { + $iframe.height( self.iframeHeight ); + editor.nodeChanged(); + } + } } - } ) - .fail( function( response ) { - self.fail( response || true ); - } ); - }, - fail: function( error ) { - if ( ! this.error ) { - if ( error ) { - this.error = error; - } else { - return; + if ( self.iframeHeight ) { + block = true; + + setTimeout( function() { + block = false; + resize(); + }, 3000 ); } - } - if ( this.error.message ) { - if ( ( this.error.type === 'not-embeddable' && this.type === 'embed' ) || this.error.type === 'not-ssl' || - this.error.type === 'no-items' ) { + $( iframeWin ).on( 'load', resize ); + + MutationObserver = iframeWin.MutationObserver || iframeWin.WebKitMutationObserver || iframeWin.MozMutationObserver; + + if ( MutationObserver ) { + observer = new MutationObserver( _.debounce( resize, 100 ) ); - this.setError( this.error.message, 'admin-media' ); + observer.observe( iframeDoc.body, { + attributes: true, + childList: true, + subtree: true + } ); } else { - this.setContent( '

' + this.original + '

', 'replace' ); + for ( i = 1; i < 6; i++ ) { + setTimeout( resize, i * 700 ); + } } - } else if ( this.error.statusText ) { - this.setError( this.error.statusText, 'admin-media' ); - } else if ( this.original ) { - this.setContent( '

' + this.original + '

', 'replace' ); - } - }, - stopPlayers: function( remove ) { - var rem = remove === 'remove'; + callback && callback.call( self, editor, node ); + }, 50 ); + }, rendered ); + }, - this.getNodes( function( editor, node, content ) { - var p, win, - iframe = $( 'iframe.wpview-sandbox', content ).get(0); + /** + * Sets a loader for all view nodes tied to this view instance. + */ + setLoader: function() { + this.setContent( + '
' + + '
' + + '
' + + '
' + ); + }, - if ( iframe && ( win = iframe.contentWindow ) && win.mejs ) { - // Sometimes ME.js may show a "Download File" placeholder and player.remove() doesn't exist there. - try { - for ( p in win.mejs.players ) { - win.mejs.players[p].pause(); + /** + * Sets an error for all view nodes tied to this view instance. + * + * @param {String} message The error message to set. + * @param {String} dashicon A dashicon ID. Optional. {@link https://developer.wordpress.org/resource/dashicons/} + */ + setError: function( message, dashicon ) { + this.setContent( + '
' + + '
' + + '

' + message + '

' + + '
' + ); + }, - if ( rem ) { - win.mejs.players[p].remove(); - } - } - } catch( er ) {} + /** + * Tries to find a text match in a given string. + * + * @param {String} content The string to scan. + * + * @return {Object} + */ + match: function( content ) { + var match = shortcode.next( this.type, content ); + + if ( match ) { + return { + index: match.index, + content: match.content, + options: { + shortcode: match.shortcode } - }); - }, - - unbind: function() { - this.stopPlayers( 'remove' ); + }; } }, /** - * Called when a TinyMCE view is clicked for editing. - * - Parses the shortcode out of the element's data attribute - * - Calls the `edit` method on the shortcode model - * - Launches the model window - * - Bind's an `update` callback which updates the element's data attribute - * re-renders the view + * Update the text of a given view node. * - * @param {HTMLElement} node + * @param {String} text The new text. + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to update. + * @param {Boolean} force Recreate the instance. Optional. */ - edit: function( node ) { - var media = wp.media[ this.type ], - self = this, - frame, data, callback; + update: function( text, editor, node, force ) { + _.find( views, function( view, type ) { + var match = view.prototype.match( text ); + + if ( match ) { + $( node ).data( 'rendered', false ); + editor.dom.setAttrib( node, 'data-wpview-text', encodeURIComponent( text ) ); + wp.mce.views.createInstance( type, text, match.options, force ).render(); + editor.focus(); + + return true; + } + } ); + }, + + /** + * Remove a given view node from the DOM. + * + * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in. + * @param {HTMLElement} node The view node to remove. + */ + remove: function( editor, node ) { + this.unbindNode.call( this, editor, node ); + editor.dom.remove( node ); + editor.focus(); + } + } ); +} )( window, window.wp, window.wp.shortcode, window.jQuery ); + +/* + * The WordPress core TinyMCE views. + * Views for the gallery, audio, video, playlist and embed shortcodes, + * and a view for embeddable URLs. + */ +( function( window, views, media, $ ) { + var base, gallery, av, embed, + schema, parser, serializer; + + function verifyHTML( string ) { + var settings = {}; + + if ( ! window.tinymce ) { + return string.replace( /<[^>]+>/g, '' ); + } + + if ( ! string || ( string.indexOf( '<' ) === -1 && string.indexOf( '>' ) === -1 ) ) { + return string; + } + + schema = schema || new window.tinymce.html.Schema( settings ); + parser = parser || new window.tinymce.html.DomParser( settings, schema ); + serializer = serializer || new window.tinymce.html.Serializer( settings, schema ); + + return serializer.serialize( parser.parse( string, { forced_root_block: false } ) ); + } + + base = { + state: [], + + edit: function( text, update ) { + var type = this.type, + frame = media[ type ].edit( text ); - $( document ).trigger( 'media:edit' ); + this.pausePlayers && this.pausePlayers(); + + _.each( this.state, function( state ) { + frame.state( state ).on( 'update', function( selection ) { + update( media[ type ].shortcode( selection ).string(), type === 'gallery' ); + } ); + } ); - data = window.decodeURIComponent( $( node ).attr('data-wpview-text') ); - frame = media.edit( data ); frame.on( 'close', function() { frame.detach(); } ); - callback = function( selection ) { - var shortcode = wp.media[ self.type ].shortcode( selection ).string(); - $( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) ); - wp.mce.views.refreshView( self, shortcode ); - frame.detach(); - }; - if ( _.isArray( self.state ) ) { - _.each( self.state, function (state) { - frame.state( state ).on( 'update', callback ); - } ); - } else { - frame.state( self.state ).on( 'update', callback ); - } frame.open(); } }; - /** - * TinyMCE handler for the video shortcode - * - * @mixes wp.mce.av - */ - wp.mce.views.register( 'video', _.extend( {}, wp.mce.av, { - state: 'video-details' - } ) ); + gallery = _.extend( {}, base, { + state: [ 'gallery-edit' ], + template: media.template( 'editor-gallery' ), - /** - * TinyMCE handler for the audio shortcode - * - * @mixes wp.mce.av - */ - wp.mce.views.register( 'audio', _.extend( {}, wp.mce.av, { - state: 'audio-details' - } ) ); + initialize: function() { + var attachments = media.gallery.attachments( this.shortcode, media.view.settings.post.id ), + attrs = this.shortcode.attrs.named, + self = this; - /** - * TinyMCE handler for the playlist shortcode - * - * @mixes wp.mce.av - */ - wp.mce.views.register( 'playlist', _.extend( {}, wp.mce.av, { - state: [ 'playlist-edit', 'video-playlist-edit' ] - } ) ); + attachments.more() + .done( function() { + attachments = attachments.toJSON(); + + _.each( attachments, function( attachment ) { + if ( attachment.sizes ) { + if ( attrs.size && attachment.sizes[ attrs.size ] ) { + attachment.thumbnail = attachment.sizes[ attrs.size ]; + } else if ( attachment.sizes.thumbnail ) { + attachment.thumbnail = attachment.sizes.thumbnail; + } else if ( attachment.sizes.full ) { + attachment.thumbnail = attachment.sizes.full; + } + } + } ); - /** - * TinyMCE handler for the embed shortcode - */ - wp.mce.embedMixin = { - View: _.extend( {}, wp.mce.av.View, { - overlay: true, - action: 'parse-embed', - initialize: function( options ) { - this.content = options.content; - this.original = options.url || options.shortcode.string(); - - if ( options.url ) { - this.shortcode = media.embed.shortcode( { - url: options.url - } ); - } else { - this.shortcode = options.shortcode; - } + self.render( self.template( { + verifyHTML: verifyHTML, + attachments: attachments, + columns: attrs.columns ? parseInt( attrs.columns, 10 ) : media.galleryDefaults.columns + } ) ); + } ) + .fail( function( jqXHR, textStatus ) { + self.setError( textStatus ); + } ); + } + } ); + + av = _.extend( {}, base, { + action: 'parse-media-shortcode', - _.bindAll( this, 'setIframes', 'setNodes', 'fetch' ); - $( this ).on( 'ready', this.setNodes ); + initialize: function() { + var self = this; - this.fetch(); + if ( this.url ) { + this.loader = false; + this.shortcode = media.embed.shortcode( { + url: this.text + } ); } - } ), - edit: function( node ) { - var embed = media.embed, - self = this, - frame, - data, - isURL = 'embedURL' === this.type; - $( document ).trigger( 'media:edit' ); + wp.ajax.post( this.action, { + post_ID: media.view.settings.post.id, + type: this.shortcode.tag, + shortcode: this.shortcode.string() + } ) + .done( function( response ) { + self.render( response ); + } ) + .fail( function( response ) { + if ( self.url ) { + self.ignore = true; + self.removeMarkers(); + } else { + self.setError( response.message || response.statusText, 'admin-media' ); + } + } ); - data = window.decodeURIComponent( $( node ).attr('data-wpview-text') ); - frame = embed.edit( data, isURL ); - frame.on( 'close', function() { - frame.detach(); + this.getEditors( function( editor ) { + editor.on( 'wpview-selected', function() { + self.pausePlayers(); + } ); } ); - frame.state( 'embed' ).props.on( 'change:url', function (model, url) { - if ( ! url ) { - return; + }, + + pausePlayers: function() { + this.getNodes( function( editor, node, content ) { + var win = $( 'iframe.wpview-sandbox', content ).get( 0 ); + + if ( win && ( win = win.contentWindow ) && win.mejs ) { + _.each( win.mejs.players, function( player ) { + try { + player.pause(); + } catch ( e ) {} + } ); } - frame.state( 'embed' ).metadata = model.toJSON(); } ); + } + } ); + + embed = _.extend( {}, av, { + action: 'parse-embed', + + edit: function( text, update ) { + var frame = media.embed.edit( text, this.url ), + self = this; + + this.pausePlayers(); + + frame.state( 'embed' ).props.on( 'change:url', function( model, url ) { + if ( url && model.get( 'url' ) ) { + frame.state( 'embed' ).metadata = model.toJSON(); + } + } ); + frame.state( 'embed' ).on( 'select', function() { - var shortcode; + var data = frame.state( 'embed' ).metadata; - if ( isURL ) { - shortcode = frame.state( 'embed' ).metadata.url; + if ( self.url ) { + update( data.url ); } else { - shortcode = embed.shortcode( frame.state( 'embed' ).metadata ).string(); + update( media.embed.shortcode( data ).string() ); } - $( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) ); - wp.mce.views.refreshView( self, shortcode ); + } ); + + frame.on( 'close', function() { frame.detach(); } ); + frame.open(); } - }; + } ); + + views.register( 'gallery', _.extend( {}, gallery ) ); - wp.mce.views.register( 'embed', _.extend( {}, wp.mce.embedMixin ) ); + views.register( 'audio', _.extend( {}, av, { + state: [ 'audio-details' ] + } ) ); - wp.mce.views.register( 'embedURL', _.extend( {}, wp.mce.embedMixin, { - toView: function( content ) { - var re = /(?:^|

)(https?:\/\/[^\s"]+?)(?:<\/p>\s*|$)/gi, - match = re.exec( tinymce.trim( content ) ); + views.register( 'video', _.extend( {}, av, { + state: [ 'video-details' ] + } ) ); - if ( ! match ) { - return; - } + views.register( 'playlist', _.extend( {}, av, { + state: [ 'playlist-edit', 'video-playlist-edit' ] + } ) ); - return { - index: match.index, - content: match[0], - options: { - url: match[1] - } - }; + views.register( 'embed', _.extend( {}, embed ) ); + + views.register( 'embedURL', _.extend( {}, embed, { + match: function( content ) { + var re = /(^|

)(https?:\/\/[^\s"]+?)(<\/p>\s*|$)/gi, + match = re.exec( content ); + + if ( match ) { + return { + index: match.index + match[1].length, + content: match[2], + options: { + url: true + } + }; + } } } ) ); - -}(jQuery)); +} )( window, window.wp.mce.views, window.wp.media, window.jQuery );