1/* global _wpmejsSettings, mejsL10n */
2(function( window, $ ) {
3
4 window.wp = window.wp || {};
5
6 function wpMediaElement() {
7 var settings = {};
8
9 /**
10 * Initialize media elements.
11 *
12 * Ensures media elements that have already been initialized won't be
13 * processed again.
14 *
15 * @memberOf wp.mediaelement
16 *
17 * @since 4.4.0
18 *
19 * @return {void}
20 */
21 function initialize() {
22 var selectors = [];
23
24 if ( typeof _wpmejsSettings !== 'undefined' ) {
25 settings = $.extend( true, {}, _wpmejsSettings );
26 }
27 settings.classPrefix = 'mejs-';
28 settings.success = settings.success || function ( mejs ) {
29 var autoplay, loop;
30
31 if ( mejs.rendererName && -1 !== mejs.rendererName.indexOf( 'flash' ) ) {
32 autoplay = mejs.attributes.autoplay && 'false' !== mejs.attributes.autoplay;
33 loop = mejs.attributes.loop && 'false' !== mejs.attributes.loop;
34
35 if ( autoplay ) {
36 mejs.addEventListener( 'canplay', function() {
37 mejs.play();
38 }, false );
39 }
40
41 if ( loop ) {
42 mejs.addEventListener( 'ended', function() {
43 mejs.play();
44 }, false );
45 }
46 }
47 };
48
49 /**
50 * Custom error handler.
51 *
52 * Sets up a custom error handler in case a video render fails, and provides a download
53 * link as the fallback.
54 *
55 * @since 4.9.3
56 *
57 * @param {object} media The wrapper that mimics all the native events/properties/methods for all renderers.
58 * @param {object} node The original HTML video, audio, or iframe tag where the media was loaded.
59 * @return {string}
60 */
61 settings.customError = function ( media, node ) {
62 // Make sure we only fall back to a download link for flash files.
63 if ( -1 !== media.rendererName.indexOf( 'flash' ) || -1 !== media.rendererName.indexOf( 'flv' ) ) {
64 return '<a href="' + node.src + '">' + mejsL10n.strings['mejs.download-file'] + '</a>';
65 }
66 };
67
68 if ( 'undefined' === typeof settings.videoShortcodeLibrary || 'mediaelement' === settings.videoShortcodeLibrary ) {
69 selectors.push( '.wp-video-shortcode' );
70 }
71 if ( 'undefined' === typeof settings.audioShortcodeLibrary || 'mediaelement' === settings.audioShortcodeLibrary ) {
72 selectors.push( '.wp-audio-shortcode' );
73 }
74 if ( ! selectors.length ) {
75 return;
76 }
77
78 // Only initialize new media elements.
79 $( selectors.join( ', ' ) )
80 .not( '.mejs-container' )
81 .filter(function () {
82 return ! $( this ).parent().hasClass( 'mejs-mediaelement' );
83 })
84 .mediaelementplayer( settings );
85 }
86
87 return {
88 initialize: initialize
89 };
90 }
91
92 /**
93 * @namespace wp.mediaelement
94 * @memberOf wp
95 */
96 window.wp.mediaelement = new wpMediaElement();
97
98 $( window.wp.mediaelement.initialize );
99
100})( window, jQuery );
101