forked from JeremyHeleine/Photo-Sphere-Viewer
-
-
Notifications
You must be signed in to change notification settings - Fork 629
/
Overlay.ts
151 lines (129 loc) · 3.74 KB
/
Overlay.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
import { CAPTURE_EVENTS_CLASS, KEY_CODES } from '../data/constants';
import { PSVError } from '../PSVError';
import type { Viewer } from '../Viewer';
import { HideOverlayEvent, KeypressEvent, ShowOverlayEvent } from '../events';
import { AbstractComponent } from './AbstractComponent';
/**
* Configuration for {@link Overlay.show}
*/
export type OverlayConfig = {
/**
* unique identifier to use with {@link Overlay.hide} and {@link Overlay.isVisible}
*/
id?: string;
/**
* SVG image/icon displayed above the text
*/
image?: string;
/**
* main message
*/
title: string;
/**
* secondary message
*/
text?: string;
/**
* if the user can hide the overlay by clicking
* @default true
*/
dissmisable?: boolean;
};
/**
* Overlay component
*/
export class Overlay extends AbstractComponent {
/**
* @internal
*/
protected override readonly state = {
visible: false,
contentId: null as string,
dissmisable: true,
};
private readonly image: HTMLElement;
private readonly title: HTMLElement;
private readonly text: HTMLElement;
/**
* @internal
*/
constructor(viewer: Viewer) {
super(viewer, {
className: `psv-overlay ${CAPTURE_EVENTS_CLASS}`,
});
this.image = document.createElement('div');
this.image.className = 'psv-overlay-image';
this.container.appendChild(this.image);
this.title = document.createElement('div');
this.title.className = 'psv-overlay-title';
this.container.appendChild(this.title);
this.text = document.createElement('div');
this.text.className = 'psv-overlay-text';
this.container.appendChild(this.text);
this.container.addEventListener('click', this);
this.viewer.addEventListener(KeypressEvent.type, this);
super.hide();
}
/**
* @internal
*/
override destroy() {
this.viewer.removeEventListener(KeypressEvent.type, this);
super.destroy();
}
/**
* @internal
*/
handleEvent(e: Event) {
if (e.type === 'click') {
if (this.isVisible() && this.state.dissmisable) {
this.hide();
e.stopPropagation();
}
} else if (e instanceof KeypressEvent) {
if (this.isVisible() && this.state.dissmisable && e.key === KEY_CODES.Escape) {
this.hide();
e.preventDefault();
}
}
}
/**
* Checks if the overlay is visible
*/
override isVisible(id?: string) {
return this.state.visible && (!id || !this.state.contentId || this.state.contentId === id);
}
/**
* @throws {@link PSVError} always
* @internal
*/
override toggle() {
throw new PSVError('Overlay cannot be toggled');
}
/**
* Displays an overlay on the viewer
*/
override show(config: string | OverlayConfig) {
if (typeof config === 'string') {
config = { title: config };
}
this.state.contentId = config.id || null;
this.state.dissmisable = config.dissmisable !== false;
this.image.innerHTML = config.image || '';
this.title.innerHTML = config.title || '';
this.text.innerHTML = config.text || '';
super.show();
this.viewer.dispatchEvent(new ShowOverlayEvent(config.id));
}
/**
* Hides the overlay
*/
override hide(id?: string) {
if (this.isVisible(id)) {
const contentId = this.state.contentId;
super.hide();
this.state.contentId = null;
this.viewer.dispatchEvent(new HideOverlayEvent(contentId));
}
}
}