forked from Hachitus/FlaTWorld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapEvents.js
86 lines (75 loc) · 2.3 KB
/
mapEvents.js
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
(function () {
'use strict';
/*---------------------
--------- API ---------
----------------------*/
window.flatworld.mapEvents = setupMapEvents();
/*---------------------
-------- PUBLIC -------
----------------------*/
/**
* This module handles map events. Like informing map movement, object selection and other changes. Not that ALL the eventlisteners and
* their callbacks will throw one event! But that event will have no extra parameters, so when you do special things, like selecting
* objects on the map, you should throw another event when that happens and you can pass on the objects that were selected from the map.
* Events atm:
* - mapdrag
* - mapzoomed
* - objectsSelected (in hexagon extension units.js)
* - mapMoved
* - mapResize
* @namespace flatworld
* @class mapEvents
* @return {Object} subsribe and publish
* @todo add mapfullscreen, mapfullSize and check if something is missing from the list
*/
function setupMapEvents () {
const TIMER_FOR_SAME_TYPE = 50;
var lastTimePublished = {};
/*---------------------
--------- API ---------
----------------------*/
return {
subscribe,
publish
};
/*---------------------
-------- PUBLIC -------
----------------------*/
function subscribe(type, cb) {
document.addEventListener(type, cb);
lastTimePublished[type] = 0;
}
function publish(type, ...data) {
var timestamp;
timestamp = new Date().getTime();
if ( (lastTimePublished[type] + TIMER_FOR_SAME_TYPE ) < timestamp) {
let eventToDispatch;
eventToDispatch = createCrossIeEvent(type);
eventToDispatch.customData = data;
document.dispatchEvent(eventToDispatch);
lastTimePublished[type] = timestamp;
}
}
}
/*---------------------
------- PRIVATE -------
----------------------*/
/**
* Basically custom events polyfill for IE11.
*
* @private
* @method createCrossIeEvent
* @param {String} type
* @return {Event} Created event object
*/
function createCrossIeEvent(type) {
var event;
try {
event = new Event(type);
} catch (e) {
event = document.createEvent('Event');
event.initEvent(type, true, true);
}
return event;
}
})();