Source: ui/play_button.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.PlayButton');
  7. goog.require('shaka.ads.AdManager');
  8. goog.require('shaka.ui.Element');
  9. goog.require('shaka.ui.Localization');
  10. goog.require('shaka.util.Dom');
  11. goog.requireType('shaka.ui.Controls');
  12. /**
  13. * @extends {shaka.ui.Element}
  14. * @implements {shaka.extern.IUIPlayButton}
  15. * @export
  16. */
  17. shaka.ui.PlayButton = class extends shaka.ui.Element {
  18. /**
  19. * @param {!HTMLElement} parent
  20. * @param {!shaka.ui.Controls} controls
  21. */
  22. constructor(parent, controls) {
  23. super(parent, controls);
  24. const AdManager = shaka.ads.AdManager;
  25. /** @protected {!HTMLButtonElement} */
  26. this.button = shaka.util.Dom.createButton();
  27. this.parent.appendChild(this.button);
  28. const LOCALE_UPDATED = shaka.ui.Localization.LOCALE_UPDATED;
  29. this.eventManager.listen(this.localization, LOCALE_UPDATED, () => {
  30. this.updateAriaLabel();
  31. });
  32. const LOCALE_CHANGED = shaka.ui.Localization.LOCALE_CHANGED;
  33. this.eventManager.listen(this.localization, LOCALE_CHANGED, () => {
  34. this.updateAriaLabel();
  35. });
  36. this.eventManager.listen(this.video, 'play', () => {
  37. this.updateAriaLabel();
  38. this.updateIcon();
  39. });
  40. this.eventManager.listen(this.video, 'pause', () => {
  41. this.updateAriaLabel();
  42. this.updateIcon();
  43. });
  44. this.eventManager.listen(this.video, 'seeking', () => {
  45. this.updateAriaLabel();
  46. this.updateIcon();
  47. });
  48. this.eventManager.listen(this.adManager, AdManager.AD_PAUSED, () => {
  49. this.updateAriaLabel();
  50. this.updateIcon();
  51. });
  52. this.eventManager.listen(this.adManager, AdManager.AD_RESUMED, () => {
  53. this.updateAriaLabel();
  54. this.updateIcon();
  55. });
  56. this.eventManager.listen(this.adManager, AdManager.AD_STARTED, () => {
  57. this.updateAriaLabel();
  58. this.updateIcon();
  59. });
  60. this.eventManager.listen(this.adManager, AdManager.AD_STOPPED, () => {
  61. this.updateAriaLabel();
  62. this.updateIcon();
  63. });
  64. this.eventManager.listen(this.button, 'click', () => {
  65. if (this.ad && this.ad.isLinear()) {
  66. this.controls.playPauseAd();
  67. } else {
  68. this.controls.playPausePresentation();
  69. }
  70. });
  71. if (this.ad) {
  72. // There was already an ad.
  73. this.updateAriaLabel();
  74. this.updateIcon();
  75. }
  76. }
  77. /**
  78. * @return {boolean}
  79. * @protected
  80. * @override
  81. */
  82. isPaused() {
  83. if (this.ad && this.ad.isLinear()) {
  84. return this.ad.isPaused();
  85. }
  86. return this.controls.presentationIsPaused();
  87. }
  88. /**
  89. * @return {boolean}
  90. * @protected
  91. * @override
  92. */
  93. isEnded() {
  94. if (this.ad && this.ad.isLinear()) {
  95. return false;
  96. }
  97. return this.video.ended;
  98. }
  99. /**
  100. * Called when the button's aria label needs to change.
  101. * To be overridden by subclasses.
  102. */
  103. updateAriaLabel() {}
  104. /**
  105. * Called when the button's icon needs to change.
  106. * To be overridden by subclasses.
  107. */
  108. updateIcon() {}
  109. };