From: Simon Brunel Date: Fri, 10 Jun 2016 20:26:35 +0000 (+0200) Subject: Rename plugin service and notification method X-Git-Tag: v2.1.5~3^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a419af4c2a9a4190fbe2ef550e20d38505cd648;p=thirdparty%2FChart.js.git Rename plugin service and notification method 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. --- diff --git a/src/chart.js b/src/chart.js index f992a9e1c..75bd47538 100644 --- a/src/chart.js +++ b/src/chart.js @@ -1,3 +1,6 @@ +/** + * @namespace Chart + */ var Chart = require('./core/core.js')(); require('./core/core.helpers')(Chart); diff --git a/src/core/core.controller.js b/src/core/core.controller.js index db521c607..eb472d63c 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -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]; }, diff --git a/src/core/core.legend.js b/src/core/core.legend.js index b3d392c05..78c89c559 100644 --- a/src/core/core.legend.js +++ b/src/core/core.legend.js @@ -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; diff --git a/src/core/core.plugin.js b/src/core/core.plugin.js index 31be4d48b..7126dcd43 100644 --- a/src/core/core.plugin.js +++ b/src/core/core.plugin.js @@ -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; }; diff --git a/src/core/core.title.js b/src/core/core.title.js index b74f6f829..4e00b82b7 100644 --- a/src/core/core.title.js +++ b/src/core/core.title.js @@ -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; diff --git a/test/core.plugin.tests.js b/test/core.plugin.tests.js index 520305e39..62e5e2188 100644 --- a/test/core.plugin.tests.js +++ b/test/core.plugin.tests.js @@ -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); });