forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.js
More file actions
345 lines (301 loc) · 10.6 KB
/
Event.js
File metadata and controls
345 lines (301 loc) · 10.6 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
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
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {SuperMap} from '../SuperMap';
import {Util} from './Util';
/**
* @name Event
* @memberOf SuperMap
* @namespace
* @description 事件处理函数.
*/
export var Event = SuperMap.Event = {
/**
* @description A hash table cache of the event observers. Keyed by element._eventCacheID
* @type {boolean}
* @default false
*/
observers: false,
/**
* @description KEY_SPACE
* @type {number}
* @default 32
*/
KEY_SPACE: 32,
/**
* @description KEY_BACKSPACE
* @type {number}
* @default 8
*/
KEY_BACKSPACE: 8,
/**
* @description KEY_TAB
* @type {number}
* @default 9
*/
KEY_TAB: 9,
/**
* @description KEY_RETURN
* @type {number}
* @default 13
*/
KEY_RETURN: 13,
/**
* @description KEY_ESC
* @type {number}
* @default 27
*/
KEY_ESC: 27,
/**
* @description KEY_LEFT
* @type {number}
* @default 37
*/
KEY_LEFT: 37,
/**
* @description KEY_UP
* @type {number}
* @default 38
*/
KEY_UP: 38,
/**
* @description KEY_RIGHT
* @type {number}
* @default 39
*/
KEY_RIGHT: 39,
/**
* @description KEY_DOWN
* @type {number}
* @default 40
*/
KEY_DOWN: 40,
/**
* @description KEY_DELETE
* @type {number}
* @default 46
*/
KEY_DELETE: 46,
/**
* @description Cross browser event element detection.
* @param {Event} event - The event
* @returns {HTMLElement} The element that caused the event
*/
element: function (event) {
return event.target || event.srcElement;
},
/**
* @description Determine whether event was caused by a single touch
* @param {Event} event - The event
* @returns {boolean}
*/
isSingleTouch: function (event) {
return event.touches && event.touches.length === 1;
},
/**
* @description Determine whether event was caused by a multi touch
* @param {Event} event - The event
* @returns {boolean}
*/
isMultiTouch: function (event) {
return event.touches && event.touches.length > 1;
},
/**
* @description Determine whether event was caused by a left click.
* @param {Event} event - The event
* @returns {boolean}
*/
isLeftClick: function (event) {
return (((event.which) && (event.which === 1)) ||
((event.button) && (event.button === 1)));
},
/**
* @description Determine whether event was caused by a right mouse click.
* @param {Event} event - The event
* @returns {boolean}
*/
isRightClick: function (event) {
return (((event.which) && (event.which === 3)) ||
((event.button) && (event.button === 2)));
},
/**
* @description Stops an event from propagating.
* @param {Event} event - The event
* @param {boolean} allowDefault - If true, we stop the event chain but still allow the default browser behaviour (text selection, radio-button clicking, etc) Default false
*/
stop: function (event, allowDefault) {
if (!allowDefault) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
},
/**
* @param {Event} event - The event。
* @param {string} tagName - html 标签名。
* @returns {HTMLElement} The first node with the given tagName, starting from the node the event was triggered on and traversing the DOM upwards
*/
findElement: function (event, tagName) {
var element = SuperMap.Event.element(event);
while (element.parentNode && (!element.tagName ||
(element.tagName.toUpperCase() != tagName.toUpperCase()))) {
element = element.parentNode;
}
return element;
},
/**
* @description 监听事件,注册事件处理方法。
* @param {(HTMLElement|string)} elementParam - 待监听的 DOM 对象或者其 ID 标识。
* @param {string} name - 监听事件的类别名称。
* @param {function} observer - 注册的事件处理方法。
* @param {boolean} [useCapture=false] - 是否捕获。
*/
observe: function (elementParam, name, observer, useCapture) {
var element = Util.getElement(elementParam);
useCapture = useCapture || false;
if (name === 'keypress' &&
(navigator.appVersion.match(/Konqueror|Safari|KHTML/)
|| element.attachEvent)) {
name = 'keydown';
}
//if observers cache has not yet been created, create it
if (!this.observers) {
this.observers = {};
}
//if not already assigned, make a new unique cache ID
if (!element._eventCacheID) {
var idPrefix = "eventCacheID_";
if (element.id) {
idPrefix = element.id + "_" + idPrefix;
}
element._eventCacheID = Util.createUniqueID(idPrefix);
}
var cacheID = element._eventCacheID;
//if there is not yet a hash entry for this element, add one
if (!this.observers[cacheID]) {
this.observers[cacheID] = [];
}
//add a new observer to this element's list
this.observers[cacheID].push({
'element': element,
'name': name,
'observer': observer,
'useCapture': useCapture
});
//add the actual browser event listener
if (element.addEventListener) {
if(name === 'mousewheel'){
// https://www.chromestatus.com/features/6662647093133312
element.addEventListener(name, observer, {useCapture: useCapture, passive: false} );
} else {
element.addEventListener(name, observer, useCapture);
}
} else if (element.attachEvent) {
element.attachEvent('on' + name, observer);
}
},
/**
* @description Given the id of an element to stop observing, cycle through the
* element's cached observers, calling stopObserving on each one,
* skipping those entries which can no longer be removed.
*
* @param {(HTMLElement|string)} elementParam -
*/
stopObservingElement: function (elementParam) {
var element = Util.getElement(elementParam);
var cacheID = element._eventCacheID;
this._removeElementObservers(SuperMap.Event.observers[cacheID]);
},
/**
* @param {Array.<Object>} elementObservers - Array of (element, name,
* observer, usecapture) objects,
* taken directly from hashtable
*/
_removeElementObservers: function (elementObservers) {
if (elementObservers) {
for (var i = elementObservers.length - 1; i >= 0; i--) {
var entry = elementObservers[i];
var args = new Array(entry.element, entry.name, entry.observer, entry.useCapture);
SuperMap.Event.stopObserving.apply(this, args);
}
}
},
/**
* @description 移除事件监听和注册的事件处理方法。注意:事件的移除和监听相对应,移除时的各属性信息必须监听时
* 保持一致才能确保事件移除成功。
* @param {(HTMLElement|string)} elementParam - 被监听的 DOM 元素或者其 ID。
* @param {string} name - 需要移除的被监听事件名称。
* @param {function} observer - 需要移除的事件处理方法。
* @param {boolean} [useCapture=false] - 是否捕获。
* @returns {boolean} Whether or not the event observer was removed
*/
stopObserving: function (elementParam, name, observer, useCapture) {
useCapture = useCapture || false;
var element = Util.getElement(elementParam);
var cacheID = element._eventCacheID;
if (name === 'keypress') {
if (navigator.appVersion.match(/Konqueror|Safari|KHTML/) ||
element.detachEvent) {
name = 'keydown';
}
}
// find element's entry in this.observers cache and remove it
var foundEntry = false;
var elementObservers = SuperMap.Event.observers[cacheID];
if (elementObservers) {
// find the specific event type in the element's list
var i = 0;
while (!foundEntry && i < elementObservers.length) {
var cacheEntry = elementObservers[i];
if ((cacheEntry.name === name) &&
(cacheEntry.observer === observer) &&
(cacheEntry.useCapture === useCapture)) {
elementObservers.splice(i, 1);
if (elementObservers.length == 0) {
delete SuperMap.Event.observers[cacheID];
}
foundEntry = true;
break;
}
i++;
}
}
//actually remove the event listener from browser
if (foundEntry) {
if (element.removeEventListener) {
element.removeEventListener(name, observer, useCapture);
} else if (element && element.detachEvent) {
element.detachEvent('on' + name, observer);
}
}
return foundEntry;
},
/**
* @description Cycle through all the element entries in the events cache and call
* stopObservingElement on each.
*/
unloadCache: function () {
// check for SuperMap.Event before checking for observers, because
// SuperMap.Event may be undefined in IE if no map instance was
// created
if (SuperMap.Event && SuperMap.Event.observers) {
for (var cacheID in SuperMap.Event.observers) {
var elementObservers = SuperMap.Event.observers[cacheID];
SuperMap.Event._removeElementObservers.apply(this,
[elementObservers]);
}
SuperMap.Event.observers = false;
}
},
CLASS_NAME: "SuperMap.Event"
};
SuperMap.Event = Event;
/* prevent memory leaks in IE */
SuperMap.Event.observe(window, 'unload', SuperMap.Event.unloadCache, false);