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

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