Skip to content

Commit

Permalink
Resolve plugin defaults for local plugins (#10484)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jul 24, 2022
1 parent 8ccff8c commit 9ba4480
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/core/core.plugins.js
Expand Up @@ -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++) {
Expand All @@ -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) {
Expand All @@ -146,34 +148,36 @@ 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) {
continue;
}
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
});
}
25 changes: 24 additions & 1 deletion test/specs/core.plugin.tests.js
Expand Up @@ -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() {}},
Expand Down Expand Up @@ -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() {}};
Expand Down

0 comments on commit 9ba4480

Please sign in to comment.