File size: 6,448 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
import * as util from '../util';
import Animation from '../animation';
import * as math from '../math';
import * as is from '../is';

let define = {

  animated: function(){
    return function animatedImpl(){
      let self = this;
      let selfIsArrayLike = self.length !== undefined;
      let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
      let cy = this._private.cy || this;

      if( !cy.styleEnabled() ){ return false; }

      let ele = all[0];

      if( ele ){
        return ele._private.animation.current.length > 0;
      }
    };
  }, // animated

  clearQueue: function(){
    return function clearQueueImpl(){
      let self = this;
      let selfIsArrayLike = self.length !== undefined;
      let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
      let cy = this._private.cy || this;

      if( !cy.styleEnabled() ){ return this; }

      for( let i = 0; i < all.length; i++ ){
        let ele = all[ i ];
        ele._private.animation.queue = [];
      }

      return this;
    };
  }, // clearQueue

  delay: function(){
    return function delayImpl( time, complete ){
      let cy = this._private.cy || this;

      if( !cy.styleEnabled() ){ return this; }

      return this.animate( {
        delay: time,
        duration: time,
        complete: complete
      } );
    };
  }, // delay

  delayAnimation: function(){
    return function delayAnimationImpl( time, complete ){
      let cy = this._private.cy || this;

      if( !cy.styleEnabled() ){ return this; }

      return this.animation( {
        delay: time,
        duration: time,
        complete: complete
      } );
    };
  }, // delay

  animation: function(){
    return function animationImpl( properties, params ){
      let self = this;
      let selfIsArrayLike = self.length !== undefined;
      let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
      let cy = this._private.cy || this;
      let isCore = !selfIsArrayLike;
      let isEles = !isCore;

      if( !cy.styleEnabled() ){ return this; }

      let style = cy.style();

      properties = util.assign( {}, properties, params );

      let propertiesEmpty = Object.keys( properties ).length === 0;

      if( propertiesEmpty ){
        return new Animation( all[0], properties ); // nothing to animate
      }

      if( properties.duration === undefined ){
        properties.duration = 400;
      }

      switch( properties.duration ){
      case 'slow':
        properties.duration = 600;
        break;
      case 'fast':
        properties.duration = 200;
        break;
      }

      if( isEles ){
        properties.style = style.getPropsList( properties.style || properties.css );

        properties.css = undefined;
      }

      if( isEles && properties.renderedPosition != null ){
        let rpos = properties.renderedPosition;
        let pan = cy.pan();
        let zoom = cy.zoom();

        properties.position = math.renderedToModelPosition( rpos, zoom, pan );
      }

      // override pan w/ panBy if set
      if( isCore && properties.panBy != null ){
        let panBy = properties.panBy;
        let cyPan = cy.pan();

        properties.pan = {
          x: cyPan.x + panBy.x,
          y: cyPan.y + panBy.y
        };
      }

      // override pan w/ center if set
      let center = properties.center || properties.centre;
      if( isCore && center != null ){
        let centerPan = cy.getCenterPan( center.eles, properties.zoom );

        if( centerPan != null ){
          properties.pan = centerPan;
        }
      }

      // override pan & zoom w/ fit if set
      if( isCore && properties.fit != null ){
        let fit = properties.fit;
        let fitVp = cy.getFitViewport( fit.eles || fit.boundingBox, fit.padding );

        if( fitVp != null ){
          properties.pan = fitVp.pan;
          properties.zoom = fitVp.zoom;
        }
      }

      // override zoom (& potentially pan) w/ zoom obj if set
      if( isCore && is.plainObject( properties.zoom ) ){
        let vp = cy.getZoomedViewport( properties.zoom );

        if( vp != null ){
          if( vp.zoomed ){ properties.zoom = vp.zoom; }

          if( vp.panned ){ properties.pan = vp.pan; }
        } else {
          properties.zoom = null; // an inavalid zoom (e.g. no delta) gets automatically destroyed
        }
      }

      return new Animation( all[0], properties );
    };
  }, // animate

  animate: function(){
    return function animateImpl( properties, params ){
      let self = this;
      let selfIsArrayLike = self.length !== undefined;
      let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
      let cy = this._private.cy || this;

      if( !cy.styleEnabled() ){ return this; }

      if( params ){
        properties = util.extend( {}, properties, params );
      }

      // manually hook and run the animation
      for( let i = 0; i < all.length; i++ ){
        let ele = all[ i ];
        let queue = ele.animated() && (properties.queue === undefined || properties.queue);

        let ani = ele.animation( properties, (queue ? { queue: true } : undefined) );

        ani.play();
      }

      return this; // chaining
    };
  }, // animate

  stop: function(){
    return function stopImpl( clearQueue, jumpToEnd ){
      let self = this;
      let selfIsArrayLike = self.length !== undefined;
      let all = selfIsArrayLike ? self : [ self ]; // put in array if not array-like
      let cy = this._private.cy || this;

      if( !cy.styleEnabled() ){ return this; }

      for( let i = 0; i < all.length; i++ ){
        let ele = all[ i ];
        let _p = ele._private;
        let anis = _p.animation.current;

        for( let j = 0; j < anis.length; j++ ){
          let ani = anis[ j ];
          let ani_p = ani._private;

          if( jumpToEnd ){
            // next iteration of the animation loop, the animation
            // will go straight to the end and be removed
            ani_p.duration = 0;
          }
        }

        // clear the queue of future animations
        if( clearQueue ){
          _p.animation.queue = [];
        }

        if( !jumpToEnd ){
          _p.animation.current = [];
        }
      }

      // we have to notify (the animation loop doesn't do it for us on `stop`)
      cy.notify('draw');

      return this;
    };
  } // stop

}; // define

export default define;