Skip to content

Commit 233a195

Browse files
Better events for force layout.
The force layout's listeners are now scoped using the "force" namespace, allowing other event listeners to be registered. In addition, the force layout now stops propagation of mouseup events when a node is moved; this allows a mouseup listener on the node to fire only when it is not moved.
1 parent 7e5fc72 commit 233a195

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

d3.layout.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,35 +384,43 @@ d3.layout.force = function() {
384384
force.drag = function() {
385385

386386
this
387-
.on("mouseover", d3_layout_forceDragOver)
388-
.on("mouseout", d3_layout_forceDragOut)
389-
.on("mousedown", d3_layout_forceDragDown);
387+
.on("mouseover.force", d3_layout_forceDragOver)
388+
.on("mouseout.force", d3_layout_forceDragOut)
389+
.on("mousedown.force", d3_layout_forceDragDown);
390390

391391
d3.select(window)
392-
.on("mousemove", dragmove)
393-
.on("mouseup", dragup);
392+
.on("mousemove.force", dragmove)
393+
.on("mouseup.force", dragup, true);
394394

395395
return force;
396396
};
397397

398398
function dragmove() {
399399
if (!d3_layout_forceDragNode) return;
400400

401-
// O NOES! The drag element was removed from the DOM.
401+
// O NOES! The drag element was removed from the DOM.
402402
if (!d3_layout_forceDragElement.parentNode) {
403-
d3_layout_forceDragNode.fixed = false;
404-
d3_layout_forceDragNode = d3_layout_forceDragElement = null;
405-
return;
403+
d3_layout_forceDragNode.fixed = false;
404+
d3_layout_forceDragNode = d3_layout_forceDragElement = null;
405+
return;
406406
}
407407

408408
var m = d3.svg.mouse(d3_layout_forceDragElement);
409+
d3_layout_forceDragMoved = true;
409410
d3_layout_forceDragNode.px = m[0];
410411
d3_layout_forceDragNode.py = m[1];
411412
force.resume(); // restart annealing
412413
}
413414

414415
function dragup() {
415416
if (!d3_layout_forceDragNode) return;
417+
418+
// If the node was moved, prevent the mouseup from propagating.
419+
if (d3_layout_forceDragMoved) {
420+
d3.event.stopPropagation();
421+
d3.event.preventDefault();
422+
}
423+
416424
dragmove();
417425
d3_layout_forceDragNode.fixed = false;
418426
d3_layout_forceDragNode = d3_layout_forceDragElement = null;
@@ -422,6 +430,7 @@ d3.layout.force = function() {
422430
};
423431

424432
var d3_layout_forceDragNode,
433+
d3_layout_forceDragMoved,
425434
d3_layout_forceDragElement;
426435

427436
function d3_layout_forceDragOver(d) {
@@ -434,8 +443,9 @@ function d3_layout_forceDragOut(d) {
434443
}
435444
}
436445

437-
function d3_layout_forceDragDown(d) {
446+
function d3_layout_forceDragDown(d, i) {
438447
(d3_layout_forceDragNode = d).fixed = true;
448+
d3_layout_forceDragMoved = false;
439449
d3_layout_forceDragElement = this;
440450
d3.event.stopPropagation();
441451
d3.event.preventDefault();

0 commit comments

Comments
 (0)