Skip to content

Commit

Permalink
refactor(Page): extract out the addListener logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tingyau.cty committed Aug 27, 2018
1 parent 539ca30 commit 33f3db2
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/components/Page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,26 @@ class Page extends BaseComponent {
},
});
}
get graph() {
return this.page.getGraph();
}

bindEvent() {
const graph = this.page.getGraph();
addListener = (target, eventName, handler) => {
if (typeof handler === 'function') target.on(eventName, handler);
}

bindEvent() {
GRAPH_MOUSE_EVENTS.forEach((event) => {
const handleNodeEvent = this.props[`onNode${upperFirst(event)}`];
const handleEdgeEvent = this.props[`onEdge${upperFirst(event)}`];

if (handleNodeEvent) {
graph.on(`node:${event}`, handleNodeEvent);
}

if (handleEdgeEvent) {
graph.on(`edge:${event}`, handleEdgeEvent);
}
this.addListener(this.graph, `node:${event}`, this.props[`onNode${upperFirst(event)}`]);
this.addListener(this.graph, `edge:${event}`, this.props[`onEdge${upperFirst(event)}`]);
});

GRAPH_OTHER_EVENTS.forEach((event) => {
const handleEvent = this.props[(`on${upperFirst(event)}`)];

if (handleEvent) {
graph.on([event], handleEvent);
}
this.addListener(this.graph, [event], this.props[`on${upperFirst(event)}`]);
});

PAGE_EVENTS.forEach((event) => {
const handleEvent = this.props[(`on${upperFirst(event)}`)];

if (handleEvent) {
this.page.on([event], handleEvent);
}
this.addListener(this.page, [event], this.props[`on${upperFirst(event)}`]);
});
}

Expand Down

0 comments on commit 33f3db2

Please sign in to comment.