Skip to content

Commit 2866d87

Browse files
committed
Add remove method to Transition.
1 parent b10d5c9 commit 2866d87

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
build/
22
*.swp
33
packages
4+
.packages
45
.pub
56
pubspec.lock
67
.idea

lib/selection/src/transition_impl.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*/
88
part of charted.selection.transition;
99

10+
// handle transitions on an element-basis, so we can cancel if another is
11+
// scheduled
12+
Map<Element, int> _transitionMap = {};
13+
1014
class _TransitionImpl implements Transition {
1115
SelectionCallback _delay = (d, i, c) => 0;
1216
SelectionCallback _duration =
@@ -20,6 +24,7 @@ class _TransitionImpl implements Transition {
2024
Map<Element, List<Map>> _attrMap = {};
2125
Map<Element, int> _durationMap = {};
2226
bool _interrupted = false;
27+
bool _remove = false;
2328
var _timerDelay = 0;
2429

2530
_TransitionImpl(this._selection, [num delay = 0]) {
@@ -98,6 +103,12 @@ class _TransitionImpl implements Transition {
98103
_attrMap[c] = tweenList;
99104
_durationMap[c] = _duration(d, i, c);
100105
_timerMap[new AnimationTimer(_tick, delay: _delay(d, i, c))] = c;
106+
107+
if(!_transitionMap.containsKey(c)) {
108+
_transitionMap[c] = 1;
109+
} else {
110+
_transitionMap[c]++;
111+
}
101112
});
102113
return true;
103114
}, delay: delay);
@@ -132,7 +143,22 @@ class _TransitionImpl implements Transition {
132143
for (Interpolator tween in _attrMap[activeNode]) {
133144
tween(ease(t));
134145
}
135-
return (t >= 1) ? true : false;
146+
147+
if (t >= 1) {
148+
if (_remove && _transitionMap[activeNode] == 1) {
149+
activeNode.remove();
150+
}
151+
152+
if(_transitionMap[activeNode] > 1) {
153+
_transitionMap[activeNode]--;
154+
} else {
155+
_transitionMap.remove(activeNode);
156+
}
157+
158+
return true;
159+
}
160+
161+
return false;
136162
}
137163

138164
// Interrupts the transition.
@@ -165,4 +191,8 @@ class _TransitionImpl implements Transition {
165191
t.durationWithCallback(_duration);
166192
return t;
167193
}
194+
195+
void remove() {
196+
_remove = true;
197+
}
168198
}

lib/selection/transition.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ abstract class Transition {
160160
*/
161161
Transition transition();
162162

163+
/**
164+
* Removes all selected elements from the DOM at the end of the transition.
165+
* If any of the selected elements have another transition scheduled when
166+
* this transition ends, said elements will not be removed.
167+
*/
168+
void remove();
169+
163170
/** Factory method to create an instance of the default implementation */
164171
factory Transition(Selection selection) => new _TransitionImpl(selection);
165172
}

0 commit comments

Comments
 (0)