Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset selection source upon clause eviction #37

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/examples/splom.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function(el) {

const columns = ['bill_length', 'bill_depth', 'flipper_length', 'body_mass'];
const table = 'penguins';
const brush = Selection.union();
const brush = Selection.single();

function scatter(x, y, row, col, n) {
const s = 135;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/Selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export class Selection extends Param {
update(clause) {
const { source, predicate } = clause;
this.active = clause;
const clauses = this.single ? [] : this.clauses.filter(c => source !== c.source);
const filtered = this.clauses.filter(c => source !== c.source);
const clauses = this.single ? [] : filtered;
if (this.single) filtered.forEach(c => c.source?.reset?.());
if (predicate) clauses.push(clause);
return super.update(clauses);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/inputs/src/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export class Menu extends MosaicClient {
}
}

reset() {
this.select.selectedIndex = this.from ? 0 : -1;
}

publish(value) {
const { selection, column } = this;
if (isSelection(selection)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/inputs/src/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class Search extends MosaicClient {
}
}

reset() {
this.searchbox.value = '';
}

publish(value) {
const { selection, column, type } = this;
if (isSelection(selection)) {
Expand Down
8 changes: 7 additions & 1 deletion packages/vgplot/src/interactors/Interval1D.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { brushX, brushY, select, min, max } from 'd3';
import { select, min, max } from 'd3';
import { isBetween } from '@uwdata/mosaic-sql';
import { brushX, brushY } from './util/brush.js';
import { closeTo } from './util/close-to.js';
import { invert } from './util/invert.js';
import { patchScreenCTM } from './util/patchScreenCTM.js';
Expand All @@ -26,6 +27,11 @@ export class Interval1D {
this.brush.on('brush end', ({ selection }) => this.publish(selection));
}

reset() {
this.value = undefined;
if (this.g) this.brush.reset(this.g);
}

activate() {
this.selection.activate(this.clause(this.value || [0, 1]));
}
Expand Down
10 changes: 8 additions & 2 deletions packages/vgplot/src/interactors/Interval2D.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { brush, select, min, max } from 'd3';
import { select, min, max } from 'd3';
import { and, isBetween } from '@uwdata/mosaic-sql';
import { brush } from './util/brush.js';
import { closeTo } from './util/close-to.js';
import { invert } from './util/invert.js';
import { patchScreenCTM } from './util/patchScreenCTM.js';
Expand Down Expand Up @@ -27,6 +28,11 @@ export class Interval2D {
this.brush.on('brush end', ({ selection }) => this.publish(selection));
}

reset() {
this.value = undefined;
if (this.g) this.brush.reset(this.g);
}

activate() {
this.selection.activate(this.clause(this.value || [[0, 1], [0, 1]]));
}
Expand All @@ -42,7 +48,7 @@ export class Interval2D {
}

if (!closeTo(xr, value?.[0]) || !closeTo(yr, value?.[1])) {
this.value = extent ? [xr, yr] : null;
this.value = extent ? [xr, yr] : undefined;
this.g.call(this.brush.move, extent);
this.selection.update(this.clause(this.value));
}
Expand Down
30 changes: 30 additions & 0 deletions packages/vgplot/src/interactors/util/brush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
brush as d3_brush, brushX as d3_brushX, brushY as d3_brushY
} from 'd3';

function wrap(brush) {
const addEventListener = brush.on;
brush._enabled = true;
brush.reset = group => {
brush._enabled = false;
brush.clear(group);
brush._enabled = true;
};
brush.on = (type, callback) => {
const f = (...args) => brush._enabled && callback(...args);
return addEventListener(type, f);
};
return brush;
}

export function brush() {
return wrap(d3_brush());
}

export function brushX() {
return wrap(d3_brushX());
}

export function brushY() {
return wrap(d3_brushY());
}