Source: ui/cast_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.CastButton');
  7. goog.require('shaka.cast.CastProxy');
  8. goog.require('shaka.ui.Controls');
  9. goog.require('shaka.ui.Element');
  10. goog.require('shaka.ui.Enums');
  11. goog.require('shaka.ui.Locales');
  12. goog.require('shaka.ui.Localization');
  13. goog.require('shaka.ui.OverflowMenu');
  14. goog.require('shaka.ui.Utils');
  15. goog.require('shaka.util.Dom');
  16. goog.require('shaka.util.Error');
  17. goog.require('shaka.util.FakeEvent');
  18. goog.requireType('shaka.cast.CastProxy');
  19. goog.requireType('shaka.ui.Controls');
  20. /**
  21. * @extends {shaka.ui.Element}
  22. * @final
  23. * @export
  24. */
  25. shaka.ui.CastButton = class extends shaka.ui.Element {
  26. /**
  27. * @param {!HTMLElement} parent
  28. * @param {!shaka.ui.Controls} controls
  29. */
  30. constructor(parent, controls) {
  31. super(parent, controls);
  32. /** @private {shaka.cast.CastProxy} */
  33. this.castProxy_ = this.controls.getCastProxy();
  34. /** @private {!HTMLButtonElement} */
  35. this.castButton_ = shaka.util.Dom.createButton();
  36. this.castButton_.classList.add('shaka-cast-button');
  37. this.castButton_.classList.add('shaka-tooltip');
  38. this.castButton_.ariaPressed = 'false';
  39. /** @private {!HTMLElement} */
  40. this.castIcon_ = shaka.util.Dom.createHTMLElement('i');
  41. this.castIcon_.classList.add('material-icons-round');
  42. this.castIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.CAST;
  43. this.castButton_.appendChild(this.castIcon_);
  44. const label = shaka.util.Dom.createHTMLElement('label');
  45. label.classList.add('shaka-overflow-button-label');
  46. label.classList.add('shaka-overflow-menu-only');
  47. this.castNameSpan_ = shaka.util.Dom.createHTMLElement('span');
  48. label.appendChild(this.castNameSpan_);
  49. this.castCurrentSelectionSpan_ =
  50. shaka.util.Dom.createHTMLElement('span');
  51. this.castCurrentSelectionSpan_.classList.add(
  52. 'shaka-current-selection-span');
  53. label.appendChild(this.castCurrentSelectionSpan_);
  54. this.castButton_.appendChild(label);
  55. this.parent.appendChild(this.castButton_);
  56. // Setup strings in the correct language
  57. this.updateLocalizedStrings_();
  58. // Setup button display and state according to the current cast status
  59. this.onCastStatusChange_();
  60. this.eventManager.listen(
  61. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  62. this.updateLocalizedStrings_();
  63. });
  64. this.eventManager.listen(
  65. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  66. this.updateLocalizedStrings_();
  67. });
  68. this.eventManager.listen(this.castButton_, 'click', () => {
  69. if (!this.controls.isOpaque()) {
  70. return;
  71. }
  72. this.onCastClick_();
  73. });
  74. this.eventManager.listen(this.controls, 'caststatuschanged', () => {
  75. this.onCastStatusChange_();
  76. });
  77. }
  78. /** @private */
  79. async onCastClick_() {
  80. if (this.castProxy_.isCasting()) {
  81. this.castProxy_.suggestDisconnect();
  82. } else {
  83. try {
  84. this.castButton_.disabled = true;
  85. await this.castProxy_.cast();
  86. this.castButton_.disabled = false;
  87. } catch (error) {
  88. this.castButton_.disabled = false;
  89. if (error.code != shaka.util.Error.Code.CAST_CANCELED_BY_USER) {
  90. this.controls.dispatchEvent(new shaka.util.FakeEvent(
  91. 'error', (new Map()).set('detail', error)));
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * @private
  98. */
  99. onCastStatusChange_() {
  100. const canCast = this.castProxy_.canCast() && this.controls.isCastAllowed();
  101. const isCasting = this.castProxy_.isCasting();
  102. const materialDesignIcons = shaka.ui.Enums.MaterialDesignIcons;
  103. shaka.ui.Utils.setDisplay(this.castButton_, canCast);
  104. this.castIcon_.textContent = isCasting ?
  105. materialDesignIcons.EXIT_CAST :
  106. materialDesignIcons.CAST;
  107. // Aria-pressed set to true when casting, set to false otherwise.
  108. if (canCast) {
  109. if (isCasting) {
  110. this.castButton_.ariaPressed = 'true';
  111. } else {
  112. this.castButton_.ariaPressed = 'false';
  113. }
  114. }
  115. this.setCurrentCastSelection_();
  116. }
  117. /**
  118. * @private
  119. */
  120. setCurrentCastSelection_() {
  121. if (this.castProxy_.isCasting()) {
  122. this.castCurrentSelectionSpan_.textContent =
  123. this.castProxy_.receiverName();
  124. } else {
  125. this.castCurrentSelectionSpan_.textContent =
  126. this.localization.resolve(shaka.ui.Locales.Ids.OFF);
  127. }
  128. }
  129. /**
  130. * @private
  131. */
  132. updateLocalizedStrings_() {
  133. const LocIds = shaka.ui.Locales.Ids;
  134. this.castButton_.ariaLabel = this.localization.resolve(LocIds.CAST);
  135. this.castNameSpan_.textContent =
  136. this.localization.resolve(LocIds.CAST);
  137. // If we're not casting, string "not casting" will be displayed,
  138. // which needs localization.
  139. this.setCurrentCastSelection_();
  140. }
  141. };
  142. /**
  143. * @implements {shaka.extern.IUIElement.Factory}
  144. * @final
  145. */
  146. shaka.ui.CastButton.Factory = class {
  147. /** @override */
  148. create(rootElement, controls) {
  149. return new shaka.ui.CastButton(rootElement, controls);
  150. }
  151. };
  152. shaka.ui.OverflowMenu.registerElement(
  153. 'cast', new shaka.ui.CastButton.Factory());
  154. shaka.ui.Controls.registerElement(
  155. 'cast', new shaka.ui.CastButton.Factory());