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.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. if (x !== undefined && y !== undefined ) {
  42. this.setPosition(x, y);
  43. }
  44. if (text !== undefined) {
  45. this.setText(text);
  46. }
  47. // create the frame
  48. this.frame = document.createElement('div');
  49. this.frame.className = 'network-tooltip';
  50. this.frame.style.color = style.fontColor;
  51. this.frame.style.backgroundColor = style.color.background;
  52. this.frame.style.borderColor = style.color.border;
  53. this.frame.style.fontSize = style.fontSize + 'px';
  54. this.frame.style.fontFamily = style.fontFace;
  55. this.container.appendChild(this.frame);
  56. }
  57. /**
  58. * @param {number} x Horizontal position of the popup window
  59. * @param {number} y Vertical position of the popup window
  60. */
  61. Popup.prototype.setPosition = function(x, y) {
  62. this.x = parseInt(x);
  63. this.y = parseInt(y);
  64. };
  65. /**
  66. * Set the content for the popup window. This can be HTML code or text.
  67. * @param {string | Element} content
  68. */
  69. Popup.prototype.setText = function(content) {
  70. if (content instanceof Element) {
  71. this.frame.innerHTML = '';
  72. this.frame.appendChild(content);
  73. }
  74. else {
  75. this.frame.innerHTML = content; // string containing text or HTML
  76. }
  77. };
  78. /**
  79. * Show the popup window
  80. * @param {boolean} show Optional. Show or hide the window
  81. */
  82. Popup.prototype.show = function (show) {
  83. if (show === undefined) {
  84. show = true;
  85. }
  86. if (show) {
  87. var height = this.frame.clientHeight;
  88. var width = this.frame.clientWidth;
  89. var maxHeight = this.frame.parentNode.clientHeight;
  90. var maxWidth = this.frame.parentNode.clientWidth;
  91. var top = (this.y - height);
  92. if (top + height + this.padding > maxHeight) {
  93. top = maxHeight - height - this.padding;
  94. }
  95. if (top < this.padding) {
  96. top = this.padding;
  97. }
  98. var left = this.x;
  99. if (left + width + this.padding > maxWidth) {
  100. left = maxWidth - width - this.padding;
  101. }
  102. if (left < this.padding) {
  103. left = this.padding;
  104. }
  105. this.frame.style.left = left + "px";
  106. this.frame.style.top = top + "px";
  107. this.frame.style.visibility = "visible";
  108. }
  109. else {
  110. this.hide();
  111. }
  112. };
  113. /**
  114. * Hide the popup window
  115. */
  116. Popup.prototype.hide = function () {
  117. this.frame.style.visibility = "hidden";
  118. };
  119. module.exports = Popup;