Skip to content

Commit

Permalink
Replace drag.filter with drag.subject.
Browse files Browse the repository at this point in the history
This makes it easier to define what’s being dragged. Fixes #12.
  • Loading branch information
mbostock committed May 9, 2016
1 parent fecdfc7 commit ec094cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
49 changes: 24 additions & 25 deletions src/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@ import noclick from "./noclick";
import nodrag from "./nodrag";
import noselect from "./noselect";

var ignore = {};

function defaultX(d) {
return d && d.x;
return (d == null ? event.subject : d).x;
}

function defaultY(d) {
return d && d.y;
return (d == null ? event.subject : d).y;
}

// Ignore right-click, since that should open the context menu.
function defaultFilter() {
return !event.sourceEvent.button;
function defaultSubject() {
return event.sourceEvent.button ? null : this;
}

function defaultContainer() {
return this.parentNode;
}

export default function(started) {
export default function() {
var x = defaultX,
y = defaultY,
filter = defaultFilter,
subject = defaultSubject,
container = defaultContainer,
active = {};

Expand All @@ -45,7 +43,6 @@ export default function(started) {
var listeners = dispatch("start", "drag", "end")
.on("start.nodrag", nodrag)
.on("start.noselect", noselect)
.on("start", started)
.on("drag.noclick", noclick)
.on("drag.noscroll", cancel);

Expand All @@ -59,7 +56,8 @@ export default function(started) {
}

function mousedowned() {
if (!start("mouse", mouse, this, arguments)) return;
var parent = container.apply(this, arguments);
if (!start("mouse", parent, mouse, this, arguments)) return;
select(event.view).on("mousemove.drag", mousemoved).on("mouseup.drag", mouseupped);
}

Expand All @@ -75,8 +73,9 @@ export default function(started) {
}

function touchstarted() {
var parent = container.apply(this, arguments);
for (var touches = event.changedTouches, i = 0, n = touches.length; i < n; ++i) {
start(touches[i].identifier, touch, this, arguments);
start(touches[i].identifier, parent, touch, this, arguments);
}
}

Expand All @@ -97,29 +96,29 @@ export default function(started) {
}
}

function beforestart() {
return filter.apply(this, arguments) ? container.apply(this, arguments) : ignore;
}

function start(id, point, that, args) {
if ((parent = customEvent(new DragEvent("beforestart", id), beforestart, that, args)) === ignore) return false;

var parent,
function start(id, parent, point, that, args) {
var p0 = point(parent, id), dx, dy,
sublisteners = listeners.copy(),
p0 = point(parent, id),
dx = x.apply(that, args) - p0[0] || 0,
dy = y.apply(that, args) - p0[1] || 0;
node;

if (!customEvent(new DragEvent("beforestart", node, id, p0[0], p0[1], sublisteners), function() {
node = event.subject = subject.apply(that, args);
if (node == null) return false;
dx = x.apply(that, args) - p0[0] || 0;
dy = y.apply(that, args) - p0[1] || 0;
return true;
})) return false;

(active[id] = function(type, p) {
if (p == null) p = point(parent, id);
customEvent(new DragEvent(type, id, p[0] + dx, p[1] + dy, sublisteners), sublisteners.apply, sublisteners, [type, that, args]);
customEvent(new DragEvent(type, node, id, p[0] + dx, p[1] + dy, sublisteners), sublisteners.apply, sublisteners, [type, that, args]);
})("start", p0);

return true;
}

drag.filter = function(_) {
return arguments.length ? (filter = typeof _ === "function" ? _ : constant(!!_), drag) : filter;
drag.subject = function(_) {
return arguments.length ? (subject = typeof _ === "function" ? _ : constant(_), drag) : subject;
};

drag.container = function(_) {
Expand Down
3 changes: 2 additions & 1 deletion src/event.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default function DragEvent(type, id, x, y, dispatch) {
export default function DragEvent(type, subject, id, x, y, dispatch) {
this.type = type;
this.subject = subject;
this.identifier = id;
this.x = x;
this.y = y;
Expand Down

0 comments on commit ec094cd

Please sign in to comment.