@shapediver/viewer.shared.services 3.3.4 → 3.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,310 +0,0 @@
1
- import { IDomEventListener } from './IDomEventListener';
2
- import { UuidGenerator } from '../uuid-generator/UuidGenerator';
3
-
4
- export class DomEventEngine {
5
- // #region Properties (15)
6
-
7
- private readonly _domEventListeners: {
8
- [key: string]: IDomEventListener
9
- } = {};
10
- private readonly _uuidGenerator: UuidGenerator = UuidGenerator.instance;
11
-
12
- private _allowListeners = {
13
- mousewheel: true,
14
- pointerdown: true,
15
- pointermove: true,
16
- pointerup: true,
17
- pointerout: true,
18
- keydown: true,
19
- keyup: true,
20
- contextmenu: true,
21
- };
22
- private _canvas: HTMLCanvasElement;
23
- private _currentPointerPosition: { x: number, y: number } = { x: 0, y: 0 };
24
- private _onContextMenu: (event: MouseEvent) => void;
25
- private _onKeyDown: (event: KeyboardEvent) => void;
26
- private _onKeyDownPointerPositionHelper: (event: PointerEvent) => void;
27
- private _onKeyUp: (event: KeyboardEvent) => void;
28
- private _onMouseWheel: (event: Event) => void;
29
- private _onPointerDown: (event: PointerEvent) => void;
30
- private _onPointerMove: (event: PointerEvent) => void;
31
- private _onPointerOut: (event: PointerEvent) => void;
32
- private _onPointerUp: (event: PointerEvent) => void;
33
- private _restrictedListenerTokens: string[] = [];
34
-
35
- // #endregion Properties (15)
36
-
37
- // #region Constructors (1)
38
-
39
- constructor(canvas: HTMLCanvasElement) {
40
- this._canvas = canvas;
41
- this._onMouseWheel = this.onMouseWheel.bind(this);
42
- this._onPointerDown = this.onPointerDown.bind(this);
43
- this._onPointerMove = this.onPointerMove.bind(this);
44
- this._onKeyDownPointerPositionHelper = this.onKeyDownPointerPositionHelper.bind(this);
45
- this._onPointerUp = this.onPointerUp.bind(this);
46
- this._onPointerOut = this.onPointerOut.bind(this);
47
- this._onKeyDown = this.onKeyDown.bind(this);
48
- this._onKeyUp = this.onKeyUp.bind(this);
49
- this._onContextMenu = this.onContextMenu.bind(this);
50
-
51
- this.addEventListeners();
52
- }
53
-
54
- // #endregion Constructors (1)
55
-
56
- // #region Public Methods (7)
57
-
58
- public addDomEventListener(listener: IDomEventListener): string {
59
- const id = this._uuidGenerator.create();
60
- this._domEventListeners[id] = listener;
61
- return id;
62
- }
63
-
64
- public addRestrictedListenerToken(token: string): void {
65
- if(this._restrictedListenerTokens.includes(token)) return;
66
- this._restrictedListenerTokens.push(token);
67
- }
68
-
69
- /**
70
- * Allow / disallow events.
71
- * This can be used to disable events for a specific viewer.
72
- *
73
- * Example use case: If you don't want to allow mouse wheel events for a specific viewer so that users can scroll past the viewer.
74
- *
75
- * Be aware that this might cause some issues with the the camera controls if the pointer events are disabled only partially.
76
- *
77
- * @param allowedListeners
78
- */
79
- public allowEventListeners(allowedListeners: {
80
- mousewheel?: boolean,
81
- pointerdown?: boolean,
82
- pointermove?: boolean,
83
- pointerup?: boolean,
84
- pointerout?: boolean,
85
- keydown?: boolean,
86
- keyup?: boolean,
87
- contextmenu?: boolean,
88
- }): void {
89
- if (typeof window === undefined) return;
90
-
91
- if (allowedListeners.mousewheel !== undefined && this._allowListeners.mousewheel !== allowedListeners.mousewheel) {
92
- if (allowedListeners.mousewheel) {
93
- this._canvas.addEventListener('mousewheel', this._onMouseWheel);
94
- this._canvas.addEventListener('MozMousePixelScroll', this._onMouseWheel); // firefox
95
- } else {
96
- this._canvas.removeEventListener('mousewheel', this._onMouseWheel);
97
- this._canvas.removeEventListener('MozMousePixelScroll', this._onMouseWheel); // firefox
98
- }
99
- this._allowListeners.mousewheel = allowedListeners.mousewheel;
100
- }
101
-
102
- if (allowedListeners.pointerdown !== undefined && this._allowListeners.pointerdown !== allowedListeners.pointerdown) {
103
- if (allowedListeners.pointerdown) {
104
- this._canvas.addEventListener('pointerdown', this._onPointerDown);
105
- } else {
106
- this._canvas.removeEventListener('pointerdown', this._onPointerDown);
107
- }
108
- this._allowListeners.pointerdown = allowedListeners.pointerdown;
109
- }
110
-
111
- if (allowedListeners.pointermove !== undefined && this._allowListeners.pointermove !== allowedListeners.pointermove) {
112
- if (allowedListeners.pointermove) {
113
- this._canvas.addEventListener('pointermove', this._onPointerMove);
114
- window.addEventListener('pointermove', this._onKeyDownPointerPositionHelper);
115
- } else {
116
- this._canvas.removeEventListener('pointermove', this._onPointerMove);
117
- window.removeEventListener('pointermove', this._onKeyDownPointerPositionHelper);
118
- }
119
- this._allowListeners.pointermove = allowedListeners.pointermove;
120
- }
121
-
122
- if (allowedListeners.pointerup !== undefined && this._allowListeners.pointerup !== allowedListeners.pointerup) {
123
- if (allowedListeners.pointerup) {
124
- this._canvas.addEventListener('pointerup', this._onPointerUp);
125
- } else {
126
- this._canvas.removeEventListener('pointerup', this._onPointerUp);
127
- }
128
- this._allowListeners.pointerup = allowedListeners.pointerup;
129
- }
130
-
131
- if (allowedListeners.pointerout !== undefined && this._allowListeners.pointerout !== allowedListeners.pointerout) {
132
- if (allowedListeners.pointerout) {
133
- this._canvas.addEventListener('pointerout', this._onPointerOut);
134
- } else {
135
- this._canvas.removeEventListener('pointerout', this._onPointerOut);
136
- }
137
- this._allowListeners.pointerout = allowedListeners.pointerout;
138
- }
139
-
140
- if (allowedListeners.keydown !== undefined && this._allowListeners.keydown !== allowedListeners.keydown) {
141
- if (allowedListeners.keydown) {
142
- window.addEventListener('keydown', this._onKeyDown);
143
- } else {
144
- window.removeEventListener('keydown', this._onKeyDown);
145
- }
146
- this._allowListeners.keydown = allowedListeners.keydown;
147
- }
148
-
149
- if (allowedListeners.keyup !== undefined && this._allowListeners.keyup !== allowedListeners.keyup) {
150
- if (allowedListeners.keyup) {
151
- window.addEventListener('keyup', this._onKeyUp);
152
- } else {
153
- window.removeEventListener('keyup', this._onKeyUp);
154
- }
155
- this._allowListeners.keyup = allowedListeners.keyup;
156
- }
157
-
158
- if (allowedListeners.contextmenu !== undefined && this._allowListeners.contextmenu !== allowedListeners.contextmenu) {
159
- if (allowedListeners.contextmenu) {
160
- this._canvas.addEventListener('contextmenu', this._onContextMenu);
161
- } else {
162
- this._canvas.removeEventListener('contextmenu', this._onContextMenu);
163
- }
164
- this._allowListeners.contextmenu = allowedListeners.contextmenu;
165
- }
166
- }
167
-
168
- public dispose() {
169
- this.removeEventListeners();
170
- }
171
-
172
- public removeAllDomEventListener(): void {
173
- for (const id in this._domEventListeners)
174
- delete this._domEventListeners[id];
175
-
176
- this._restrictedListenerTokens = [];
177
- }
178
-
179
- public removeDomEventListener(id: string): boolean {
180
- if (this._domEventListeners[id]) {
181
- delete this._domEventListeners[id];
182
- this._restrictedListenerTokens = this._restrictedListenerTokens.filter(t => t !== id);
183
- return true;
184
- }
185
- return false;
186
- }
187
-
188
- public removeRestrictedListenerToken(token: string): void {
189
- const index = this._restrictedListenerTokens.indexOf(token);
190
- if (index !== -1) this._restrictedListenerTokens.splice(index, 1);
191
- }
192
-
193
- // #endregion Public Methods (7)
194
-
195
- // #region Private Methods (11)
196
-
197
- private addEventListeners() {
198
- if (typeof window === undefined) return;
199
-
200
- this._canvas.addEventListener('mousewheel', this._onMouseWheel);
201
- this._canvas.addEventListener('MozMousePixelScroll', this._onMouseWheel); // firefox
202
-
203
- this._canvas.addEventListener('pointerdown', this._onPointerDown);
204
- this._canvas.addEventListener('pointermove', this._onPointerMove);
205
- this._canvas.addEventListener('pointerup', this._onPointerUp);
206
- this._canvas.addEventListener('pointerout', this._onPointerOut);
207
-
208
- window.addEventListener('keyup', this._onKeyUp);
209
- window.addEventListener('keydown', this._onKeyDown);
210
- window.addEventListener('pointermove', this._onKeyDownPointerPositionHelper);
211
-
212
- // just prevent right click menu
213
- this._canvas.addEventListener('contextmenu', this._onContextMenu);
214
- }
215
-
216
- private onContextMenu(event: MouseEvent): void {
217
- event.preventDefault();
218
- }
219
-
220
- private onKeyDown(event: KeyboardEvent): void {
221
- if (this._canvas === document.elementFromPoint(this._currentPointerPosition.x, this._currentPointerPosition.y)) {
222
- Object.entries(this._domEventListeners).forEach(([key, listener]) => {
223
- if (this._restrictedListenerTokens.length === 0 || this._restrictedListenerTokens.includes(key)) {
224
- listener.onKeyDown(event);
225
- }
226
- });
227
- }
228
- }
229
-
230
- private onKeyDownPointerPositionHelper(event: PointerEvent): void {
231
- this._currentPointerPosition = { x: event.pageX, y: event.pageY };
232
- }
233
-
234
- private onKeyUp(event: KeyboardEvent): void {
235
- if (this._canvas === document.elementFromPoint(this._currentPointerPosition.x, this._currentPointerPosition.y)) {
236
- Object.entries(this._domEventListeners).forEach(([key, listener]) => {
237
- if (this._restrictedListenerTokens.length === 0 || this._restrictedListenerTokens.includes(key)) {
238
- listener.onKeyUp(event);
239
- }
240
- });
241
- }
242
- }
243
-
244
- private onMouseWheel(event: Event): void {
245
- event.preventDefault();
246
- event.stopPropagation();
247
- Object.entries(this._domEventListeners).forEach(([key, listener]) => {
248
- if (this._restrictedListenerTokens.length === 0 || this._restrictedListenerTokens.includes(key)) {
249
- listener.onMouseWheel(<WheelEvent>event);
250
- }
251
- });
252
- }
253
-
254
- private onPointerDown(event: PointerEvent): void {
255
- event.preventDefault();
256
- Object.entries(this._domEventListeners).forEach(([key, listener]) => {
257
- if (this._restrictedListenerTokens.length === 0 || this._restrictedListenerTokens.includes(key)) {
258
- listener.onPointerDown(event);
259
- }
260
- });
261
- }
262
-
263
- private onPointerMove(event: PointerEvent): void {
264
- event.preventDefault();
265
- Object.entries(this._domEventListeners).forEach(([key, listener]) => {
266
- if (this._restrictedListenerTokens.length === 0 || this._restrictedListenerTokens.includes(key)) {
267
- listener.onPointerMove(event);
268
- }
269
- });
270
- }
271
-
272
- private onPointerOut(event: PointerEvent): void {
273
- event.preventDefault();
274
- Object.entries(this._domEventListeners).forEach(([key, listener]) => {
275
- if (this._restrictedListenerTokens.length === 0 || this._restrictedListenerTokens.includes(key)) {
276
- listener.onPointerOut(event);
277
- listener.onPointerEnd(event);
278
- }
279
- });
280
- }
281
-
282
- private onPointerUp(event: PointerEvent): void {
283
- event.preventDefault();
284
- Object.entries(this._domEventListeners).forEach(([key, listener]) => {
285
- if (this._restrictedListenerTokens.length === 0 || this._restrictedListenerTokens.includes(key)) {
286
- listener.onPointerUp(event);
287
- listener.onPointerEnd(event);
288
- }
289
- });
290
- }
291
-
292
- private removeEventListeners() {
293
- if (typeof window === undefined) return;
294
-
295
- this._canvas.removeEventListener('mousewheel', this._onMouseWheel);
296
- this._canvas.removeEventListener('MozMousePixelScroll', this._onMouseWheel); // firefox
297
-
298
- this._canvas.removeEventListener('pointerdown', this._onPointerDown);
299
- this._canvas.removeEventListener('pointermove', this._onPointerMove);
300
- this._canvas.removeEventListener('pointerup', this._onPointerUp);
301
- this._canvas.removeEventListener('pointerout', this._onPointerOut);
302
-
303
- window.removeEventListener('keydown', this._onKeyDown);
304
- window.removeEventListener('keyup', this._onKeyUp);
305
- window.removeEventListener('pointermove', this._onKeyDownPointerPositionHelper);
306
- this._canvas.removeEventListener('contextmenu', this._onContextMenu);
307
- }
308
-
309
- // #endregion Private Methods (11)
310
- }
@@ -1,14 +0,0 @@
1
- export interface IDomEventListener {
2
- // #region Public Methods (8)
3
-
4
- onKeyDown(event: KeyboardEvent): void;
5
- onKeyUp(event: KeyboardEvent): void;
6
- onMouseWheel(event: WheelEvent): void;
7
- onPointerDown(event: PointerEvent): void;
8
- onPointerEnd(event: PointerEvent): void;
9
- onPointerMove(event: PointerEvent): void;
10
- onPointerOut(event: PointerEvent): void;
11
- onPointerUp(event: PointerEvent): void;
12
-
13
- // #endregion Public Methods (8)
14
- }
@@ -1,131 +0,0 @@
1
- import { EVENTTYPE, MainEventTypes } from './EventTypes'
2
- import { IListener } from './interfaces/IListener'
3
- import { ICallback } from './interfaces/ICallback'
4
- import { IEvent } from './interfaces/IEvent'
5
- import { UuidGenerator } from '../uuid-generator/UuidGenerator'
6
- import { Logger } from '../logger/Logger'
7
-
8
- export class EventEngine {
9
- // #region Properties (4)
10
-
11
- private static _instance: EventEngine;
12
-
13
- private _eventListeners: {
14
- [key: string]: IListener[]
15
- };
16
-
17
- protected readonly _logger: Logger = Logger.instance;
18
- protected readonly _uuidGenerator: UuidGenerator = UuidGenerator.instance;
19
-
20
- // #endregion Properties (4)
21
-
22
- // #region Constructors (1)
23
-
24
- private constructor() {
25
- this._eventListeners = {};
26
- for (const type in EVENTTYPE) {
27
- const subEventType = EVENTTYPE[type as keyof typeof EVENTTYPE];
28
- this._eventListeners[type.toLowerCase()] = [];
29
- for (const subtype in subEventType) {
30
- this._eventListeners[subEventType[subtype as keyof typeof subEventType]] = [];
31
- }
32
- }
33
- }
34
-
35
- // #endregion Constructors (1)
36
-
37
- // #region Public Static Accessors (1)
38
-
39
- public static get instance() {
40
- return this._instance || (this._instance = new this());
41
- }
42
-
43
- // #endregion Public Static Accessors (1)
44
-
45
- // #region Public Methods (3)
46
-
47
- /**
48
- * Adds a listener that listenes to the provided type. If no valid type is specified, an error is thrown.
49
- *
50
- * @param type the type of the event
51
- * @param cb the callback that should be called
52
- * @returns an unique token to be able to remove the listener
53
- */
54
- public addListener(type: string | MainEventTypes, cb: ICallback): string {
55
- const typeString: string = this.convertTypeToString(type);
56
- if(!typeString) return '';
57
- const token = this._uuidGenerator.create();
58
- this._eventListeners[typeString]?.push({ token, cb });
59
- return token;
60
- }
61
-
62
- /**
63
- * Emits the event to all callbacks that listen to the type.
64
- *
65
- * @param type the type of the event
66
- * @param event the event to emit
67
- */
68
- public emitEvent(type: string | MainEventTypes, event: IEvent): void {
69
- const typeString: string = this.convertTypeToString(type);
70
-
71
- if (this._eventListeners[typeString] && this._eventListeners[typeString].length !== 0) {
72
- const cbs = this._eventListeners[typeString]!.map(el => el.cb)
73
- for (let i = 0; i < cbs.length; i++) {
74
- cbs[i](event);
75
- }
76
- }
77
-
78
- if(typeString.includes('.'))
79
- this.emitEvent(typeString.substr(0, typeString.indexOf('.')), event);
80
- }
81
-
82
- /**
83
- * Removes a listener with the specified token.
84
- *
85
- * @param token the token of the listener
86
- * @returns result of the targeted operation
87
- */
88
- public removeListener(token: string): boolean {
89
- for (const type in EVENTTYPE) {
90
- const subEventType = EVENTTYPE[type as keyof typeof EVENTTYPE];
91
- const typeLowerCase = type.toLowerCase();
92
- for (let i = 0; i < this._eventListeners[typeLowerCase]!.length; i++) {
93
- if (this._eventListeners[typeLowerCase]![i].token === token) {
94
- this._eventListeners[typeLowerCase]!.splice(i, 1);
95
- return true;
96
- }
97
- }
98
- for (const subtype in subEventType) {
99
- for (let i = 0; i < this._eventListeners[subEventType[subtype as keyof typeof subEventType]]!.length; i++) {
100
- if (this._eventListeners[subEventType[subtype as keyof typeof subEventType]]![i].token === token) {
101
- this._eventListeners[subEventType[subtype as keyof typeof subEventType]]!.splice(i, 1);
102
- return true;
103
- }
104
- }
105
- }
106
- }
107
- return false;
108
- }
109
-
110
- // #endregion Public Methods (3)
111
-
112
- // #region Private Methods (1)
113
-
114
- private convertTypeToString(type: string | MainEventTypes): string {
115
- let typeString: string = '';
116
- if(typeof type === 'string') typeString = type;
117
-
118
- for (const mainType in EVENTTYPE)
119
- if(type === EVENTTYPE[mainType as keyof typeof EVENTTYPE])
120
- typeString = mainType.toLowerCase();
121
-
122
- if(!typeString || !this._eventListeners[typeString]) {
123
- this._logger.warn('EventEngine.convertTypeToString: No valid type provided.');
124
- return '';
125
- }
126
-
127
- return typeString;
128
- }
129
-
130
- // #endregion Private Methods (1)
131
- }