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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 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} disableRingingSound if true the ringing sound wont be played.
  24. */
  25. constructor(callee, disableRingingSound) {
  26. this._containerId = 'ringOverlay';
  27. this._audioContainerId = 'ringOverlayRinging';
  28. this.isRinging = true;
  29. this.callee = callee;
  30. this.disableRingingSound = disableRingingSound;
  31. this.render();
  32. if(!disableRingingSound)
  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.disableRingingSound ? ""
  68. : "<audio id=\"" + this._audioContainerId
  69. + "\" src=\"./sounds/ring.ogg\" />";
  70. return `
  71. <div id="${this._containerId}" class='ringing' >
  72. <div class='ringing__content'>
  73. ${callingLabel}
  74. <img class='ringing__avatar' src="${callee.getAvatarUrl()}" />
  75. <div class="ringing__caller-info">
  76. <p>${callee.getName()}${callerStateLabel}</p>
  77. </div>
  78. </div>
  79. ${audioHTML}
  80. </div>`;
  81. }
  82. /**
  83. *
  84. */
  85. render() {
  86. this.htmlStr = this._getHtmlStr(this.callee);
  87. this._attach();
  88. }
  89. /**
  90. * Destroys and clears all the objects (html elements and audio interval)
  91. * related to the ring overlay.
  92. */
  93. destroy() {
  94. this.isRinging = false;
  95. this._stopAudio();
  96. this._detach();
  97. }
  98. _attach() {
  99. $("body").append(this.htmlStr);
  100. }
  101. _detach() {
  102. $(`#${this._containerId}`).remove();
  103. }
  104. /**
  105. * Stops the ringing and clears related timers.
  106. */
  107. _stopAudio() {
  108. if (this.interval) {
  109. clearInterval(this.interval);
  110. }
  111. if(this._timeout) {
  112. clearTimeout(this._timeout);
  113. }
  114. }
  115. /**
  116. * Sets the interval that is going to play the ringing sound.
  117. */
  118. _setAudioTimeout() {
  119. this.interval = setInterval( () => {
  120. this.audio.play();
  121. }, 5000);
  122. }
  123. }
  124. export default {
  125. /**
  126. * Shows the ring overlay for the passed callee.
  127. * @param callee {class User} the callee. Instance of User class from
  128. * TokenData.js
  129. * @param {boolean} disableRingingSound if true the ringing sound wont be played.
  130. */
  131. show(callee, disableRingingSound = false) {
  132. if(overlay) {
  133. this.hide();
  134. }
  135. overlay = new RingOverlay(callee, disableRingingSound);
  136. APP.UI.addListener(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE,
  137. onAvatarDisplayed);
  138. },
  139. /**
  140. * Hides the ring overlay. Destroys all the elements related to the ring
  141. * overlay.
  142. */
  143. hide() {
  144. if(!overlay) {
  145. return false;
  146. }
  147. overlay.destroy();
  148. overlay = null;
  149. APP.UI.removeListener(UIEvents.LARGE_VIDEO_AVATAR_VISIBLE,
  150. onAvatarDisplayed);
  151. return true;
  152. },
  153. /**
  154. * Checks whether or not the ring overlay is currently displayed.
  155. *
  156. * @returns {boolean} true if the ring overlay is currently displayed or
  157. * false otherwise.
  158. */
  159. isVisible () {
  160. return overlay !== null;
  161. }
  162. };