Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RingOverlay.js 4.8KB

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