/* global tinymce */ /** * 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. */ // Ensure the global `wp` object exists. window.wp = window.wp || {}; ( function( $ ) { 'use strict'; var views = {}, instances = {}, media = wp.media, 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 '' + '
\u00a0
' + '\u00a0
', function( self, editor, node ) { $( self ).trigger( 'ready', [ editor, node ] ); }, 'wrap' ); }, unbind: function() {}, getNodes: function( callback ) { var nodes = []; _.each( tinymce.editors, function( editor ) { if ( editor.plugins.wpview ) { $( editor.getBody() ) .find( '[data-wpview-text="' + this.encodedText + '"]' ) .each( function ( i, node ) { if ( callback ) { callback( editor, node ); } nodes.push( node ); } ); } }, this ); return nodes; }, setContent: function( html, callback, option ) { var self = this; this.getNodes( function ( editor, element ) { var contentWrap = $( element ).find( '.wpview-content' ), wrap = element; if ( contentWrap.length && option !== 'wrap' ) { element = contentWrap = contentWrap[0]; } if ( _.isString( html ) ) { if ( option === 'replace' ) { element = editor.dom.replace( editor.dom.createFragment( html ), wrap ); } else { editor.dom.setHTML( element, html ); } } else { if ( option === 'replace' ) { element = editor.dom.replace( html, wrap ); } else { $( element ).empty().append( html ); } } if ( _.isFunction( callback ) ) { callback( self, editor, $( element ).find( '.wpview-content' )[0] ); } } ); }, /* jshint scripturl: true */ createIframe: function ( content ) { var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; if ( content.indexOf( '