2016-06-13 21:11:44 +00:00
|
|
|
/* global $ */
|
2016-09-13 04:10:18 +00:00
|
|
|
/* jshint -W101 */
|
2016-06-13 21:11:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows ring overlay
|
|
|
|
*/
|
|
|
|
class RingOverlay {
|
|
|
|
/**
|
|
|
|
* @param callee instance of User class from TokenData.js
|
|
|
|
*/
|
|
|
|
constructor(callee) {
|
2016-09-13 04:10:18 +00:00
|
|
|
this._containerId = 'ringOverlay';
|
|
|
|
this._audioContainerId = 'ringOverlayRinging';
|
|
|
|
|
2016-06-13 21:11:44 +00:00
|
|
|
this.callee = callee;
|
2016-09-13 04:10:18 +00:00
|
|
|
this.render();
|
|
|
|
this.audio = document.getElementById(this._audioContainerId);
|
|
|
|
this.audio.play();
|
2016-06-13 21:11:44 +00:00
|
|
|
this._setAudioTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds and appends the ring overlay to the html document
|
|
|
|
*/
|
2016-09-13 04:10:18 +00:00
|
|
|
_getHtmlStr(callee) {
|
|
|
|
return `
|
|
|
|
<div id="${this._containerId}" class='ringing' >
|
|
|
|
<div class='ringing__content'>
|
|
|
|
<p>Calling...</p>
|
|
|
|
<img class='ringing__avatar' src="${callee.getAvatarUrl()}" />
|
|
|
|
<div class="ringing__caller-info">
|
|
|
|
<p>${callee.getName()}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<audio id="${this._audioContainerId}" src="/sounds/ring.ogg" />
|
|
|
|
</div>`;
|
2016-06-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-13 04:10:18 +00:00
|
|
|
*
|
2016-06-13 21:11:44 +00:00
|
|
|
*/
|
2016-09-13 04:10:18 +00:00
|
|
|
render() {
|
|
|
|
this.htmlStr = this._getHtmlStr(this.callee);
|
|
|
|
this._attach();
|
2016-06-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys and clears all the objects (html elements and audio interval)
|
|
|
|
* related to the ring overlay.
|
|
|
|
*/
|
|
|
|
destroy() {
|
2016-09-13 04:10:18 +00:00
|
|
|
if (this.interval) {
|
2016-06-13 21:11:44 +00:00
|
|
|
clearInterval(this.interval);
|
2016-09-13 04:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
_attach() {
|
|
|
|
$("body").append(this.htmlStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
_detach() {
|
|
|
|
$(`#${this._containerId}`).remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the interval that is going to play the ringing sound.
|
|
|
|
*/
|
|
|
|
_setAudioTimeout() {
|
|
|
|
this.interval = setInterval( () => {
|
|
|
|
this.audio.play();
|
|
|
|
}, 5000);
|
2016-06-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the current ring overlay instance.
|
|
|
|
* Note: We want to have only 1 instance at a time.
|
|
|
|
*/
|
|
|
|
let overlay = null;
|
|
|
|
|
|
|
|
export default {
|
|
|
|
/**
|
|
|
|
* Shows the ring overlay for the passed callee.
|
|
|
|
* @param callee {class User} the callee. Instance of User class from
|
|
|
|
* TokenData.js
|
|
|
|
*/
|
|
|
|
show(callee) {
|
|
|
|
if(overlay) {
|
|
|
|
this.hide();
|
|
|
|
}
|
2016-09-13 04:10:18 +00:00
|
|
|
|
2016-06-13 21:11:44 +00:00
|
|
|
overlay = new RingOverlay(callee);
|
|
|
|
},
|
2016-09-13 04:10:18 +00:00
|
|
|
|
2016-06-13 21:11:44 +00:00
|
|
|
/**
|
|
|
|
* Hides the ring overlay. Destroys all the elements related to the ring
|
|
|
|
* overlay.
|
|
|
|
*/
|
|
|
|
hide() {
|
2016-09-13 04:10:18 +00:00
|
|
|
if(!overlay) {
|
2016-06-13 21:11:44 +00:00
|
|
|
return false;
|
2016-09-13 04:10:18 +00:00
|
|
|
}
|
2016-06-13 21:11:44 +00:00
|
|
|
overlay.destroy();
|
|
|
|
overlay = null;
|
|
|
|
return true;
|
2016-08-01 20:21:23 +00:00
|
|
|
},
|
2016-09-13 04:10:18 +00:00
|
|
|
|
2016-08-01 20:21:23 +00:00
|
|
|
/**
|
|
|
|
* Checks whether or not the ring overlay is currently displayed.
|
|
|
|
*
|
|
|
|
* @returns {boolean} true if the ring overlay is currently displayed or
|
|
|
|
* false otherwise.
|
|
|
|
*/
|
2016-09-13 04:10:18 +00:00
|
|
|
isVisible () {
|
2016-08-01 20:21:23 +00:00
|
|
|
return overlay !== null;
|
2016-06-13 21:11:44 +00:00
|
|
|
}
|
|
|
|
};
|