2015-07-31 12:23:50 -07:00
|
|
|
import {isPresent, isString, StringWrapper, isBlank} from 'angular2/src/facade/lang';
|
2015-07-04 15:04:50 +02:00
|
|
|
import {Directive, LifecycleEvent} from 'angular2/annotations';
|
2015-05-20 17:19:46 -07:00
|
|
|
import {ElementRef} from 'angular2/core';
|
2015-06-23 14:26:02 -07:00
|
|
|
import {Renderer} from 'angular2/src/render/api';
|
2015-07-31 12:23:50 -07:00
|
|
|
import {
|
|
|
|
KeyValueDiffer,
|
|
|
|
IterableDiffer,
|
|
|
|
IterableDiffers,
|
|
|
|
KeyValueDiffers
|
|
|
|
} from 'angular2/change_detection';
|
2015-06-21 11:54:21 +02:00
|
|
|
import {ListWrapper, StringMapWrapper, isListLikeIterable} from 'angular2/src/facade/collection';
|
2015-03-26 17:51:08 +01:00
|
|
|
|
2015-06-19 09:41:58 +02:00
|
|
|
/**
|
|
|
|
* Adds and removes CSS classes based on an {expression} value.
|
|
|
|
*
|
|
|
|
* The result of expression is used to add and remove CSS classes using the following logic,
|
|
|
|
* based on expression's value type:
|
|
|
|
* - {string} - all the CSS classes (space - separated) are added
|
|
|
|
* - {Array} - all the CSS classes (Array elements) are added
|
|
|
|
* - {Object} - each key corresponds to a CSS class name while values
|
|
|
|
* are interpreted as {boolean} expression. If a given expression
|
|
|
|
* evaluates to {true} a corresponding CSS class is added - otherwise
|
|
|
|
* it is removed.
|
|
|
|
*
|
|
|
|
* # Example:
|
|
|
|
*
|
|
|
|
* ```
|
2015-08-06 10:38:40 +02:00
|
|
|
* <div class="message" [ng-class]="{error: errorCount > 0}">
|
2015-06-19 09:41:58 +02:00
|
|
|
* Please check errors.
|
|
|
|
* </div>
|
|
|
|
* ```
|
|
|
|
*/
|
2015-07-23 16:22:14 -07:00
|
|
|
@Directive({
|
2015-08-06 10:38:40 +02:00
|
|
|
selector: '[ng-class]',
|
2015-07-23 16:22:14 -07:00
|
|
|
lifecycle: [LifecycleEvent.onCheck, LifecycleEvent.onDestroy],
|
2015-08-06 10:38:40 +02:00
|
|
|
properties: ['rawClass: ng-class']
|
2015-07-23 16:22:14 -07:00
|
|
|
})
|
2015-08-06 10:38:40 +02:00
|
|
|
export class NgClass {
|
2015-07-31 12:23:50 -07:00
|
|
|
private _differ: any;
|
|
|
|
private _mode: string;
|
2015-06-18 15:40:12 -07:00
|
|
|
_rawClass;
|
|
|
|
|
2015-07-31 12:23:50 -07:00
|
|
|
constructor(private _iterableDiffers: IterableDiffers, private _keyValueDiffers: KeyValueDiffers,
|
|
|
|
private _ngEl: ElementRef, private _renderer: Renderer) {}
|
2015-03-26 17:51:08 +01:00
|
|
|
|
2015-06-18 15:40:12 -07:00
|
|
|
set rawClass(v) {
|
2015-06-21 11:54:21 +02:00
|
|
|
this._cleanupClasses(this._rawClass);
|
2015-06-18 15:40:12 -07:00
|
|
|
|
2015-06-21 11:54:21 +02:00
|
|
|
if (isString(v)) {
|
|
|
|
v = v.split(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._rawClass = v;
|
2015-07-31 12:23:50 -07:00
|
|
|
if (isPresent(v)) {
|
|
|
|
if (isListLikeIterable(v)) {
|
|
|
|
this._differ = this._iterableDiffers.find(v).create(null);
|
|
|
|
this._mode = 'iterable';
|
|
|
|
} else {
|
|
|
|
this._differ = this._keyValueDiffers.find(v).create(null);
|
|
|
|
this._mode = 'keyValue';
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 17:51:08 +01:00
|
|
|
}
|
|
|
|
|
2015-06-21 11:54:21 +02:00
|
|
|
onCheck(): void {
|
2015-07-31 12:23:50 -07:00
|
|
|
if (isPresent(this._differ)) {
|
|
|
|
var changes = this._differ.diff(this._rawClass);
|
|
|
|
if (isPresent(changes)) {
|
|
|
|
if (this._mode == 'iterable') {
|
|
|
|
this._applyIterableChanges(changes);
|
|
|
|
} else {
|
|
|
|
this._applyKeyValueChanges(changes);
|
|
|
|
}
|
2015-06-21 11:54:21 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-18 15:40:12 -07:00
|
|
|
}
|
2015-06-18 15:01:19 +02:00
|
|
|
|
2015-07-23 16:22:14 -07:00
|
|
|
onDestroy(): void { this._cleanupClasses(this._rawClass); }
|
|
|
|
|
2015-06-21 11:54:21 +02:00
|
|
|
private _cleanupClasses(rawClassVal): void {
|
|
|
|
if (isPresent(rawClassVal)) {
|
|
|
|
if (isListLikeIterable(rawClassVal)) {
|
|
|
|
ListWrapper.forEach(rawClassVal, (className) => { this._toggleClass(className, false); });
|
|
|
|
} else {
|
|
|
|
StringMapWrapper.forEach(rawClassVal, (expVal, className) => {
|
|
|
|
if (expVal) this._toggleClass(className, false);
|
|
|
|
});
|
|
|
|
}
|
2015-03-26 17:51:08 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-21 11:54:21 +02:00
|
|
|
|
2015-07-31 12:23:50 -07:00
|
|
|
private _applyKeyValueChanges(changes: any): void {
|
|
|
|
changes.forEachAddedItem((record) => { this._toggleClass(record.key, record.currentValue); });
|
|
|
|
changes.forEachChangedItem((record) => { this._toggleClass(record.key, record.currentValue); });
|
|
|
|
changes.forEachRemovedItem((record) => {
|
2015-06-21 11:54:21 +02:00
|
|
|
if (record.previousValue) {
|
|
|
|
this._toggleClass(record.key, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-31 12:23:50 -07:00
|
|
|
private _applyIterableChanges(changes: any): void {
|
|
|
|
changes.forEachAddedItem((record) => { this._toggleClass(record.item, true); });
|
|
|
|
changes.forEachRemovedItem((record) => { this._toggleClass(record.item, false); });
|
2015-06-21 11:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private _toggleClass(className: string, enabled): void {
|
|
|
|
this._renderer.setElementClass(this._ngEl, className, enabled);
|
|
|
|
}
|
2015-03-26 17:51:08 +01:00
|
|
|
}
|