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