]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Allow to register/unregister an array of plugins
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 10 Jun 2016 20:27:06 +0000 (22:27 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 10 Jun 2016 22:17:44 +0000 (00:17 +0200)
The plugins service now accepts an array of plugin instances to register or unregister (for consistency, renamed `Chart.plugins.remove` to `unregister`). Also added a few methods to manipulate registered plugins, such as `count`, `getAll` and `clear` (mainly used by our unit tests).

src/core/core.plugin.js
test/core.plugin.tests.js

index 2b020ac687c37fb3750645c439fa218d0690986c..5fce4007715448f0c4952aaf08ba770dc0c491ff 100644 (file)
@@ -7,25 +7,62 @@ module.exports = function(Chart) {
        /**
         * The plugin service singleton
         * @namespace Chart.plugins
+        * @since 2.1.0
         */
        Chart.plugins = {
                _plugins: [],
 
-               // Register a new plugin
-               register: function(plugin) {
+               /**
+                * Registers the given plugin(s) if not already registered.
+                * @param {Array|Object} plugins plugin instance(s).
+                */
+               register: function(plugins) {
                        var p = this._plugins;
-                       if (p.indexOf(plugin) === -1) {
-                               p.push(plugin);
-                       }
+                       ([]).concat(plugins).forEach(function(plugin) {
+                               if (p.indexOf(plugin) === -1) {
+                                       p.push(plugin);
+                               }
+                       });
                },
 
-               // Remove a registered plugin
-               remove: function(plugin) {
+               /**
+                * Unregisters the given plugin(s) only if registered.
+                * @param {Array|Object} plugins plugin instance(s).
+                */
+               unregister: function(plugins) {
                        var p = this._plugins;
-                       var idx = p.indexOf(plugin);
-                       if (idx !== -1) {
-                               p.splice(idx, 1);
-                       }
+                       ([]).concat(plugins).forEach(function(plugin) {
+                               var idx = p.indexOf(plugin);
+                               if (idx !== -1) {
+                                       p.splice(idx, 1);
+                               }
+                       });
+               },
+
+               /**
+                * Remove all registered p^lugins.
+                * @since 2.1.5
+                */
+               clear: function() {
+                       this._plugins = [];
+               },
+
+               /**
+                * Returns the number of registered plugins?
+                * @returns {Number}
+                * @since 2.1.5
+                */
+               count: function() {
+                       return this._plugins.length;
+               },
+
+               /**
+                * Returns all registered plugin intances.
+                * @returns {Array} array of plugin objects.
+                * @since 2.1.5
+                */
+               getAll: function() {
+                       return this._plugins;
                },
 
                /**
index c3c290128bbcce091bd771094d26c14a86d743a4..0d4186af8d603963ad809cf5d16b329e0ff64cea 100644 (file)
@@ -1,44 +1,72 @@
-// Plugin tests
-describe('Test the plugin system', function() {
+describe('Chart.plugins', function() {
        var oldPlugins;
 
        beforeAll(function() {
-               oldPlugins = Chart.plugins._plugins;
+               oldPlugins = Chart.plugins.getAll();
        });
 
        afterAll(function() {
-               Chart.plugins._plugins = oldPlugins;
+               Chart.plugins.register(oldPlugins);
        });
 
        beforeEach(function() {
-               Chart.plugins._plugins = [];
+               Chart.plugins.clear();
        });
 
-       it ('Should register plugins', function() {
-               var myplugin = {};
-               Chart.plugins.register(myplugin);
-               expect(Chart.plugins._plugins.length).toBe(1);
+       describe('Chart.plugins.register', function() {
+               it('should register a plugin', function() {
+                       Chart.plugins.register({});
+                       expect(Chart.plugins.count()).toBe(1);
+                       Chart.plugins.register({});
+                       expect(Chart.plugins.count()).toBe(2);
+               });
+
+               it('should register an array of plugins', function() {
+                       Chart.plugins.register([{}, {}, {}]);
+                       expect(Chart.plugins.count()).toBe(3);
+               });
 
-               // Should only add plugin once
-               Chart.plugins.register(myplugin);
-               expect(Chart.plugins._plugins.length).toBe(1);
+               it('should succeed to register an already registered plugin', function() {
+                       var plugin = {};
+                       Chart.plugins.register(plugin);
+                       expect(Chart.plugins.count()).toBe(1);
+                       Chart.plugins.register(plugin);
+                       expect(Chart.plugins.count()).toBe(1);
+                       Chart.plugins.register([{}, plugin, plugin]);
+                       expect(Chart.plugins.count()).toBe(2);
+               });
        });
 
-       it ('Should allow unregistering plugins', function() {
-               var myplugin = {};
-               Chart.plugins.register(myplugin);
-               expect(Chart.plugins._plugins.length).toBe(1);
+       describe('Chart.plugins.unregister', function() {
+               it('should unregister a plugin', function() {
+                       var plugin = {};
+                       Chart.plugins.register(plugin);
+                       expect(Chart.plugins.count()).toBe(1);
+                       Chart.plugins.unregister(plugin);
+                       expect(Chart.plugins.count()).toBe(0);
+               });
 
-               // Should only add plugin once
-               Chart.plugins.remove(myplugin);
-               expect(Chart.plugins._plugins.length).toBe(0);
+               it('should unregister an array of plugins', function() {
+                       var plugins = [{}, {}, {}];
+                       Chart.plugins.register(plugins);
+                       expect(Chart.plugins.count()).toBe(3);
+                       Chart.plugins.unregister(plugins.slice(0, 2));
+                       expect(Chart.plugins.count()).toBe(1);
+               });
 
-               // Removing a plugin that doesn't exist should not error
-               Chart.plugins.remove(myplugin);
+               it('should succeed to unregister a plugin not registered', function() {
+                       var plugin = {};
+                       Chart.plugins.register(plugin);
+                       expect(Chart.plugins.count()).toBe(1);
+                       Chart.plugins.unregister({});
+                       expect(Chart.plugins.count()).toBe(1);
+                       Chart.plugins.unregister([{}, plugin]);
+                       expect(Chart.plugins.count()).toBe(0);
+               });
        });
 
        describe('Chart.plugins.notify', function() {
-               it ('should call plugins with arguments', function() {
+               it('should call plugins with arguments', function() {
                        var myplugin = {
                                count: 0,
                                trigger: function(chart) {