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.

140 lines
3.5 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. var styleAttr = this.frame.style;
  50. styleAttr.position = "absolute";
  51. styleAttr.visibility = "hidden";
  52. styleAttr.border = "1px solid " + style.color.border;
  53. styleAttr.color = style.fontColor;
  54. styleAttr.fontSize = style.fontSize + "px";
  55. styleAttr.fontFamily = style.fontFace;
  56. styleAttr.padding = this.padding + "px";
  57. styleAttr.backgroundColor = style.color.background;
  58. styleAttr.borderRadius = "3px";
  59. styleAttr.MozBorderRadius = "3px";
  60. styleAttr.WebkitBorderRadius = "3px";
  61. styleAttr.boxShadow = "3px 3px 10px rgba(128, 128, 128, 0.5)";
  62. styleAttr.whiteSpace = "nowrap";
  63. this.container.appendChild(this.frame);
  64. }
  65. /**
  66. * @param {number} x Horizontal position of the popup window
  67. * @param {number} y Vertical position of the popup window
  68. */
  69. Popup.prototype.setPosition = function(x, y) {
  70. this.x = parseInt(x);
  71. this.y = parseInt(y);
  72. };
  73. /**
  74. * Set the content for the popup window. This can be HTML code or text.
  75. * @param {string | Element} content
  76. */
  77. Popup.prototype.setText = function(content) {
  78. if (content instanceof Element) {
  79. this.frame.innerHTML = '';
  80. this.frame.appendChild(content);
  81. }
  82. else {
  83. this.frame.innerHTML = content; // string containing text or HTML
  84. }
  85. };
  86. /**
  87. * Show the popup window
  88. * @param {boolean} show Optional. Show or hide the window
  89. */
  90. Popup.prototype.show = function (show) {
  91. if (show === undefined) {
  92. show = true;
  93. }
  94. if (show) {
  95. var height = this.frame.clientHeight;
  96. var width = this.frame.clientWidth;
  97. var maxHeight = this.frame.parentNode.clientHeight;
  98. var maxWidth = this.frame.parentNode.clientWidth;
  99. var top = (this.y - height);
  100. if (top + height + this.padding > maxHeight) {
  101. top = maxHeight - height - this.padding;
  102. }
  103. if (top < this.padding) {
  104. top = this.padding;
  105. }
  106. var left = this.x;
  107. if (left + width + this.padding > maxWidth) {
  108. left = maxWidth - width - this.padding;
  109. }
  110. if (left < this.padding) {
  111. left = this.padding;
  112. }
  113. this.frame.style.left = left + "px";
  114. this.frame.style.top = top + "px";
  115. this.frame.style.visibility = "visible";
  116. }
  117. else {
  118. this.hide();
  119. }
  120. };
  121. /**
  122. * Hide the popup window
  123. */
  124. Popup.prototype.hide = function () {
  125. this.frame.style.visibility = "hidden";
  126. };
  127. module.exports = Popup;