Source: ui/ad_counter.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.AdCounter');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.ads.AdManager');
  9. goog.require('shaka.ui.Element');
  10. goog.require('shaka.ui.Locales');
  11. goog.require('shaka.ui.Localization');
  12. goog.require('shaka.ui.Utils');
  13. goog.require('shaka.util.Dom');
  14. goog.require('shaka.util.Timer');
  15. goog.requireType('shaka.ui.Controls');
  16. /**
  17. * @extends {shaka.ui.Element}
  18. * @final
  19. * @export
  20. */
  21. shaka.ui.AdCounter = class extends shaka.ui.Element {
  22. /**
  23. * @param {!HTMLElement} parent
  24. * @param {!shaka.ui.Controls} controls
  25. */
  26. constructor(parent, controls) {
  27. super(parent, controls);
  28. /** @private {!HTMLElement} */
  29. this.container_ = shaka.util.Dom.createHTMLElement('div');
  30. this.container_.classList.add('shaka-ad-counter');
  31. this.parent.appendChild(this.container_);
  32. /** @private {!HTMLElement} */
  33. this.span_ = shaka.util.Dom.createHTMLElement('span');
  34. this.span_.classList.add('shaka-ad-counter-span');
  35. this.container_.appendChild(this.span_);
  36. /**
  37. * The timer that tracks down the ad progress.
  38. *
  39. * @private {shaka.util.Timer}
  40. */
  41. this.timer_ = new shaka.util.Timer(() => {
  42. this.onTimerTick_();
  43. });
  44. this.updateAriaLabel_();
  45. this.eventManager.listen(
  46. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  47. this.updateAriaLabel_();
  48. });
  49. this.eventManager.listen(
  50. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  51. this.updateAriaLabel_();
  52. });
  53. this.eventManager.listen(
  54. this.adManager, shaka.ads.AdManager.AD_STARTED, () => {
  55. this.onAdStarted_();
  56. });
  57. this.eventManager.listen(
  58. this.adManager, shaka.ads.AdManager.AD_STOPPED, () => {
  59. this.reset_();
  60. });
  61. if (this.ad) {
  62. // There was already an ad.
  63. this.onAdStarted_();
  64. }
  65. }
  66. /**
  67. * @private
  68. */
  69. updateAriaLabel_() {
  70. // TODO
  71. }
  72. /**
  73. * @private
  74. */
  75. onAdStarted_() {
  76. this.timer_.tickNow();
  77. this.timer_.tickEvery(0.5);
  78. }
  79. /**
  80. * @private
  81. */
  82. onTimerTick_() {
  83. goog.asserts.assert(this.ad != null,
  84. 'this.ad should exist at this point');
  85. const secondsLeft = Math.round(this.ad.getRemainingTime());
  86. const adDuration = this.ad.getDuration();
  87. if (secondsLeft == -1 || adDuration == -1) {
  88. // Not enough information about the ad. Don't show the
  89. // counter just yet.
  90. return;
  91. }
  92. if (secondsLeft > 0) {
  93. const timePassed = adDuration - secondsLeft;
  94. const timePassedStr =
  95. shaka.ui.Utils.buildTimeString(timePassed, /* showHour= */ false);
  96. const adLength = shaka.ui.Utils.buildTimeString(
  97. adDuration, /* showHour= */ false);
  98. const timeString = timePassedStr + ' / ' + adLength;
  99. const adsInAdPod = this.ad.getSequenceLength();
  100. // If there's more than one ad in the sequence, show the time
  101. // without the word 'Ad' (it will be shown by another element).
  102. // Otherwise, the format is "Ad: 0:05 / 0:10."
  103. if (adsInAdPod > 1) {
  104. this.span_.textContent = timeString;
  105. } else {
  106. const LocIds = shaka.ui.Locales.Ids;
  107. const raw = this.localization.resolve(LocIds.AD_TIME);
  108. this.span_.textContent = raw.replace('[AD_TIME]', timeString);
  109. }
  110. } else {
  111. this.reset_();
  112. }
  113. }
  114. /**
  115. * @private
  116. */
  117. reset_() {
  118. this.timer_.stop();
  119. // Controls are going to hide the whole ad panel once the ad is over,
  120. // this is just a safeguard.
  121. this.span_.textContent = '';
  122. }
  123. /**
  124. * @override
  125. */
  126. release() {
  127. this.timer_.stop();
  128. this.timer_ = null;
  129. super.release();
  130. }
  131. };