-
Notifications
You must be signed in to change notification settings - Fork 0
/
emit.js
24 lines (22 loc) · 974 Bytes
/
emit.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
define(["heya-has/sniff", "heya-dom/dom"], function(has, dom){
"use strict";
//TODO: the algorithm below is depricated,
// MDN suggests to use event constructors
return function emit(target, evt){
// use the native event emitting mechanism if it is available on the target object
// create a generic event
// we could create branch into the different types of event constructors, but
// that would be a lot of extra code, with little benefit that I can see, seems
// best to use the generic constructor and copy properties over, making it
// easy to have events look like the ones created with specific initializers
var nativeEvent = target.ownerDocument.createEvent("HTMLEvents");
nativeEvent.initEvent(evt.type, !!evt.bubbles, !!evt.cancelable);
// and copy all our properties over
for(var name in evt){
if(!(name in nativeEvent)){
nativeEvent[name] = evt[name];
}
}
return target.dispatchEvent(nativeEvent) && nativeEvent;
};
});