mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
PluginService as ES6 class (#7075)
* PluginService as ES6 class * Review update
This commit is contained in:
parent
9c5a1aabce
commit
f5655c511e
@ -22,12 +22,13 @@ defaults._set('plugins', {});
|
|||||||
* @namespace Chart.plugins
|
* @namespace Chart.plugins
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
*/
|
*/
|
||||||
export default {
|
class PluginService {
|
||||||
|
constructor() {
|
||||||
/**
|
/**
|
||||||
* Globally registered plugins.
|
* Globally registered plugins.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_plugins: [],
|
this._plugins = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This identifier is used to invalidate the descriptors cache attached to each chart
|
* This identifier is used to invalidate the descriptors cache attached to each chart
|
||||||
@ -35,13 +36,14 @@ export default {
|
|||||||
* incremented and descriptors are regenerated during following API calls.
|
* incremented and descriptors are regenerated during following API calls.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_cacheId: 0,
|
this._cacheId = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the given plugin(s) if not already registered.
|
* Registers the given plugin(s) if not already registered.
|
||||||
* @param {IPlugin[]|IPlugin} plugins plugin instance(s).
|
* @param {IPlugin[]|IPlugin} plugins plugin instance(s).
|
||||||
*/
|
*/
|
||||||
register: function(plugins) {
|
register(plugins) {
|
||||||
var p = this._plugins;
|
var p = this._plugins;
|
||||||
([]).concat(plugins).forEach(function(plugin) {
|
([]).concat(plugins).forEach(function(plugin) {
|
||||||
if (p.indexOf(plugin) === -1) {
|
if (p.indexOf(plugin) === -1) {
|
||||||
@ -50,13 +52,13 @@ export default {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this._cacheId++;
|
this._cacheId++;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters the given plugin(s) only if registered.
|
* Unregisters the given plugin(s) only if registered.
|
||||||
* @param {IPlugin[]|IPlugin} plugins plugin instance(s).
|
* @param {IPlugin[]|IPlugin} plugins plugin instance(s).
|
||||||
*/
|
*/
|
||||||
unregister: function(plugins) {
|
unregister(plugins) {
|
||||||
var p = this._plugins;
|
var p = this._plugins;
|
||||||
([]).concat(plugins).forEach(function(plugin) {
|
([]).concat(plugins).forEach(function(plugin) {
|
||||||
var idx = p.indexOf(plugin);
|
var idx = p.indexOf(plugin);
|
||||||
@ -66,34 +68,34 @@ export default {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this._cacheId++;
|
this._cacheId++;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all registered plugins.
|
* Remove all registered plugins.
|
||||||
* @since 2.1.5
|
* @since 2.1.5
|
||||||
*/
|
*/
|
||||||
clear: function() {
|
clear() {
|
||||||
this._plugins = [];
|
this._plugins = [];
|
||||||
this._cacheId++;
|
this._cacheId++;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of registered plugins?
|
* Returns the number of registered plugins?
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
* @since 2.1.5
|
* @since 2.1.5
|
||||||
*/
|
*/
|
||||||
count: function() {
|
count() {
|
||||||
return this._plugins.length;
|
return this._plugins.length;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all registered plugin instances.
|
* Returns all registered plugin instances.
|
||||||
* @returns {IPlugin[]} array of plugin objects.
|
* @returns {IPlugin[]} array of plugin objects.
|
||||||
* @since 2.1.5
|
* @since 2.1.5
|
||||||
*/
|
*/
|
||||||
getAll: function() {
|
getAll() {
|
||||||
return this._plugins;
|
return this._plugins;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls enabled plugins for `chart` on the specified hook and with the given args.
|
* Calls enabled plugins for `chart` on the specified hook and with the given args.
|
||||||
@ -104,7 +106,7 @@ export default {
|
|||||||
* @param {Array} [args] - Extra arguments to apply to the hook call.
|
* @param {Array} [args] - Extra arguments to apply to the hook call.
|
||||||
* @returns {boolean} false if any of the plugins return false, else returns true.
|
* @returns {boolean} false if any of the plugins return false, else returns true.
|
||||||
*/
|
*/
|
||||||
notify: function(chart, hook, args) {
|
notify(chart, hook, args) {
|
||||||
var descriptors = this.descriptors(chart);
|
var descriptors = this.descriptors(chart);
|
||||||
var ilen = descriptors.length;
|
var ilen = descriptors.length;
|
||||||
var i, descriptor, plugin, params, method;
|
var i, descriptor, plugin, params, method;
|
||||||
@ -123,14 +125,14 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns descriptors of enabled plugins for the given chart.
|
* Returns descriptors of enabled plugins for the given chart.
|
||||||
* @returns {object[]} [{ plugin, options }]
|
* @returns {object[]} [{ plugin, options }]
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
descriptors: function(chart) {
|
descriptors(chart) {
|
||||||
var cache = chart.$plugins || (chart.$plugins = {});
|
var cache = chart.$plugins || (chart.$plugins = {});
|
||||||
if (cache.id === this._cacheId) {
|
if (cache.id === this._cacheId) {
|
||||||
return cache.descriptors;
|
return cache.descriptors;
|
||||||
@ -167,7 +169,7 @@ export default {
|
|||||||
cache.descriptors = descriptors;
|
cache.descriptors = descriptors;
|
||||||
cache.id = this._cacheId;
|
cache.id = this._cacheId;
|
||||||
return descriptors;
|
return descriptors;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invalidates cache for the given chart: descriptors hold a reference on plugin option,
|
* Invalidates cache for the given chart: descriptors hold a reference on plugin option,
|
||||||
@ -175,10 +177,13 @@ export default {
|
|||||||
* https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
|
* https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_invalidate: function(chart) {
|
_invalidate(chart) {
|
||||||
delete chart.$plugins;
|
delete chart.$plugins;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// singleton instance
|
||||||
|
export default new PluginService();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin extension hooks.
|
* Plugin extension hooks.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user