poster-image.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _clickableComponent = require('./clickable-component.js');
  4. var _clickableComponent2 = _interopRequireDefault(_clickableComponent);
  5. var _component = require('./component.js');
  6. var _component2 = _interopRequireDefault(_component);
  7. var _fn = require('./utils/fn.js');
  8. var Fn = _interopRequireWildcard(_fn);
  9. var _dom = require('./utils/dom.js');
  10. var Dom = _interopRequireWildcard(_dom);
  11. var _browser = require('./utils/browser.js');
  12. var browser = _interopRequireWildcard(_browser);
  13. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  15. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  16. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  17. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
  18. * @file poster-image.js
  19. */
  20. /**
  21. * A `ClickableComponent` that handles showing the poster image for the player.
  22. *
  23. * @extends ClickableComponent
  24. */
  25. var PosterImage = function (_ClickableComponent) {
  26. _inherits(PosterImage, _ClickableComponent);
  27. /**
  28. * Create an instance of this class.
  29. *
  30. * @param {Player} player
  31. * The `Player` that this class should attach to.
  32. *
  33. * @param {Object} [options]
  34. * The key/value store of player options.
  35. */
  36. function PosterImage(player, options) {
  37. _classCallCheck(this, PosterImage);
  38. var _this = _possibleConstructorReturn(this, _ClickableComponent.call(this, player, options));
  39. _this.update();
  40. player.on('posterchange', Fn.bind(_this, _this.update));
  41. return _this;
  42. }
  43. /**
  44. * Clean up and dispose of the `PosterImage`.
  45. */
  46. PosterImage.prototype.dispose = function dispose() {
  47. this.player().off('posterchange', this.update);
  48. _ClickableComponent.prototype.dispose.call(this);
  49. };
  50. /**
  51. * Create the `PosterImage`s DOM element.
  52. *
  53. * @return {Element}
  54. * The element that gets created.
  55. */
  56. PosterImage.prototype.createEl = function createEl() {
  57. var el = Dom.createEl('div', {
  58. className: 'vjs-poster',
  59. // Don't want poster to be tabbable.
  60. tabIndex: -1
  61. });
  62. // To ensure the poster image resizes while maintaining its original aspect
  63. // ratio, use a div with `background-size` when available. For browsers that
  64. // do not support `background-size` (e.g. IE8), fall back on using a regular
  65. // img element.
  66. if (!browser.BACKGROUND_SIZE_SUPPORTED) {
  67. this.fallbackImg_ = Dom.createEl('img');
  68. el.appendChild(this.fallbackImg_);
  69. }
  70. return el;
  71. };
  72. /**
  73. * An {@link EventTarget~EventListener} for {@link Player#posterchange} events.
  74. *
  75. * @listens Player#posterchange
  76. *
  77. * @param {EventTarget~Event} [event]
  78. * The `Player#posterchange` event that triggered this function.
  79. */
  80. PosterImage.prototype.update = function update(event) {
  81. var url = this.player().poster();
  82. this.setSrc(url);
  83. // If there's no poster source we should display:none on this component
  84. // so it's not still clickable or right-clickable
  85. if (url) {
  86. this.show();
  87. } else {
  88. this.hide();
  89. }
  90. };
  91. /**
  92. * Set the source of the `PosterImage` depending on the display method.
  93. *
  94. * @param {string} url
  95. * The URL to the source for the `PosterImage`.
  96. */
  97. PosterImage.prototype.setSrc = function setSrc(url) {
  98. if (this.fallbackImg_) {
  99. this.fallbackImg_.src = url;
  100. } else {
  101. var backgroundImage = '';
  102. // Any falsey values should stay as an empty string, otherwise
  103. // this will throw an extra error
  104. if (url) {
  105. backgroundImage = 'url("' + url + '")';
  106. }
  107. this.el_.style.backgroundImage = backgroundImage;
  108. }
  109. };
  110. /**
  111. * An {@link EventTarget~EventListener} for clicks on the `PosterImage`. See
  112. * {@link ClickableComponent#handleClick} for instances where this will be triggered.
  113. *
  114. * @listens tap
  115. * @listens click
  116. * @listens keydown
  117. *
  118. * @param {EventTarget~Event} event
  119. + The `click`, `tap` or `keydown` event that caused this function to be called.
  120. */
  121. PosterImage.prototype.handleClick = function handleClick(event) {
  122. // We don't want a click to trigger playback when controls are disabled
  123. if (!this.player_.controls()) {
  124. return;
  125. }
  126. if (this.player_.paused()) {
  127. this.player_.play();
  128. } else {
  129. this.player_.pause();
  130. }
  131. };
  132. return PosterImage;
  133. }(_clickableComponent2['default']);
  134. _component2['default'].registerComponent('PosterImage', PosterImage);
  135. exports['default'] = PosterImage;