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. label.classList.add('shaka-simple-overflow-button-label-inline');
  48. this.castNameSpan_ = shaka.util.Dom.createHTMLElement('span');
  49. label.appendChild(this.castNameSpan_);
  50. this.castCurrentSelectionSpan_ =
  51. shaka.util.Dom.createHTMLElement('span');
  52. this.castCurrentSelectionSpan_.classList.add(
  53. 'shaka-current-selection-span');
  54. label.appendChild(this.castCurrentSelectionSpan_);
  55. this.castButton_.appendChild(label);
  56. this.parent.appendChild(this.castButton_);
  57. // Setup strings in the correct language
  58. this.updateLocalizedStrings_();
  59. // Setup button display and state according to the current cast status
  60. this.onCastStatusChange_();
  61. this.eventManager.listen(
  62. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  63. this.updateLocalizedStrings_();
  64. });
  65. this.eventManager.listen(
  66. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  67. this.updateLocalizedStrings_();
  68. });
  69. this.eventManager.listen(this.castButton_, 'click', () => {
  70. if (!this.controls.isOpaque()) {
  71. return;
  72. }
  73. this.onCastClick_();
  74. });
  75. this.eventManager.listen(this.controls, 'caststatuschanged', () => {
  76. this.onCastStatusChange_();
  77. });
  78. }
  79. /** @private */
  80. async onCastClick_() {
  81. if (this.castProxy_.isCasting()) {
  82. this.castProxy_.suggestDisconnect();
  83. } else {
  84. try {
  85. this.castButton_.disabled = true;
  86. await this.castProxy_.cast();
  87. this.castButton_.disabled = false;
  88. } catch (error) {
  89. this.castButton_.disabled = false;
  90. if (error.code != shaka.util.Error.Code.CAST_CANCELED_BY_USER) {
  91. this.controls.dispatchEvent(new shaka.util.FakeEvent(
  92. 'error', (new Map()).set('detail', error)));
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * @private
  99. */
  100. onCastStatusChange_() {
  101. const canCast = this.castProxy_.canCast() && this.controls.isCastAllowed();
  102. const isCasting = this.castProxy_.isCasting();
  103. const materialDesignIcons = shaka.ui.Enums.MaterialDesignIcons;
  104. shaka.ui.Utils.setDisplay(this.castButton_, canCast);
  105. this.castIcon_.textContent = isCasting ?
  106. materialDesignIcons.EXIT_CAST :
  107. materialDesignIcons.CAST;
  108. // Aria-pressed set to true when casting, set to false otherwise.
  109. if (canCast) {
  110. if (isCasting) {
  111. this.castButton_.ariaPressed = 'true';
  112. } else {
  113. this.castButton_.ariaPressed = 'false';
  114. }
  115. }
  116. this.setCurrentCastSelection_();
  117. }
  118. /**
  119. * @private
  120. */
  121. setCurrentCastSelection_() {
  122. if (this.castProxy_.isCasting()) {
  123. this.castCurrentSelectionSpan_.textContent =
  124. this.castProxy_.receiverName();
  125. } else {
  126. this.castCurrentSelectionSpan_.textContent =
  127. this.localization.resolve(shaka.ui.Locales.Ids.OFF);
  128. }
  129. }
  130. /**
  131. * @private
  132. */
  133. updateLocalizedStrings_() {
  134. const LocIds = shaka.ui.Locales.Ids;
  135. this.castButton_.ariaLabel = this.localization.resolve(LocIds.CAST);
  136. this.castNameSpan_.textContent =
  137. this.localization.resolve(LocIds.CAST);
  138. // If we're not casting, string "not casting" will be displayed,
  139. // which needs localization.
  140. this.setCurrentCastSelection_();
  141. }
  142. };
  143. /**
  144. * @implements {shaka.extern.IUIElement.Factory}
  145. * @final
  146. */
  147. shaka.ui.CastButton.Factory = class {
  148. /** @override */
  149. create(rootElement, controls) {
  150. return new shaka.ui.CastButton(rootElement, controls);
  151. }
  152. };
  153. shaka.ui.OverflowMenu.registerElement(
  154. 'cast', new shaka.ui.CastButton.Factory());
  155. shaka.ui.Controls.registerElement(
  156. 'cast', new shaka.ui.CastButton.Factory());