-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDispatcher.js
128 lines (106 loc) · 3.43 KB
/
EventDispatcher.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
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
/**
* Event Dispatcher base class
*
* @namespace core
* @author Georgi Popov
* @version 1.0
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GPLv2
* @uses com/magadanski/core/core.js
*/
define('com.magadanski.core.EventDispatcher', function () {
var that;
// private properties
// private methods
/**
* Core EventDispatcher class that should be extended in case you need ability to fire custom events from an object
*
* @class EventDispatcher
* @constructor
* @since 1.0
*/
var EventDispatcher = function () {
that = this;
// priviledged properties
that.events = {};
// priviledged methods
// constructor
}
com.magadanski.core.EventDispatcher = EventDispatcher;
// public methods
/**
* Attaches an event handler to a specific event type
*
* @method addEventListener
* @param {string} eventType The type of the event you'd like to listen for
* @param {function(event:Event)} callback The function to be executed when the event occurs
* @return {void}
*/
EventDispatcher.prototype.addEventListener = function (eventType, callback) {
var that = this;
if (typeof(eventType.split) != 'undefined') {
eventType = eventType.split(/[\s\,]/);
}
for (var e in eventType) {
var eventName = eventType[e];
if (eventName.length > 1) {
if (typeof(that.events[eventName]) == 'undefined') {
that.events[eventName] = [];
}
that.events[eventName].push(callback);
}
}
}
/**
* Removes a previously attached event to a specific event type.
*
* @method removeEventListener
* @param {string} eventType The type of the event you'd like to unbind a handler for
* @param {function(event:Event)} callback This must be a reference to the same Function object that has been set as a listener. Otherwise the removal will not take place.
* @return {void}
*/
EventDispatcher.prototype.removeEventListener = function (eventType, callback) {
var that = this;
if (typeof(eventType.split) != 'undefined') {
eventType = eventType.split(',');
}
for (var e in eventType) {
var eventName = eventType[e].replace(/\s/, '');
if (typeof(that.events[eventName]) != 'undedined') {
for (var c in that.events[eventName]) {
var shouldCallbackBeRemoved = false;
if (typeof(callback) !== 'Function') {
shouldCallbackBeRemoved = true;
} else if (c == callback || that.events[eventName][c] == callback) {
shouldCallbackBeRemoved = true;
}
if (shouldCallbackBeRemoved) {
that.events[eventName].splice(c, 1);
}
}
}
}
}
/**
* Fires an event on behalf of the object
*
* @method dispatchEvent
* @param {string} eventType The type of the event you'd like to fire
* @param {Event} eventObj The actual event object that will be passed to listener functions. This can be used to store additional information for the situation.
* @return {void}
*/
EventDispatcher.prototype.dispatchEvent = function (eventType, eventObj) {
var that = this;
if (typeof(eventObj) == 'undefined') {
eventObj = {}; // TODO: check options to migrate to native JS event objects
}
if (typeof(that.events[eventType]) == 'object') {
for (var callback in that.events[eventType]) {
eventObj.type = eventType;
eventObj.currentTarget = this;
if (typeof(that.events[eventType][callback]) == 'function') {
that.events[eventType][callback](eventObj);
}
}
}
}
});