plugin.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import videojs from 'video.js';
  2. import QualityLevelList from './quality-level-list.js';
  3. import {version as VERSION} from '../package.json';
  4. /**
  5. * Initialization function for the qualityLevels plugin. Sets up the QualityLevelList and
  6. * event handlers.
  7. *
  8. * @param {Player} player Player object.
  9. * @param {Object} options Plugin options object.
  10. * @return {QualityLevelList} a list of QualityLevels
  11. */
  12. const initPlugin = function(player, options) {
  13. const originalPluginFn = player.qualityLevels;
  14. const qualityLevelList = new QualityLevelList();
  15. const disposeHandler = function() {
  16. qualityLevelList.dispose();
  17. player.qualityLevels = originalPluginFn;
  18. player.off('dispose', disposeHandler);
  19. };
  20. player.on('dispose', disposeHandler);
  21. player.qualityLevels = () => qualityLevelList;
  22. player.qualityLevels.VERSION = VERSION;
  23. return qualityLevelList;
  24. };
  25. /**
  26. * A video.js plugin.
  27. *
  28. * In the plugin function, the value of `this` is a video.js `Player`
  29. * instance. You cannot rely on the player being in a "ready" state here,
  30. * depending on how the plugin is invoked. This may or may not be important
  31. * to you; if not, remove the wait for "ready"!
  32. *
  33. * @param {Object} options Plugin options object
  34. * @return {QualityLevelList} a list of QualityLevels
  35. */
  36. const qualityLevels = function(options) {
  37. return initPlugin(this, videojs.obj.merge({}, options));
  38. };
  39. // Register the plugin with video.js.
  40. videojs.registerPlugin('qualityLevels', qualityLevels);
  41. // Include the version number.
  42. qualityLevels.VERSION = VERSION;
  43. export default qualityLevels;