2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-06-15 10:18:50 +02:00
|
|
|
import {Directive, DoCheck, ElementRef, Input, KeyValueChanges, KeyValueDiffer, KeyValueDiffers, Renderer2} from '@angular/core';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2015-07-08 16:21:24 +02:00
|
|
|
/**
|
2016-09-12 10:20:33 -07:00
|
|
|
* @ngModule CommonModule
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2018-03-29 11:08:56 +01:00
|
|
|
* @usageNotes
|
2015-07-08 16:21:24 +02:00
|
|
|
* ```
|
2016-09-12 10:20:33 -07:00
|
|
|
* <some-element [ngStyle]="{'font-style': styleExp}">...</some-element>
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2016-09-12 10:20:33 -07:00
|
|
|
* <some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>
|
2015-10-05 15:30:02 +02:00
|
|
|
*
|
2016-09-12 10:20:33 -07:00
|
|
|
* <some-element [ngStyle]="objExp">...</some-element>
|
2015-10-05 15:30:02 +02:00
|
|
|
* ```
|
2015-07-08 16:21:24 +02:00
|
|
|
*
|
2016-09-12 10:20:33 -07:00
|
|
|
* @description
|
|
|
|
*
|
2018-03-29 11:12:34 +01:00
|
|
|
* Update an HTML element styles.
|
|
|
|
*
|
2016-09-12 10:20:33 -07:00
|
|
|
* The styles are updated according to the value of the expression evaluation:
|
2016-10-21 22:47:44 +05:30
|
|
|
* - keys are style names with an optional `.<unit>` suffix (ie 'top.px', 'font-style.em'),
|
2016-09-12 10:20:33 -07:00
|
|
|
* - values are the values assigned to those properties (expressed in the given unit).
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-07-08 16:21:24 +02:00
|
|
|
*/
|
2016-07-11 15:09:04 -07:00
|
|
|
@Directive({selector: '[ngStyle]'})
|
2015-08-31 18:32:32 -07:00
|
|
|
export class NgStyle implements DoCheck {
|
2016-09-22 10:34:00 -07:00
|
|
|
private _ngStyle: {[key: string]: string};
|
2016-10-27 20:28:11 +02:00
|
|
|
private _differ: KeyValueDiffer<string, string|number>;
|
2015-06-21 14:18:28 +02:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
2017-06-15 10:18:50 +02:00
|
|
|
private _differs: KeyValueDiffers, private _ngEl: ElementRef, private _renderer: Renderer2) {}
|
2015-06-21 14:18:28 +02:00
|
|
|
|
2016-07-07 16:35:13 -07:00
|
|
|
@Input()
|
|
|
|
set ngStyle(v: {[key: string]: string}) {
|
2016-07-11 15:09:04 -07:00
|
|
|
this._ngStyle = v;
|
2016-09-08 19:25:25 -07:00
|
|
|
if (!this._differ && v) {
|
2017-02-09 16:33:44 -05:00
|
|
|
this._differ = this._differs.find(v).create();
|
2015-07-31 12:23:50 -07:00
|
|
|
}
|
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() {
|
2016-09-08 19:25:25 -07:00
|
|
|
if (this._differ) {
|
|
|
|
const changes = this._differ.diff(this._ngStyle);
|
|
|
|
if (changes) {
|
2015-07-31 12:23:50 -07:00
|
|
|
this._applyChanges(changes);
|
|
|
|
}
|
2015-06-21 14:18:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 20:28:11 +02:00
|
|
|
private _applyChanges(changes: KeyValueChanges<string, string|number>): void {
|
|
|
|
changes.forEachRemovedItem((record) => this._setStyle(record.key, null));
|
|
|
|
changes.forEachAddedItem((record) => this._setStyle(record.key, record.currentValue));
|
|
|
|
changes.forEachChangedItem((record) => this._setStyle(record.key, record.currentValue));
|
2015-06-21 14:18:28 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 09:54:02 -07:00
|
|
|
private _setStyle(nameAndUnit: string, value: string|number|null|undefined): void {
|
2016-09-08 19:25:25 -07:00
|
|
|
const [name, unit] = nameAndUnit.split('.');
|
2016-10-27 20:28:11 +02:00
|
|
|
value = value != null && unit ? `${value}${unit}` : value;
|
2016-08-04 20:00:43 +02:00
|
|
|
|
2018-01-19 17:14:05 -08:00
|
|
|
if (value != null) {
|
|
|
|
this._renderer.setStyle(this._ngEl.nativeElement, name, value as string);
|
|
|
|
} else {
|
|
|
|
this._renderer.removeStyle(this._ngEl.nativeElement, name);
|
|
|
|
}
|
2015-06-21 14:18:28 +02:00
|
|
|
}
|
|
|
|
}
|