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

Commit c44fc6d

Browse files
matskoIgorMinar
authored andcommitted
refactor($animate): use $animate.$$setClassImmediately to save code
1 parent 1efaf3d commit c44fc6d

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/ng/animate.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ var $AnimateProvider = ['$provide', function($provide) {
100100
return defer.promise;
101101
}
102102

103-
function resolveElementClasses(element, cache) {
103+
function resolveElementClasses(element, classes) {
104104
var toAdd = [], toRemove = [];
105105

106106
var hasClasses = createMap();
107107
forEach((element.attr('class') || '').split(/\s+/), function(className) {
108108
hasClasses[className] = true;
109109
});
110110

111-
forEach(cache.classes, function(status, className) {
111+
forEach(classes, function(status, className) {
112112
var hasClass = hasClasses[className];
113113

114114
// If the most recent class manipulation (via $animate) was to remove the class, and the
@@ -288,20 +288,12 @@ var $AnimateProvider = ['$provide', function($provide) {
288288
* @param {string} remove the CSS class which will be removed from the element
289289
* @return {Promise} the animation callback promise
290290
*/
291-
setClass : function(element, add, remove, runSynchronously) {
291+
setClass : function(element, add, remove) {
292292
var self = this;
293293
var STORAGE_KEY = '$$animateClasses';
294294
var createdCache = false;
295295
element = jqLite(element);
296296

297-
if (runSynchronously) {
298-
// TODO(@caitp/@matsko): Remove undocumented `runSynchronously` parameter, and always
299-
// perform DOM manipulation asynchronously or in postDigest.
300-
self.$$addClassImmediately(element, add);
301-
self.$$removeClassImmediately(element, remove);
302-
return asyncPromise();
303-
}
304-
305297
var cache = element.data(STORAGE_KEY);
306298
if (!cache) {
307299
cache = {
@@ -322,11 +314,14 @@ var $AnimateProvider = ['$provide', function($provide) {
322314
var cache = element.data(STORAGE_KEY);
323315
element.removeData(STORAGE_KEY);
324316

325-
var classes = cache && resolveElementClasses(element, cache);
326-
327-
if (classes) {
328-
if (classes[0]) self.$$addClassImmediately(element, classes[0]);
329-
if (classes[1]) self.$$removeClassImmediately(element, classes[1]);
317+
// in the event that the element is removed before postDigest
318+
// is run then the cache will be undefined and there will be
319+
// no need anymore to add or remove and of the element classes
320+
if (cache) {
321+
var classes = resolveElementClasses(element, cache.classes);
322+
if (classes) {
323+
self.$$setClassImmediately(element, classes[0], classes[1]);
324+
}
330325
}
331326

332327
done();
@@ -337,6 +332,12 @@ var $AnimateProvider = ['$provide', function($provide) {
337332
return cache.promise;
338333
},
339334

335+
$$setClassImmediately : function(element, add, remove) {
336+
add && this.$$addClassImmediately(element, add);
337+
remove && this.$$removeClassImmediately(element, remove);
338+
return asyncPromise();
339+
},
340+
340341
enabled : noop,
341342
cancel : noop
342343
};

src/ngAnimate/animate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ angular.module('ngAnimate', ['ng'])
10001000
// TODO(@caitp/@matsko): Don't use private/undocumented API here --- we should not be
10011001
// changing the DOM synchronously in this case. The `true` parameter must eventually be
10021002
// removed.
1003-
return $delegate.setClass(element, add, remove, true);
1003+
return $delegate.$$setClassImmediately(element, add, remove);
10041004
}
10051005

10061006
// we're using a combined array for both the add and remove

0 commit comments

Comments
 (0)