您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiPopover.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. };
  80. /**
  81. * Creates the popover html.
  82. */
  83. JitsiPopover.prototype.createPopover = function () {
  84. $("body").append(this.template);
  85. let popoverElem = $(".jitsipopover > .jitsipopover__content");
  86. popoverElem.html(this.options.content);
  87. if(typeof this.options.onBeforePosition === "function") {
  88. this.options.onBeforePosition($(".jitsipopover"));
  89. }
  90. var self = this;
  91. $(".jitsipopover").on("mouseenter", function () {
  92. self.popoverIsHovered = true;
  93. if(typeof self.onHoverPopover === "function") {
  94. self.onHoverPopover(self.popoverIsHovered);
  95. }
  96. }).on("mouseleave", function () {
  97. self.popoverIsHovered = false;
  98. self.hide();
  99. if(typeof self.onHoverPopover === "function") {
  100. self.onHoverPopover(self.popoverIsHovered);
  101. }
  102. });
  103. this.refreshPosition();
  104. };
  105. /**
  106. * Adds a hover listener to the popover.
  107. */
  108. JitsiPopover.prototype.addOnHoverPopover = function (listener) {
  109. this.onHoverPopover = listener;
  110. };
  111. /**
  112. * Refreshes the position of the popover.
  113. */
  114. JitsiPopover.prototype.refreshPosition = function () {
  115. $(".jitsipopover").position({
  116. my: "bottom",
  117. at: "top",
  118. collision: "fit",
  119. of: this.element,
  120. using: function (position, elements) {
  121. var calcLeft = elements.target.left - elements.element.left +
  122. elements.target.width/2;
  123. $(".jitsipopover").css(
  124. {top: position.top, left: position.left, display: "table"});
  125. $(".jitsipopover > .arrow").css({left: calcLeft});
  126. $(".jitsipopover > .jitsipopover__menu-padding").css(
  127. {left: calcLeft - 50});
  128. }
  129. });
  130. };
  131. /**
  132. * Updates the content of popover.
  133. * @param content new content
  134. */
  135. JitsiPopover.prototype.updateContent = function (content) {
  136. this.options.content = content;
  137. if(!this.popoverShown)
  138. return;
  139. $(".jitsipopover").remove();
  140. this.createPopover();
  141. };
  142. JitsiPopover.enabled = true;
  143. return JitsiPopover;
  144. })();
  145. module.exports = JitsiPopover;