forked from arieh/Events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.js
289 lines (232 loc) · 7.7 KB
/
Events.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
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
!function(){
var compat = 'createEvent' in document,
pseudo_regex = /([a-zA-Z0-9.]+)(\:([a-zA-Z]*))?(\((.*)\))?/,
addEvent, fireEvent, removeEvent, addEventOnce, Events;
function indexOf(arr, target){
var i, item;
if (arr.indexOf) return arr.indexOf(target);
for(i=0; item = arr[i]; i++) if (item == target) return i;
return -1;
}
function removeOn(string){
return string.replace(/^on([A-Z])/, function(full, first){
return first.toLowerCase();
});
}
function getPseudo(string){
var match = string.match(pseudo_regex);
return {
name : match[1],
pseudo : match[3],
args : match[5]
};
}
function processType(type){
return getPseudo(removeOn(type));
}
function createEvent(type, dis, args){
var ev;
if (compat){
ev = document.createEvent('UIEvents');
ev.initUIEvent(type, false, false, window, 1);
}else{
ev = {};
}
ev.dispatcher = dis;
ev.args = args;
return ev;
}
/**
* Events Provider.
*
* Can function either as a standalone or a Mixin
*
* @param {Element} el element to use as event target. Optional
*
*/
Events = function Events(el){
var $this = this;
if (!compat){
this.$events = {};
}else{
this.$event_element = el || document.createElement('events');
}
this.$latched = {};
/**
* Adds an event
*
* @param {String} the event type
* @param {Function} a function to add
*
* @return this
*/
this.addEvent = addEvent;
/**
* dispatches an event
*
* @param {String} event type
* @param {Mixed} arguments to pass with the event
*
* @return this
*/
this.fireEvent = fireEvent;
/**
* removes a function from an event
*
* @param {String} event type
* @param {Function} function to remove from stack
*
* @return this
*/
this.removeEvent = removeEvent;
/**
* Adds an event for one execution, then removes it
*
* @param {String} the event type
* @param {Function} a function to add
*
* @return this
*/
this.addEventOnce = addEventOnce;
this.addEvent('destroy',function(){
$this.$event_element = null;
});
};
Events.removeOn = removeOn;
Events.getPseudos = getPseudo;
Events.processType = processType;
/*
* Events.Pesudoes allows you to add pseudo behaviors
*
* Each object in the collection can hold both addEvent and fireEvent Methods
*
* The addEvent method will be fired *instead* of the normal behavior, and will be passed
* the event type and fn
*
* The fireEvent method will be fired *after* the fireEvent method, and will be passed
* the event name and the event object created
*
* Look at examples to see how it can be used
*/
Events.Pseudoes = {
once : {
addEvent : function(type,fn){
return this.addEventOnce(type, fn);
}
},
latched : {
fireEvent : function(type, args){
if (!this.$latched) this.$latched = {};
this.$latched[type] = {args : args};
this.fireEvent(type,args);
}
},
times : {
addEvent : function(type, fn, ammount){
var count = 0, $this = this;
this.addEvent(type, function times(){
fn.apply(null, arguments);
count+=1;
if (count == ammount) $this.removeEvent(type,times);
});
}
},
delayed : {
addEvent : function(type, fn, delay){
this.addEvent(type, function(){
setTimeout(fn,delay);
});
},
fireEvent : function(type, args, delay){
var $this = this;
setTimeout(function(){
$this.fireEvent(type, args);
}, delay);
}
}
};
this.Events = Events;
addEvent = compat ?
function(type,fn){
var type = processType(type),
pseudo_fn = Events.Pseudoes[type.pseudo] && Events.Pseudoes[type.pseudo].addEvent,
args = this.$latched[type.name] && this.$latched[type.name].args,
ev;
if (pseudo_fn){
return pseudo_fn.apply(this,[type.name,fn,type.args]);
}
if (this.$latched && this.$latched[type.name]){
ev = createEvent(type.name, this, args);
fn.apply(null,[ev]);
}
this.$event_element.addEventListener(type.name,fn,false);
return this;
} :
function(type,fn){
var type = processType(type),
pseudo_fn = Events.Pseudoes[type.pseudo] && Events.Pseudoes[type.pseudo].addEvent,
args = this.$latched[type.name] && this.$latched[type.name].args,
ev;
if (pseudo_fn){
return pseudo_fn.apply(this,[type.name,fn,type.args]);
}
if (this.$latched && this.$latched[type.name]){
ev = createEvent(type.name, args);
fn.apply(null,[ev]);
}
if (!this.$events[type.name]) this.$events[type.name] = [fn];
else this.$evetns[type.name].push(fn);
return this;
};
fireEvent = compat ?
function(type, args){
var type = processType(type),
pseudo_fn = Events.Pseudoes[type.pseudo] && Events.Pseudoes[type.pseudo].fireEvent,
ev;
if (pseudo_fn){
return pseudo_fn.call(this,type.name,args);
}
ev = createEvent(type.name, this, args);
this.$event_element.dispatchEvent(ev);
return this;
} :
function(type, args){
var type = processType(type),
pseudo_fn = Events.Pseudoes[type.pseudo] && Events.Pseudoes[type.pseudo].fireEvent,
ev, i, fn;
if (pseudo_fn){
return pseudo_fn.call(this,type.name,args);
}
if (!this.$events[type.name]) return this;
ev = createEvent(type.name, this, args);
for (i=0; fn = this.$events[type.name]; i++){
fn.apply(null,[ev]);
}
if (pseudo_fn){
Events.Pseudoes[type.pseudo].fireEvent.call(this,type,ev);
}
return this;
};
removeEvent = compat ?
function(type, fn){
var type = processType(type);
this.$event_element.removeEventListener(type.name,fn,false);
return this;
} : function(type, fn){
var index,
type = processType(type);
if (!this.$events[type.name]) return this;
index = indexOf(this.$events[type.name],fn);
if (index <0) return this;
this.$events[type.name].splice(index,1);
return this;
};
addEventOnce = function(type, fn){
var $this = this,
type = processType(type);
this.addEvent(type.name, function once(e){
fn.apply(null,[e]);
$this.removeEvent(type.name,once);
});
};
}.call(this);