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.Utils');
  8. goog.require('shaka.ui.Element');
  9. goog.require('shaka.ui.Enums');
  10. goog.require('shaka.ui.Locales');
  11. goog.require('shaka.ui.Localization');
  12. goog.require('shaka.util.Dom');
  13. goog.requireType('shaka.ui.Controls');
  14. /**
  15. * @extends {shaka.ui.Element}
  16. * @implements {shaka.extern.IUIPlayButton}
  17. * @export
  18. */
  19. shaka.ui.PlayButton = class extends shaka.ui.Element {
  20. /**
  21. * @param {!HTMLElement} parent
  22. * @param {!shaka.ui.Controls} controls
  23. */
  24. constructor(parent, controls) {
  25. super(parent, controls);
  26. /** @protected {!HTMLButtonElement} */
  27. this.button = shaka.util.Dom.createButton();
  28. this.parent.appendChild(this.button);
  29. const LOCALE_UPDATED = shaka.ui.Localization.LOCALE_UPDATED;
  30. this.eventManager.listen(this.localization, LOCALE_UPDATED, () => {
  31. this.updateAriaLabel();
  32. });
  33. const LOCALE_CHANGED = shaka.ui.Localization.LOCALE_CHANGED;
  34. this.eventManager.listen(this.localization, LOCALE_CHANGED, () => {
  35. this.updateAriaLabel();
  36. });
  37. this.eventManager.listen(this.video, 'play', () => {
  38. this.updateAriaLabel();
  39. this.updateIcon();
  40. });
  41. this.eventManager.listen(this.video, 'pause', () => {
  42. this.updateAriaLabel();
  43. this.updateIcon();
  44. });
  45. this.eventManager.listen(this.video, 'seeking', () => {
  46. this.updateAriaLabel();
  47. this.updateIcon();
  48. });
  49. this.eventManager.listen(this.player, 'loaded', () => {
  50. this.updateAriaLabel();
  51. this.updateIcon();
  52. });
  53. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_PAUSED, () => {
  54. this.updateAriaLabel();
  55. this.updateIcon();
  56. });
  57. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_RESUMED, () => {
  58. this.updateAriaLabel();
  59. this.updateIcon();
  60. });
  61. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_STARTED, () => {
  62. this.updateAriaLabel();
  63. this.updateIcon();
  64. });
  65. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_STOPPED, () => {
  66. this.updateAriaLabel();
  67. this.updateIcon();
  68. });
  69. this.eventManager.listen(this.button, 'click', () => {
  70. if (!this.controls.isOpaque()) {
  71. return;
  72. }
  73. this.controls.playPausePresentation();
  74. });
  75. this.updateAriaLabel();
  76. this.updateIcon();
  77. }
  78. /**
  79. * @return {boolean}
  80. * @protected
  81. * @override
  82. */
  83. isPaused() {
  84. if (this.ad && this.ad.isLinear()) {
  85. return this.ad.isPaused();
  86. }
  87. return this.controls.presentationIsPaused();
  88. }
  89. /**
  90. * @return {boolean}
  91. * @protected
  92. * @override
  93. */
  94. isEnded() {
  95. if (this.ad && this.ad.isLinear()) {
  96. return false;
  97. }
  98. return this.player ? this.player.isEnded() : true;
  99. }
  100. /**
  101. * Called when the button's aria label needs to change.
  102. * To be overridden by subclasses, if necessary
  103. */
  104. updateAriaLabel() {
  105. const LocIds = shaka.ui.Locales.Ids;
  106. if (this.isEnded() && this.video.duration) {
  107. this.button.ariaLabel = this.localization.resolve(LocIds.REPLAY);
  108. } else {
  109. const label = this.isPaused() ? LocIds.PLAY : LocIds.PAUSE;
  110. this.button.ariaLabel = this.localization.resolve(label);
  111. }
  112. }
  113. /**
  114. * Called when the button's icon needs to change.
  115. * To be overridden by subclasses.
  116. */
  117. updateIcon() {
  118. const Icons = shaka.ui.Enums.MaterialDesignIcons;
  119. if (this.isEnded() && this.video.duration) {
  120. this.button.textContent = Icons.REPLAY;
  121. } else {
  122. this.button.textContent = this.isPaused() ? Icons.PLAY : Icons.PAUSE;
  123. }
  124. }
  125. };