-
Notifications
You must be signed in to change notification settings - Fork 3
/
leta.event.dispatch.js
40 lines (35 loc) · 1023 Bytes
/
leta.event.dispatch.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
/**
* event.dispatch
* better than batchBind
*/
;(function (win, undefined) {
var $E = Leta.event;
function bubbleTo (el, endEl, key) {
if (!el || (el && el == document)) {
return null;
} else if (el == endEl || (el.getAttribute && el.getAttribute(key))) {
return el;
} else if (el.parentNode) {
return bubbleTo(el.parentNode, endEl, key);
} else {
return null;
}
}
function dispatch (el, type, key, distributor) {
if (typeof key == 'object') {
distributor = key;
key = 'data-cmd';
}
$E.on(el, type, function (e) {
var tar = bubbleTo(e.target, el, key);
if (tar) {
var cmd = tar.getAttribute(key);
distributor[cmd] && distributor[cmd].call && distributor[cmd].call(tar, e, tar);
}
});
}
Leta.extend($E, {
bubbleTo: bubbleTo,
dispatch: dispatch
});
})(window);