From: Simon Brunel Date: Thu, 3 Nov 2016 21:40:47 +0000 (+0100) Subject: Add support for local plugins and plugin options X-Git-Tag: v2.5.0~1^2~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3187a788e17f79ad69da509748aeb64cdbac48f0;p=thirdparty%2FChart.js.git Add support for local plugins and plugin options Plugins can now be declared in the chart `config.plugins` array and will only be applied to the associated chart(s), after the globally registered plugins. Plugin specific options are now scoped under the `config.options.plugins` options. Hooks now receive the chart instance as first argument and the plugin options as last argument. --- diff --git a/src/chart.js b/src/chart.js index 2c5e62826..7c490e7ea 100644 --- a/src/chart.js +++ b/src/chart.js @@ -5,13 +5,13 @@ var Chart = require('./core/core.js')(); require('./core/core.helpers')(Chart); require('./core/core.canvasHelpers')(Chart); +require('./core/core.plugin.js')(Chart); require('./core/core.element')(Chart); require('./core/core.animation')(Chart); require('./core/core.controller')(Chart); require('./core/core.datasetController')(Chart); require('./core/core.layoutService')(Chart); require('./core/core.scaleService')(Chart); -require('./core/core.plugin.js')(Chart); require('./core/core.ticks.js')(Chart); require('./core/core.scale')(Chart); require('./core/core.title')(Chart); diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 61f99b0eb..4e2877388 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -258,7 +258,7 @@ module.exports = function(Chart) { var me = this; // Before init plugin notification - Chart.plugins.notify('beforeInit', [me]); + Chart.plugins.notify(me, 'beforeInit'); me.bindEvents(); @@ -273,7 +273,7 @@ module.exports = function(Chart) { me.update(); // After init plugin notification - Chart.plugins.notify('afterInit', [me]); + Chart.plugins.notify(me, 'afterInit'); return me; }, @@ -315,7 +315,7 @@ module.exports = function(Chart) { if (!silent) { // Notify any plugins about the resize var newSize = {width: newWidth, height: newHeight}; - Chart.plugins.notify('resize', [me, newSize]); + Chart.plugins.notify(me, 'resize', [newSize]); // Notify of resize if (me.options.onResize) { @@ -460,7 +460,7 @@ module.exports = function(Chart) { var me = this; updateConfig(me); - Chart.plugins.notify('beforeUpdate', [me]); + Chart.plugins.notify(me, 'beforeUpdate'); // In case the entire data object changed me.tooltip._data = me.data; @@ -476,7 +476,7 @@ module.exports = function(Chart) { Chart.layoutService.update(me, me.chart.width, me.chart.height); // Apply changes to the datasets that require the scales to have been calculated i.e BorderColor changes - Chart.plugins.notify('afterScaleUpdate', [me]); + Chart.plugins.notify(me, 'afterScaleUpdate'); // Can only reset the new controllers after the scales have been updated helpers.each(newControllers, function(controller) { @@ -486,7 +486,7 @@ module.exports = function(Chart) { me.updateDatasets(); // Do this before render so that any plugins that need final scale updates can use it - Chart.plugins.notify('afterUpdate', [me]); + Chart.plugins.notify(me, 'afterUpdate'); if (me._bufferedRender) { me._bufferedRequest = { @@ -530,18 +530,18 @@ module.exports = function(Chart) { var me = this; var i, ilen; - if (Chart.plugins.notify('beforeDatasetsUpdate', [me])) { + if (Chart.plugins.notify(me, 'beforeDatasetsUpdate')) { for (i = 0, ilen = me.data.datasets.length; i < ilen; ++i) { me.getDatasetMeta(i).controller.update(); } - Chart.plugins.notify('afterDatasetsUpdate', [me]); + Chart.plugins.notify(me, 'afterDatasetsUpdate'); } }, render: function(duration, lazy) { var me = this; - Chart.plugins.notify('beforeRender', [me]); + Chart.plugins.notify(me, 'beforeRender'); var animationOptions = me.options.animation; if (animationOptions && ((typeof duration !== 'undefined' && duration !== 0) || (typeof duration === 'undefined' && animationOptions.duration !== 0))) { @@ -577,7 +577,7 @@ module.exports = function(Chart) { var easingDecimal = ease || 1; me.clear(); - Chart.plugins.notify('beforeDraw', [me, easingDecimal]); + Chart.plugins.notify(me, 'beforeDraw', [easingDecimal]); // Draw all the scales helpers.each(me.boxes, function(box) { @@ -587,7 +587,7 @@ module.exports = function(Chart) { me.scale.draw(); } - Chart.plugins.notify('beforeDatasetsDraw', [me, easingDecimal]); + Chart.plugins.notify(me, 'beforeDatasetsDraw', [easingDecimal]); // Draw each dataset via its respective controller (reversed to support proper line stacking) helpers.each(me.data.datasets, function(dataset, datasetIndex) { @@ -596,12 +596,12 @@ module.exports = function(Chart) { } }, me, true); - Chart.plugins.notify('afterDatasetsDraw', [me, easingDecimal]); + Chart.plugins.notify(me, 'afterDatasetsDraw', [easingDecimal]); // Finally draw the tooltip me.tooltip.transition(easingDecimal).draw(); - Chart.plugins.notify('afterDraw', [me, easingDecimal]); + Chart.plugins.notify(me, 'afterDraw', [easingDecimal]); }, // Get the single element that was clicked on @@ -701,7 +701,7 @@ module.exports = function(Chart) { me.chart.ctx = null; } - Chart.plugins.notify('destroy', [me]); + Chart.plugins.notify(me, 'destroy'); delete Chart.instances[me.id]; }, diff --git a/src/core/core.plugin.js b/src/core/core.plugin.js index fe6ac31f1..657c85a35 100644 --- a/src/core/core.plugin.js +++ b/src/core/core.plugin.js @@ -2,7 +2,10 @@ module.exports = function(Chart) { - var noop = Chart.helpers.noop; + var helpers = Chart.helpers; + var noop = helpers.noop; + + Chart.defaults.global.plugins = {}; /** * The plugin service singleton @@ -10,8 +13,20 @@ module.exports = function(Chart) { * @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). @@ -23,6 +38,8 @@ module.exports = function(Chart) { p.push(plugin); } }); + + this._cacheId++; }, /** @@ -37,6 +54,8 @@ module.exports = function(Chart) { p.splice(idx, 1); } }); + + this._cacheId++; }, /** @@ -45,6 +64,7 @@ module.exports = function(Chart) { */ clear: function() { this._plugins = []; + this._cacheId++; }, /** @@ -66,28 +86,78 @@ module.exports = function(Chart) { }, /** - * Calls registered plugins on the specified extension, with the given args. This - * method immediately returns as soon as a plugin explicitly returns false. The + * 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(extension, args) { - var plugins = this._plugins; - var ilen = plugins.length; - var i, plugin; + notify: function(chart, extension, args) { + var descriptors = this.descriptors(chart); + var ilen = descriptors.length; + var i, descriptor, plugin, params, method; for (i=0; i