vis.js is a dynamic, browser-based visualization library
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.

132 lines
3.1 KiB

  1. /**
  2. * Popup is a class to create a popup window with some text
  3. */
  4. class Popup {
  5. /**
  6. * @param {Element} container The container object.
  7. * @param {string} overflowMethod How the popup should act to overflowing ('flip' or 'cap')
  8. */
  9. constructor(container, overflowMethod) {
  10. this.container = container;
  11. this.overflowMethod = overflowMethod || 'cap';
  12. this.x = 0;
  13. this.y = 0;
  14. this.padding = 5;
  15. this.hidden = false;
  16. // create the frame
  17. this.frame = document.createElement('div');
  18. this.frame.className = 'vis-tooltip';
  19. this.container.appendChild(this.frame);
  20. }
  21. /**
  22. * @param {number} x Horizontal position of the popup window
  23. * @param {number} y Vertical position of the popup window
  24. */
  25. setPosition(x, y) {
  26. this.x = parseInt(x);
  27. this.y = parseInt(y);
  28. }
  29. /**
  30. * Set the content for the popup window. This can be HTML code or text.
  31. * @param {string | Element} content
  32. */
  33. setText(content) {
  34. if (content instanceof Element) {
  35. this.frame.innerHTML = '';
  36. this.frame.appendChild(content);
  37. }
  38. else {
  39. this.frame.innerHTML = content; // string containing text or HTML
  40. }
  41. }
  42. /**
  43. * Show the popup window
  44. * @param {boolean} [doShow] Show or hide the window
  45. */
  46. show(doShow) {
  47. if (doShow === undefined) {
  48. doShow = true;
  49. }
  50. if (doShow === true) {
  51. var height = this.frame.clientHeight;
  52. var width = this.frame.clientWidth;
  53. var maxHeight = this.frame.parentNode.clientHeight;
  54. var maxWidth = this.frame.parentNode.clientWidth;
  55. var left = 0, top = 0;
  56. if (this.overflowMethod == 'flip') {
  57. var isLeft = false, isTop = true; // Where around the position it's located
  58. if (this.y - height < this.padding) {
  59. isTop = false;
  60. }
  61. if (this.x + width > maxWidth - this.padding) {
  62. isLeft = true;
  63. }
  64. if (isLeft) {
  65. left = this.x - width;
  66. } else {
  67. left = this.x;
  68. }
  69. if (isTop) {
  70. top = this.y - height;
  71. } else {
  72. top = this.y;
  73. }
  74. } else {
  75. top = (this.y - height);
  76. if (top + height + this.padding > maxHeight) {
  77. top = maxHeight - height - this.padding;
  78. }
  79. if (top < this.padding) {
  80. top = this.padding;
  81. }
  82. left = this.x;
  83. if (left + width + this.padding > maxWidth) {
  84. left = maxWidth - width - this.padding;
  85. }
  86. if (left < this.padding) {
  87. left = this.padding;
  88. }
  89. }
  90. this.frame.style.left = left + "px";
  91. this.frame.style.top = top + "px";
  92. this.frame.style.visibility = "visible";
  93. this.hidden = false;
  94. }
  95. else {
  96. this.hide();
  97. }
  98. }
  99. /**
  100. * Hide the popup window
  101. */
  102. hide() {
  103. this.hidden = true;
  104. this.frame.style.left = "0";
  105. this.frame.style.top = "0";
  106. this.frame.style.visibility = "hidden";
  107. }
  108. /**
  109. * Remove the popup window
  110. */
  111. destroy() {
  112. this.frame.parentNode.removeChild(this.frame); // Remove element from DOM
  113. }
  114. }
  115. export default Popup;