]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Replace `onEvent` by `before/afterEvent`
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Sun, 22 Jan 2017 20:10:17 +0000 (21:10 +0100)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 28 Jan 2017 00:32:35 +0000 (19:32 -0500)
docs/09-Advanced.md
src/core/core.controller.js
src/core/core.legend.js
src/core/core.plugin.js
test/platform.dom.tests.js

index d3d69d9afc953dfbc22bb7297b7edbf835c5a487..05642fcab4366ffb8095fc72d706fa0a8b123e02 100644 (file)
@@ -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) {}
 }
 ```
 
index f67b29ff934829641f5faf33a7498de9c6ab70bc..b38df58b291d4b94f0af1a1e119a98b9dcdcb53a 100644 (file)
@@ -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;
index b80bdbe25ad74300a26f77534ec3b026778c470a..4816b285f2e4a6c57ba5478b44fc267d5482c12d 100644 (file)
@@ -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);
index f89b412d2e14bfabc576b79a7ae7ad42f2325ef8..bda2ff45739e5b7e6a37427c9b328eb1240feeaf 100644 (file)
@@ -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.
index 19c5bbe1aabd64df0116e5baa3d035880865fb2f..a022cc75d5aaa975e282dbb75cb85a553e8a55f9 100644 (file)
@@ -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;
                                }
                        };