|
1 | 1 | import {Directive, onCheck} from 'angular2/annotations'; |
2 | 2 | import {ElementRef} from 'angular2/core'; |
3 | 3 | 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'; |
5 | 5 | 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'; |
6 | 10 |
|
7 | 11 | @Directive({selector: '[class]', lifecycle: [onCheck], properties: ['rawClass: class']}) |
8 | 12 | export class CSSClass { |
9 | | - _pipe; |
| 13 | + _pipe: Pipe; |
10 | 14 | _rawClass; |
11 | 15 |
|
12 | 16 | constructor(private _pipeRegistry: PipeRegistry, private _ngEl: ElementRef, |
13 | 17 | private _renderer: Renderer) {} |
14 | 18 |
|
15 | 19 | set rawClass(v) { |
16 | | - this._rawClass = v; |
17 | | - this._pipe = this._pipeRegistry.get('keyValDiff', this._rawClass); |
18 | | - } |
| 20 | + this._cleanupClasses(this._rawClass); |
19 | 21 |
|
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); |
22 | 28 | } |
23 | 29 |
|
24 | | - onCheck() { |
| 30 | + onCheck(): void { |
25 | 31 | 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 | + } |
27 | 39 | } |
28 | 40 |
|
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 | + } |
38 | 50 | } |
39 | 51 | } |
| 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 | + } |
40 | 71 | } |
0 commit comments