wp-mediaelement.js 2.3 KB

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