Skip to content

Commit

Permalink
Append clear to top-level signals to account for input bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
arvind committed Apr 5, 2019
1 parent 1a3ec0c commit 053f24b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/compile/selection/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export function parseUnitSelection(model: UnitModel, selDefs: Dict<SelectionDef>
}
}

// Define "clear" defaults here as it depends on "on".
if (selDef.clear !== false) {
const trigger = selDef.on === 'mouseover' ? 'mouseout' : 'dblclick';
selDef.clear = parseSelector(selDef.clear || trigger, 'scope');
}

name = varName(name);
const selCmpt = (selCmpts[name] = {
...selDef,
Expand Down
25 changes: 16 additions & 9 deletions src/compile/selection/transforms/clear.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import {Update} from 'vega';
import {selector as parseSelector} from 'vega-event-selector';
import {TUPLE} from '..';
import {varName} from '../../../util';
import inputBindings from './inputs';
import {TransformCompiler} from './transforms';

const clear: TransformCompiler = {
has: selCmpt => {
return selCmpt.clear !== false;
},

signals: (model, selCmpt, signals) => {
const on = selCmpt.events[0].type;
const trigger = on === 'mouseover' ? 'mouseout' : 'dblclick';
const events = selCmpt.clear ? parseSelector(selCmpt.clear, 'scope') : parseSelector(trigger, 'scope');
topLevelSignals: (model, selCmpt, signals) => {
if (inputBindings.has(selCmpt)) {
selCmpt.project.forEach(proj => {
const idx = signals.findIndex(n => n.name === varName(`${selCmpt.name}_${proj.field}`));
if (idx !== -1) {
signals[idx].on.push({events: selCmpt.clear, update: 'null'});
}
});
}

return signals;
},

signals: (model, selCmpt, signals) => {
function addClear(idx: number, update: Update) {
if (idx !== -1 && signals[idx].on) {
signals[idx].on.push({
events: events,
update: update
});
signals[idx].on.push({events: selCmpt.clear, update});
}
}

Expand Down
36 changes: 30 additions & 6 deletions test/compile/selection/clear.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable quotemark */

import {selector as parseSelector} from 'vega-event-selector';
import {assembleUnitSelectionSignals} from '../../../src/compile/selection/assemble';
import {assembleTopLevelSignals} from '../../../src/compile/selection/assemble';
import interval from '../../../src/compile/selection/interval';
import multi from '../../../src/compile/selection/multi';
import {parseUnitSelection} from '../../../src/compile/selection/parse';
Expand All @@ -26,7 +26,14 @@ describe('Clear selection transform, single and multi types', () => {
three: {type: 'single', clear: 'mouseout'},
four: {type: 'multi', clear: 'mouseout'},
five: {type: 'single', clear: false},
six: {type: 'multi', clear: false}
six: {type: 'multi', clear: false},
seven: {
type: 'single',
fields: ['Year'],
bind: {
Year: {input: 'range', min: 1970, max: 1980, step: 1}
}
}
}));

it('identifies transform invocation', () => {
Expand Down Expand Up @@ -85,7 +92,7 @@ describe('Clear selection transform, single and multi types', () => {
'datum && item().mark.marktype !== \'group\' ? {unit: "", fields: three_tuple_fields, values: [(item().isVoronoi ? datum.datum : datum)["_vgsid_"]]} : null',
force: true
},
{events: parseSelector(selCmpts['three'].clear, 'scope'), update: 'null'}
{events: parseSelector('mouseout', 'scope'), update: 'null'}
]
}
]);
Expand All @@ -102,13 +109,30 @@ describe('Clear selection transform, single and multi types', () => {
'datum && item().mark.marktype !== \'group\' ? {unit: "", fields: four_tuple_fields, values: [(item().isVoronoi ? datum.datum : datum)["_vgsid_"]]} : null',
force: true
},
{events: parseSelector(selCmpts['four'].clear, 'scope'), update: 'null'}
{events: parseSelector('mouseout', 'scope'), update: 'null'}
]
}
]);

const signals = assembleUnitSelectionSignals(model, []);
expect(signals).toEqual(expect.arrayContaining([...oneSg, ...twoSg]));
expect(assembleTopLevelSignals(model, [])).toEqual(
expect.arrayContaining([
{
name: 'seven_Year',
value: null,
on: [
{
events: [{source: 'scope', type: 'click'}],
update: 'datum && item().mark.marktype !== \'group\' ? datum["Year"] : null'
},
{
events: [{source: 'scope', type: 'dblclick'}],
update: 'null'
}
],
bind: {input: 'range', min: 1970, max: 1980, step: 1}
}
])
);
});
});

Expand Down

0 comments on commit 053f24b

Please sign in to comment.