forked from JeremyHeleine/Photo-Sphere-Viewer
-
-
Notifications
You must be signed in to change notification settings - Fork 629
/
Tooltip.ts
414 lines (366 loc) · 12.5 KB
/
Tooltip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { PSVError } from '../PSVError';
import type { Viewer } from '../Viewer';
import { HideTooltipEvent, ShowTooltipEvent } from '../events';
import { addClasses, cleanCssPosition, cssPositionIsOrdered, getStyleProperty } from '../utils';
import { AbstractComponent } from './AbstractComponent';
/**
* Object defining the tooltip position
*/
export type TooltipPosition = {
/**
* Position of the tip of the arrow of the tooltip, in pixels
*/
top: number;
/**
* Position of the tip of the arrow of the tooltip, in pixels
*/
left: number;
/**
* Tooltip position toward it's arrow tip.
* Accepted values are combinations of `top`, `center`, `bottom` and `left`, `center`, `right`.
*/
position?: string | [string, string];
/**
* @internal
*/
box?: { width: number; height: number };
};
/**
* Configuration for {@link Viewer.createTooltip}
*/
export type TooltipConfig = TooltipPosition & {
/**
* HTML content of the tooltip
*/
content: string;
/**
* Additional CSS class added to the tooltip
*/
className?: string;
/**
* CSS properties added to the tooltip
*/
style?: Record<string, string>;
/**
* Userdata associated to the tooltip
*/
data?: any;
};
type TooltipStyle = {
posClass: [string, string];
width: number;
height: number;
top: number;
left: number;
arrowTop: number;
arrowLeft: number;
};
const enum TooltipState {
NONE,
SHOWING,
HIDING,
READY,
}
/**
* Tooltip component
* @description Never instanciate tooltips directly use {@link Viewer#createTooltip} instead
*/
export class Tooltip extends AbstractComponent {
/**
* @internal
*/
protected override readonly state = {
visible: true,
arrow: 0,
border: 0,
state: TooltipState.NONE,
width: 0,
height: 0,
pos: '',
config: null as TooltipPosition,
data: null as any,
};
private readonly content: HTMLElement;
private readonly arrow: HTMLElement;
/**
* @internal
*/
constructor(viewer: Viewer, config: TooltipConfig) {
super(viewer, {
className: 'psv-tooltip',
});
this.content = document.createElement('div');
this.content.className = 'psv-tooltip-content';
this.container.appendChild(this.content);
this.arrow = document.createElement('div');
this.arrow.className = 'psv-tooltip-arrow';
this.container.appendChild(this.arrow);
this.container.addEventListener('transitionend', this);
// allows to interact with static tooltips
this.container.addEventListener('touchdown', (e) => e.stopPropagation());
this.container.addEventListener('mousedown', (e) => e.stopPropagation());
this.container.style.top = '-1000px';
this.container.style.left = '-1000px';
this.show(config);
}
/**
* @internal
*/
handleEvent(e: Event) {
if (e.type === 'transitionend') {
this.__onTransitionEnd(e as TransitionEvent);
}
}
/**
* @internal
*/
override destroy() {
delete this.state.data;
super.destroy();
}
/**
* @throws {@link PSVError} always
* @internal
*/
override toggle() {
throw new PSVError('Tooltip cannot be toggled');
}
/**
* Displays the tooltip on the viewer
* @internal
*/
override show(config: TooltipConfig) {
if (this.state.state !== TooltipState.NONE) {
throw new PSVError('Initialized tooltip cannot be re-initialized');
}
if (config.className) {
addClasses(this.container, config.className);
}
if (config.style) {
Object.assign(this.container.style, config.style);
}
this.state.state = TooltipState.READY;
this.update(config.content, config);
this.state.data = config.data;
this.state.state = TooltipState.SHOWING;
this.viewer.dispatchEvent(new ShowTooltipEvent(this, this.state.data));
this.__waitImages();
}
/**
* Updates the content of the tooltip, optionally with a new position
* @throws {@link PSVError} if the configuration is invalid
*/
update(content: string, config?: TooltipPosition) {
this.content.innerHTML = content;
const rect = this.container.getBoundingClientRect();
this.state.width = rect.right - rect.left;
this.state.height = rect.bottom - rect.top;
this.state.arrow = parseInt(getStyleProperty(this.arrow, 'border-top-width'), 10);
this.state.border = parseInt(getStyleProperty(this.container, 'border-top-left-radius'), 10);
this.move(config ?? this.state.config);
this.__waitImages();
}
/**
* Moves the tooltip to a new position
* @throws {@link PSVError} if the configuration is invalid
*/
move(config: TooltipPosition) {
if (this.state.state !== TooltipState.SHOWING && this.state.state !== TooltipState.READY) {
throw new PSVError('Uninitialized tooltip cannot be moved');
}
config.box = config.box ?? this.state.config?.box ?? { width: 0, height: 0 };
this.state.config = config;
const t = this.container;
const a = this.arrow;
// compute size
const style: TooltipStyle = {
posClass: cleanCssPosition(config.position, { allowCenter: false, cssOrder: false }) || ['top', 'center'],
width: this.state.width,
height: this.state.height,
top: 0,
left: 0,
arrowTop: 0,
arrowLeft: 0,
};
// set initial position
this.__computeTooltipPosition(style, config);
// correct position if overflow
let swapY = null;
let swapX = null;
if (style.top < 0) {
swapY = 'bottom';
} else if (style.top + style.height > this.viewer.state.size.height) {
swapY = 'top';
}
if (style.left < 0) {
swapX = 'right';
} else if (style.left + style.width > this.viewer.state.size.width) {
swapX = 'left';
}
if (swapX || swapY) {
const ordered = cssPositionIsOrdered(style.posClass);
if (swapY) {
style.posClass[ordered ? 0 : 1] = swapY;
}
if (swapX) {
style.posClass[ordered ? 1 : 0] = swapX;
}
this.__computeTooltipPosition(style, config);
}
// apply position
t.style.top = style.top + 'px';
t.style.left = style.left + 'px';
a.style.top = style.arrowTop + 'px';
a.style.left = style.arrowLeft + 'px';
const newPos = style.posClass.join('-');
if (newPos !== this.state.pos) {
t.classList.remove(`psv-tooltip--${this.state.pos}`);
this.state.pos = newPos;
t.classList.add(`psv-tooltip--${this.state.pos}`);
}
}
/**
* Hides the tooltip
*/
override hide() {
this.container.classList.remove('psv-tooltip--visible');
this.state.state = TooltipState.HIDING;
this.viewer.dispatchEvent(new HideTooltipEvent(this.state.data));
}
/**
* Finalize transition
*/
private __onTransitionEnd(e: TransitionEvent) {
if (e.propertyName === 'transform') {
switch (this.state.state) {
case TooltipState.SHOWING:
this.container.classList.add('psv-tooltip--visible');
this.state.state = TooltipState.READY;
break;
case TooltipState.HIDING:
this.state.state = TooltipState.NONE;
this.destroy();
break;
default:
// nothing
}
}
}
/**
* Computes the position of the tooltip and its arrow
*/
private __computeTooltipPosition(style: TooltipStyle, config: TooltipPosition) {
const arrow = this.state.arrow;
const top = config.top;
const height = style.height;
const left = config.left;
const width = style.width;
const offsetSide = arrow + this.state.border;
const offsetX = config.box.width / 2 + arrow * 2;
const offsetY = config.box.height / 2 + arrow * 2;
switch (style.posClass.join('-')) {
case 'top-left':
style.top = top - offsetY - height;
style.left = left + offsetSide - width;
style.arrowTop = height;
style.arrowLeft = width - offsetSide - arrow;
break;
case 'top-center':
style.top = top - offsetY - height;
style.left = left - width / 2;
style.arrowTop = height;
style.arrowLeft = width / 2 - arrow;
break;
case 'top-right':
style.top = top - offsetY - height;
style.left = left - offsetSide;
style.arrowTop = height;
style.arrowLeft = arrow;
break;
case 'bottom-left':
style.top = top + offsetY;
style.left = left + offsetSide - width;
style.arrowTop = -arrow * 2;
style.arrowLeft = width - offsetSide - arrow;
break;
case 'bottom-center':
style.top = top + offsetY;
style.left = left - width / 2;
style.arrowTop = -arrow * 2;
style.arrowLeft = width / 2 - arrow;
break;
case 'bottom-right':
style.top = top + offsetY;
style.left = left - offsetSide;
style.arrowTop = -arrow * 2;
style.arrowLeft = arrow;
break;
case 'left-top':
style.top = top + offsetSide - height;
style.left = left - offsetX - width;
style.arrowTop = height - offsetSide - arrow;
style.arrowLeft = width;
break;
case 'center-left':
style.top = top - height / 2;
style.left = left - offsetX - width;
style.arrowTop = height / 2 - arrow;
style.arrowLeft = width;
break;
case 'left-bottom':
style.top = top - offsetSide;
style.left = left - offsetX - width;
style.arrowTop = arrow;
style.arrowLeft = width;
break;
case 'right-top':
style.top = top + offsetSide - height;
style.left = left + offsetX;
style.arrowTop = height - offsetSide - arrow;
style.arrowLeft = -arrow * 2;
break;
case 'center-right':
style.top = top - height / 2;
style.left = left + offsetX;
style.arrowTop = height / 2 - arrow;
style.arrowLeft = -arrow * 2;
break;
case 'right-bottom':
style.top = top - offsetSide;
style.left = left + offsetX;
style.arrowTop = arrow;
style.arrowLeft = -arrow * 2;
break;
// no default
}
}
/**
* If the tooltip contains images, recompute its size once they are loaded
*/
private __waitImages() {
const images = this.content.querySelectorAll('img') as NodeListOf<HTMLImageElement>;
if (images.length > 0) {
const promises: Array<Promise<any>> = [];
images.forEach((image) => {
if (!image.complete) {
promises.push(
new Promise((resolve) => {
image.onload = resolve;
image.onerror = resolve;
})
);
}
});
if (promises.length) {
Promise.all(promises).then(() => {
if (this.state.state === TooltipState.SHOWING || this.state.state === TooltipState.READY) {
const rect = this.container.getBoundingClientRect();
this.state.width = rect.right - rect.left;
this.state.height = rect.bottom - rect.top;
this.move(this.state.config);
}
});
}
}
}
}