forked from mapbox/geojson.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3-compat.js
48 lines (42 loc) · 1.32 KB
/
d3-compat.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
(function() {
// get a reference to the d3.selection prototype,
// and keep a reference to the old d3.selection.on
var d3_selectionPrototype = d3.selection.prototype,
d3_on = d3_selectionPrototype.on;
// our shims are organized by event:
// "desired-event": ["shimmed-event", wrapperFunction]
var shims = {
"mouseenter": ["mouseover", relatedTarget],
"mouseleave": ["mouseout", relatedTarget]
};
// rewrite the d3.selection.on function to shim the events with wrapped
// callbacks
d3_selectionPrototype.on = function(evt, callback, useCapture) {
var bits = evt.split("."),
type = bits.shift(),
shim = shims[type];
if (shim) {
evt = bits.length ? [shim[0], bits].join(".") : shim[0];
if (typeof callback === "function") {
callback = shim[1](callback);
}
return d3_on.call(this, evt, callback, useCapture);
} else {
return d3_on.apply(this, arguments);
}
};
function relatedTarget(callback) {
return function() {
var related = d3.event.relatedTarget;
if (this === related || childOf(this, related)) {
return undefined;
}
return callback.apply(this, arguments);
};
}
function childOf(p, c) {
if (p === c) return false;
while (c && c !== p) c = c.parentNode;
return c === p;
}
})();