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

fix(ngClassOdd/Even): proper update with filtering/ordering #1264

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,38 @@
function classDirective(name, selector) {
name = 'ngClass' + name;
return ngDirective(function(scope, element, attr) {
if (selector !== true) {
scope.$watch("$index", function(newVal, oldVal) {
if (newVal !== oldVal && ((newVal + oldVal) % 2 === 1)) {
if (newVal % 2 !== selector) {
removeClass(scope.$eval(attr[name]));
} else {
addClass(scope.$eval(attr[name]));
}
}
}, true);
}

scope.$watch(attr[name], function(newVal, oldVal) {
if (selector === true || scope.$index % 2 === selector) {
if (oldVal && (newVal !== oldVal)) {
if (isObject(oldVal) && !isArray(oldVal))
oldVal = map(oldVal, function(v, k) { if (v) return k });
element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal);
}
if (isObject(newVal) && !isArray(newVal))
newVal = map(newVal, function(v, k) { if (v) return k });
if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); }
removeClass(oldVal);
}
addClass(newVal);
}
}, true);

function removeClass(classVal) {
if (isObject(classVal) && !isArray(classVal))
classVal = map(classVal, function(v, k) { if (v) return k });
element.removeClass(isArray(classVal) ? classVal.join(' ') : classVal);
}

function addClass(classVal) {
if (isObject(classVal) && !isArray(classVal))
classVal = map(classVal, function(v, k) { if (v) return k });
if (classVal) element.addClass(isArray(classVal) ? classVal.join(' ') : classVal);
}
});
}

Expand Down
44 changes: 44 additions & 0 deletions test/ng/directive/ngClassSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,48 @@ describe('ngClass', function() {
expect(e2.hasClass('C')).toBeFalsy();
expect(e2.hasClass('D')).toBeFalsy();
}));


it('should update ngClassOdd/Even when model is changed by filtering', inject(function($rootScope, $compile) {
element = $compile('<ul>' +
'<li ng-repeat="i in items" ' +
'ng-class-odd="\'odd\'" ng-class-even="\'even\'"></li>' +
'<ul>')($rootScope);
$rootScope.items = ['a','b','a'];
$rootScope.$digest();

$rootScope.items = ['a','a'];
$rootScope.$digest();

var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);

expect(e1.hasClass('odd')).toBeTruthy();
expect(e1.hasClass('even')).toBeFalsy();

expect(e2.hasClass('even')).toBeTruthy();
expect(e2.hasClass('odd')).toBeFalsy();
}));


it('should update ngClassOdd/Even when model is changed by sorting', inject(function($rootScope, $compile) {
element = $compile('<ul>' +
'<li ng-repeat="i in items" ' +
'ng-class-odd="\'odd\'" ng-class-even="\'even\'">i</li>' +
'<ul>')($rootScope);
$rootScope.items = ['a','b'];
$rootScope.$digest();

$rootScope.items = ['b','a'];
$rootScope.$digest();

var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);

expect(e1.hasClass('odd')).toBeTruthy();
expect(e1.hasClass('even')).toBeFalsy();

expect(e2.hasClass('even')).toBeTruthy();
expect(e2.hasClass('odd')).toBeFalsy();
}));
});