Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RingOverlay.js 3.7KB

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