forked from cornerstonejs/cornerstone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.js
More file actions
107 lines (85 loc) · 2.61 KB
/
Copy pathevents.js
File metadata and controls
107 lines (85 loc) · 2.61 KB
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
const EVENTS = {
NEW_IMAGE: 'cornerstonenewimage',
INVALIDATED: 'cornerstoneinvalidated',
PRE_RENDER: 'cornerstoneprerender',
IMAGE_CACHE_MAXIMUM_SIZE_CHANGED: 'cornerstoneimagecachemaximumsizechanged',
IMAGE_CACHE_PROMISE_REMOVED: 'cornerstoneimagecachepromiseremoved',
IMAGE_CACHE_FULL: 'cornerstoneimagecachefull',
IMAGE_CACHE_CHANGED: 'cornerstoneimagecachechanged',
WEBGL_TEXTURE_REMOVED: 'cornerstonewebgltextureremoved',
WEBGL_TEXTURE_CACHE_FULL: 'cornerstonewebgltexturecachefull',
IMAGE_LOADED: 'cornerstoneimageloaded',
IMAGE_LOAD_FAILED: 'cornerstoneimageloadfailed',
ELEMENT_RESIZED: 'cornerstoneelementresized',
IMAGE_RENDERED: 'cornerstoneimagerendered',
LAYER_ADDED: 'cornerstonelayeradded',
LAYER_REMOVED: 'cornerstonelayerremoved',
ACTIVE_LAYER_CHANGED: 'cornerstoneactivelayerchanged',
ELEMENT_DISABLED: 'cornerstoneelementdisabled'
};
export default EVENTS;
/**
* EventTarget - Provides the [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) interface
*
* @class
* @memberof Polyfills
*/
class EventTarget {
constructor () {
this.listeners = {};
this.namespaces = {};
}
addEventNamespaceListener (type, callback) {
if (type.indexOf('.') <= 0) {
return;
}
this.namespaces[type] = callback;
this.addEventListener(type.split('.')[0], callback);
}
removeEventNamespaceListener (type) {
if (type.indexOf('.') <= 0 || !this.namespaces[type]) {
return;
}
this.removeEventListener(type.split('.')[0], this.namespaces[type]);
delete this.namespaces[type];
}
addEventListener (type, callback) {
// Check if it is an event namespace
if (type.indexOf('.') > 0) {
this.addEventNamespaceListener(type, callback);
return;
}
if (!(type in this.listeners)) {
this.listeners[type] = [];
}
this.listeners[type].push(callback);
}
removeEventListener (type, callback) {
// Check if it is an event namespace
if (type.indexOf('.') > 0) {
this.removeEventNamespaceListener(type);
return;
}
if (!(type in this.listeners)) {
return;
}
const stack = this.listeners[type];
for (let i = 0, l = stack.length; i < l; i++) {
if (stack[i] === callback) {
stack.splice(i, 1);
return;
}
}
}
dispatchEvent (event) {
if (!(event.type in this.listeners)) {
return true;
}
const stack = this.listeners[event.type];
for (let i = 0, l = stack.length; i < l; i++) {
stack[i].call(this, event);
}
return !event.defaultPrevented;
}
}
export const events = new EventTarget();