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.

128 lines
3.0 KiB

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