Skip to content

Commit

Permalink
Merge branch 'fix-dispatch'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 22, 2011
2 parents 5d57fa7 + 1b3a3b3 commit 2df8c62
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 133 deletions.
65 changes: 29 additions & 36 deletions d3.js
Expand Up @@ -10,7 +10,7 @@ try {
d3_style_setProperty.call(this, name, value + "", priority);
};
}
d3 = {version: "2.5.1"}; // semver
d3 = {version: "2.5.2"}; // semver
var d3_array = d3_arraySlice; // conversion for NodeLists

function d3_arrayCopy(pseudoarray) {
Expand Down Expand Up @@ -46,13 +46,22 @@ function d3_this() {
d3.functor = function(v) {
return typeof v === "function" ? v : function() { return v; };
};
// A getter-setter method that preserves the appropriate `this` context.
d3.rebind = function(object, method) {
// Copies a variable number of methods from source to target.
d3.rebind = function(target, source) {
var i = 1, n = arguments.length, method;
while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
return target;
};

// Method is assumed to be a standard D3 getter-setter:
// If passed with no arguments, gets the value.
// If passed with arguments, sets the value and returns the target.
function d3_rebind(target, source, method) {
return function() {
var x = method.apply(object, arguments);
return arguments.length ? object : x;
var value = method.apply(source, arguments);
return arguments.length ? target : value;
};
};
}
d3.ascending = function(a, b) {
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
};
Expand Down Expand Up @@ -494,7 +503,9 @@ d3_dispatch.prototype.on = function(type, listener) {
type = type.substring(0, i);
}

this[type].on(name, listener);
return arguments.length < 2
? this[type].on(name)
: (this[type].on(name, listener), this);
};

function d3_dispatch_event() {
Expand All @@ -506,24 +517,25 @@ function d3_dispatch_event() {
i = -1,
n = z.length,
l;
while (++i < n) if ((l = z[i])._on) l.apply(this, arguments);
while (++i < n) if (l = z[i].on) l.apply(this, arguments);
}

dispatch.on = function(name, listener) {
var l, i;

// remove the old listener, if any
// return the current listener, if any
if (arguments.length < 2) return (l = listenerByName[name]) && l.on;

// remove the old listener, if any (with copy-on-write)
if (l = listenerByName[name]) {
l._on = false;
l.on = null;
listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
delete listenerByName[name];
}

// add the new listener, if any
if (listener) {
listener._on = true;
listeners.push(listener);
listenerByName[name] = listener;
listeners.push(listenerByName[name] = {on: listener});
}

return dispatch;
Expand Down Expand Up @@ -2411,11 +2423,7 @@ function d3_scale_linear(domain, range, interpolate, clamp) {
};

function d3_scale_linearRebind(scale, linear) {
scale.range = d3.rebind(scale, linear.range);
scale.rangeRound = d3.rebind(scale, linear.rangeRound);
scale.interpolate = d3.rebind(scale, linear.interpolate);
scale.clamp = d3.rebind(scale, linear.clamp);
return scale;
return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
}

function d3_scale_linearNice(dx) {
Expand Down Expand Up @@ -4109,18 +4117,13 @@ d3.svg.brush = function() {
|| (y && extent[0][1] === extent[1][1]);
};

brush.on = function(type, listener) {
event.on(type, listener);
return brush;
};

d3.select(window)
.on("mousemove.brush", d3_svg_brushMove)
.on("mouseup.brush", d3_svg_brushUp)
.on("keydown.brush", d3_svg_brushKeydown)
.on("keyup.brush", d3_svg_brushKeyup);

return brush;
return d3.rebind(brush, event, "on");
};

var d3_svg_brush,
Expand Down Expand Up @@ -4305,12 +4308,7 @@ d3.behavior.drag = function() {
d3_behavior_dragDispatch("dragstart");
}

drag.on = function(type, listener) {
event.on(type, listener);
return drag;
};

return drag;
return d3.rebind(drag, event, "on");
};

var d3_behavior_dragEvent,
Expand Down Expand Up @@ -4450,12 +4448,7 @@ d3.behavior.zoom = function() {
return zoom;
};

zoom.on = function(type, listener) {
event.on(type, listener);
return zoom;
};

return zoom;
return d3.rebind(zoom, event, "on");
};

var d3_behavior_zoomDiv,
Expand Down
13 changes: 4 additions & 9 deletions d3.layout.js
Expand Up @@ -324,11 +324,6 @@ d3.layout.force = function() {
return (alpha *= .99) < .005;
}

force.on = function(type, listener) {
event.on(type, listener);
return force;
};

force.nodes = function(x) {
if (!arguments.length) return nodes;
nodes = x;
Expand Down Expand Up @@ -489,7 +484,7 @@ d3.layout.force = function() {
d3_layout_forceDragForce = force;
}

return force;
return d3.rebind(force, event, "on");
};

var d3_layout_forceDragForce,
Expand Down Expand Up @@ -1120,10 +1115,10 @@ d3.layout.hierarchy = function() {

// A method assignment helper for hierarchy subclasses.
function d3_layout_hierarchyRebind(object, hierarchy) {
object.sort = d3.rebind(object, hierarchy.sort);
object.children = d3.rebind(object, hierarchy.children);
d3.rebind(object, hierarchy, "sort", "children", "value");

// Add an alias for links, for convenience.
object.links = d3_layout_hierarchyLinks;
object.value = d3.rebind(object, hierarchy.value);

// If the new API is used, enabling inlining.
object.nodes = function(d) {
Expand Down
2 changes: 1 addition & 1 deletion d3.layout.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions d3.time.js
Expand Up @@ -570,12 +570,7 @@ function d3_time_scale(linear, methods, format) {
};

// TOOD expose d3_scale_linear_rebind?
scale.range = d3.rebind(scale, linear.range);
scale.rangeRound = d3.rebind(scale, linear.rangeRound);
scale.interpolate = d3.rebind(scale, linear.interpolate);
scale.clamp = d3.rebind(scale, linear.clamp);

return scale;
return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
}

// TODO expose d3_scaleExtent?
Expand Down
2 changes: 1 addition & 1 deletion d3.time.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "d3",
"version": "2.5.1",
"version": "2.5.2",
"description": "A small, free JavaScript library for manipulating documents based on data.",
"keywords": [
"dom",
Expand Down
7 changes: 1 addition & 6 deletions src/behavior/drag.js
Expand Up @@ -28,12 +28,7 @@ d3.behavior.drag = function() {
d3_behavior_dragDispatch("dragstart");
}

drag.on = function(type, listener) {
event.on(type, listener);
return drag;
};

return drag;
return d3.rebind(drag, event, "on");
};

var d3_behavior_dragEvent,
Expand Down
7 changes: 1 addition & 6 deletions src/behavior/zoom.js
Expand Up @@ -69,12 +69,7 @@ d3.behavior.zoom = function() {
return zoom;
};

zoom.on = function(type, listener) {
event.on(type, listener);
return zoom;
};

return zoom;
return d3.rebind(zoom, event, "on");
};

var d3_behavior_zoomDiv,
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.js
@@ -1 +1 @@
d3 = {version: "2.5.1"}; // semver
d3 = {version: "2.5.2"}; // semver
17 changes: 10 additions & 7 deletions src/core/dispatch.js
Expand Up @@ -18,7 +18,9 @@ d3_dispatch.prototype.on = function(type, listener) {
type = type.substring(0, i);
}

this[type].on(name, listener);
return arguments.length < 2
? this[type].on(name)
: (this[type].on(name, listener), this);
};

function d3_dispatch_event() {
Expand All @@ -30,24 +32,25 @@ function d3_dispatch_event() {
i = -1,
n = z.length,
l;
while (++i < n) if ((l = z[i])._on) l.apply(this, arguments);
while (++i < n) if (l = z[i].on) l.apply(this, arguments);
}

dispatch.on = function(name, listener) {
var l, i;

// remove the old listener, if any
// return the current listener, if any
if (arguments.length < 2) return (l = listenerByName[name]) && l.on;

// remove the old listener, if any (with copy-on-write)
if (l = listenerByName[name]) {
l._on = false;
l.on = null;
listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
delete listenerByName[name];
}

// add the new listener, if any
if (listener) {
listener._on = true;
listeners.push(listener);
listenerByName[name] = listener;
listeners.push(listenerByName[name] = {on: listener});
}

return dispatch;
Expand Down
19 changes: 14 additions & 5 deletions src/core/rebind.js
@@ -1,7 +1,16 @@
// A getter-setter method that preserves the appropriate `this` context.
d3.rebind = function(object, method) {
// Copies a variable number of methods from source to target.
d3.rebind = function(target, source) {
var i = 1, n = arguments.length, method;
while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
return target;
};

// Method is assumed to be a standard D3 getter-setter:
// If passed with no arguments, gets the value.
// If passed with arguments, sets the value and returns the target.
function d3_rebind(target, source, method) {
return function() {
var x = method.apply(object, arguments);
return arguments.length ? object : x;
var value = method.apply(source, arguments);
return arguments.length ? target : value;
};
};
}
7 changes: 1 addition & 6 deletions src/layout/force.js
Expand Up @@ -113,11 +113,6 @@ d3.layout.force = function() {
return (alpha *= .99) < .005;
}

force.on = function(type, listener) {
event.on(type, listener);
return force;
};

force.nodes = function(x) {
if (!arguments.length) return nodes;
nodes = x;
Expand Down Expand Up @@ -278,7 +273,7 @@ d3.layout.force = function() {
d3_layout_forceDragForce = force;
}

return force;
return d3.rebind(force, event, "on");
};

var d3_layout_forceDragForce,
Expand Down
6 changes: 3 additions & 3 deletions src/layout/hierarchy.js
Expand Up @@ -81,10 +81,10 @@ d3.layout.hierarchy = function() {

// A method assignment helper for hierarchy subclasses.
function d3_layout_hierarchyRebind(object, hierarchy) {
object.sort = d3.rebind(object, hierarchy.sort);
object.children = d3.rebind(object, hierarchy.children);
d3.rebind(object, hierarchy, "sort", "children", "value");

// Add an alias for links, for convenience.
object.links = d3_layout_hierarchyLinks;
object.value = d3.rebind(object, hierarchy.value);

// If the new API is used, enabling inlining.
object.nodes = function(d) {
Expand Down
6 changes: 1 addition & 5 deletions src/scale/linear.js
Expand Up @@ -72,11 +72,7 @@ function d3_scale_linear(domain, range, interpolate, clamp) {
};

function d3_scale_linearRebind(scale, linear) {
scale.range = d3.rebind(scale, linear.range);
scale.rangeRound = d3.rebind(scale, linear.rangeRound);
scale.interpolate = d3.rebind(scale, linear.interpolate);
scale.clamp = d3.rebind(scale, linear.clamp);
return scale;
return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
}

function d3_scale_linearNice(dx) {
Expand Down
7 changes: 1 addition & 6 deletions src/svg/brush.js
Expand Up @@ -168,18 +168,13 @@ d3.svg.brush = function() {
|| (y && extent[0][1] === extent[1][1]);
};

brush.on = function(type, listener) {
event.on(type, listener);
return brush;
};

d3.select(window)
.on("mousemove.brush", d3_svg_brushMove)
.on("mouseup.brush", d3_svg_brushUp)
.on("keydown.brush", d3_svg_brushKeydown)
.on("keyup.brush", d3_svg_brushKeyup);

return brush;
return d3.rebind(brush, event, "on");
};

var d3_svg_brush,
Expand Down
7 changes: 1 addition & 6 deletions src/time/scale.js
Expand Up @@ -38,12 +38,7 @@ function d3_time_scale(linear, methods, format) {
};

// TOOD expose d3_scale_linear_rebind?
scale.range = d3.rebind(scale, linear.range);
scale.rangeRound = d3.rebind(scale, linear.rangeRound);
scale.interpolate = d3.rebind(scale, linear.interpolate);
scale.clamp = d3.rebind(scale, linear.clamp);

return scale;
return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
}

// TODO expose d3_scaleExtent?
Expand Down

0 comments on commit 2df8c62

Please sign in to comment.