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.

JitsiPopover.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* global $ */
  2. var JitsiPopover = (function () {
  3. /**
  4. * The default options
  5. */
  6. const defaultOptions = {
  7. skin: 'white',
  8. content: '',
  9. hasArrow: true,
  10. onBeforePosition: undefined
  11. };
  12. /**
  13. * Constructs new JitsiPopover and attaches it to the element
  14. * @param element jquery selector
  15. * @param options the options for the popover.
  16. * - {Function} onBeforePosition - function executed just before
  17. * positioning the popover. Useful for translation.
  18. * @constructor
  19. */
  20. function JitsiPopover(element, options)
  21. {
  22. this.options = Object.assign({}, defaultOptions, options);
  23. this.elementIsHovered = false;
  24. this.popoverIsHovered = false;
  25. this.popoverShown = false;
  26. element.data("jitsi_popover", this);
  27. this.element = element;
  28. this.template = this.getTemplate();
  29. var self = this;
  30. this.element.on("mouseenter", function () {
  31. self.elementIsHovered = true;
  32. self.show();
  33. }).on("mouseleave", function () {
  34. self.elementIsHovered = false;
  35. setTimeout(function () {
  36. self.hide();
  37. }, 10);
  38. });
  39. }
  40. /**
  41. * Returns template for popover
  42. */
  43. JitsiPopover.prototype.getTemplate = function () {
  44. let arrow = '';
  45. if (this.options.hasArrow) {
  46. arrow = '<div class="arrow"></div>';
  47. }
  48. return (
  49. `<div class="jitsipopover ${this.options.skin}">
  50. ${arrow}
  51. <div class="jitsipopover__content"></div>
  52. <div class="jitsipopover__menu-padding"></div>
  53. </div>`
  54. );
  55. };
  56. /**
  57. * Shows the popover
  58. */
  59. JitsiPopover.prototype.show = function () {
  60. if(!JitsiPopover.enabled)
  61. return;
  62. this.createPopover();
  63. this.popoverShown = true;
  64. };
  65. /**
  66. * Hides the popover if not hovered or popover is not shown.
  67. */
  68. JitsiPopover.prototype.hide = function () {
  69. if(!this.elementIsHovered && !this.popoverIsHovered &&
  70. this.popoverShown) {
  71. this.forceHide();
  72. }
  73. };
  74. /**
  75. * Hides the popover and clears the document elements added by popover.
  76. */
  77. JitsiPopover.prototype.forceHide = function () {
  78. $(".jitsipopover").remove();
  79. this.popoverShown = false;
  80. if(this.popoverIsHovered) { //the browser is not firing hover events
  81. //when the element was on hover if got removed.
  82. this.popoverIsHovered = false;
  83. this.onHoverPopover(this.popoverIsHovered);
  84. }
  85. };
  86. /**
  87. * Creates the popover html.
  88. */
  89. JitsiPopover.prototype.createPopover = function () {
  90. $("body").append(this.template);
  91. let popoverElem = $(".jitsipopover > .jitsipopover__content");
  92. popoverElem.html(this.options.content);
  93. if(typeof this.options.onBeforePosition === "function") {
  94. this.options.onBeforePosition($(".jitsipopover"));
  95. }
  96. var self = this;
  97. $(".jitsipopover").on("mouseenter", function () {
  98. self.popoverIsHovered = true;
  99. if(typeof self.onHoverPopover === "function") {
  100. self.onHoverPopover(self.popoverIsHovered);
  101. }
  102. }).on("mouseleave", function () {
  103. self.popoverIsHovered = false;
  104. self.hide();
  105. if(typeof self.onHoverPopover === "function") {
  106. self.onHoverPopover(self.popoverIsHovered);
  107. }
  108. });
  109. this.refreshPosition();
  110. };
  111. /**
  112. * Adds a hover listener to the popover.
  113. */
  114. JitsiPopover.prototype.addOnHoverPopover = function (listener) {
  115. this.onHoverPopover = listener;
  116. };
  117. /**
  118. * Refreshes the position of the popover.
  119. */
  120. JitsiPopover.prototype.refreshPosition = function () {
  121. $(".jitsipopover").position({
  122. my: "bottom",
  123. at: "top",
  124. collision: "fit",
  125. of: this.element,
  126. using: function (position, elements) {
  127. var calcLeft = elements.target.left - elements.element.left +
  128. elements.target.width/2;
  129. $(".jitsipopover").css(
  130. {top: position.top, left: position.left, display: "table"});
  131. $(".jitsipopover > .arrow").css({left: calcLeft});
  132. $(".jitsipopover > .jitsipopover__menu-padding").css(
  133. {left: calcLeft - 50});
  134. }
  135. });
  136. };
  137. /**
  138. * Updates the content of popover.
  139. * @param content new content
  140. */
  141. JitsiPopover.prototype.updateContent = function (content) {
  142. this.options.content = content;
  143. if(!this.popoverShown)
  144. return;
  145. $(".jitsipopover").remove();
  146. this.createPopover();
  147. };
  148. JitsiPopover.enabled = true;
  149. return JitsiPopover;
  150. })();
  151. module.exports = JitsiPopover;