Source: ui/ad_position.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.AdPosition');
  7. goog.require('shaka.ads.Utils');
  8. goog.require('shaka.ui.Element');
  9. goog.require('shaka.ui.Locales');
  10. goog.require('shaka.ui.Localization');
  11. goog.require('shaka.ui.Utils');
  12. goog.require('shaka.util.Dom');
  13. goog.requireType('shaka.ui.Controls');
  14. /**
  15. * @extends {shaka.ui.Element}
  16. * @final
  17. * @export
  18. */
  19. shaka.ui.AdPosition = 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. /** @private {!HTMLElement} */
  27. this.container_ = shaka.util.Dom.createHTMLElement('div');
  28. this.container_.classList.add('shaka-ad-position');
  29. shaka.ui.Utils.setDisplay(this.container_, false);
  30. this.parent.appendChild(this.container_);
  31. /** @private {!HTMLElement} */
  32. this.span_ = shaka.util.Dom.createHTMLElement('span');
  33. this.span_.classList.add('shaka-ad-position-span');
  34. this.container_.appendChild(this.span_);
  35. this.updateAriaLabel_();
  36. this.eventManager.listen(
  37. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  38. if (!this.ad) {
  39. return;
  40. }
  41. this.updateAriaLabel_();
  42. this.setPosition_();
  43. });
  44. this.eventManager.listen(
  45. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  46. if (!this.ad) {
  47. return;
  48. }
  49. this.updateAriaLabel_();
  50. this.setPosition_();
  51. });
  52. this.eventManager.listen(
  53. this.adManager, shaka.ads.Utils.AD_STARTED, () => {
  54. this.setPosition_();
  55. });
  56. this.eventManager.listen(
  57. this.adManager, shaka.ads.Utils.AD_STOPPED, () => {
  58. this.span_.textContent = '';
  59. shaka.ui.Utils.setDisplay(this.container_, false);
  60. });
  61. if (this.ad) {
  62. // There was already an ad.
  63. this.setPosition_();
  64. }
  65. }
  66. /**
  67. * @private
  68. */
  69. updateAriaLabel_() {
  70. // TODO
  71. }
  72. /**
  73. * @private
  74. */
  75. setPosition_() {
  76. const adsInAdPod = this.ad.getSequenceLength();
  77. if (adsInAdPod > 1 && this.ad.isLinear()) {
  78. // If it's a single ad, showing 'Ad 1 of 1' isn't helpful.
  79. // Only show this element if there's more than 1 ad and it's a linear ad.
  80. const LocIds = shaka.ui.Locales.Ids;
  81. const adPosition = this.ad.getPositionInSequence();
  82. this.span_.textContent = this.localization.resolve(LocIds.AD_PROGRESS)
  83. .replace('[AD_ON]', String(adPosition))
  84. .replace('[NUM_ADS]', String(adsInAdPod));
  85. shaka.ui.Utils.setDisplay(this.container_, true);
  86. }
  87. }
  88. };