UNPKG

6.45 kBJavaScriptView Raw
1import * as util from '../util';
2import Animation from '../animation';
3import * as math from '../math';
4import * as is from '../is';
5
6let define = {
7
8 animated: function(){
9 return function animatedImpl(){
10 let self = this;
11 let selfIsArrayLike = self.length !== undefined;
12 let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
13 let cy = this._private.cy || this;
14
15 if( !cy.styleEnabled() ){ return false; }
16
17 let ele = all[0];
18
19 if( ele ){
20 return ele._private.animation.current.length > 0;
21 }
22 };
23 }, // animated
24
25 clearQueue: function(){
26 return function clearQueueImpl(){
27 let self = this;
28 let selfIsArrayLike = self.length !== undefined;
29 let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
30 let cy = this._private.cy || this;
31
32 if( !cy.styleEnabled() ){ return this; }
33
34 for( let i = 0; i < all.length; i++ ){
35 let ele = all[ i ];
36 ele._private.animation.queue = [];
37 }
38
39 return this;
40 };
41 }, // clearQueue
42
43 delay: function(){
44 return function delayImpl( time, complete ){
45 let cy = this._private.cy || this;
46
47 if( !cy.styleEnabled() ){ return this; }
48
49 return this.animate( {
50 delay: time,
51 duration: time,
52 complete: complete
53 } );
54 };
55 }, // delay
56
57 delayAnimation: function(){
58 return function delayAnimationImpl( time, complete ){
59 let cy = this._private.cy || this;
60
61 if( !cy.styleEnabled() ){ return this; }
62
63 return this.animation( {
64 delay: time,
65 duration: time,
66 complete: complete
67 } );
68 };
69 }, // delay
70
71 animation: function(){
72 return function animationImpl( properties, params ){
73 let self = this;
74 let selfIsArrayLike = self.length !== undefined;
75 let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
76 let cy = this._private.cy || this;
77 let isCore = !selfIsArrayLike;
78 let isEles = !isCore;
79
80 if( !cy.styleEnabled() ){ return this; }
81
82 let style = cy.style();
83
84 properties = util.assign( {}, properties, params );
85
86 let propertiesEmpty = Object.keys( properties ).length === 0;
87
88 if( propertiesEmpty ){
89 return new Animation( all[0], properties ); // nothing to animate
90 }
91
92 if( properties.duration === undefined ){
93 properties.duration = 400;
94 }
95
96 switch( properties.duration ){
97 case 'slow':
98 properties.duration = 600;
99 break;
100 case 'fast':
101 properties.duration = 200;
102 break;
103 }
104
105 if( isEles ){
106 properties.style = style.getPropsList( properties.style || properties.css );
107
108 properties.css = undefined;
109 }
110
111 if( isEles && properties.renderedPosition != null ){
112 let rpos = properties.renderedPosition;
113 let pan = cy.pan();
114 let zoom = cy.zoom();
115
116 properties.position = math.renderedToModelPosition( rpos, zoom, pan );
117 }
118
119 // override pan w/ panBy if set
120 if( isCore && properties.panBy != null ){
121 let panBy = properties.panBy;
122 let cyPan = cy.pan();
123
124 properties.pan = {
125 x: cyPan.x + panBy.x,
126 y: cyPan.y + panBy.y
127 };
128 }
129
130 // override pan w/ center if set
131 let center = properties.center || properties.centre;
132 if( isCore && center != null ){
133 let centerPan = cy.getCenterPan( center.eles, properties.zoom );
134
135 if( centerPan != null ){
136 properties.pan = centerPan;
137 }
138 }
139
140 // override pan & zoom w/ fit if set
141 if( isCore && properties.fit != null ){
142 let fit = properties.fit;
143 let fitVp = cy.getFitViewport( fit.eles || fit.boundingBox, fit.padding );
144
145 if( fitVp != null ){
146 properties.pan = fitVp.pan;
147 properties.zoom = fitVp.zoom;
148 }
149 }
150
151 // override zoom (& potentially pan) w/ zoom obj if set
152 if( isCore && is.plainObject( properties.zoom ) ){
153 let vp = cy.getZoomedViewport( properties.zoom );
154
155 if( vp != null ){
156 if( vp.zoomed ){ properties.zoom = vp.zoom; }
157
158 if( vp.panned ){ properties.pan = vp.pan; }
159 } else {
160 properties.zoom = null; // an inavalid zoom (e.g. no delta) gets automatically destroyed
161 }
162 }
163
164 return new Animation( all[0], properties );
165 };
166 }, // animate
167
168 animate: function(){
169 return function animateImpl( properties, params ){
170 let self = this;
171 let selfIsArrayLike = self.length !== undefined;
172 let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
173 let cy = this._private.cy || this;
174
175 if( !cy.styleEnabled() ){ return this; }
176
177 if( params ){
178 properties = util.extend( {}, properties, params );
179 }
180
181 // manually hook and run the animation
182 for( let i = 0; i < all.length; i++ ){
183 let ele = all[ i ];
184 let queue = ele.animated() && (properties.queue === undefined || properties.queue);
185
186 let ani = ele.animation( properties, (queue ? { queue: true } : undefined) );
187
188 ani.play();
189 }
190
191 return this; // chaining
192 };
193 }, // animate
194
195 stop: function(){
196 return function stopImpl( clearQueue, jumpToEnd ){
197 let self = this;
198 let selfIsArrayLike = self.length !== undefined;
199 let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
200 let cy = this._private.cy || this;
201
202 if( !cy.styleEnabled() ){ return this; }
203
204 for( let i = 0; i < all.length; i++ ){
205 let ele = all[ i ];
206 let _p = ele._private;
207 let anis = _p.animation.current;
208
209 for( let j = 0; j < anis.length; j++ ){
210 let ani = anis[ j ];
211 let ani_p = ani._private;
212
213 if( jumpToEnd ){
214 // next iteration of the animation loop, the animation
215 // will go straight to the end and be removed
216 ani_p.duration = 0;
217 }
218 }
219
220 // clear the queue of future animations
221 if( clearQueue ){
222 _p.animation.queue = [];
223 }
224
225 if( !jumpToEnd ){
226 _p.animation.current = [];
227 }
228 }
229
230 // we have to notify (the animation loop doesn't do it for us on `stop`)
231 cy.notify('draw');
232
233 return this;
234 };
235 } // stop
236
237}; // define
238
239export default define;