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.

137 lines
3.4 KiB

  1. /**
  2. * Popup is a class to create a popup window with some text
  3. * @param {Element} container The container object.
  4. * @param {Number} [x]
  5. * @param {Number} [y]
  6. * @param {String} [text]
  7. * @param {Object} [style] An object containing borderColor,
  8. * backgroundColor, etc.
  9. */
  10. class Popup {
  11. constructor(container, x, y, text, style) {
  12. if (container) {
  13. this.container = container;
  14. }
  15. else {
  16. this.container = document.body;
  17. }
  18. // x, y and text are optional, see if a style object was passed in their place
  19. if (style === undefined) {
  20. if (typeof x === "object") {
  21. style = x;
  22. x = undefined;
  23. } else if (typeof text === "object") {
  24. style = text;
  25. text = undefined;
  26. } else {
  27. // for backwards compatibility, in case clients other than Network are creating Popup directly
  28. style = {
  29. fontColor: 'black',
  30. fontSize: 14, // px
  31. fontFace: 'verdana',
  32. color: {
  33. border: '#666',
  34. background: '#FFFFC6'
  35. }
  36. }
  37. }
  38. }
  39. this.x = 0;
  40. this.y = 0;
  41. this.padding = 5;
  42. this.hidden = false;
  43. if (x !== undefined && y !== undefined) {
  44. this.setPosition(x, y);
  45. }
  46. if (text !== undefined) {
  47. this.setText(text);
  48. }
  49. // create the frame
  50. this.frame = document.createElement('div');
  51. this.frame.className = 'network-tooltip';
  52. this.frame.style.color = style.fontColor;
  53. this.frame.style.backgroundColor = style.color.background;
  54. this.frame.style.borderColor = style.color.border;
  55. this.frame.style.fontSize = style.fontSize + 'px';
  56. this.frame.style.fontFamily = style.fontFace;
  57. this.container.appendChild(this.frame);
  58. }
  59. /**
  60. * @param {number} x Horizontal position of the popup window
  61. * @param {number} y Vertical position of the popup window
  62. */
  63. setPosition(x, y) {
  64. this.x = parseInt(x);
  65. this.y = parseInt(y);
  66. }
  67. /**
  68. * Set the content for the popup window. This can be HTML code or text.
  69. * @param {string | Element} content
  70. */
  71. setText(content) {
  72. if (content instanceof Element) {
  73. this.frame.innerHTML = '';
  74. this.frame.appendChild(content);
  75. }
  76. else {
  77. this.frame.innerHTML = content; // string containing text or HTML
  78. }
  79. }
  80. /**
  81. * Show the popup window
  82. * @param {boolean} show Optional. Show or hide the window
  83. */
  84. show(show) {
  85. if (show === undefined) {
  86. show = true;
  87. }
  88. if (show === true) {
  89. var height = this.frame.clientHeight;
  90. var width = this.frame.clientWidth;
  91. var maxHeight = this.frame.parentNode.clientHeight;
  92. var maxWidth = this.frame.parentNode.clientWidth;
  93. var top = (this.y - height);
  94. if (top + height + this.padding > maxHeight) {
  95. top = maxHeight - height - this.padding;
  96. }
  97. if (top < this.padding) {
  98. top = this.padding;
  99. }
  100. var left = this.x;
  101. if (left + width + this.padding > maxWidth) {
  102. left = maxWidth - width - this.padding;
  103. }
  104. if (left < this.padding) {
  105. left = this.padding;
  106. }
  107. this.frame.style.left = left + "px";
  108. this.frame.style.top = top + "px";
  109. this.frame.style.visibility = "visible";
  110. this.hidden = false;
  111. }
  112. else {
  113. this.hide();
  114. }
  115. }
  116. /**
  117. * Hide the popup window
  118. */
  119. hide() {
  120. this.hidden = true;
  121. this.frame.style.visibility = "hidden";
  122. }
  123. }
  124. export default Popup;