From 7205ff5e2aa4515bae0c62bb9d8355745837270e Mon Sep 17 00:00:00 2001 From: Simon Brunel Date: Sun, 22 Jan 2017 21:10:17 +0100 Subject: [PATCH] Replace `onEvent` by `before/afterEvent` --- docs/09-Advanced.md | 9 +++------ src/core/core.controller.js | 10 +++++++--- src/core/core.legend.js | 2 +- src/core/core.plugin.js | 16 ++++++++++++++++ test/platform.dom.tests.js | 2 +- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/docs/09-Advanced.md b/docs/09-Advanced.md index d3d69d9af..05642fcab 100644 --- a/docs/09-Advanced.md +++ b/docs/09-Advanced.md @@ -439,12 +439,9 @@ Plugins should implement the `IPlugin` interface: destroy: function(chartInstance) { } - /** - * Called when an event occurs on the chart - * @param e {Core.Event} the Chart.js wrapper around the native event. e.native is the original event - * @return {Boolean} true if the chart is changed and needs to re-render - */ - onEvent: function(chartInstance, e) {} + // Called when an event occurs on the chart + beforeEvent: function(chartInstance, event) {} + afterEvent: function(chartInstance, event) {} } ``` diff --git a/src/core/core.controller.js b/src/core/core.controller.js index f67b29ff9..b38df58b2 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -664,7 +664,10 @@ module.exports = function(Chart) { eventHandler: function(e) { var me = this; var tooltip = me.tooltip; - var hoverOptions = me.options.hover; + + if (plugins.notify(me, 'beforeEvent', [e]) === false) { + return; + } // Buffer any update calls so that renders do not occur me._bufferedRender = true; @@ -672,7 +675,8 @@ module.exports = function(Chart) { var changed = me.handleEvent(e); changed |= tooltip && tooltip.handleEvent(e); - changed |= plugins.notify(me, 'onEvent', [e]); + + plugins.notify(me, 'afterEvent', [e]); var bufferedRequest = me._bufferedRequest; if (bufferedRequest) { @@ -684,7 +688,7 @@ module.exports = function(Chart) { // We only need to render at this point. Updating will cause scales to be // recomputed generating flicker & using more memory than necessary. - me.render(hoverOptions.animationDuration, true); + me.render(me.options.hover.animationDuration, true); } me._bufferedRender = false; diff --git a/src/core/core.legend.js b/src/core/core.legend.js index b80bdbe25..4816b285f 100644 --- a/src/core/core.legend.js +++ b/src/core/core.legend.js @@ -526,7 +526,7 @@ module.exports = function(Chart) { delete chartInstance.legend; } }, - onEvent: function(chartInstance, e) { + afterEvent: function(chartInstance, e) { var legend = chartInstance.legend; if (legend) { legend.handleEvent(e); diff --git a/src/core/core.plugin.js b/src/core/core.plugin.js index f89b412d2..bda2ff457 100644 --- a/src/core/core.plugin.js +++ b/src/core/core.plugin.js @@ -274,6 +274,22 @@ module.exports = function(Chart) { * @param {Number} easingValue - The current animation value, between 0.0 and 1.0. * @param {Object} options - The plugin options. */ + /** + * @method IPlugin#beforeEvent + * @desc Called before processing the specified `event`. If any plugin returns `false`, + * the event will be discarded. + * @param {Chart.Controller} chart - The chart instance. + * @param {IEvent} event - The event object. + * @param {Object} options - The plugin options. + */ + /** + * @method IPlugin#afterEvent + * @desc Called after the `event` has been consumed. Note that this hook + * will not be called if the `event` has been previously discarded. + * @param {Chart.Controller} chart - The chart instance. + * @param {IEvent} event - The event object. + * @param {Object} options - The plugin options. + */ /** * @method IPlugin#resize * @desc Called after the chart as been resized. diff --git a/test/platform.dom.tests.js b/test/platform.dom.tests.js index 19c5bbe1a..a022cc75d 100644 --- a/test/platform.dom.tests.js +++ b/test/platform.dom.tests.js @@ -320,7 +320,7 @@ describe('Platform.dom', function() { it('should notify plugins about events', function() { var notifiedEvent; var plugin = { - onEvent: function(chart, e) { + afterEvent: function(chart, e) { notifiedEvent = e; } }; -- 2.47.3