* @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++) {
if (plugins.indexOf(plugin) === -1) {
plugins.push(plugin);
+ localIds[plugin.id] = true;
}
}
- return plugins;
+ return {plugins, localIds};
}
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) {
}
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
+ });
}
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() {}},
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() {}};