1 | import './tooltip.css';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | class Popup {
|
7 | |
8 |
|
9 |
|
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 |
|
21 | this.frame = document.createElement('div');
|
22 | this.frame.className = 'vis-tooltip';
|
23 | this.container.appendChild(this.frame);
|
24 | }
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 | setPosition(x, y) {
|
31 | this.x = parseInt(x);
|
32 | this.y = parseInt(y);
|
33 | }
|
34 |
|
35 | |
36 |
|
37 |
|
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;
|
46 | }
|
47 | }
|
48 |
|
49 | |
50 |
|
51 |
|
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;
|
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 |
|
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 |
|
128 |
|
129 | destroy() {
|
130 | this.frame.parentNode.removeChild(this.frame);
|
131 | }
|
132 | }
|
133 |
|
134 | export default Popup;
|