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.

105 lines
2.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. */
  8. function Popup(container, x, y, text) {
  9. if (container) {
  10. this.container = container;
  11. }
  12. else {
  13. this.container = document.body;
  14. }
  15. this.x = 0;
  16. this.y = 0;
  17. this.padding = 5;
  18. if (x !== undefined && y !== undefined ) {
  19. this.setPosition(x, y);
  20. }
  21. if (text !== undefined) {
  22. this.setText(text);
  23. }
  24. // create the frame
  25. this.frame = document.createElement("div");
  26. var style = this.frame.style;
  27. style.position = "absolute";
  28. style.visibility = "hidden";
  29. style.border = "1px solid #666";
  30. style.color = "black";
  31. style.padding = this.padding + "px";
  32. style.backgroundColor = "#FFFFC6";
  33. style.borderRadius = "3px";
  34. style.MozBorderRadius = "3px";
  35. style.WebkitBorderRadius = "3px";
  36. style.boxShadow = "3px 3px 10px rgba(128, 128, 128, 0.5)";
  37. style.whiteSpace = "nowrap";
  38. this.container.appendChild(this.frame);
  39. }
  40. /**
  41. * @param {number} x Horizontal position of the popup window
  42. * @param {number} y Vertical position of the popup window
  43. */
  44. Popup.prototype.setPosition = function(x, y) {
  45. this.x = parseInt(x);
  46. this.y = parseInt(y);
  47. };
  48. /**
  49. * Set the text for the popup window. This can be HTML code
  50. * @param {string} text
  51. */
  52. Popup.prototype.setText = function(text) {
  53. this.frame.innerHTML = text;
  54. };
  55. /**
  56. * Show the popup window
  57. * @param {boolean} show Optional. Show or hide the window
  58. */
  59. Popup.prototype.show = function (show) {
  60. if (show === undefined) {
  61. show = true;
  62. }
  63. if (show) {
  64. var height = this.frame.clientHeight;
  65. var width = this.frame.clientWidth;
  66. var maxHeight = this.frame.parentNode.clientHeight;
  67. var maxWidth = this.frame.parentNode.clientWidth;
  68. var top = (this.y - height);
  69. if (top + height + this.padding > maxHeight) {
  70. top = maxHeight - height - this.padding;
  71. }
  72. if (top < this.padding) {
  73. top = this.padding;
  74. }
  75. var left = this.x;
  76. if (left + width + this.padding > maxWidth) {
  77. left = maxWidth - width - this.padding;
  78. }
  79. if (left < this.padding) {
  80. left = this.padding;
  81. }
  82. this.frame.style.left = left + "px";
  83. this.frame.style.top = top + "px";
  84. this.frame.style.visibility = "visible";
  85. }
  86. else {
  87. this.hide();
  88. }
  89. };
  90. /**
  91. * Hide the popup window
  92. */
  93. Popup.prototype.hide = function () {
  94. this.frame.style.visibility = "hidden";
  95. };