Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RingOverlay.js 4.1KB

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