Make WordPress Core

Changeset 34346


Ignore:
Timestamp:
09/20/2015 03:16:23 AM (11 years ago)
Author:
wonderboymusic
Message:

MediaElement, update wp-mediaelement.js:

  • create a public initialize method on the wp.mediaelement namespace
  • make it idempotent to prevent nasty side-effects caused by initializing media elements more than once

Props bradyvercher.
Fixes #32423.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/mediaelement/wp-mediaelement.js

    r31471 r34346  
    11/* global mejs, _wpmejsSettings */
    2 (function ($) {
     2(function( window, $ ) {
     3
     4        window.wp = window.wp || {};
     5
    36        // add mime-type aliases to MediaElement plugin support
    47        mejs.plugins.silverlight[0].types.push('video/x-ms-wmv');
    58        mejs.plugins.silverlight[0].types.push('audio/x-ms-wma');
    69
    7         $(function () {
     10        function wpMediaElement() {
    811                var settings = {};
    912
    10                 if ( typeof _wpmejsSettings !== 'undefined' ) {
    11                         settings = _wpmejsSettings;
     13                /**
     14                 * Initialize media elements.
     15                 *
     16                 * Ensures media elements that have already been initialized won't be
     17                 * processed again.
     18                 *
     19                 * @since 4.4.0
     20                 */
     21                function initialize() {
     22                        if ( typeof _wpmejsSettings !== 'undefined' ) {
     23                                settings = _wpmejsSettings;
     24                        }
     25
     26                        settings.success = settings.success || function (mejs) {
     27                                var autoplay, loop;
     28
     29                                if ( 'flash' === mejs.pluginType ) {
     30                                        autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay;
     31                                        loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop;
     32
     33                                        autoplay && mejs.addEventListener( 'canplay', function () {
     34                                                mejs.play();
     35                                        }, false );
     36
     37                                        loop && mejs.addEventListener( 'ended', function () {
     38                                                mejs.play();
     39                                        }, false );
     40                                }
     41                        };
     42
     43                        // Only initialize new media elements.
     44                        $( '.wp-audio-shortcode, .wp-video-shortcode' )
     45                                .not( '.mejs-container' )
     46                                .filter(function () {
     47                                        return ! $( this ).parent().hasClass( '.mejs-mediaelement' );
     48                                })
     49                                .mediaelementplayer( settings );
    1250                }
    1351
    14                 settings.success = settings.success || function (mejs) {
    15                         var autoplay, loop;
     52                return {
     53                        initialize: initialize
     54                };
     55        }
    1656
    17                         if ( 'flash' === mejs.pluginType ) {
    18                                 autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay;
    19                                 loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop;
     57        window.wp.mediaelement = new wpMediaElement();
    2058
    21                                 autoplay && mejs.addEventListener( 'canplay', function () {
    22                                         mejs.play();
    23                                 }, false );
     59        $( document ).on( 'ready', window.wp.mediaelement.initialize );
    2460
    25                                 loop && mejs.addEventListener( 'ended', function () {
    26                                         mejs.play();
    27                                 }, false );
    28                         }
    29                 };
    30 
    31                 $('.wp-audio-shortcode, .wp-video-shortcode').mediaelementplayer( settings );
    32         });
    33 
    34 }(jQuery));
     61})( window, jQuery );
Note: See TracChangeset for help on using the changeset viewer.