From 33f3db20326d90b8e4be9d00c374138433b2451e Mon Sep 17 00:00:00 2001 From: "tingyau.cty" Date: Mon, 27 Aug 2018 15:21:10 +0800 Subject: [PATCH] refactor(Page): extract out the addListener logic --- src/components/Page/index.js | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/components/Page/index.js b/src/components/Page/index.js index 2a5bbe11..16722365 100644 --- a/src/components/Page/index.js +++ b/src/components/Page/index.js @@ -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)}`]); }); }