-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
core.animations.js
227 lines (204 loc) · 5.81 KB
/
core.animations.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import animator from './core.animator.js';
import Animation from './core.animation.js';
import defaults from './core.defaults.js';
import {isArray, isObject, _splitKey} from '../helpers/helpers.core.js';
export default class Animations {
constructor(chart, config) {
this._chart = chart;
this._properties = new Map();
this._pathProperties = new Map();
this.configure(config);
}
configure(config) {
if (!isObject(config)) {
return;
}
const animationOptions = Object.keys(defaults.animation);
const animatedProps = this._properties;
Object.getOwnPropertyNames(config).forEach(key => {
const cfg = config[key];
if (!isObject(cfg)) {
return;
}
const resolved = {};
for (const option of animationOptions) {
resolved[option] = cfg[option];
}
(isArray(cfg.properties) && cfg.properties || [key]).forEach((prop) => {
if (prop === key || !animatedProps.has(prop)) {
animatedProps.set(prop, resolved);
}
});
});
const pathAnimatedProps = this._pathProperties;
loadPathOptions(animatedProps, pathAnimatedProps);
}
/**
* Utility to handle animation of `options`.
* @private
*/
_animateOptions(target, values) {
const newOptions = values.options;
const options = resolveTargetOptions(target, newOptions);
if (!options) {
return [];
}
const animations = this._createAnimations(options, newOptions);
if (newOptions.$shared) {
// Going to shared options:
// After all animations are done, assign the shared options object to the element
// So any new updates to the shared options are observed
awaitAll(target.options.$animations, newOptions).then(() => {
target.options = newOptions;
}, () => {
// rejected, noop
});
}
return animations;
}
/**
* @private
*/
_createAnimations(target, values) {
const animatedProps = this._properties;
const pathAnimatedProps = this._pathProperties;
const animations = [];
const running = target.$animations || (target.$animations = {});
const props = Object.keys(values);
const date = Date.now();
const manageItem = function(tgt, vals, prop, subProp) {
const key = subProp || prop;
const value = vals[key];
let animation = running[prop];
const cfg = animatedProps.get(prop);
if (animation) {
if (cfg && animation.active()) {
// There is an existing active animation, let's update that
animation.update(cfg, value, date);
return;
}
animation.cancel();
}
if (!cfg || !cfg.duration) {
// not animated, set directly to new value
tgt[key] = value;
return;
}
running[prop] = animation = new Animation(cfg, tgt, key, value);
animations.push(animation);
};
let i;
for (i = props.length - 1; i >= 0; --i) {
const prop = props[i];
if (prop.charAt(0) === '$') {
continue;
}
if (prop === 'options') {
animations.push(...this._animateOptions(target, values));
continue;
}
if (pathAnimatedProps.has(prop)) {
pathAnimatedProps.forEach(function(item) {
const newTarget = getInnerObject(target, item);
const newValues = getInnerObject(values, item);
if (newTarget && newValues) {
manageItem(newTarget, newValues, item.prop, item.key);
}
});
} else {
manageItem(target, values, prop);
}
}
return animations;
}
/**
* Update `target` properties to new values, using configured animations
* @param {object} target - object to update
* @param {object} values - new target properties
* @returns {boolean|undefined} - `true` if animations were started
**/
update(target, values) {
if (this._properties.size === 0) {
// Nothing is animated, just apply the new values.
Object.assign(target, values);
return;
}
const animations = this._createAnimations(target, values);
if (animations.length) {
animator.add(this._chart, animations);
return true;
}
}
}
function loadPathOptions(props, pathProps) {
props.forEach(function(v, k) {
const value = parserPathOptions(k);
if (value) {
const mapKey = value.path[0];
const mapValue = pathProps.get(mapKey) || [];
mapValue.push(value);
pathProps.set(mapKey, value);
}
});
}
function awaitAll(animations, properties) {
const running = [];
const keys = Object.keys(properties);
for (let i = 0; i < keys.length; i++) {
const anim = animations[keys[i]];
if (anim && anim.active()) {
running.push(anim.wait());
}
}
// @ts-ignore
return Promise.all(running);
}
function resolveTargetOptions(target, newOptions) {
if (!newOptions) {
return;
}
let options = target.options;
if (!options) {
target.options = newOptions;
return;
}
if (options.$shared) {
// Going from shared options to distinct one:
// Create new options object containing the old shared values and start updating that.
target.options = options = Object.assign({}, options, {$shared: false, $animations: {}});
}
return options;
}
function parserPathOptions(key) {
if (key.includes('.')) {
return parseKeys(key, _splitKey(key));
}
}
function parseKeys(key, keys) {
const result = {
prop: key,
path: []
};
for (let i = 0, n = keys.length; i < n; i++) {
const k = keys[i];
if (!k.trim().length) { // empty string
return;
}
if (i === (n - 1)) {
result.key = k;
} else {
result.path.push(k);
}
}
return result;
}
function getInnerObject(target, pathOpts) {
let obj = target;
for (const p of pathOpts.path) {
obj = obj[p];
if (!isObject(obj)) {
return;
}
}
return obj;
}