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.Constants');
  9. goog.require('shaka.ui.Element');
  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. * @export
  17. */
  18. shaka.ui.PlayButton = class extends shaka.ui.Element {
  19. /**
  20. * @param {!HTMLElement} parent
  21. * @param {!shaka.ui.Controls} controls
  22. */
  23. constructor(parent, controls) {
  24. super(parent, controls);
  25. const AdManager = shaka.ads.AdManager;
  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.adManager, AdManager.AD_PAUSED, () => {
  46. this.updateAriaLabel();
  47. this.updateIcon();
  48. });
  49. this.eventManager.listen(this.adManager, AdManager.AD_RESUMED, () => {
  50. this.updateAriaLabel();
  51. this.updateIcon();
  52. });
  53. this.eventManager.listen(this.adManager, AdManager.AD_STARTED, () => {
  54. this.updateAriaLabel();
  55. this.updateIcon();
  56. });
  57. this.eventManager.listen(this.button, 'click', () => {
  58. if (this.ad) {
  59. this.controls.playPauseAd();
  60. } else {
  61. this.controls.playPausePresentation();
  62. }
  63. });
  64. if (this.ad) {
  65. // There was already an ad.
  66. this.updateAriaLabel();
  67. this.updateIcon();
  68. }
  69. }
  70. /**
  71. * @return {boolean}
  72. * @protected
  73. */
  74. isPaused() {
  75. if (this.ad) {
  76. return this.ad.isPaused();
  77. }
  78. return this.controls.presentationIsPaused();
  79. }
  80. /** @protected */
  81. updateAriaLabel() {
  82. const LocIds = shaka.ui.Locales.Ids;
  83. const label = this.isPaused() ? LocIds.PLAY : LocIds.PAUSE;
  84. this.button.setAttribute(shaka.ui.Constants.ARIA_LABEL,
  85. this.localization.resolve(label));
  86. }
  87. /**
  88. * Called when the button's icon needs to change.
  89. * To be overridden by subclasses.
  90. */
  91. updateIcon() {}
  92. };