-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathelements.js
137 lines (118 loc) · 4.36 KB
/
elements.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
import {Animations} from 'chart.js';
import {defined, isObject} from 'chart.js/helpers';
import {annotationTypes} from './types';
const directUpdater = {
update: Object.assign
};
/**
* @typedef { import("chart.js").Chart } Chart
* @typedef { import("chart.js").UpdateMode } UpdateMode
* @typedef { import('../../types/options').AnnotationPluginOptions } AnnotationPluginOptions
*/
/**
* Resolve the annotation type, checking if is supported.
* @param {string} [type=line] - annotation type
* @returns {string} resolved annotation type
*/
export function resolveType(type = 'line') {
if (annotationTypes[type]) {
return type;
}
console.warn(`Unknown annotation type: '${type}', defaulting to 'line'`);
return 'line';
}
/**
* @param {Chart} chart
* @param {Object} state
* @param {AnnotationPluginOptions} options
* @param {UpdateMode} mode
*/
export function updateElements(chart, state, options, mode) {
const animations = resolveAnimations(chart, options.animations, mode);
const annotations = state.annotations;
const elements = resyncElements(state.elements, annotations);
for (let i = 0; i < annotations.length; i++) {
const annotationOptions = annotations[i];
const element = getOrCreateElement(elements, i, annotationOptions.type);
const resolver = annotationOptions.setContext(getContext(chart, element, annotationOptions));
const properties = element.resolveElementProperties(chart, resolver);
properties.skip = toSkip(properties);
if ('elements' in properties) {
updateSubElements(element, properties, resolver, animations);
// Remove the sub-element definitions from properties, so the actual elements
// are not overwritten by their definitions
delete properties.elements;
}
if (!defined(element.x)) {
// If the element is newly created, assing the properties directly - to
// make them readily awailable to any scriptable options. If we do not do this,
// the properties retruned by `resolveElementProperties` are available only
// after options resolution.
Object.assign(element, properties);
}
properties.options = resolveOptions(resolver);
animations.update(element, properties);
}
}
function toSkip(properties) {
return isNaN(properties.x) || isNaN(properties.y);
}
function resolveAnimations(chart, animOpts, mode) {
if (mode === 'reset' || mode === 'none' || mode === 'resize') {
return directUpdater;
}
return new Animations(chart, animOpts);
}
function updateSubElements(mainElement, {elements, initProperties}, resolver, animations) {
const subElements = mainElement.elements || (mainElement.elements = []);
subElements.length = elements.length;
for (let i = 0; i < elements.length; i++) {
const definition = elements[i];
const properties = definition.properties;
const subElement = getOrCreateElement(subElements, i, definition.type, initProperties);
const subResolver = resolver[definition.optionScope].override(definition);
properties.options = resolveOptions(subResolver);
animations.update(subElement, properties);
}
}
function getOrCreateElement(elements, index, type, initProperties) {
const elementClass = annotationTypes[resolveType(type)];
let element = elements[index];
if (!element || !(element instanceof elementClass)) {
element = elements[index] = new elementClass();
if (isObject(initProperties)) {
Object.assign(element, initProperties);
}
}
return element;
}
function resolveOptions(resolver, path = []) {
const result = {};
for (const key of Object.getOwnPropertyNames(resolver)) {
if (path.includes(key)) {
// TODO: this is slow, should figure out why the keys start looping instead.
continue;
}
const value = resolver[key];
result[key] = isObject(value) ? resolveOptions(value, [...path, key]) : value;
}
return result;
}
function getContext(chart, element, annotation) {
return element.$context || (element.$context = Object.assign(Object.create(chart.getContext()), {
element,
id: annotation.id,
type: 'annotation'
}));
}
function resyncElements(elements, annotations) {
const count = annotations.length;
const start = elements.length;
if (start < count) {
const add = count - start;
elements.splice(start, 0, ...new Array(add));
} else if (start > count) {
elements.splice(count, start - count);
}
return elements;
}