-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
core.plugins.js
159 lines (136 loc) · 4.18 KB
/
core.plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import defaults from './core.defaults';
import registry from './core.registry';
import {isNullOrUndef} from '../helpers';
import {callback as callCallback, mergeIf, valueOrDefault} from '../helpers/helpers.core';
/**
* @typedef { import("./core.controller").default } Chart
* @typedef { import("../platform/platform.base").ChartEvent } ChartEvent
* @typedef { import("../plugins/plugin.tooltip").default } Tooltip
*/
export default class PluginService {
constructor() {
this._init = [];
}
/**
* Calls enabled plugins for `chart` on the specified hook and with the given args.
* This method immediately returns as soon as a plugin explicitly returns false. The
* returned value can be used, for instance, to interrupt the current action.
* @param {Chart} chart - The chart instance for which plugins should be called.
* @param {string} hook - The name of the plugin method to call (e.g. 'beforeUpdate').
* @param {object} [args] - Extra arguments to apply to the hook call.
* @returns {boolean} false if any of the plugins return false, else returns true.
*/
notify(chart, hook, args) {
const me = this;
if (hook === 'beforeInit') {
me._init = me._createDescriptors(chart, true);
me._notify(me._init, chart, 'install');
}
const descriptors = me._descriptors(chart);
const result = me._notify(descriptors, chart, hook, args);
if (hook === 'destroy') {
me._notify(descriptors, chart, 'stop');
me._notify(me._init, chart, 'uninstall');
}
return result;
}
/**
* @private
*/
_notify(descriptors, chart, hook, args) {
args = args || {};
for (const descriptor of descriptors) {
const plugin = descriptor.plugin;
const method = plugin[hook];
const params = [chart, args, descriptor.options];
if (callCallback(method, params, plugin) === false && args.cancelable) {
return false;
}
}
return true;
}
invalidate() {
// When plugins are registered, there is the possibility of a double
// invalidate situation. In this case, we only want to invalidate once.
// If we invalidate multiple times, the `_oldCache` is lost and all of the
// plugins are restarted without being correctly stopped.
// See https://github.com/chartjs/Chart.js/issues/8147
if (!isNullOrUndef(this._cache)) {
this._oldCache = this._cache;
this._cache = undefined;
}
}
/**
* @param {Chart} chart
* @private
*/
_descriptors(chart) {
if (this._cache) {
return this._cache;
}
const descriptors = this._cache = this._createDescriptors(chart);
this._notifyStateChanges(chart);
return descriptors;
}
_createDescriptors(chart, all) {
const config = chart && chart.config;
const options = valueOrDefault(config.options && config.options.plugins, {});
const plugins = allPlugins(config);
// options === false => all plugins are disabled
return options === false && !all ? [] : createDescriptors(plugins, options, all);
}
/**
* @param {Chart} chart
* @private
*/
_notifyStateChanges(chart) {
const previousDescriptors = this._oldCache || [];
const descriptors = this._cache;
const diff = (a, b) => a.filter(x => !b.some(y => x.plugin.id === y.plugin.id));
this._notify(diff(previousDescriptors, descriptors), chart, 'stop');
this._notify(diff(descriptors, previousDescriptors), chart, 'start');
}
}
/**
* @param {import("./core.config").default} config
*/
function allPlugins(config) {
const plugins = [];
const keys = Object.keys(registry.plugins.items);
for (let i = 0; i < keys.length; i++) {
plugins.push(registry.getPlugin(keys[i]));
}
const local = config.plugins || [];
for (let i = 0; i < local.length; i++) {
const plugin = local[i];
if (plugins.indexOf(plugin) === -1) {
plugins.push(plugin);
}
}
return plugins;
}
function getOpts(options, all) {
if (!all && options === false) {
return null;
}
if (options === true) {
return {};
}
return options;
}
function createDescriptors(plugins, options, all) {
const result = [];
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i];
const id = plugin.id;
const opts = getOpts(options[id], all);
if (opts === null) {
continue;
}
result.push({
plugin,
options: mergeIf({}, [opts, defaults.plugins[id]])
});
}
return result;
}