From 9ba44809745a77855485dc44af6e0c9574226446 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Sun, 24 Jul 2022 17:56:48 +0300 Subject: [PATCH] Resolve plugin defaults for local plugins (#10484) --- src/core/core.plugins.js | 30 +++++++++++++++++------------- test/specs/core.plugin.tests.js | 25 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/core/core.plugins.js b/src/core/core.plugins.js index 05d1e5c84fb..c1693ca1267 100644 --- a/src/core/core.plugins.js +++ b/src/core/core.plugins.js @@ -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 + }); } diff --git a/test/specs/core.plugin.tests.js b/test/specs/core.plugin.tests.js index 285dbab5b54..836ead0fca3 100644 --- a/test/specs/core.plugin.tests.js +++ b/test/specs/core.plugin.tests.js @@ -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() {}};