You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RingOverlay.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* global $, APP */
  2. import UIEvents from "../../../service/UI/UIEvents";
  3. /**
  4. * Store the current ring overlay instance.
  5. * Note: We want to have only 1 instance at a time.
  6. */
  7. let overlay = null;
  8. /**
  9. * Handler for UIEvents.LARGE_VIDEO_AVATAR_VISIBLE event.
  10. * @param {boolean} shown indicates whether the avatar on the large video is
  11. * currently displayed or not.
  12. */
  13. function onAvatarVisible(shown) {
  14. overlay._changeBackground(shown);
  15. }
  16. /**
  17. * Shows ring overlay
  18. */
  19. class RingOverlay {
  20. /**
  21. *
  22. * @param callee The callee (Object) as defined by the JWT support.
  23. * @param {boolean} disableRinging if true the ringing sound wont be played.
  24. */
  25. constructor(callee, disableRinging) {
  26. this._containerId = 'ringOverlay';
  27. this._audioContainerId = 'ringOverlayRinging';
  28. this.isRinging = true;
  29. this.callee = callee;
  30. this.disableRinging = disableRinging;
  31. this.render();
  32. if (!disableRinging)
  33. this._initAudio();
  34. this._timeout
  35. = setTimeout(
  36. () => {
  37. this.destroy();
  38. this.render();
  39. },
  40. 30000);
  41. }
  42. /**
  43. * Initializes the audio element and setups the interval for playing it.
  44. */
  45. _initAudio() {
  46. this.audio = document.getElementById(this._audioContainerId);
  47. this.audio.play();
  48. this.interval = setInterval(() => this.audio.play(), 5000);
  49. }
  50. /**
  51. * Chagnes the background of the ring overlay.
  52. * @param {boolean} solid - if true the new background will be the solid
  53. * one, otherwise the background will be default one.
  54. * NOTE: The method just toggles solidBG css class.
  55. */
  56. _changeBackground(solid) {
  57. const container = $("#" + this._containerId);
  58. if (solid) {
  59. container.addClass("solidBG");
  60. } else {
  61. container.removeClass("solidBG");
  62. }
  63. }
  64. /**
  65. * Builds and appends the ring overlay to the html document
  66. */
  67. _getHtmlStr(callee) {
  68. let callingLabel = this.isRinging ? "<p>Calling...</p>" : "";
  69. let callerStateLabel = this.isRinging ? "" : " isn't available";
  70. let audioHTML = this.disableRinging ? ""
  71. : "<audio id=\"" + this._audioContainerId
  72. + "\" src=\"./sounds/ring.ogg\" />";
  73. return `
  74. <div id="${this._containerId}" class='ringing' >
  75. <div class='ringing__content'>
  76. ${callingLabel}
  77. <img class='ringing__avatar' src="${callee.avatarUrl}" />
  78. <div class="ringing__caller-info">
  79. <p>${callee.name}${callerStateLabel}</p>
  80. </div>
  81. </div>
  82. ${audioHTML}
  83. </div>`;
  84. }
  85. /**
  86. *
  87. */
  88. render() {
  89. this.htmlStr = this._getHtmlStr(this.callee);
  90. this._attach();
  91. }
  92. /**
  93. * Destroys and clears all the objects (html elements and audio interval)
  94. * related to the ring overlay.
  95. */
  96. destroy() {
  97. this.isRinging = false;
  98. this._stopAudio();
  99. this._detach();
  100. }
  101. _attach() {
  102. $("body").append(this.htmlStr);
  103. }
  104. _detach() {
  105. $(`#${this._containerId}`).remove();
  106. }
  107. /**
  108. * Stops the ringing and clears related timers.
  109. */
  110. _stopAudio() {
  111. if (this.interval) {
  112. clearInterval(this.interval);
  113. }
  114. if (this._timeout) {
  115. clearTimeout(this._timeout);
  116. }
  117. }
  118. }
  119. export default {
  120. /**
  121. * Shows the ring overlay for the passed callee.
  122. *
  123. * @param {Object} callee - The callee. Object containing data about
  124. * callee.
  125. * @param {boolean} disableRinging - If true the ringing sound won't be
  126. * played.
  127. * @returns {void}
  128. */
  129. show(callee, disableRinging = false) {
  130. if (overlay) {
  131. this.hide();
  132. }
  133. overlay = new RingOverlay(callee, disableRinging);
  134. APP.UI.addListener(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE,
  135. onAvatarVisible);
  136. },
  137. /**
  138. * Hides the ring overlay. Destroys all the elements related to the ring
  139. * overlay.
  140. */
  141. hide() {
  142. if (!overlay) {
  143. return false;
  144. }
  145. overlay.destroy();
  146. overlay = null;
  147. APP.UI.removeListener(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE,
  148. onAvatarVisible);
  149. return true;
  150. },
  151. /**
  152. * Checks whether or not the ring overlay is currently displayed.
  153. *
  154. * @returns {boolean} true if the ring overlay is currently displayed or
  155. * false otherwise.
  156. */
  157. isVisible() {
  158. return overlay !== null;
  159. }
  160. };