mediaelement-migrate.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* global console, MediaElementPlayer, mejs */
  2. (function ( window, $ ) {
  3. // Reintegrate `plugins` since they don't exist in MEJS anymore; it won't affect anything in the player
  4. if (mejs.plugins === undefined) {
  5. mejs.plugins = {};
  6. mejs.plugins.silverlight = [];
  7. mejs.plugins.silverlight.push({
  8. types: []
  9. });
  10. }
  11. // Inclusion of old `HtmlMediaElementShim` if it doesn't exist
  12. mejs.HtmlMediaElementShim = mejs.HtmlMediaElementShim || {
  13. getTypeFromFile: mejs.Utils.getTypeFromFile
  14. };
  15. // Add missing global variables for backward compatibility
  16. if (mejs.MediaFeatures === undefined) {
  17. mejs.MediaFeatures = mejs.Features;
  18. }
  19. if (mejs.Utility === undefined) {
  20. mejs.Utility = mejs.Utils;
  21. }
  22. /**
  23. * Create missing variables and have default `classPrefix` overridden to avoid issues.
  24. *
  25. * `media` is now a fake wrapper needed to simplify manipulation of various media types,
  26. * so in order to access the `video` or `audio` tag, use `media.originalNode` or `player.node`;
  27. * `player.container` used to be jQuery but now is a HTML element, and many elements inside
  28. * the player rely on it being a HTML now, so its conversion is difficult; however, a
  29. * `player.$container` new variable has been added to be used as jQuery object
  30. */
  31. var init = MediaElementPlayer.prototype.init;
  32. MediaElementPlayer.prototype.init = function () {
  33. this.options.classPrefix = 'mejs-';
  34. this.$media = this.$node = $( this.node );
  35. init.call( this );
  36. };
  37. var ready = MediaElementPlayer.prototype._meReady;
  38. MediaElementPlayer.prototype._meReady = function () {
  39. this.container = $( this.container) ;
  40. this.controls = $( this.controls );
  41. this.layers = $( this.layers );
  42. ready.apply( this, arguments );
  43. };
  44. // Override method so certain elements can be called with jQuery
  45. MediaElementPlayer.prototype.getElement = function ( el ) {
  46. return $ !== undefined && el instanceof $ ? el[0] : el;
  47. };
  48. // Add jQuery ONLY to most of custom features' arguments for backward compatibility; default features rely 100%
  49. // on the arguments being HTML elements to work properly
  50. MediaElementPlayer.prototype.buildfeatures = function ( player, controls, layers, media ) {
  51. var defaultFeatures = [
  52. 'playpause',
  53. 'current',
  54. 'progress',
  55. 'duration',
  56. 'tracks',
  57. 'volume',
  58. 'fullscreen'
  59. ];
  60. for (var i = 0, total = this.options.features.length; i < total; i++) {
  61. var feature = this.options.features[i];
  62. if (this['build' + feature]) {
  63. try {
  64. // Use jQuery for non-default features
  65. if (defaultFeatures.indexOf(feature) === -1) {
  66. this['build' + feature]( player, $(controls), $(layers), media );
  67. } else {
  68. this['build' + feature]( player, controls, layers, media );
  69. }
  70. } catch (e) {
  71. console.error( 'error building ' + feature, e );
  72. }
  73. }
  74. }
  75. };
  76. })( window, jQuery );