You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.7 KiB

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