wp-mediaelement.js 2.2 KB

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