]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Plugin system + tests
authorEvert Timberg <evert.timberg+github@gmail.com>
Sun, 17 Apr 2016 16:02:33 +0000 (12:02 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sun, 17 Apr 2016 16:02:33 +0000 (12:02 -0400)
src/chart.js
src/core/core.plugin.js [new file with mode: 0644]
test/core.plugin.tests.js [new file with mode: 0644]

index d0c4fd067b29e9ebd3c29f5284528b1399bb2e52..8f1b428550832bc18f86446c71ce66cb33e6f8da 100644 (file)
@@ -18,6 +18,7 @@ require('./core/core.controller')(Chart);
 require('./core/core.datasetController')(Chart);
 require('./core/core.layoutService')(Chart);
 require('./core/core.legend')(Chart);
+require('./core/core.plugin.js')(Chart);
 require('./core/core.scale')(Chart);
 require('./core/core.scaleService')(Chart);
 require('./core/core.title')(Chart);
diff --git a/src/core/core.plugin.js b/src/core/core.plugin.js
new file mode 100644 (file)
index 0000000..c98cef1
--- /dev/null
@@ -0,0 +1,55 @@
+"use strict";
+
+module.exports = function(Chart) {
+       var helpers = Chart.helpers;
+
+       // Plugins are stored here
+       Chart.plugins = [];
+       Chart.pluginService = {
+               // Register a new plugin
+               register: function(plugin) {
+                       if (Chart.plugins.indexOf(plugin) === -1) {
+                               Chart.plugins.push(plugin);
+                       }
+               },
+
+               // Remove a registered plugin
+               remove: function(plugin) {
+                       var idx = Chart.plugins.indexOf(plugin);
+                       if (idx !== -1) {
+                               Chart.plugins.splice(idx, 1);
+                       }
+               },
+
+               // Iterate over all plugins
+               notifyPlugins: function(method, chartInstance, scope) {
+                       helpers.each(Chart.plugins, function(plugin) {
+                               if (plugin[method] && typeof plugin[method] === 'function') {
+                                       plugin[method].call(scope, chartInstance);
+                               }
+                       }, scope);
+               }
+       };
+
+       Chart.PluginBase = Chart.Element.extend({
+               // Plugin methods. All functions are passed the chart instance
+
+               // Called at start of chart init
+               beforeInit: helpers.noop,
+
+               // Called at end of chart init
+               afterInit: helpers.noop,
+
+               // Called at start of update
+               beforeUpdate: helpers.noop,
+
+               // Called at end of update
+               afterUpdate: helpers.noop,
+
+               // Called at start of draw
+               beforeDraw: helpers.noop,
+
+               // Called at end of draw
+               afterDraw: helpers.noop,
+       });
+};
\ No newline at end of file
diff --git a/test/core.plugin.tests.js b/test/core.plugin.tests.js
new file mode 100644 (file)
index 0000000..4f59c63
--- /dev/null
@@ -0,0 +1,43 @@
+// Plugin tests
+describe('Test the plugin system', function() {
+       beforeEach(function() {
+               Chart.plugins = [];
+       });
+
+       it ('Should register plugins', function() {
+               var myplugin = {};
+               Chart.pluginService.register(myplugin);
+               expect(Chart.plugins.length).toBe(1);
+
+               // Should only add plugin once
+               Chart.pluginService.register(myplugin);
+               expect(Chart.plugins.length).toBe(1);           
+       });
+
+       it ('Should allow unregistering plugins', function() {
+               var myplugin = {};
+               Chart.pluginService.register(myplugin);
+               expect(Chart.plugins.length).toBe(1);
+
+               // Should only add plugin once
+               Chart.pluginService.remove(myplugin);
+               expect(Chart.plugins.length).toBe(0);           
+
+               // Removing a plugin that doesn't exist should not error
+               Chart.pluginService.remove(myplugin);
+       });
+
+       it ('Should allow notifying plugins', function() {
+               var myplugin = {
+                       count: 0,
+                       trigger: function(chart) {
+                               myplugin.count = chart.count;
+                       }
+               };
+               Chart.pluginService.register(myplugin);
+               
+               Chart.pluginService.notifyPlugins('trigger', { count: 10 });
+
+               expect(myplugin.count).toBe(10);
+       });
+});