equal
deleted
inserted
replaced
|
1 d3.dispatch = function() { |
|
2 var dispatch = new d3_dispatch(), |
|
3 i = -1, |
|
4 n = arguments.length; |
|
5 while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(); |
|
6 return dispatch; |
|
7 }; |
|
8 |
|
9 function d3_dispatch() {} |
|
10 |
|
11 d3_dispatch.prototype.on = function(type, listener) { |
|
12 var i = type.indexOf("."), |
|
13 name = ""; |
|
14 |
|
15 // Extract optional namespace, e.g., "click.foo" |
|
16 if (i > 0) { |
|
17 name = type.substring(i + 1); |
|
18 type = type.substring(0, i); |
|
19 } |
|
20 |
|
21 this[type].on(name, listener); |
|
22 }; |
|
23 |
|
24 function d3_dispatch_event() { |
|
25 var listeners = [], |
|
26 listenerByName = {}; |
|
27 |
|
28 function dispatch() { |
|
29 var z = listeners, // defensive reference |
|
30 i = -1, |
|
31 n = z.length, |
|
32 l; |
|
33 while (++i < n) if ((l = z[i])._on) l.apply(this, arguments); |
|
34 } |
|
35 |
|
36 dispatch.on = function(name, listener) { |
|
37 var l, i; |
|
38 |
|
39 // remove the old listener, if any |
|
40 if (l = listenerByName[name]) { |
|
41 l._on = false; |
|
42 listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1)); |
|
43 delete listenerByName[name]; |
|
44 } |
|
45 |
|
46 // add the new listener, if any |
|
47 if (listener) { |
|
48 listener._on = true; |
|
49 listeners.push(listener); |
|
50 listenerByName[name] = listener; |
|
51 } |
|
52 |
|
53 return dispatch; |
|
54 }; |
|
55 |
|
56 return dispatch; |
|
57 }; |