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.
+/**
+ * @namespace Chart
+ */
var Chart = require('./core/core.js')();
require('./core/core.helpers')(Chart);
initialize: function initialize() {
var me = this;
// Before init plugin notification
- Chart.pluginService.notifyPlugins('beforeInit', [me]);
+ Chart.plugins.notify('beforeInit', [me]);
me.bindEvents();
me.update();
// After init plugin notification
- Chart.pluginService.notifyPlugins('afterInit', [me]);
+ Chart.plugins.notify('afterInit', [me]);
return me;
},
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) {
// 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) {
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;
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) {
}, 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))) {
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) {
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) {
}
}, 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
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];
},
});
// Register the legend plugin
- Chart.pluginService.register({
+ Chart.plugins.register({
beforeInit: function(chartInstance) {
var opts = chartInstance.options;
var legendOpts = opts.legend;
"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);
}
// 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);
}
}
};
- var noop = helpers.noop;
Chart.PluginBase = Chart.Element.extend({
// Plugin methods. All functions are passed the chart instance
// 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;
};
},
update: function(maxWidth, maxHeight, margins) {
var me = this;
-
+
// Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
me.beforeUpdate();
fontFamily = valueOrDefault(opts.fontFamily, globalDefaults.defaultFontFamily),
titleFont = helpers.fontString(fontSize, fontStyle, fontFamily),
rotation = 0,
- titleX,
+ titleX,
titleY,
top = me.top,
left = me.left,
});
// Register the title plugin
- Chart.pluginService.register({
+ Chart.plugins.register({
beforeInit: function(chartInstance) {
var opts = chartInstance.options;
var titleOpts = opts.title;
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() {
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);
});