Skip to content

Commit 8c993dc

Browse files
pkozlowski-opensourcevicb
authored andcommitted
feat(CSSClass): add support for string and array expresions
Closes angular#2025
1 parent 2c11205 commit 8c993dc

File tree

2 files changed

+242
-80
lines changed

2 files changed

+242
-80
lines changed
Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,71 @@
11
import {Directive, onCheck} from 'angular2/annotations';
22
import {ElementRef} from 'angular2/core';
33
import {PipeRegistry} from 'angular2/src/change_detection/pipes/pipe_registry';
4-
import {isPresent} from 'angular2/src/facade/lang';
4+
import {Pipe} from 'angular2/src/change_detection/pipes/pipe';
55
import {Renderer} from 'angular2/src/render/api';
6+
import {KeyValueChanges} from 'angular2/src/change_detection/pipes/keyvalue_changes';
7+
import {IterableChanges} from 'angular2/src/change_detection/pipes/iterable_changes';
8+
import {isPresent, isString, StringWrapper} from 'angular2/src/facade/lang';
9+
import {ListWrapper, StringMapWrapper, isListLikeIterable} from 'angular2/src/facade/collection';
610

711
@Directive({selector: '[class]', lifecycle: [onCheck], properties: ['rawClass: class']})
812
export class CSSClass {
9-
_pipe;
13+
_pipe: Pipe;
1014
_rawClass;
1115

1216
constructor(private _pipeRegistry: PipeRegistry, private _ngEl: ElementRef,
1317
private _renderer: Renderer) {}
1418

1519
set rawClass(v) {
16-
this._rawClass = v;
17-
this._pipe = this._pipeRegistry.get('keyValDiff', this._rawClass);
18-
}
20+
this._cleanupClasses(this._rawClass);
1921

20-
_toggleClass(className, enabled): void {
21-
this._renderer.setElementClass(this._ngEl, className, enabled);
22+
if (isString(v)) {
23+
v = v.split(' ');
24+
}
25+
26+
this._rawClass = v;
27+
this._pipe = this._pipeRegistry.get(isListLikeIterable(v) ? 'iterableDiff' : 'keyValDiff', v);
2228
}
2329

24-
onCheck() {
30+
onCheck(): void {
2531
var diff = this._pipe.transform(this._rawClass);
26-
if (isPresent(diff)) this._applyChanges(diff.wrapped);
32+
if (isPresent(diff) && isPresent(diff.wrapped)) {
33+
if (diff.wrapped instanceof IterableChanges) {
34+
this._applyArrayChanges(diff.wrapped);
35+
} else {
36+
this._applyObjectChanges(diff.wrapped);
37+
}
38+
}
2739
}
2840

29-
private _applyChanges(diff) {
30-
if (isPresent(diff)) {
31-
diff.forEachAddedItem((record) => { this._toggleClass(record.key, record.currentValue); });
32-
diff.forEachChangedItem((record) => { this._toggleClass(record.key, record.currentValue); });
33-
diff.forEachRemovedItem((record) => {
34-
if (record.previousValue) {
35-
this._toggleClass(record.key, false);
36-
}
37-
});
41+
private _cleanupClasses(rawClassVal): void {
42+
if (isPresent(rawClassVal)) {
43+
if (isListLikeIterable(rawClassVal)) {
44+
ListWrapper.forEach(rawClassVal, (className) => { this._toggleClass(className, false); });
45+
} else {
46+
StringMapWrapper.forEach(rawClassVal, (expVal, className) => {
47+
if (expVal) this._toggleClass(className, false);
48+
});
49+
}
3850
}
3951
}
52+
53+
private _applyObjectChanges(diff: KeyValueChanges): void {
54+
diff.forEachAddedItem((record) => { this._toggleClass(record.key, record.currentValue); });
55+
diff.forEachChangedItem((record) => { this._toggleClass(record.key, record.currentValue); });
56+
diff.forEachRemovedItem((record) => {
57+
if (record.previousValue) {
58+
this._toggleClass(record.key, false);
59+
}
60+
});
61+
}
62+
63+
private _applyArrayChanges(diff: IterableChanges): void {
64+
diff.forEachAddedItem((record) => { this._toggleClass(record.item, true); });
65+
diff.forEachRemovedItem((record) => { this._toggleClass(record.item, false); });
66+
}
67+
68+
private _toggleClass(className: string, enabled): void {
69+
this._renderer.setElementClass(this._ngEl, className, enabled);
70+
}
4071
}

0 commit comments

Comments
 (0)