Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit cae3239

Browse files
committed
feat($animate): allow $animate to pass custom styles into animations
$animate now supports an optional parameter which provides CSS styling which will be provided into the CSS-based animations as well as any custom animation functions. Once the animation is complete then the styles will be applied directly to the element. If no animation is detected or the `ngAnimate` module is not active then the styles will be applied immediately. BREAKING CHANGE: staggering animations that use transitions will now always block the transition from starting (via `transition: 0s none`) up until the stagger step kicks in. The former behaviour was that the block was removed as soon as the pending class was added. This fix allows for styles to be applied in the pending class without causing an animation to trigger prematurely.
1 parent 28133cb commit cae3239

File tree

4 files changed

+564
-69
lines changed

4 files changed

+564
-69
lines changed

src/ng/animate.js

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ var $AnimateProvider = ['$provide', function($provide) {
122122
}
123123
});
124124

125-
return (toAdd.length + toRemove.length) > 0 && [toAdd.length && toAdd, toRemove.length && toRemove];
125+
return (toAdd.length + toRemove.length) > 0 &&
126+
[toAdd.length ? toAdd : null, toRemove.length ? toRemove : null];
126127
}
127128

128129
function cachedClassManipulation(cache, classes, op) {
@@ -144,6 +145,17 @@ var $AnimateProvider = ['$provide', function($provide) {
144145
return currentDefer.promise;
145146
}
146147

148+
function applyStyles(element, options) {
149+
if (angular.isObject(options)) {
150+
if (options.from || options.to) {
151+
options = extend(options.from || {}, options.to || {});
152+
} else {
153+
delete options.tempClasses;
154+
}
155+
element.css(options);
156+
}
157+
}
158+
147159
/**
148160
*
149161
* @ngdoc service
@@ -176,9 +188,11 @@ var $AnimateProvider = ['$provide', function($provide) {
176188
* a child (if the after element is not present)
177189
* @param {DOMElement} after the sibling element which will append the element
178190
* after itself
191+
* @param {object=} options an optional collection of styles that will be applied to the element.
179192
* @return {Promise} the animation callback promise
180193
*/
181-
enter : function(element, parent, after) {
194+
enter : function(element, parent, after, options) {
195+
applyStyles(element, options);
182196
after ? after.after(element)
183197
: parent.prepend(element);
184198
return asyncPromise();
@@ -192,9 +206,10 @@ var $AnimateProvider = ['$provide', function($provide) {
192206
* @description Removes the element from the DOM. When the function is called a promise
193207
* is returned that will be resolved at a later time.
194208
* @param {DOMElement} element the element which will be removed from the DOM
209+
* @param {object=} options an optional collection of options that will be applied to the element.
195210
* @return {Promise} the animation callback promise
196211
*/
197-
leave : function(element) {
212+
leave : function(element, options) {
198213
element.remove();
199214
return asyncPromise();
200215
},
@@ -214,12 +229,13 @@ var $AnimateProvider = ['$provide', function($provide) {
214229
* inserted into (if the after element is not present)
215230
* @param {DOMElement} after the sibling element where the element will be
216231
* positioned next to
232+
* @param {object=} options an optional collection of options that will be applied to the element.
217233
* @return {Promise} the animation callback promise
218234
*/
219-
move : function(element, parent, after) {
235+
move : function(element, parent, after, options) {
220236
// Do not remove element before insert. Removing will cause data associated with the
221237
// element to be dropped. Insert will implicitly do the remove.
222-
return this.enter(element, parent, after);
238+
return this.enter(element, parent, after, options);
223239
},
224240

225241
/**
@@ -232,20 +248,23 @@ var $AnimateProvider = ['$provide', function($provide) {
232248
* @param {DOMElement} element the element which will have the className value
233249
* added to it
234250
* @param {string} className the CSS class which will be added to the element
251+
* @param {object=} options an optional collection of options that will be applied to the element.
235252
* @return {Promise} the animation callback promise
236253
*/
237-
addClass : function(element, className) {
238-
return this.setClass(element, className, []);
254+
addClass : function(element, className, options) {
255+
return this.setClass(element, className, [], options);
239256
},
240257

241-
$$addClassImmediately : function(element, className) {
258+
$$addClassImmediately : function(element, className, options) {
242259
element = jqLite(element);
243260
className = !isString(className)
244261
? (isArray(className) ? className.join(' ') : '')
245262
: className;
246263
forEach(element, function (element) {
247264
jqLiteAddClass(element, className);
248265
});
266+
applyStyles(element, options);
267+
return asyncPromise();
249268
},
250269

251270
/**
@@ -258,20 +277,22 @@ var $AnimateProvider = ['$provide', function($provide) {
258277
* @param {DOMElement} element the element which will have the className value
259278
* removed from it
260279
* @param {string} className the CSS class which will be removed from the element
280+
* @param {object=} options an optional collection of options that will be applied to the element.
261281
* @return {Promise} the animation callback promise
262282
*/
263-
removeClass : function(element, className) {
264-
return this.setClass(element, [], className);
283+
removeClass : function(element, className, options) {
284+
return this.setClass(element, [], className, options);
265285
},
266286

267-
$$removeClassImmediately : function(element, className) {
287+
$$removeClassImmediately : function(element, className, options) {
268288
element = jqLite(element);
269289
className = !isString(className)
270290
? (isArray(className) ? className.join(' ') : '')
271291
: className;
272292
forEach(element, function (element) {
273293
jqLiteRemoveClass(element, className);
274294
});
295+
applyStyles(element, options);
275296
return asyncPromise();
276297
},
277298

@@ -286,9 +307,10 @@ var $AnimateProvider = ['$provide', function($provide) {
286307
* removed from it
287308
* @param {string} add the CSS classes which will be added to the element
288309
* @param {string} remove the CSS class which will be removed from the element
310+
* @param {object=} options an optional collection of options that will be applied to the element.
289311
* @return {Promise} the animation callback promise
290312
*/
291-
setClass : function(element, add, remove) {
313+
setClass : function(element, add, remove, options) {
292314
var self = this;
293315
var STORAGE_KEY = '$$animateClasses';
294316
var createdCache = false;
@@ -297,9 +319,12 @@ var $AnimateProvider = ['$provide', function($provide) {
297319
var cache = element.data(STORAGE_KEY);
298320
if (!cache) {
299321
cache = {
300-
classes: {}
322+
classes: {},
323+
options : options
301324
};
302325
createdCache = true;
326+
} else if (options && cache.options) {
327+
cache.options = angular.extend(cache.options || {}, options);
303328
}
304329

305330
var classes = cache.classes;
@@ -320,7 +345,7 @@ var $AnimateProvider = ['$provide', function($provide) {
320345
if (cache) {
321346
var classes = resolveElementClasses(element, cache.classes);
322347
if (classes) {
323-
self.$$setClassImmediately(element, classes[0], classes[1]);
348+
self.$$setClassImmediately(element, classes[0], classes[1], cache.options);
324349
}
325350
}
326351

@@ -332,9 +357,10 @@ var $AnimateProvider = ['$provide', function($provide) {
332357
return cache.promise;
333358
},
334359

335-
$$setClassImmediately : function(element, add, remove) {
360+
$$setClassImmediately : function(element, add, remove, options) {
336361
add && this.$$addClassImmediately(element, add);
337362
remove && this.$$removeClassImmediately(element, remove);
363+
applyStyles(element, options);
338364
return asyncPromise();
339365
},
340366

0 commit comments

Comments
 (0)