Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve plugin defaults for local plugins #10484

Merged
merged 1 commit into from Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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