From: Simon Brunel Date: Fri, 10 Jun 2016 20:27:06 +0000 (+0200) Subject: Allow to register/unregister an array of plugins X-Git-Tag: v2.1.5~3^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dfdbd4465c6d35760aca60e7ff9bc58b62ac7cb9;p=thirdparty%2FChart.js.git Allow to register/unregister an array of plugins 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). --- diff --git a/src/core/core.plugin.js b/src/core/core.plugin.js index 2b020ac68..5fce40077 100644 --- a/src/core/core.plugin.js +++ b/src/core/core.plugin.js @@ -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; }, /** diff --git a/test/core.plugin.tests.js b/test/core.plugin.tests.js index c3c290128..0d4186af8 100644 --- a/test/core.plugin.tests.js +++ b/test/core.plugin.tests.js @@ -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) {