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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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} 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. _stopAudio() {
  105. if (this.interval) {
  106. clearInterval(this.interval);
  107. }
  108. if(this._timeout) {
  109. clearTimeout(this._timeout);
  110. }
  111. }
  112. /**
  113. * Sets the interval that is going to play the ringing sound.
  114. */
  115. _setAudioTimeout() {
  116. this.interval = setInterval( () => {
  117. this.audio.play();
  118. }, 5000);
  119. }
  120. }
  121. export default {
  122. /**
  123. * Shows the ring overlay for the passed callee.
  124. * @param callee {class User} the callee. Instance of User class from
  125. * TokenData.js
  126. * @param {boolean} disableRingingSound if true the ringing sound wont be played.
  127. */
  128. show(callee, disableRingingSound = false) {
  129. if(overlay) {
  130. this.hide();
  131. }
  132. overlay = new RingOverlay(callee, disableRingingSound);
  133. APP.UI.addListener(UIEvents.LARGE_VIDEO_AVATAR_DISPLAYED,
  134. onAvatarDisplayed);
  135. },
  136. /**
  137. * Hides the ring overlay. Destroys all the elements related to the ring
  138. * overlay.
  139. */
  140. hide() {
  141. if(!overlay) {
  142. return false;
  143. }
  144. overlay.destroy();
  145. overlay = null;
  146. APP.UI.removeListener(UIEvents.LARGE_VIDEO_AVATAR_DISPLAYED,
  147. onAvatarDisplayed);
  148. return true;
  149. },
  150. /**
  151. * Checks whether or not the ring overlay is currently displayed.
  152. *
  153. * @returns {boolean} true if the ring overlay is currently displayed or
  154. * false otherwise.
  155. */
  156. isVisible () {
  157. return overlay !== null;
  158. }
  159. };