]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Rename plugin service and notification method
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 10 Jun 2016 20:26:35 +0000 (22:26 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 10 Jun 2016 20:26:35 +0000 (22:26 +0200)
Rename `Chart.pluginService` to `Chart.plugins` (so move the old Chart.plugins array as a private member of the service), and rename `notifyPlugins` to `notify` for consistency with other service methods.

src/chart.js
src/core/core.controller.js
src/core/core.legend.js
src/core/core.plugin.js
src/core/core.title.js
test/core.plugin.tests.js

index f992a9e1c45a35b2473026994f64a01760f9143d..75bd47538416d3e9336a1b4ec48de02ac0eea19d 100644 (file)
@@ -1,3 +1,6 @@
+/**
+ * @namespace Chart
+ */
 var Chart = require('./core/core.js')();
 
 require('./core/core.helpers')(Chart);
index db521c607eb3fa2854db11f1f39262af33847d94..eb472d63cc1376ed5319e9f4e2d36a26185a3733 100644 (file)
@@ -45,7 +45,7 @@ module.exports = function(Chart) {
                initialize: function initialize() {
                        var me = this;
                        // Before init plugin notification
-                       Chart.pluginService.notifyPlugins('beforeInit', [me]);
+                       Chart.plugins.notify('beforeInit', [me]);
 
                        me.bindEvents();
 
@@ -60,7 +60,7 @@ module.exports = function(Chart) {
                        me.update();
 
                        // After init plugin notification
-                       Chart.pluginService.notifyPlugins('afterInit', [me]);
+                       Chart.plugins.notify('afterInit', [me]);
 
                        return me;
                },
@@ -83,7 +83,7 @@ module.exports = function(Chart) {
                        var newWidth = helpers.getMaximumWidth(canvas);
                        var aspectRatio = chart.aspectRatio;
                        var newHeight = (me.options.maintainAspectRatio && isNaN(aspectRatio) === false && isFinite(aspectRatio) && aspectRatio !== 0) ? newWidth / aspectRatio : helpers.getMaximumHeight(canvas);
-                       
+
                        var sizeChanged = chart.width !== newWidth || chart.height !== newHeight;
 
                        if (!sizeChanged) {
@@ -97,7 +97,7 @@ module.exports = function(Chart) {
 
                        // Notify any plugins about the resize
                        var newSize = { width: newWidth, height: newHeight };
-                       Chart.pluginService.notifyPlugins('resize', [me, newSize]);
+                       Chart.plugins.notify('resize', [me, newSize]);
 
                        // Notify of resize
                        if (me.options.onResize) {
@@ -225,7 +225,7 @@ module.exports = function(Chart) {
 
                update: function update(animationDuration, lazy) {
                        var me = this;
-                       Chart.pluginService.notifyPlugins('beforeUpdate', [me]);
+                       Chart.plugins.notify('beforeUpdate', [me]);
 
                        // In case the entire data object changed
                        me.tooltip._data = me.data;
@@ -241,7 +241,7 @@ module.exports = function(Chart) {
                        Chart.layoutService.update(me, me.chart.width, me.chart.height);
 
                        // Apply changes to the dataets that require the scales to have been calculated i.e BorderColor chages
-                       Chart.pluginService.notifyPlugins('afterScaleUpdate', [me]);
+                       Chart.plugins.notify('afterScaleUpdate', [me]);
 
                        // Can only reset the new controllers after the scales have been updated
                        helpers.each(newControllers, function(controller) {
@@ -254,14 +254,14 @@ module.exports = function(Chart) {
                        }, me);
 
                        // Do this before render so that any plugins that need final scale updates can use it
-                       Chart.pluginService.notifyPlugins('afterUpdate', [me]);
+                       Chart.plugins.notify('afterUpdate', [me]);
 
                        me.render(animationDuration, lazy);
                },
 
                render: function render(duration, lazy) {
                        var me = this;
-                       Chart.pluginService.notifyPlugins('beforeRender', [me]);
+                       Chart.plugins.notify('beforeRender', [me]);
 
                        var animationOptions = me.options.animation;
                        if (animationOptions && ((typeof duration !== 'undefined' && duration !== 0) || (typeof duration === 'undefined' && animationOptions.duration !== 0))) {
@@ -297,7 +297,7 @@ module.exports = function(Chart) {
                        var easingDecimal = ease || 1;
                        me.clear();
 
-                       Chart.pluginService.notifyPlugins('beforeDraw', [me, easingDecimal]);
+                       Chart.plugins.notify('beforeDraw', [me, easingDecimal]);
 
                        // Draw all the scales
                        helpers.each(me.boxes, function(box) {
@@ -307,7 +307,7 @@ module.exports = function(Chart) {
                                me.scale.draw();
                        }
 
-                       Chart.pluginService.notifyPlugins('beforeDatasetDraw', [me, easingDecimal]);
+                       Chart.plugins.notify('beforeDatasetDraw', [me, easingDecimal]);
 
                        // Draw each dataset via its respective controller (reversed to support proper line stacking)
                        helpers.each(me.data.datasets, function(dataset, datasetIndex) {
@@ -316,12 +316,12 @@ module.exports = function(Chart) {
                                }
                        }, me, true);
 
-                       Chart.pluginService.notifyPlugins('afterDatasetDraw', [me, easingDecimal]);
+                       Chart.plugins.notify('afterDatasetDraw', [me, easingDecimal]);
 
                        // Finally draw the tooltip
                        me.tooltip.transition(easingDecimal).draw();
 
-                       Chart.pluginService.notifyPlugins('afterDraw', [me, easingDecimal]);
+                       Chart.plugins.notify('afterDraw', [me, easingDecimal]);
                },
 
                // Get the single element that was clicked on
@@ -470,7 +470,7 @@ module.exports = function(Chart) {
                        canvas.style.width = me.chart.originalCanvasStyleWidth;
                        canvas.style.height = me.chart.originalCanvasStyleHeight;
 
-                       Chart.pluginService.notifyPlugins('destroy', [me]);
+                       Chart.plugins.notify('destroy', [me]);
 
                        delete Chart.instances[me.id];
                },
index b3d392c05fc49e1b770294993a631e0d565359e9..78c89c55925262657d54aa3f39fc825512b81d8a 100644 (file)
@@ -420,7 +420,7 @@ module.exports = function(Chart) {
        });
 
        // Register the legend plugin
-       Chart.pluginService.register({
+       Chart.plugins.register({
                beforeInit: function(chartInstance) {
                        var opts = chartInstance.options;
                        var legendOpts = opts.legend;
index 31be4d48b4aab82e6dba844d13450c3825b99da9..7126dcd43d5a49e88b0443f341d1751b0f0c0b4e 100644 (file)
@@ -1,14 +1,20 @@
 "use strict";
 
 module.exports = function(Chart) {
+
        var helpers = Chart.helpers;
+       var noop = helpers.noop;
+
+       /**
+        * The plugin service singleton
+        * @namespace Chart.plugins
+        */
+       Chart.plugins = {
+               _plugins: [],
 
-       // Plugins are stored here
-       Chart.plugins = [];
-       Chart.pluginService = {
                // Register a new plugin
                register: function(plugin) {
-                       var p = Chart.plugins;
+                       var p = this._plugins;
                        if (p.indexOf(plugin) === -1) {
                                p.push(plugin);
                        }
@@ -16,16 +22,20 @@ module.exports = function(Chart) {
 
                // Remove a registered plugin
                remove: function(plugin) {
-                       var p = Chart.plugins;
+                       var p = this._plugins;
                        var idx = p.indexOf(plugin);
                        if (idx !== -1) {
                                p.splice(idx, 1);
                        }
                },
 
-               // Iterate over all plugins
-               notifyPlugins: function(method, args, scope) {
-                       helpers.each(Chart.plugins, function(plugin) {
+               /**
+                * Calls registered plugins on the specified method, with the given args. This
+                * method immediately returns as soon as a plugin explicitly returns false.
+                * @returns {Boolean} false if any of the plugins return false, else returns true.
+                */
+               notify: function(method, args, scope) {
+                       helpers.each(this._plugins, function(plugin) {
                                if (plugin[method] && typeof plugin[method] === 'function') {
                                        plugin[method].apply(scope, args);
                                }
@@ -33,7 +43,6 @@ module.exports = function(Chart) {
                }
        };
 
-       var noop = helpers.noop;
        Chart.PluginBase = Chart.Element.extend({
                // Plugin methods. All functions are passed the chart instance
 
@@ -58,4 +67,12 @@ module.exports = function(Chart) {
                // Called during destroy
                destroy: noop
        });
+
+       /**
+        * Provided for backward compatibility, use Chart.plugins instead
+        * @namespace Chart.pluginService
+        * @deprecated since version 2.1.5
+        * @todo remove me at version 3
+        */
+       Chart.pluginService = Chart.plugins;
 };
index b74f6f8291c4d636c76760a1d6cf7a231d1a7d8f..4e00b82b774a7458b98d87917bc21336ed8429b8 100644 (file)
@@ -38,7 +38,7 @@ module.exports = function(Chart) {
                },
                update: function(maxWidth, maxHeight, margins) {
                        var me = this;
-                       
+
                        // Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
                        me.beforeUpdate();
 
@@ -155,7 +155,7 @@ module.exports = function(Chart) {
                                        fontFamily = valueOrDefault(opts.fontFamily, globalDefaults.defaultFontFamily),
                                        titleFont = helpers.fontString(fontSize, fontStyle, fontFamily),
                                        rotation = 0,
-                                       titleX, 
+                                       titleX,
                                        titleY,
                                        top = me.top,
                                        left = me.left,
@@ -187,7 +187,7 @@ module.exports = function(Chart) {
        });
 
        // Register the title plugin
-       Chart.pluginService.register({
+       Chart.plugins.register({
                beforeInit: function(chartInstance) {
                        var opts = chartInstance.options;
                        var titleOpts = opts.title;
index 520305e3980ae236dd24d166c81824bafcff4aee..62e5e21880822cbe372d8c53a43d5fd1a50b6817 100644 (file)
@@ -3,37 +3,38 @@ describe('Test the plugin system', function() {
        var oldPlugins;
 
        beforeAll(function() {
-               oldPlugins = Chart.plugins;
+               oldPlugins = Chart.plugins._plugins;
        });
+
        afterAll(function() {
-               Chart.plugins = oldPlugins;
+               Chart.plugins._plugins = oldPlugins;
        });
 
        beforeEach(function() {
-               Chart.plugins = [];
+               Chart.plugins._plugins = [];
        });
 
        it ('Should register plugins', function() {
                var myplugin = {};
-               Chart.pluginService.register(myplugin);
-               expect(Chart.plugins.length).toBe(1);
+               Chart.plugins.register(myplugin);
+               expect(Chart.plugins._plugins.length).toBe(1);
 
                // Should only add plugin once
-               Chart.pluginService.register(myplugin);
-               expect(Chart.plugins.length).toBe(1);           
+               Chart.plugins.register(myplugin);
+               expect(Chart.plugins._plugins.length).toBe(1);
        });
 
        it ('Should allow unregistering plugins', function() {
                var myplugin = {};
-               Chart.pluginService.register(myplugin);
-               expect(Chart.plugins.length).toBe(1);
+               Chart.plugins.register(myplugin);
+               expect(Chart.plugins._plugins.length).toBe(1);
 
                // Should only add plugin once
-               Chart.pluginService.remove(myplugin);
-               expect(Chart.plugins.length).toBe(0);           
+               Chart.plugins.remove(myplugin);
+               expect(Chart.plugins._plugins.length).toBe(0);
 
                // Removing a plugin that doesn't exist should not error
-               Chart.pluginService.remove(myplugin);
+               Chart.plugins.remove(myplugin);
        });
 
        it ('Should allow notifying plugins', function() {
@@ -43,9 +44,9 @@ describe('Test the plugin system', function() {
                                myplugin.count = chart.count;
                        }
                };
-               Chart.pluginService.register(myplugin);
-               
-               Chart.pluginService.notifyPlugins('trigger', [{ count: 10 }]);
+               Chart.plugins.register(myplugin);
+
+               Chart.plugins.notify('trigger', [{ count: 10 }]);
 
                expect(myplugin.count).toBe(10);
        });