From 2ba763c9bacff97a3f303a2d9ce87401ee22c6b2 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 14 Mar 2014 13:47:24 +0000 Subject: [PATCH] Move `removePlayer()` and `unsetPlayer()` to `wp.media.mixin` so that other classes can take advantage of them. * `removePlayer()` is an alternative to MediaElement's player removal method that does not re-add the audio|video element back to the DOM. * `unsetPlayer()` will check for an existing `player` property on the passed instance and pause all players before calling `unsetPlayer()` See #27389. git-svn-id: https://develop.svn.wordpress.org/trunk@27538 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/js/media-editor.js | 47 ++++++++++++++++++++++++++++ src/wp-includes/js/media-views.js | 49 ++---------------------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/src/wp-includes/js/media-editor.js b/src/wp-includes/js/media-editor.js index 1f3208a23b..a8458fb178 100644 --- a/src/wp-includes/js/media-editor.js +++ b/src/wp-includes/js/media-editor.js @@ -305,6 +305,53 @@ window.mejs.players[p].pause(); } } + }, + + /** + * Override the MediaElement method for removing a player. + * MediaElement tries to pull the audio/video tag out of + * its container and re-add it to the DOM. + */ + removePlayer: function() { + var t = this.player, featureIndex, feature; + + // invoke features cleanup + for ( featureIndex in t.options.features ) { + feature = t.options.features[featureIndex]; + if ( t['clean' + feature] ) { + try { + t['clean' + feature](t); + } catch (e) {} + } + } + + if ( ! t.isDynamic ) { + t.$node.remove(); + } + + if ( 'native' !== t.media.pluginType ) { + t.media.remove(); + } + + delete window.mejs.players[t.id]; + + t.container.remove(); + t.globalUnbind(); + delete t.node.player; + }, + + /** + * Allows any class that has set 'player' to a MediaElementPlayer + * instance to remove the player when listening to events. + * + * Examples: modal closes, shortcode properties are removed, etc. + */ + unsetPlayer : function() { + if ( this.player ) { + wp.media.mixin.pauseAllPlayers(); + wp.media.mixin.removePlayer.apply( this ); + this.player = false; + } } }; diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 03c746f8ee..4c2114c472 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -6547,11 +6547,11 @@ */ media.view.MediaDetails = media.view.Settings.AttachmentDisplay.extend({ initialize: function() { - _.bindAll(this, 'success', 'unsetPlayer'); + _.bindAll(this, 'success'); - this.listenTo( this.controller, 'close', this.unsetPlayer ); + this.listenTo( this.controller, 'close', media.mixin.unsetPlayer ); this.on( 'ready', this.setPlayer ); - this.on( 'media:setting:remove', this.unsetPlayer ); + this.on( 'media:setting:remove', media.mixin.unsetPlayer, this ); this.on( 'media:setting:remove', this.render ); this.on( 'media:setting:remove', this.setPlayer ); this.events = _.extend( this.events, { @@ -6606,49 +6606,6 @@ return this; }, - /** - * Override the MediaElement method for removing a player. - * MediaElement tries to pull the audio/video tag out of - * its container and re-add it to the DOM. - */ - removePlayer: function() { - var t = this.player, featureIndex, feature; - - // invoke features cleanup - for ( featureIndex in t.options.features ) { - feature = t.options.features[featureIndex]; - if ( t['clean' + feature] ) { - try { - t['clean' + feature](t); - } catch (e) {} - } - } - - if ( ! t.isDynamic ) { - t.$node.remove(); - } - - if ( 'native' !== t.media.pluginType ) { - t.media.remove(); - } - - delete window.mejs.players[t.id]; - - t.container.remove(); - t.globalUnbind(); - delete t.node.player; - }, - - unsetPlayer : function() { - if ( this.player ) { - if ( _.isUndefined( this.mejs.pluginType ) ) { - this.mejs.pause(); - } - this.removePlayer(); - this.player = false; - } - }, - success : function (mejs) { var autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay;