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.

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