2016-04-12 09:40:37 -07:00
|
|
|
import {
|
|
|
|
DoCheck,
|
2016-05-26 15:07:51 -07:00
|
|
|
KeyValueChangeRecord,
|
2016-04-12 09:40:37 -07:00
|
|
|
KeyValueDiffer,
|
|
|
|
KeyValueDiffers,
|
|
|
|
ElementRef,
|
|
|
|
Directive,
|
|
|
|
Renderer
|
2016-04-28 17:50:03 -07:00
|
|
|
} from '@angular/core';
|
|
|
|
import {isPresent, isBlank} from '../../src/facade/lang';
|
2015-06-21 14:18:28 +02:00
|
|
|
|
2015-07-08 16:21:24 +02:00
|
|
|
/**
|
2015-10-05 15:30:02 +02:00
|
|
|
* The `NgStyle` directive changes styles based on a result of expression evaluation.
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2015-11-23 16:02:19 -08:00
|
|
|
* An expression assigned to the `ngStyle` property must evaluate to an object and the
|
2015-10-05 15:30:02 +02:00
|
|
|
* corresponding element styles are updated based on changes to this object. Style names to update
|
|
|
|
* are taken from the object's keys, and values - from the corresponding object's values.
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2015-11-17 09:41:31 -08:00
|
|
|
* ### Syntax
|
2015-10-05 15:30:02 +02:00
|
|
|
*
|
2015-11-23 16:02:19 -08:00
|
|
|
* - `<div [ngStyle]="{'font-style': style}"></div>`
|
|
|
|
* - `<div [ngStyle]="styleExp"></div>` - here the `styleExp` must evaluate to an object
|
2015-10-05 15:30:02 +02:00
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/YamGS6GkUh9GqWNQhCyM?p=preview)):
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 17:50:03 -07:00
|
|
|
* import {Component} from '@angular/core';
|
|
|
|
* import {NgStyle} from '@angular/common';
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2015-10-05 15:30:02 +02:00
|
|
|
* @Component({
|
2015-11-23 16:02:19 -08:00
|
|
|
* selector: 'ngStyle-example',
|
2015-10-05 15:30:02 +02:00
|
|
|
* template: `
|
2015-11-23 16:02:19 -08:00
|
|
|
* <h1 [ngStyle]="{'font-style': style, 'font-size': size, 'font-weight': weight}">
|
2015-10-05 15:30:02 +02:00
|
|
|
* Change style of this text!
|
|
|
|
* </h1>
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2015-10-05 15:30:02 +02:00
|
|
|
* <hr>
|
|
|
|
*
|
|
|
|
* <label>Italic: <input type="checkbox" (change)="changeStyle($event)"></label>
|
|
|
|
* <label>Bold: <input type="checkbox" (change)="changeWeight($event)"></label>
|
|
|
|
* <label>Size: <input type="text" [value]="size" (change)="size = $event.target.value"></label>
|
|
|
|
* `,
|
|
|
|
* directives: [NgStyle]
|
|
|
|
* })
|
|
|
|
* export class NgStyleExample {
|
|
|
|
* style = 'normal';
|
|
|
|
* weight = 'normal';
|
|
|
|
* size = '20px';
|
|
|
|
*
|
|
|
|
* changeStyle($event: any) {
|
|
|
|
* this.style = $event.target.checked ? 'italic' : 'normal';
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* changeWeight($event: any) {
|
|
|
|
* this.weight = $event.target.checked ? 'bold' : 'normal';
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2015-10-05 15:30:02 +02:00
|
|
|
* In this example the `font-style`, `font-size` and `font-weight` styles will be updated
|
|
|
|
* based on the `style` property's value changes.
|
2015-07-08 16:21:24 +02:00
|
|
|
*/
|
2015-11-23 16:02:19 -08:00
|
|
|
@Directive({selector: '[ngStyle]', inputs: ['rawStyle: ngStyle']})
|
2015-08-31 18:32:32 -07:00
|
|
|
export class NgStyle implements DoCheck {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-02-11 17:01:17 -08:00
|
|
|
_rawStyle: {[key: string]: string};
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-07-31 12:23:50 -07:00
|
|
|
_differ: KeyValueDiffer;
|
2015-06-21 14:18:28 +02:00
|
|
|
|
2016-04-12 09:40:37 -07:00
|
|
|
constructor(private _differs: KeyValueDiffers, private _ngEl: ElementRef,
|
|
|
|
private _renderer: Renderer) {}
|
2015-06-21 14:18:28 +02:00
|
|
|
|
2016-02-11 17:01:17 -08:00
|
|
|
set rawStyle(v: {[key: string]: string}) {
|
2015-06-21 14:18:28 +02:00
|
|
|
this._rawStyle = v;
|
2015-07-31 12:23:50 -07:00
|
|
|
if (isBlank(this._differ) && isPresent(v)) {
|
|
|
|
this._differ = this._differs.find(this._rawStyle).create(null);
|
|
|
|
}
|
2015-06-21 14:18:28 +02:00
|
|
|
}
|
|
|
|
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
ngDoCheck() {
|
2015-07-31 12:23:50 -07:00
|
|
|
if (isPresent(this._differ)) {
|
|
|
|
var changes = this._differ.diff(this._rawStyle);
|
|
|
|
if (isPresent(changes)) {
|
|
|
|
this._applyChanges(changes);
|
|
|
|
}
|
2015-06-21 14:18:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 12:23:50 -07:00
|
|
|
private _applyChanges(changes: any): void {
|
2016-02-11 17:01:17 -08:00
|
|
|
changes.forEachAddedItem(
|
2016-02-20 08:52:51 -08:00
|
|
|
(record: KeyValueChangeRecord) => { this._setStyle(record.key, record.currentValue); });
|
2016-02-11 17:01:17 -08:00
|
|
|
changes.forEachChangedItem(
|
2016-02-20 08:52:51 -08:00
|
|
|
(record: KeyValueChangeRecord) => { this._setStyle(record.key, record.currentValue); });
|
|
|
|
changes.forEachRemovedItem(
|
|
|
|
(record: KeyValueChangeRecord) => { this._setStyle(record.key, null); });
|
2015-06-21 14:18:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private _setStyle(name: string, val: string): void {
|
2015-12-02 10:35:51 -08:00
|
|
|
this._renderer.setElementStyle(this._ngEl.nativeElement, name, val);
|
2015-06-21 14:18:28 +02:00
|
|
|
}
|
|
|
|
}
|