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.adManager, shaka.ads.Utils.AD_PAUSED, () => {
  50. this.updateAriaLabel();
  51. this.updateIcon();
  52. });
  53. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_RESUMED, () => {
  54. this.updateAriaLabel();
  55. this.updateIcon();
  56. });
  57. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_STARTED, () => {
  58. this.updateAriaLabel();
  59. this.updateIcon();
  60. });
  61. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_STOPPED, () => {
  62. this.updateAriaLabel();
  63. this.updateIcon();
  64. });
  65. this.eventManager.listen(this.button, 'click', () => {
  66. if (!this.controls.isOpaque()) {
  67. return;
  68. }
  69. this.controls.playPausePresentation();
  70. });
  71. this.updateAriaLabel();
  72. this.updateIcon();
  73. }
  74. /**
  75. * @return {boolean}
  76. * @protected
  77. * @override
  78. */
  79. isPaused() {
  80. if (this.ad && this.ad.isLinear()) {
  81. return this.ad.isPaused();
  82. }
  83. return this.controls.presentationIsPaused();
  84. }
  85. /**
  86. * @return {boolean}
  87. * @protected
  88. * @override
  89. */
  90. isEnded() {
  91. if (this.ad && this.ad.isLinear()) {
  92. return false;
  93. }
  94. return this.player ? this.player.isEnded() : true;
  95. }
  96. /**
  97. * Called when the button's aria label needs to change.
  98. * To be overridden by subclasses, if necessary
  99. */
  100. updateAriaLabel() {
  101. const LocIds = shaka.ui.Locales.Ids;
  102. if (this.isEnded()) {
  103. this.button.ariaLabel = this.localization.resolve(LocIds.REPLAY);
  104. } else {
  105. const label = this.isPaused() ? LocIds.PLAY : LocIds.PAUSE;
  106. this.button.ariaLabel = this.localization.resolve(label);
  107. }
  108. }
  109. /**
  110. * Called when the button's icon needs to change.
  111. * To be overridden by subclasses.
  112. */
  113. updateIcon() {
  114. const Icons = shaka.ui.Enums.MaterialDesignIcons;
  115. if (this.isEnded()) {
  116. this.button.textContent = Icons.REPLAY;
  117. } else {
  118. this.button.textContent = this.isPaused() ? Icons.PLAY : Icons.PAUSE;
  119. }
  120. }
  121. };