]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Resolve plugin defaults for local plugins (#10484)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sun, 24 Jul 2022 14:56:48 +0000 (17:56 +0300)
committerGitHub <noreply@github.com>
Sun, 24 Jul 2022 14:56:48 +0000 (17:56 +0300)
src/core/core.plugins.js
test/specs/core.plugin.tests.js

index 05d1e5c84fbe7b3823f6ff49ac9f3b9ab5a912de..c1693ca1267ca134f2569d7c7ae5081c350b79be 100644 (file)
@@ -118,6 +118,7 @@ export default class PluginService {
  * @param {import("./core.config").default} config
  */
 function allPlugins(config) {
+  const localIds = {};
   const plugins = [];
   const keys = Object.keys(registry.plugins.items);
   for (let i = 0; i < keys.length; i++) {
@@ -130,10 +131,11 @@ function allPlugins(config) {
 
     if (plugins.indexOf(plugin) === -1) {
       plugins.push(plugin);
+      localIds[plugin.id] = true;
     }
   }
 
-  return plugins;
+  return {plugins, localIds};
 }
 
 function getOpts(options, all) {
@@ -146,12 +148,11 @@ function getOpts(options, all) {
   return options;
 }
 
-function createDescriptors(chart, plugins, options, all) {
+function createDescriptors(chart, {plugins, localIds}, options, all) {
   const result = [];
   const context = chart.getContext();
 
-  for (let i = 0; i < plugins.length; i++) {
-    const plugin = plugins[i];
+  for (const plugin of plugins) {
     const id = plugin.id;
     const opts = getOpts(options[id], all);
     if (opts === null) {
@@ -159,21 +160,24 @@ function createDescriptors(chart, plugins, options, all) {
     }
     result.push({
       plugin,
-      options: pluginOpts(chart.config, plugin, opts, context)
+      options: pluginOpts(chart.config, {plugin, local: localIds[id]}, opts, context)
     });
   }
 
   return result;
 }
 
-/**
- * @param {import("./core.config").default} config
- * @param {*} plugin
- * @param {*} opts
- * @param {*} context
- */
-function pluginOpts(config, plugin, opts, context) {
+function pluginOpts(config, {plugin, local}, opts, context) {
   const keys = config.pluginScopeKeys(plugin);
   const scopes = config.getOptionScopes(opts, keys);
-  return config.createResolver(scopes, context, [''], {scriptable: false, indexable: false, allKeys: true});
+  if (local && plugin.defaults) {
+    // make sure plugin defaults are in scopes for local (not registered) plugins
+    scopes.push(plugin.defaults);
+  }
+  return config.createResolver(scopes, context, [''], {
+    // These are just defaults that plugins can override
+    scriptable: false,
+    indexable: false,
+    allKeys: true
+  });
 }
index 285dbab5b54d2c9c5e02234d5a8a7b1ae98720e0..836ead0fca3fa66357adb5f672ff2157a3eea04c 100644 (file)
@@ -224,7 +224,7 @@ describe('Chart.plugins', function() {
       Chart.unregister(plugins.a);
     });
 
-    it('should not called plugins when config.options.plugins.{id} is FALSE', function() {
+    it('should not call plugins when config.options.plugins.{id} is FALSE', function() {
       var plugins = {
         a: {id: 'a', hook: function() {}},
         b: {id: 'b', hook: function() {}},
@@ -297,6 +297,29 @@ describe('Chart.plugins', function() {
       Chart.unregister(plugin);
     });
 
+    // https://github.com/chartjs/Chart.js/issues/10482
+    it('should resolve defaults for local plugins', function() {
+      var plugin = {id: 'a', hook: function() {}, defaults: {bar: 'bar'}};
+      var chart = window.acquireChart({
+        plugins: [plugin],
+        options: {
+          plugins: {
+            a: {
+              foo: 'foo'
+            }
+          }
+        },
+      });
+
+      spyOn(plugin, 'hook');
+      chart.notifyPlugins('hook');
+
+      expect(plugin.hook).toHaveBeenCalled();
+      expect(plugin.hook.calls.first().args[2]).toEqualOptions({foo: 'foo', bar: 'bar'});
+
+      Chart.unregister(plugin);
+    });
+
     // https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
     it('should update plugin options', function() {
       var plugin = {id: 'a', hook: function() {}};