2015-05-20 17:19:46 -07:00
|
|
|
import {Directive} from 'angular2/annotations';
|
|
|
|
import {ElementRef} from 'angular2/core';
|
|
|
|
|
2015-03-26 17:51:08 +01:00
|
|
|
import {isPresent} from 'angular2/src/facade/lang';
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
|
2015-05-26 15:54:10 +02:00
|
|
|
@Directive({selector: '[class]', properties: ['iterableChanges: class | keyValDiff']})
|
2015-03-26 17:51:08 +01:00
|
|
|
export class CSSClass {
|
|
|
|
_domEl;
|
2015-05-20 17:19:46 -07:00
|
|
|
constructor(ngEl: ElementRef) { this._domEl = ngEl.domElement; }
|
2015-03-26 17:51:08 +01:00
|
|
|
|
2015-05-20 17:19:46 -07:00
|
|
|
_toggleClass(className, enabled): void {
|
2015-03-26 17:51:08 +01:00
|
|
|
if (enabled) {
|
|
|
|
DOM.addClass(this._domEl, className);
|
|
|
|
} else {
|
|
|
|
DOM.removeClass(this._domEl, className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set iterableChanges(changes) {
|
|
|
|
if (isPresent(changes)) {
|
|
|
|
changes.forEachAddedItem((record) => { this._toggleClass(record.key, record.currentValue); });
|
2015-05-20 17:19:46 -07:00
|
|
|
changes.forEachChangedItem(
|
|
|
|
(record) => { this._toggleClass(record.key, record.currentValue); });
|
2015-03-26 17:51:08 +01:00
|
|
|
changes.forEachRemovedItem((record) => {
|
|
|
|
if (record.previousValue) {
|
|
|
|
DOM.removeClass(this._domEl, record.key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|