Skip to content

Commit

Permalink
Merge pull request #75 from woutervh-/pass-mode-to-event-listeners
Browse files Browse the repository at this point in the history
Pass along mode to BrushEvent
  • Loading branch information
Fil authored Aug 26, 2020
2 parents 13642ce + 9b28a8b commit a7b4524
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,4 @@ When a [brush event listener](#brush_on) is invoked, it receives the current bru
* `type` - the string “start”, “brush” or “end”; see [*brush*.on](#brush_on).
* `selection` - the current [brush selection](#brushSelection).
* `sourceEvent` - the underlying input event, such as mousemove or touchmove.
* `mode` - the string “drag”, “space”, “handle” or “center”; the mode of the brush.
21 changes: 11 additions & 10 deletions src/brush.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,20 @@ function brush(dim) {
if (++this.active === 1) this.state.emitter = this, this.starting = true;
return this;
},
start: function(event) {
if (this.starting) this.starting = false, this.emit("start", event);
start: function(event, mode) {
if (this.starting) this.starting = false, this.emit("start", event, mode);
else this.emit("brush", event);
return this;
},
brush: function(event) {
this.emit("brush", event);
brush: function(event, mode) {
this.emit("brush", event, mode);
return this;
},
end: function(event) {
if (--this.active === 0) delete this.state.emitter, this.emit("end", event);
end: function(event, mode) {
if (--this.active === 0) delete this.state.emitter, this.emit("end", event, mode);
return this;
},
emit: function(type, event) {
emit: function(type, event, mode) {
var d = select(this.that).datum();
listeners.call(
type,
Expand All @@ -323,6 +323,7 @@ function brush(dim) {
sourceEvent: event,
target: brush,
selection: dim.output(this.state.selection),
mode,
dispatch: listeners
}),
d
Expand Down Expand Up @@ -407,7 +408,7 @@ function brush(dim) {
}

redraw.call(that);
emit.start(event);
emit.start(event, mode.name);

function moved(event) {
for (const p of event.changedTouches || [event]) {
Expand Down Expand Up @@ -485,7 +486,7 @@ function brush(dim) {
|| selection[1][1] !== s1) {
state.selection = [[w1, n1], [e1, s1]];
redraw.call(that);
emit.brush(event);
emit.brush(event, mode.name);
}
}

Expand All @@ -503,7 +504,7 @@ function brush(dim) {
overlay.attr("cursor", cursors.overlay);
if (state.selection) selection = state.selection; // May be set by brush.move (on start)!
if (empty(selection)) state.selection = null, redraw.call(that);
emit.end(event);
emit.end(event, mode.name);
}

function keydowned(event) {
Expand Down
2 changes: 2 additions & 0 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ export default function BrushEvent(type, {
sourceEvent,
target,
selection,
mode,
dispatch
}) {
Object.defineProperties(this, {
type: {value: type, enumerable: true, configurable: true},
sourceEvent: {value: sourceEvent, enumerable: true, configurable: true},
target: {value: target, enumerable: true, configurable: true},
selection: {value: selection, enumerable: true, configurable: true},
mode: {value: mode, enumerable: true, configurable: true},
_: {value: dispatch}
});
}

0 comments on commit a7b4524

Please sign in to comment.