/
core.element.js
119 lines (93 loc) · 2.4 KB
/
core.element.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
'use strict';
import color from 'chartjs-color';
import helpers from '../helpers/index';
import {isNumber} from '../helpers/helpers.math';
function interpolate(start, view, model, ease) {
var keys = Object.keys(model);
var i, ilen, key, actual, origin, target, type, c0, c1;
for (i = 0, ilen = keys.length; i < ilen; ++i) {
key = keys[i];
target = model[key];
// if a value is added to the model after pivot() has been called, the view
// doesn't contain it, so let's initialize the view to the target value.
if (!Object.prototype.hasOwnProperty.call(view, key)) {
view[key] = target;
}
actual = view[key];
if (actual === target || key[0] === '_') {
continue;
}
if (!Object.prototype.hasOwnProperty.call(start, key)) {
start[key] = actual;
}
origin = start[key];
type = typeof target;
if (type === typeof origin) {
if (type === 'string') {
c0 = color(origin);
if (c0.valid) {
c1 = color(target);
if (c1.valid) {
view[key] = c1.mix(c0, ease).rgbString();
continue;
}
}
} else if (helpers.isFinite(origin) && helpers.isFinite(target)) {
view[key] = origin + (target - origin) * ease;
continue;
}
}
view[key] = target;
}
}
class Element {
constructor(configuration) {
helpers.extend(this, configuration);
// this.hidden = false; we assume Element has an attribute called hidden, but do not initialize to save memory
}
pivot(animationsDisabled) {
var me = this;
if (animationsDisabled) {
me._view = me._model;
return me;
}
if (!me._view) {
me._view = helpers.extend({}, me._model);
}
me._start = {};
return me;
}
transition(ease) {
var me = this;
var model = me._model;
var start = me._start;
var view = me._view;
// No animation -> No Transition
if (!model || ease === 1) {
// _model has to be cloned to _view
// Otherwise, when _model properties are set on hover, _view.* is also set to the same value, and hover animation doesn't occur
me._view = helpers.extend({}, model);
me._start = null;
return me;
}
if (!view) {
view = me._view = {};
}
if (!start) {
start = me._start = {};
}
interpolate(start, view, model, ease);
return me;
}
tooltipPosition() {
return {
x: this._model.x,
y: this._model.y
};
}
hasValue() {
return isNumber(this._model.x) && isNumber(this._model.y);
}
}
Element.extend = helpers.inherits;
export default Element;