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. * @export
  15. */
  16. shaka.ui.PlayButton = class extends shaka.ui.Element {
  17. /**
  18. * @param {!HTMLElement} parent
  19. * @param {!shaka.ui.Controls} controls
  20. */
  21. constructor(parent, controls) {
  22. super(parent, controls);
  23. const AdManager = shaka.ads.AdManager;
  24. /** @protected {!HTMLButtonElement} */
  25. this.button = shaka.util.Dom.createButton();
  26. this.parent.appendChild(this.button);
  27. const LOCALE_UPDATED = shaka.ui.Localization.LOCALE_UPDATED;
  28. this.eventManager.listen(this.localization, LOCALE_UPDATED, () => {
  29. this.updateAriaLabel();
  30. });
  31. const LOCALE_CHANGED = shaka.ui.Localization.LOCALE_CHANGED;
  32. this.eventManager.listen(this.localization, LOCALE_CHANGED, () => {
  33. this.updateAriaLabel();
  34. });
  35. this.eventManager.listen(this.video, 'play', () => {
  36. this.updateAriaLabel();
  37. this.updateIcon();
  38. });
  39. this.eventManager.listen(this.video, 'pause', () => {
  40. this.updateAriaLabel();
  41. this.updateIcon();
  42. });
  43. this.eventManager.listen(this.video, 'seeking', () => {
  44. this.updateAriaLabel();
  45. this.updateIcon();
  46. });
  47. this.eventManager.listen(this.adManager, AdManager.AD_PAUSED, () => {
  48. this.updateAriaLabel();
  49. this.updateIcon();
  50. });
  51. this.eventManager.listen(this.adManager, AdManager.AD_RESUMED, () => {
  52. this.updateAriaLabel();
  53. this.updateIcon();
  54. });
  55. this.eventManager.listen(this.adManager, AdManager.AD_STARTED, () => {
  56. this.updateAriaLabel();
  57. this.updateIcon();
  58. });
  59. this.eventManager.listen(this.button, 'click', () => {
  60. if (this.ad && this.ad.isLinear()) {
  61. this.controls.playPauseAd();
  62. } else {
  63. this.controls.playPausePresentation();
  64. }
  65. });
  66. if (this.ad) {
  67. // There was already an ad.
  68. this.updateAriaLabel();
  69. this.updateIcon();
  70. }
  71. }
  72. /**
  73. * @return {boolean}
  74. * @protected
  75. */
  76. isPaused() {
  77. if (this.ad && this.ad.isLinear()) {
  78. return this.ad.isPaused();
  79. }
  80. return this.controls.presentationIsPaused();
  81. }
  82. /**
  83. * Called when the button's aria label needs to change.
  84. * To be overridden by subclasses.
  85. */
  86. updateAriaLabel() {}
  87. /**
  88. * Called when the button's icon needs to change.
  89. * To be overridden by subclasses.
  90. */
  91. updateIcon() {}
  92. };