Skip to content

Commit

Permalink
feat(event): emit plot events
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed May 16, 2023
1 parent 86052fc commit 7e1071f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
8 changes: 8 additions & 0 deletions __tests__/integration/api-chart-on-item-element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ describe('chart.on', () => {
await fired;
});

it('chart.on("plot:click", callback) should emit plot events', async () => {
await finished;
const [fired, resolve] = createPromise();
chart.on(`plot:${ChartEvent.CLICK}`, () => resolve());
dispatchFirstElementEvent(canvas, 'click', { detail: 1 });
await fired;
});

afterAll(() => {
canvas?.destroy();
});
Expand Down
39 changes: 20 additions & 19 deletions __tests__/plots/api/chart-on-item-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,28 @@ export function chartOnItemElement(context) {
.style('draggable', true)
.style('droppable', true);

chart.on('interval:click', log('interval:click'));
chart.on('element:click', log('element:click'));
chart.on('interval:dblclick', log('interval:dblclick'));
// chart.on('interval:click', log('interval:click'));
// chart.on('element:click', log('element:click'));
chart.on('plot:click', () => console.log('plot:click'));
// chart.on('interval:dblclick', log('interval:dblclick'));

chart.on('interval:pointertap', log('interval:pointertap'));
chart.on('interval:pointerdown', log('interval:pointerdown'));
chart.on('interval:pointerup', log('interval:pointerup'));
chart.on('interval:pointerover', log('interval:pointerover'));
chart.on('interval:pointerout', log('interval:pointerout'));
chart.on('interval:pointermove', log('interval:pointermove'));
chart.on('interval:pointerenter', log('interval:pointerenter'));
chart.on('interval:pointerleave', log('interval:pointerleave'));
chart.on('interval:pointerupoutside', log('interval:pointerupoutside'));
// chart.on('interval:pointertap', log('interval:pointertap'));
// chart.on('interval:pointerdown', log('interval:pointerdown'));
// chart.on('interval:pointerup', log('interval:pointerup'));
// chart.on('interval:pointerover', log('interval:pointerover'));
// chart.on('interval:pointerout', log('interval:pointerout'));
// chart.on('interval:pointermove', log('interval:pointermove'));
// chart.on('interval:pointerenter', log('interval:pointerenter'));
// chart.on('interval:pointerleave', log('interval:pointerleave'));
// chart.on('interval:pointerupoutside', log('interval:pointerupoutside'));

chart.on('interval:dragstart', log('interval:dragstart'));
chart.on('interval:drag', log('interval:drag'));
chart.on('interval:dragend', log('interval:dragend'));
chart.on('interval:dragenter', log('interval:dragenter'));
chart.on('interval:dragleave', log('interval:dragleave'));
chart.on('interval:dragover', log('interval:dragover'));
chart.on('interval:drop', log('interval:drop'));
// chart.on('interval:dragstart', log('interval:dragstart'));
// chart.on('interval:drag', log('interval:drag'));
// chart.on('interval:dragend', log('interval:dragend'));
// chart.on('interval:dragenter', log('interval:dragenter'));
// chart.on('interval:dragleave', log('interval:dragleave'));
// chart.on('interval:dragover', log('interval:dragover'));
// chart.on('interval:drop', log('interval:drop'));

const finished = chart.render();

Expand Down
14 changes: 11 additions & 3 deletions src/interaction/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function bubblesEvent(eventType, view, emitter, predicate = (event) => true) {
if (!predicate(e)) return;
const { target } = e;
const { className: elementType, markType } = target;

// Emit plot events.
emitter.emit(`plot:${eventType}`, e);

// If click plot area, do not emit extra events.
if (elementType === 'plot') return;

// Emit wrapped events.
if (elementType === 'element') {
const e1 = {
...e,
Expand All @@ -27,10 +35,10 @@ function bubblesEvent(eventType, view, emitter, predicate = (event) => true) {
};
emitter.emit(`element:${eventType}`, e1);
emitter.emit(`${markType}:${eventType}`, e1);
return;
} else {
// @todo Handle click axis and legend.
emitter.emit(`${elementType}:${eventType}`, e);
}
// @todo Handle click axis and legend.
emitter.emit(`${elementType}:click`);
};
}

Expand Down

0 comments on commit 7e1071f

Please sign in to comment.