diff --git a/wp-includes/js/mce-view.js b/wp-includes/js/mce-view.js index c522d85e57..ab33cb4bcf 100644 --- a/wp-includes/js/mce-view.js +++ b/wp-includes/js/mce-view.js @@ -14,6 +14,9 @@ window.wp = window.wp || {}; var views = {}, instances = {}, media = wp.media, + mediaWindows = [], + windowIdx = 0, + waitInterval = 50, viewOptions = ['encodedText']; // Create the `wp.mce` object if necessary. @@ -228,7 +231,7 @@ window.wp = window.wp || {}; iframeDoc.body.className = editor.getBody().className; }); } - }, 50 ); + }, waitInterval ); }); } else { this.setContent( body ); @@ -591,13 +594,68 @@ window.wp = window.wp || {}; this.fetch(); this.getEditors( function( editor ) { - editor.on( 'hide', self.stopPlayers ); + editor.on( 'hide', function () { + mediaWindows = []; + windowIdx = 0; + self.stopPlayers(); + } ); }); }, + pauseOtherWindows: function ( win ) { + _.each( mediaWindows, function ( mediaWindow ) { + if ( mediaWindow.sandboxId !== win.sandboxId ) { + _.each( mediaWindow.mejs.players, function ( player ) { + player.pause(); + } ); + } + } ); + }, + + iframeLoaded: function (win) { + return _.bind( function () { + var callback; + if ( ! win.mejs || _.isEmpty( win.mejs.players ) ) { + return; + } + + win.sandboxId = windowIdx; + windowIdx++; + mediaWindows.push( win ); + + callback = _.bind( function () { + this.pauseOtherWindows( win ); + }, this ); + + if ( ! _.isEmpty( win.mejs.MediaPluginBridge.pluginMediaElements ) ) { + _.each( win.mejs.MediaPluginBridge.pluginMediaElements, function ( mediaElement ) { + mediaElement.addEventListener( 'play', callback ); + } ); + } + + _.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 ); + }, + + deferredListen: function () { + window.setTimeout( _.bind( this.listenToSandboxes, this ), this.getNodes().length * waitInterval ); + }, + setNodes: function () { if ( this.parsed ) { this.setIframes( this.parsed.head, this.parsed.body ); + this.deferredListen(); } else { this.fail(); } @@ -617,6 +675,7 @@ window.wp = window.wp || {}; if ( response ) { self.parsed = response; self.setIframes( response.head, response.body ); + self.deferredListen(); } else { self.fail( true ); } diff --git a/wp-includes/js/mce-view.min.js b/wp-includes/js/mce-view.min.js index 6a77e881ff..3c809ff4e2 100644 --- a/wp-includes/js/mce-view.min.js +++ b/wp-includes/js/mce-view.min.js @@ -1 +1 @@ -window.wp=window.wp||{},function(a){"use strict";var b={},c={},d=wp.media,e=["encodedText"];wp.mce=wp.mce||{},wp.mce.View=function(a){a=a||{},this.type=a.type,_.extend(this,_.pick(a,e)),this.initialize.apply(this,arguments)},_.extend(wp.mce.View.prototype,{initialize:function(){},getHtml:function(){return""},loadingPlaceholder:function(){return'
',"wrap"),a(this).trigger("ready"),this.rendered(!0))},unbind:function(){},getEditors:function(a){var b=[];return _.each(tinymce.editors,function(c){c.plugins.wpview&&(a&&a(c),b.push(c))},this),b},getNodes:function(b){var c=[],d=this;return this.getEditors(function(e){a(e.getBody()).find('[data-wpview-text="'+d.encodedText+'"]').each(function(d,f){b&&b(e,f,a(f).find(".wpview-content").get(0)),c.push(f)})}),c},setContent:function(a,b){this.getNodes(function(c,d,e){var f="wrap"===b||"replace"===b?d:e,g=a;_.isString(g)&&(g=c.dom.createFragment(g)),"replace"===b?c.dom.replace(g,f):(f.innerHTML="",f.appendChild(g))})},setIframes:function(b,c){var d=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver,e="video"===this.type||"audio"===this.type||"playlist"===this.type;b||-1!==c.indexOf("