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.6KB

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