-
Notifications
You must be signed in to change notification settings - Fork 325
/
hooks.js
44 lines (39 loc) · 1.18 KB
/
hooks.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
import {isFunction, callback} from 'chart.js/helpers';
import {loadHooks} from './helpers';
/**
* @typedef { import("chart.js").Chart } Chart
* @typedef { import('../../types/options').AnnotationPluginOptions } AnnotationPluginOptions
* @typedef { import('../../types/element').AnnotationElement } AnnotationElement
*/
export const elementHooks = ['afterDraw', 'beforeDraw'];
/**
* @param {Chart} chart
* @param {Object} state
* @param {AnnotationPluginOptions} options
*/
export function updateHooks(chart, state, options) {
const visibleElements = state.visibleElements;
state.hooked = loadHooks(options, elementHooks, state.hooks);
if (!state.hooked) {
visibleElements.forEach(scope => {
if (!state.hooked) {
elementHooks.forEach(hook => {
if (isFunction(scope.options[hook])) {
state.hooked = true;
}
});
}
});
}
}
/**
* @param {Object} state
* @param {AnnotationElement} element
* @param {string} hook
*/
export function invokeHook(state, element, hook) {
if (state.hooked) {
const callbackHook = element.options[hook] || state.hooks[hook];
return callback(callbackHook, [element.$context]);
}
}