@@ -81,10 +81,20 @@ var $AnimateProvider = ['$provide', function($provide) {
81
81
return this . $$classNameFilter ;
82
82
} ;
83
83
84
- this . $get = [ '$timeout' , '$$asyncCallback' , function ( $timeout , $$asyncCallback ) {
85
-
86
- function async ( fn ) {
87
- fn && $$asyncCallback ( fn ) ;
84
+ this . $get = [ '$$q' , '$$asyncCallback' , function ( $$q , $$asyncCallback ) {
85
+
86
+ var currentDefer ;
87
+ function asyncPromise ( ) {
88
+ // only serve one instance of a promise in order to save CPU cycles
89
+ if ( ! currentDefer ) {
90
+ currentDefer = $$q . defer ( ) ;
91
+ currentDefer . promise . cancel = noop ; //ngAnimate.$animate provides this
92
+ $$asyncCallback ( function ( ) {
93
+ currentDefer . resolve ( ) ;
94
+ currentDefer = null ;
95
+ } ) ;
96
+ }
97
+ return currentDefer . promise ;
88
98
}
89
99
90
100
/**
@@ -112,39 +122,34 @@ var $AnimateProvider = ['$provide', function($provide) {
112
122
* @name $animate#enter
113
123
* @kind function
114
124
* @description Inserts the element into the DOM either after the `after` element or
115
- * as the first child within the `parent` element. Once complete, the done() callback
116
- * will be fired (if provided) .
125
+ * as the first child within the `parent` element. When the function is called a promise
126
+ * is returned that will be resolved at a later time .
117
127
* @param {DOMElement } element the element which will be inserted into the DOM
118
128
* @param {DOMElement } parent the parent element which will append the element as
119
129
* a child (if the after element is not present)
120
130
* @param {DOMElement } after the sibling element which will append the element
121
131
* after itself
122
- * @param {Function= } done callback function that will be called after the element has been
123
- * inserted into the DOM
132
+ * @return {Promise } the animation callback promise
124
133
*/
125
- enter : function ( element , parent , after , done ) {
126
- after
127
- ? after . after ( element )
128
- : parent . prepend ( element ) ;
129
- async ( done ) ;
130
- return noop ;
134
+ enter : function ( element , parent , after ) {
135
+ after ? after . after ( element )
136
+ : parent . prepend ( element ) ;
137
+ return asyncPromise ( ) ;
131
138
} ,
132
139
133
140
/**
134
141
*
135
142
* @ngdoc method
136
143
* @name $animate#leave
137
144
* @kind function
138
- * @description Removes the element from the DOM. Once complete, the done() callback will be
139
- * fired (if provided) .
145
+ * @description Removes the element from the DOM. When the function is called a promise
146
+ * is returned that will be resolved at a later time .
140
147
* @param {DOMElement } element the element which will be removed from the DOM
141
- * @param {Function= } done callback function that will be called after the element has been
142
- * removed from the DOM
148
+ * @return {Promise } the animation callback promise
143
149
*/
144
- leave : function ( element , done ) {
150
+ leave : function ( element ) {
145
151
element . remove ( ) ;
146
- async ( done ) ;
147
- return noop ;
152
+ return asyncPromise ( ) ;
148
153
} ,
149
154
150
155
/**
@@ -153,46 +158,43 @@ var $AnimateProvider = ['$provide', function($provide) {
153
158
* @name $animate#move
154
159
* @kind function
155
160
* @description Moves the position of the provided element within the DOM to be placed
156
- * either after the `after` element or inside of the `parent` element. Once complete, the
157
- * done() callback will be fired (if provided) .
161
+ * either after the `after` element or inside of the `parent` element. When the function
162
+ * is called a promise is returned that will be resolved at a later time .
158
163
*
159
164
* @param {DOMElement } element the element which will be moved around within the
160
165
* DOM
161
166
* @param {DOMElement } parent the parent element where the element will be
162
167
* inserted into (if the after element is not present)
163
168
* @param {DOMElement } after the sibling element where the element will be
164
169
* positioned next to
165
- * @param {Function= } done the callback function (if provided) that will be fired after the
166
- * element has been moved to its new position
170
+ * @return {Promise } the animation callback promise
167
171
*/
168
- move : function ( element , parent , after , done ) {
172
+ move : function ( element , parent , after ) {
169
173
// Do not remove element before insert. Removing will cause data associated with the
170
174
// element to be dropped. Insert will implicitly do the remove.
171
- return this . enter ( element , parent , after , done ) ;
175
+ return this . enter ( element , parent , after ) ;
172
176
} ,
173
177
174
178
/**
175
179
*
176
180
* @ngdoc method
177
181
* @name $animate#addClass
178
182
* @kind function
179
- * @description Adds the provided className CSS class value to the provided element. Once
180
- * complete, the done() callback will be fired (if provided) .
183
+ * @description Adds the provided className CSS class value to the provided element.
184
+ * When the function is called a promise is returned that will be resolved at a later time .
181
185
* @param {DOMElement } element the element which will have the className value
182
186
* added to it
183
187
* @param {string } className the CSS class which will be added to the element
184
- * @param {Function= } done the callback function (if provided) that will be fired after the
185
- * className value has been added to the element
188
+ * @return {Promise } the animation callback promise
186
189
*/
187
- addClass : function ( element , className , done ) {
190
+ addClass : function ( element , className ) {
188
191
className = ! isString ( className )
189
192
? ( isArray ( className ) ? className . join ( ' ' ) : '' )
190
193
: className ;
191
194
forEach ( element , function ( element ) {
192
195
jqLiteAddClass ( element , className ) ;
193
196
} ) ;
194
- async ( done ) ;
195
- return noop ;
197
+ return asyncPromise ( ) ;
196
198
} ,
197
199
198
200
/**
@@ -201,22 +203,20 @@ var $AnimateProvider = ['$provide', function($provide) {
201
203
* @name $animate#removeClass
202
204
* @kind function
203
205
* @description Removes the provided className CSS class value from the provided element.
204
- * Once complete, the done() callback will be fired (if provided) .
206
+ * When the function is called a promise is returned that will be resolved at a later time .
205
207
* @param {DOMElement } element the element which will have the className value
206
208
* removed from it
207
209
* @param {string } className the CSS class which will be removed from the element
208
- * @param {Function= } done the callback function (if provided) that will be fired after the
209
- * className value has been removed from the element
210
+ * @return {Promise } the animation callback promise
210
211
*/
211
- removeClass : function ( element , className , done ) {
212
- className = isString ( className ) ?
213
- className :
214
- isArray ( className ) ? className . join ( ' ' ) : '' ;
212
+ removeClass : function ( element , className ) {
213
+ className = ! isString ( className )
214
+ ? ( isArray ( className ) ? className . join ( ' ' ) : '' )
215
+ : className ;
215
216
forEach ( element , function ( element ) {
216
217
jqLiteRemoveClass ( element , className ) ;
217
218
} ) ;
218
- async ( done ) ;
219
- return noop ;
219
+ return asyncPromise ( ) ;
220
220
} ,
221
221
222
222
/**
@@ -225,22 +225,21 @@ var $AnimateProvider = ['$provide', function($provide) {
225
225
* @name $animate#setClass
226
226
* @kind function
227
227
* @description Adds and/or removes the given CSS classes to and from the element.
228
- * Once complete, the done() callback will be fired (if provided) .
228
+ * When the function is called a promise is returned that will be resolved at a later time .
229
229
* @param {DOMElement } element the element which will have its CSS classes changed
230
230
* removed from it
231
231
* @param {string } add the CSS classes which will be added to the element
232
232
* @param {string } remove the CSS class which will be removed from the element
233
- * @param {Function= } done the callback function (if provided) that will be fired after the
234
- * CSS classes have been set on the element
233
+ * @return {Promise } the animation callback promise
235
234
*/
236
- setClass : function ( element , add , remove , done ) {
235
+ setClass : function ( element , add , remove ) {
237
236
this . addClass ( element , add ) ;
238
237
this . removeClass ( element , remove ) ;
239
- async ( done ) ;
240
- return noop ;
238
+ return asyncPromise ( ) ;
241
239
} ,
242
240
243
- enabled : noop
241
+ enabled : noop ,
242
+ cancel : noop
244
243
} ;
245
244
} ] ;
246
245
} ] ;
0 commit comments