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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* global $ */
  2. /* jshint -W101 */
  3. /**
  4. * Shows ring overlay
  5. */
  6. class RingOverlay {
  7. /**
  8. * @param callee instance of User class from TokenData.js
  9. */
  10. constructor(callee) {
  11. this._containerId = 'ringOverlay';
  12. this._audioContainerId = 'ringOverlayRinging';
  13. this.isRinging = true;
  14. this.callee = callee;
  15. this.render();
  16. this.audio = document.getElementById(this._audioContainerId);
  17. this.audio.play();
  18. this._setAudioTimeout();
  19. this._timeout = setTimeout(() => {
  20. this.destroy();
  21. this.render();
  22. }, 30000);
  23. }
  24. /**
  25. * Builds and appends the ring overlay to the html document
  26. */
  27. _getHtmlStr(callee) {
  28. let callingLabel = this.isRinging? "<p>Calling...</p>" : "";
  29. let callerStateLabel = this.isRinging? "" : " isn't available";
  30. return `
  31. <div id="${this._containerId}" class='ringing' >
  32. <div class='ringing__content'>
  33. ${callingLabel}
  34. <img class='ringing__avatar' src="${callee.getAvatarUrl()}" />
  35. <div class="ringing__caller-info">
  36. <p>${callee.getName()}${callerStateLabel}</p>
  37. </div>
  38. </div>
  39. <audio id="${this._audioContainerId}" src="/sounds/ring.ogg" />
  40. </div>`;
  41. }
  42. /**
  43. *
  44. */
  45. render() {
  46. this.htmlStr = this._getHtmlStr(this.callee);
  47. this._attach();
  48. }
  49. /**
  50. * Destroys and clears all the objects (html elements and audio interval)
  51. * related to the ring overlay.
  52. */
  53. destroy() {
  54. this._stopAudio();
  55. this._detach();
  56. }
  57. _attach() {
  58. $("body").append(this.htmlStr);
  59. }
  60. _detach() {
  61. $(`#${this._containerId}`).remove();
  62. }
  63. _stopAudio() {
  64. this.isRinging = false;
  65. if (this.interval) {
  66. clearInterval(this.interval);
  67. }
  68. if(this._timeout) {
  69. clearTimeout(this._timeout);
  70. }
  71. }
  72. /**
  73. * Sets the interval that is going to play the ringing sound.
  74. */
  75. _setAudioTimeout() {
  76. this.interval = setInterval( () => {
  77. this.audio.play();
  78. }, 5000);
  79. }
  80. }
  81. /**
  82. * Store the current ring overlay instance.
  83. * Note: We want to have only 1 instance at a time.
  84. */
  85. let overlay = null;
  86. export default {
  87. /**
  88. * Shows the ring overlay for the passed callee.
  89. * @param callee {class User} the callee. Instance of User class from
  90. * TokenData.js
  91. */
  92. show(callee) {
  93. if(overlay) {
  94. this.hide();
  95. }
  96. overlay = new RingOverlay(callee);
  97. },
  98. /**
  99. * Hides the ring overlay. Destroys all the elements related to the ring
  100. * overlay.
  101. */
  102. hide() {
  103. if(!overlay) {
  104. return false;
  105. }
  106. overlay.destroy();
  107. overlay = null;
  108. return true;
  109. },
  110. /**
  111. * Checks whether or not the ring overlay is currently displayed.
  112. *
  113. * @returns {boolean} true if the ring overlay is currently displayed or
  114. * false otherwise.
  115. */
  116. isVisible () {
  117. return overlay !== null;
  118. }
  119. };