wp/wp-includes/js/mediaelement/wp-mediaelement.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 /* global mejs, _wpmejsSettings */
     1 /* global _wpmejsSettings, mejsL10n */
     2 (function ($) {
     2 (function( window, $ ) {
     3 	// add mime-type aliases to MediaElement plugin support
       
     4 	mejs.plugins.silverlight[0].types.push('video/x-ms-wmv');
       
     5 	mejs.plugins.silverlight[0].types.push('audio/x-ms-wma');
       
     6 
     3 
     7 	$(function () {
     4 	window.wp = window.wp || {};
       
     5 
       
     6 	function wpMediaElement() {
     8 		var settings = {};
     7 		var settings = {};
     9 
     8 
    10 		if ( typeof _wpmejsSettings !== 'undefined' ) {
     9 		/**
    11 			settings = _wpmejsSettings;
    10 		 * Initialize media elements.
       
    11 		 *
       
    12 		 * Ensures media elements that have already been initialized won't be
       
    13 		 * processed again.
       
    14 		 *
       
    15 		 * @since 4.4.0
       
    16 		 *
       
    17 		 * @returns {void}
       
    18 		 */
       
    19 		function initialize() {
       
    20 			if ( typeof _wpmejsSettings !== 'undefined' ) {
       
    21 				settings = $.extend( true, {}, _wpmejsSettings );
       
    22 			}
       
    23 			settings.classPrefix = 'mejs-';
       
    24 			settings.success = settings.success || function ( mejs ) {
       
    25 				var autoplay, loop;
       
    26 
       
    27 				if ( mejs.rendererName && -1 !== mejs.rendererName.indexOf( 'flash' ) ) {
       
    28 					autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay;
       
    29 					loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop;
       
    30 
       
    31 					if ( autoplay ) {
       
    32 						mejs.addEventListener( 'canplay', function() {
       
    33 							mejs.play();
       
    34 						}, false );
       
    35 					}
       
    36 
       
    37 					if ( loop ) {
       
    38 						mejs.addEventListener( 'ended', function() {
       
    39 							mejs.play();
       
    40 						}, false );
       
    41 					}
       
    42 				}
       
    43 			};
       
    44 
       
    45 			/**
       
    46 			 * Custom error handler.
       
    47 			 *
       
    48 			 * Sets up a custom error handler in case a video render fails, and provides a download
       
    49 			 * link as the fallback.
       
    50 			 *
       
    51 			 * @since 4.9.3
       
    52 			 *
       
    53 			 * @param {object} media The wrapper that mimics all the native events/properties/methods for all renderers.
       
    54 			 * @param {object} node  The original HTML video, audio, or iframe tag where the media was loaded.
       
    55 			 * @returns {string}
       
    56 			 */
       
    57 			settings.customError = function ( media, node ) {
       
    58 				// Make sure we only fall back to a download link for flash files.
       
    59 				if ( -1 !== media.rendererName.indexOf( 'flash' ) || -1 !== media.rendererName.indexOf( 'flv' ) ) {
       
    60 					return '<a href="' + node.src + '">' + mejsL10n.strings['mejs.download-video'] + '</a>';
       
    61 				}
       
    62 			};
       
    63 
       
    64 			// Only initialize new media elements.
       
    65 			$( '.wp-audio-shortcode, .wp-video-shortcode' )
       
    66 				.not( '.mejs-container' )
       
    67 				.filter(function () {
       
    68 					return ! $( this ).parent().hasClass( 'mejs-mediaelement' );
       
    69 				})
       
    70 				.mediaelementplayer( settings );
    12 		}
    71 		}
    13 
    72 
    14 		settings.success = settings.success || function (mejs) {
    73 		return {
    15 			var autoplay, loop;
    74 			initialize: initialize
       
    75 		};
       
    76 	}
    16 
    77 
    17 			if ( 'flash' === mejs.pluginType ) {
    78 	window.wp.mediaelement = new wpMediaElement();
    18 				autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay;
       
    19 				loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop;
       
    20 
    79 
    21 				autoplay && mejs.addEventListener( 'canplay', function () {
    80 	$( window.wp.mediaelement.initialize );
    22 					mejs.play();
       
    23 				}, false );
       
    24 
    81 
    25 				loop && mejs.addEventListener( 'ended', function () {
    82 })( window, jQuery );
    26 					mejs.play();
       
    27 				}, false );
       
    28 			}
       
    29 		};
       
    30 
       
    31 		$('.wp-audio-shortcode, .wp-video-shortcode').mediaelementplayer( settings );
       
    32 	});
       
    33 
       
    34 }(jQuery));