OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. All rights reserved. | 2 * Copyright 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style | 4 * Use of this source code is governed by a BSD-style |
5 * license that can be found in the LICENSE file or at | 5 * license that can be found in the LICENSE file or at |
6 * https://developers.google.com/open-source/licenses/bsd | 6 * https://developers.google.com/open-source/licenses/bsd |
7 */ | 7 */ |
8 part of charted.selection.transition; | 8 part of charted.selection.transition; |
9 | 9 |
10 // handle transitions on an element-basis, so we can cancel if another is | 10 // handle transitions on an element-basis, so we can cancel if another is |
11 // scheduled | 11 // scheduled |
12 Map<Element, int> _transitionMap = {}; | 12 Map<Element, int> _transitionMap = {}; |
13 | 13 |
14 class _TransitionImpl implements Transition { | 14 class _TransitionImpl implements Transition { |
15 SelectionCallback _delay = (d, i, c) => 0; | 15 SelectionCallback _delay = (d, i, c) => 0; |
16 SelectionCallback _duration = (d, i, c) => | 16 SelectionCallback _duration = |
17 Transition.defaultDurationMilliseconds; | 17 (d, i, c) => Transition.defaultDurationMilliseconds; |
18 Selection _selection; | 18 Selection _selection; |
19 Map _attrs = {}; | 19 Map _attrs = {}; |
20 Map _styles = {}; | 20 Map _styles = {}; |
21 Map _attrTweens = {}; | 21 Map _attrTweens = {}; |
22 Map _styleTweens = {}; | 22 Map _styleTweens = {}; |
23 Map<AnimationTimer, Element> _timerMap = {}; | 23 Map<AnimationTimer, Element> _timerMap = {}; |
24 Map<Element, List<Map>> _attrMap = {}; | 24 Map<Element, List<Interpolator>> _attrMap = {}; |
25 Map<Element, int> _durationMap = {}; | 25 Map<Element, int> _durationMap = {}; |
26 bool _interrupted = false; | 26 bool _interrupted = false; |
27 bool _remove = false; | 27 bool _remove = false; |
28 var _timerDelay = 0; | 28 var _timerDelay = 0; |
29 | 29 |
30 _TransitionImpl(this._selection, [num delay = 0]) { | 30 _TransitionImpl(this._selection, [num delay = 0]) { |
31 _transitionNode(delay); | 31 _transitionNode(delay); |
32 _timerDelay = delay; | 32 _timerDelay = delay; |
33 } | 33 } |
34 | 34 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 void styleTween(String property, StyleTweenCallback tween, | 75 void styleTween(String property, StyleTweenCallback tween, |
76 [String priority]) { | 76 [String priority]) { |
77 _styleTweens[property] = {'callback': tween, 'priority': priority}; | 77 _styleTweens[property] = {'callback': tween, 'priority': priority}; |
78 } | 78 } |
79 | 79 |
80 // Starts a timer that registers all attributes, durations, and delays for the | 80 // Starts a timer that registers all attributes, durations, and delays for the |
81 // transition of the current selection. | 81 // transition of the current selection. |
82 _transitionNode(num delay) { | 82 _transitionNode(num delay) { |
83 new AnimationTimer((elapsed) { | 83 new AnimationTimer((elapsed) { |
84 _selection.each((d, i, c) { | 84 _selection.each((d, i, c) { |
85 var tweenList = []; | 85 var tweenList = <Interpolator>[]; |
86 _attrs.forEach((key, value) { | 86 _attrs.forEach((key, value) { |
87 tweenList.add(_getAttrInterpolator(c, key, value(d, i, c))); | 87 tweenList.add(_getAttrInterpolator(c, key, value(d, i, c))); |
88 }); | 88 }); |
89 _attrTweens.forEach((key, value) { | 89 _attrTweens.forEach((key, value) { |
90 tweenList.add( | 90 tweenList.add( |
91 (t) => c.setAttribute(key, value(d, i, c.getAttribute(key))(t))); | 91 (t) => c.setAttribute(key, value(d, i, c.getAttribute(key))(t))); |
92 }); | 92 }); |
93 _styles.forEach((key, value) { | 93 _styles.forEach((key, value) { |
94 tweenList.add(_getStyleInterpolator( | 94 tweenList.add(_getStyleInterpolator( |
95 c, key, value['callback'](d, i, c), value['priority'])); | 95 c, key, value['callback'](d, i, c), value['priority'])); |
(...skipping 14 matching lines...) Expand all Loading... |
110 _transitionMap[c] = 1; | 110 _transitionMap[c] = 1; |
111 } else { | 111 } else { |
112 _transitionMap[c]++; | 112 _transitionMap[c]++; |
113 } | 113 } |
114 }); | 114 }); |
115 return true; | 115 return true; |
116 }, delay: delay); | 116 }, delay: delay); |
117 } | 117 } |
118 | 118 |
119 // Returns the correct interpolator function for the old and new attribute. | 119 // Returns the correct interpolator function for the old and new attribute. |
120 _getAttrInterpolator(Element element, String attrName, newValue) { | 120 Interpolator _getAttrInterpolator( |
| 121 Element element, String attrName, newValue) { |
121 var attr = element.attributes[attrName]; | 122 var attr = element.attributes[attrName]; |
122 var interpolator = createStringInterpolator(attr, newValue.toString()); | 123 var interpolator = createStringInterpolator(attr, newValue.toString()); |
123 return (t) => element.setAttribute(attrName, interpolator(t).toString()); | 124 return (t) => element.setAttribute(attrName, interpolator(t).toString()); |
124 } | 125 } |
125 | 126 |
126 // Returns the correct interpolator function for the old and new style. | 127 // Returns the correct interpolator function for the old and new style. |
127 _getStyleInterpolator(Element element, String styleName, newValue, priority) { | 128 Interpolator _getStyleInterpolator( |
| 129 Element element, String styleName, newValue, priority) { |
128 var style = element.style.getPropertyValue(styleName); | 130 var style = element.style.getPropertyValue(styleName); |
129 | 131 |
130 var interpolator = createStringInterpolator(style, newValue.toString()); | 132 var interpolator = createStringInterpolator(style, newValue.toString()); |
131 | 133 |
132 return (t) => element.style | 134 return (t) => element.style |
133 .setProperty(styleName, interpolator(t).toString(), priority); | 135 .setProperty(styleName, interpolator(t).toString(), priority); |
134 } | 136 } |
135 | 137 |
136 // Ticks of the transition, this is the callback registered to the | 138 // Ticks of the transition, this is the callback registered to the |
137 // ChartedTimer, called on each animation frame until the transition duration | 139 // ChartedTimer, called on each animation frame until the transition duration |
138 // has been reached. | 140 // has been reached. |
139 bool _tick(elapsed) { | 141 bool _tick(int elapsed) { |
140 if (_interrupted) { | 142 if (_interrupted) { |
141 return true; | 143 return true; |
142 } | 144 } |
143 var activeNode = _timerMap[AnimationTimer.active]; | 145 var activeNode = _timerMap[AnimationTimer.active]; |
144 var t = elapsed / _durationMap[activeNode]; | 146 var t = elapsed / _durationMap[activeNode]; |
145 for (Interpolator tween in _attrMap[activeNode]) { | 147 for (Interpolator tween in _attrMap[activeNode]) { |
146 tween(ease(t)); | 148 tween(ease(t)); |
147 } | 149 } |
148 | 150 |
149 if (t >= 1) { | 151 if (t >= 1) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 var t = new _TransitionImpl(_selection, delay); | 194 var t = new _TransitionImpl(_selection, delay); |
193 t.ease = ease; | 195 t.ease = ease; |
194 t.durationWithCallback(_duration); | 196 t.durationWithCallback(_duration); |
195 return t; | 197 return t; |
196 } | 198 } |
197 | 199 |
198 void remove() { | 200 void remove() { |
199 _remove = true; | 201 _remove = true; |
200 } | 202 } |
201 } | 203 } |
OLD | NEW |