UNPKG

3.18 kBJavaScriptView Raw
1import './tooltip.css';
2
3/**
4 * Popup is a class to create a popup window with some text
5 */
6class Popup {
7 /**
8 * @param {Element} container The container object.
9 * @param {string} overflowMethod How the popup should act to overflowing ('flip' or 'cap')
10 */
11 constructor(container, overflowMethod) {
12 this.container = container;
13 this.overflowMethod = overflowMethod || 'cap';
14
15 this.x = 0;
16 this.y = 0;
17 this.padding = 5;
18 this.hidden = false;
19
20 // create the frame
21 this.frame = document.createElement('div');
22 this.frame.className = 'vis-tooltip';
23 this.container.appendChild(this.frame);
24 }
25
26 /**
27 * @param {number} x Horizontal position of the popup window
28 * @param {number} y Vertical position of the popup window
29 */
30 setPosition(x, y) {
31 this.x = parseInt(x);
32 this.y = parseInt(y);
33 }
34
35 /**
36 * Set the content for the popup window. This can be HTML code or text.
37 * @param {string | Element} content
38 */
39 setText(content) {
40 if (content instanceof Element) {
41 this.frame.innerHTML = '';
42 this.frame.appendChild(content);
43 }
44 else {
45 this.frame.innerHTML = content; // string containing text or HTML
46 }
47 }
48
49 /**
50 * Show the popup window
51 * @param {boolean} [doShow] Show or hide the window
52 */
53 show(doShow) {
54 if (doShow === undefined) {
55 doShow = true;
56 }
57
58 if (doShow === true) {
59 var height = this.frame.clientHeight;
60 var width = this.frame.clientWidth;
61 var maxHeight = this.frame.parentNode.clientHeight;
62 var maxWidth = this.frame.parentNode.clientWidth;
63
64 var left = 0, top = 0;
65
66 if (this.overflowMethod == 'flip') {
67 var isLeft = false, isTop = true; // Where around the position it's located
68
69 if (this.y - height < this.padding) {
70 isTop = false;
71 }
72
73 if (this.x + width > maxWidth - this.padding) {
74 isLeft = true;
75 }
76
77 if (isLeft) {
78 left = this.x - width;
79 } else {
80 left = this.x;
81 }
82
83 if (isTop) {
84 top = this.y - height;
85 } else {
86 top = this.y;
87 }
88 } else {
89 top = (this.y - height);
90 if (top + height + this.padding > maxHeight) {
91 top = maxHeight - height - this.padding;
92 }
93 if (top < this.padding) {
94 top = this.padding;
95 }
96
97 left = this.x;
98 if (left + width + this.padding > maxWidth) {
99 left = maxWidth - width - this.padding;
100 }
101 if (left < this.padding) {
102 left = this.padding;
103 }
104 }
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 /**
117 * Hide the popup window
118 */
119 hide() {
120 this.hidden = true;
121 this.frame.style.left = "0";
122 this.frame.style.top = "0";
123 this.frame.style.visibility = "hidden";
124 }
125
126 /**
127 * Remove the popup window
128 */
129 destroy() {
130 this.frame.parentNode.removeChild(this.frame); // Remove element from DOM
131 }
132}
133
134export default Popup;