mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-07 20:59:08 +02:00
Make Chart static properties and methods read only (#8431)
This commit is contained in:
parent
c4bc737080
commit
5baafac8ad
@ -66,6 +66,12 @@ function getCanvas(item) {
|
|||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const instances = {};
|
||||||
|
const getChart = (key) => {
|
||||||
|
const canvas = getCanvas(key);
|
||||||
|
return Object.values(instances).filter((c) => c.canvas === canvas).pop();
|
||||||
|
};
|
||||||
|
|
||||||
class Chart {
|
class Chart {
|
||||||
|
|
||||||
// eslint-disable-next-line max-statements
|
// eslint-disable-next-line max-statements
|
||||||
@ -74,7 +80,7 @@ class Chart {
|
|||||||
|
|
||||||
this.config = config = new Config(config);
|
this.config = config = new Config(config);
|
||||||
const initialCanvas = getCanvas(item);
|
const initialCanvas = getCanvas(item);
|
||||||
const existingChart = Chart.getChart(initialCanvas);
|
const existingChart = getChart(initialCanvas);
|
||||||
if (existingChart) {
|
if (existingChart) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Canvas is already in use. Chart with ID \'' + existingChart.id + '\'' +
|
'Canvas is already in use. Chart with ID \'' + existingChart.id + '\'' +
|
||||||
@ -118,7 +124,7 @@ class Chart {
|
|||||||
this.$context = undefined;
|
this.$context = undefined;
|
||||||
|
|
||||||
// Add the chart instance to the global namespace
|
// Add the chart instance to the global namespace
|
||||||
Chart.instances[me.id] = me;
|
instances[me.id] = me;
|
||||||
|
|
||||||
if (!context || !canvas) {
|
if (!context || !canvas) {
|
||||||
// The given item is not a compatible context2d element, let's return before finalizing
|
// The given item is not a compatible context2d element, let's return before finalizing
|
||||||
@ -840,7 +846,7 @@ class Chart {
|
|||||||
|
|
||||||
me.notifyPlugins('destroy');
|
me.notifyPlugins('destroy');
|
||||||
|
|
||||||
delete Chart.instances[me.id];
|
delete instances[me.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
toBase64Image(...args) {
|
toBase64Image(...args) {
|
||||||
@ -1090,27 +1096,47 @@ class Chart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// These are available to both, UMD and ESM packages
|
|
||||||
Chart.defaults = defaults;
|
|
||||||
Chart.instances = {};
|
|
||||||
Chart.registry = registry;
|
|
||||||
Chart.version = version;
|
|
||||||
|
|
||||||
Chart.getChart = (key) => {
|
|
||||||
const canvas = getCanvas(key);
|
|
||||||
return Object.values(Chart.instances).filter((c) => c.canvas === canvas).pop();
|
|
||||||
};
|
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const invalidatePlugins = () => each(Chart.instances, (chart) => chart._plugins.invalidate());
|
const invalidatePlugins = () => each(Chart.instances, (chart) => chart._plugins.invalidate());
|
||||||
|
|
||||||
Chart.register = (...items) => {
|
const enumerable = true;
|
||||||
registry.add(...items);
|
|
||||||
invalidatePlugins();
|
// These are available to both, UMD and ESM packages. Read Only!
|
||||||
};
|
Object.defineProperties(Chart, {
|
||||||
Chart.unregister = (...items) => {
|
defaults: {
|
||||||
registry.remove(...items);
|
enumerable,
|
||||||
invalidatePlugins();
|
value: defaults
|
||||||
};
|
},
|
||||||
|
instances: {
|
||||||
|
enumerable,
|
||||||
|
value: instances
|
||||||
|
},
|
||||||
|
registry: {
|
||||||
|
enumerable,
|
||||||
|
value: registry
|
||||||
|
},
|
||||||
|
version: {
|
||||||
|
enumerable,
|
||||||
|
value: version
|
||||||
|
},
|
||||||
|
getChart: {
|
||||||
|
enumerable,
|
||||||
|
value: getChart
|
||||||
|
},
|
||||||
|
register: {
|
||||||
|
enumerable,
|
||||||
|
value: (...items) => {
|
||||||
|
registry.add(...items);
|
||||||
|
invalidatePlugins();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
unregister: {
|
||||||
|
enumerable,
|
||||||
|
value: (...items) => {
|
||||||
|
registry.remove(...items);
|
||||||
|
invalidatePlugins();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default Chart;
|
export default Chart;
|
||||||
|
Loading…
Reference in New Issue
Block a user