2016-10-25 22:57:29 +00:00
|
|
|
/* global $ */
|
2014-10-16 15:11:26 +00:00
|
|
|
var JitsiPopover = (function () {
|
2016-10-25 22:57:29 +00:00
|
|
|
/**
|
|
|
|
* The default options
|
|
|
|
*/
|
|
|
|
const defaultOptions = {
|
|
|
|
skin: 'white',
|
|
|
|
content: '',
|
|
|
|
hasArrow: true,
|
|
|
|
onBeforePosition: undefined
|
|
|
|
};
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
/**
|
|
|
|
* Constructs new JitsiPopover and attaches it to the element
|
|
|
|
* @param element jquery selector
|
|
|
|
* @param options the options for the popover.
|
2016-10-25 22:57:29 +00:00
|
|
|
* - {Function} onBeforePosition - function executed just before
|
|
|
|
* positioning the popover. Useful for translation.
|
2014-10-16 15:11:26 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function JitsiPopover(element, options)
|
|
|
|
{
|
2016-10-25 22:57:29 +00:00
|
|
|
this.options = Object.assign({}, defaultOptions, options);
|
2014-10-16 15:11:26 +00:00
|
|
|
|
|
|
|
this.elementIsHovered = false;
|
|
|
|
this.popoverIsHovered = false;
|
|
|
|
this.popoverShown = false;
|
|
|
|
|
|
|
|
element.data("jitsi_popover", this);
|
|
|
|
this.element = element;
|
2016-10-20 13:18:20 +00:00
|
|
|
this.template = this.getTemplate();
|
2014-10-16 15:11:26 +00:00
|
|
|
var self = this;
|
|
|
|
this.element.on("mouseenter", function () {
|
|
|
|
self.elementIsHovered = true;
|
|
|
|
self.show();
|
|
|
|
}).on("mouseleave", function () {
|
|
|
|
self.elementIsHovered = false;
|
|
|
|
setTimeout(function () {
|
|
|
|
self.hide();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-20 13:18:20 +00:00
|
|
|
/**
|
|
|
|
* Returns template for popover
|
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.getTemplate = function () {
|
|
|
|
let arrow = '';
|
|
|
|
if (this.options.hasArrow) {
|
|
|
|
arrow = '<div class="arrow"></div>';
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
`<div class="jitsipopover ${this.options.skin}">
|
|
|
|
${arrow}
|
|
|
|
<div class="jitsipopover__content"></div>
|
2016-10-31 16:12:28 +00:00
|
|
|
<div class="jitsipopover__menu-padding"></div>
|
|
|
|
</div>`
|
2016-10-20 13:18:20 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
/**
|
|
|
|
* Shows the popover
|
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.show = function () {
|
2015-08-06 23:34:40 +00:00
|
|
|
if(!JitsiPopover.enabled)
|
|
|
|
return;
|
2014-10-16 15:11:26 +00:00
|
|
|
this.createPopover();
|
|
|
|
this.popoverShown = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides the popover
|
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.hide = function () {
|
2015-07-28 21:52:32 +00:00
|
|
|
if(!this.elementIsHovered && !this.popoverIsHovered &&
|
|
|
|
this.popoverShown) {
|
2014-10-17 13:51:23 +00:00
|
|
|
this.forceHide();
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-17 13:51:23 +00:00
|
|
|
/**
|
2015-07-28 21:52:32 +00:00
|
|
|
* Hides the popover.
|
2014-10-17 13:51:23 +00:00
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.forceHide = function () {
|
|
|
|
$(".jitsipopover").remove();
|
|
|
|
this.popoverShown = false;
|
2016-10-28 16:16:40 +00:00
|
|
|
if(this.popoverIsHovered) { //the browser is not firing hover events
|
|
|
|
//when the element was on hover if got removed.
|
|
|
|
this.popoverIsHovered = false;
|
|
|
|
this.onHoverPopover(this.popoverIsHovered);
|
|
|
|
}
|
2014-10-17 13:51:23 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
/**
|
2015-07-28 21:52:32 +00:00
|
|
|
* Creates the popover html.
|
2014-10-16 15:11:26 +00:00
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.createPopover = function () {
|
|
|
|
$("body").append(this.template);
|
2016-10-21 18:17:17 +00:00
|
|
|
let popoverElem = $(".jitsipopover > .jitsipopover__content");
|
2016-10-21 17:11:22 +00:00
|
|
|
popoverElem.html(this.options.content);
|
2016-10-25 22:57:29 +00:00
|
|
|
if(typeof this.options.onBeforePosition === "function") {
|
|
|
|
this.options.onBeforePosition($(".jitsipopover"));
|
|
|
|
}
|
2014-10-16 15:11:26 +00:00
|
|
|
var self = this;
|
|
|
|
$(".jitsipopover").on("mouseenter", function () {
|
|
|
|
self.popoverIsHovered = true;
|
2016-10-27 18:17:17 +00:00
|
|
|
if(typeof self.onHoverPopover === "function") {
|
|
|
|
self.onHoverPopover(self.popoverIsHovered);
|
|
|
|
}
|
2014-10-16 15:11:26 +00:00
|
|
|
}).on("mouseleave", function () {
|
|
|
|
self.popoverIsHovered = false;
|
|
|
|
self.hide();
|
2016-10-27 18:17:17 +00:00
|
|
|
if(typeof self.onHoverPopover === "function") {
|
|
|
|
self.onHoverPopover(self.popoverIsHovered);
|
|
|
|
}
|
2014-10-16 15:11:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.refreshPosition();
|
|
|
|
};
|
|
|
|
|
2016-10-27 18:17:17 +00:00
|
|
|
/**
|
|
|
|
* Adds a hover listener to the popover.
|
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.addOnHoverPopover = function (listener) {
|
|
|
|
this.onHoverPopover = listener;
|
|
|
|
};
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
/**
|
2015-07-28 21:52:32 +00:00
|
|
|
* Refreshes the position of the popover.
|
2014-10-16 15:11:26 +00:00
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.refreshPosition = function () {
|
|
|
|
$(".jitsipopover").position({
|
|
|
|
my: "bottom",
|
|
|
|
at: "top",
|
|
|
|
collision: "fit",
|
|
|
|
of: this.element,
|
|
|
|
using: function (position, elements) {
|
2015-07-28 21:52:32 +00:00
|
|
|
var calcLeft = elements.target.left - elements.element.left +
|
|
|
|
elements.target.width/2;
|
|
|
|
$(".jitsipopover").css(
|
|
|
|
{top: position.top, left: position.left, display: "table"});
|
2014-10-16 15:11:26 +00:00
|
|
|
$(".jitsipopover > .arrow").css({left: calcLeft});
|
2016-10-13 15:00:02 +00:00
|
|
|
$(".jitsipopover > .jitsipopover__menu-padding").css(
|
2015-07-28 21:52:32 +00:00
|
|
|
{left: calcLeft - 50});
|
2014-10-16 15:11:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the content of popover.
|
|
|
|
* @param content new content
|
|
|
|
*/
|
|
|
|
JitsiPopover.prototype.updateContent = function (content) {
|
|
|
|
this.options.content = content;
|
|
|
|
if(!this.popoverShown)
|
|
|
|
return;
|
|
|
|
$(".jitsipopover").remove();
|
|
|
|
this.createPopover();
|
|
|
|
};
|
|
|
|
|
2015-08-06 23:34:40 +00:00
|
|
|
JitsiPopover.enabled = true;
|
|
|
|
|
2014-10-16 15:11:26 +00:00
|
|
|
return JitsiPopover;
|
2015-01-07 14:54:03 +00:00
|
|
|
})();
|
|
|
|
|
2016-10-25 22:57:29 +00:00
|
|
|
module.exports = JitsiPopover;
|