'use strict'; module.exports = function(Chart) { var helpers = Chart.helpers; var noop = helpers.noop; Chart.defaults.global.plugins = {}; /** * The plugin service singleton * @namespace Chart.plugins * @since 2.1.0 */ Chart.plugins = { /** * Globally registered plugins. * @private */ _plugins: [], /** * This identifier is used to invalidate the descriptors cache attached to each chart * when a global plugin is registered or unregistered. In this case, the cache ID is * incremented and descriptors are regenerated during following API calls. * @private */ _cacheId: 0, /** * Registers the given plugin(s) if not already registered. * @param {Array|Object} plugins plugin instance(s). */ register: function(plugins) { var p = this._plugins; ([]).concat(plugins).forEach(function(plugin) { if (p.indexOf(plugin) === -1) { p.push(plugin); } }); this._cacheId++; }, /** * Unregisters the given plugin(s) only if registered. * @param {Array|Object} plugins plugin instance(s). */ unregister: function(plugins) { var p = this._plugins; ([]).concat(plugins).forEach(function(plugin) { var idx = p.indexOf(plugin); if (idx !== -1) { p.splice(idx, 1); } }); this._cacheId++; }, /** * Remove all registered plugins. * @since 2.1.5 */ clear: function() { this._plugins = []; this._cacheId++; }, /** * Returns the number of registered plugins? * @returns {Number} * @since 2.1.5 */ count: function() { return this._plugins.length; }, /** * Returns all registered plugin instances. * @returns {Array} array of plugin objects. * @since 2.1.5 */ getAll: function() { return this._plugins; }, /** * Calls enabled plugins for chart, on the specified extension and with the given args. * This method immediately returns as soon as a plugin explicitly returns false. The * returned value can be used, for instance, to interrupt the current action. * @param {Object} chart chart instance for which plugins should be called. * @param {String} extension the name of the plugin method to call (e.g. 'beforeUpdate'). * @param {Array} [args] extra arguments to apply to the extension call. * @returns {Boolean} false if any of the plugins return false, else returns true. */ notify: function(chart, extension, args) { var descriptors = this.descriptors(chart); var ilen = descriptors.length; var i, descriptor, plugin, params, method; for (i=0; i