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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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></div>`
  53. );
  54. };
  55. /**
  56. * Shows the popover
  57. */
  58. JitsiPopover.prototype.show = function () {
  59. if(!JitsiPopover.enabled)
  60. return;
  61. this.createPopover();
  62. this.popoverShown = true;
  63. };
  64. /**
  65. * Hides the popover
  66. */
  67. JitsiPopover.prototype.hide = function () {
  68. if(!this.elementIsHovered && !this.popoverIsHovered &&
  69. this.popoverShown) {
  70. this.forceHide();
  71. }
  72. };
  73. /**
  74. * Hides the popover.
  75. */
  76. JitsiPopover.prototype.forceHide = function () {
  77. $(".jitsipopover").remove();
  78. this.popoverShown = false;
  79. if(this.popoverIsHovered) { //the browser is not firing hover events
  80. //when the element was on hover if got removed.
  81. this.popoverIsHovered = false;
  82. this.onHoverPopover(this.popoverIsHovered);
  83. }
  84. };
  85. /**
  86. * Creates the popover html.
  87. */
  88. JitsiPopover.prototype.createPopover = function () {
  89. $("body").append(this.template);
  90. let popoverElem = $(".jitsipopover > .jitsipopover__content");
  91. popoverElem.html(this.options.content);
  92. if(typeof this.options.onBeforePosition === "function") {
  93. this.options.onBeforePosition($(".jitsipopover"));
  94. }
  95. var self = this;
  96. $(".jitsipopover").on("mouseenter", function () {
  97. self.popoverIsHovered = true;
  98. if(typeof self.onHoverPopover === "function") {
  99. self.onHoverPopover(self.popoverIsHovered);
  100. }
  101. }).on("mouseleave", function () {
  102. self.popoverIsHovered = false;
  103. self.hide();
  104. if(typeof self.onHoverPopover === "function") {
  105. self.onHoverPopover(self.popoverIsHovered);
  106. }
  107. });
  108. this.refreshPosition();
  109. };
  110. /**
  111. * Adds a hover listener to the popover.
  112. */
  113. JitsiPopover.prototype.addOnHoverPopover = function (listener) {
  114. this.onHoverPopover = listener;
  115. };
  116. /**
  117. * Refreshes the position of the popover.
  118. */
  119. JitsiPopover.prototype.refreshPosition = function () {
  120. $(".jitsipopover").position({
  121. my: "bottom",
  122. at: "top",
  123. collision: "fit",
  124. of: this.element,
  125. using: function (position, elements) {
  126. var calcLeft = elements.target.left - elements.element.left +
  127. elements.target.width/2;
  128. $(".jitsipopover").css(
  129. {top: position.top, left: position.left, display: "table"});
  130. $(".jitsipopover > .arrow").css({left: calcLeft});
  131. $(".jitsipopover > .jitsipopover__menu-padding").css(
  132. {left: calcLeft - 50});
  133. }
  134. });
  135. };
  136. /**
  137. * Updates the content of popover.
  138. * @param content new content
  139. */
  140. JitsiPopover.prototype.updateContent = function (content) {
  141. this.options.content = content;
  142. if(!this.popoverShown)
  143. return;
  144. $(".jitsipopover").remove();
  145. this.createPopover();
  146. };
  147. JitsiPopover.enabled = true;
  148. return JitsiPopover;
  149. })();
  150. module.exports = JitsiPopover;