-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathpropagateEvents.js
57 lines (46 loc) · 1.35 KB
/
propagateEvents.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
import circular from 'circular';
import fireEvent from 'Ractive/prototype/shared/fireEvent';
import log from 'utils/log';
// TODO how should event arguments be handled? e.g.
// <widget on-foo='bar:1,2,3'/>
// The event 'bar' will be fired on the parent instance
// when 'foo' fires on the child, but the 1,2,3 arguments
// will be lost
var Fragment;
circular.push( function () {
Fragment = circular.Fragment;
});
export default function propagateEvents ( component, eventsDescriptor ) {
var eventName;
for ( eventName in eventsDescriptor ) {
if ( eventsDescriptor.hasOwnProperty( eventName ) ) {
propagateEvent( component.instance, component.root, eventName, eventsDescriptor[ eventName ] );
}
}
}
function propagateEvent ( childInstance, parentInstance, eventName, proxyEventName ) {
if ( typeof proxyEventName !== 'string' ) {
log.error({
debug: parentInstance.debug,
message: 'noComponentEventArguments'
});
}
childInstance.on( eventName, function () {
var options;
// semi-weak test, but what else? tag the event obj ._isEvent ?
if ( arguments[0].node ) {
options = {
event: Array.prototype.shift.call( arguments ),
args: arguments
};
}
else {
options = {
args: Array.prototype.slice.call( arguments )
};
}
fireEvent( parentInstance, proxyEventName, options );
// cancel bubbling
return false;
});
}