Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
refactor(api): use Renderer2 instead of Renderer (#372)
Browse files Browse the repository at this point in the history
* fix(api): getDisplayStyle() defaults to 'inline'

* refactor(api): use Renderer2 instead of Renderer

* Use Renderer2 instead of Renderer
* Do not extend the Angular base directives, but instead inject them into the constructor.
* improve support for future releases of Angular 5

Fixes #326.
  • Loading branch information
ThomasBurleson authored and mmalerba committed Aug 10, 2017
1 parent 9fb0877 commit 695bf37
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 75 deletions.
13 changes: 7 additions & 6 deletions src/demo-app/app/docs-layout-responsive/responsiveStyle.demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ import {Component} from '@angular/core';
<md-card-content>
<div class="containerX">
<div fxLayout="row" fxFlex class="coloredContainerX box">
<div
fxFlex
<div fxLayout="row"
fxFlex
class="coloredContainerX box">
<div fxFlex
style="font-style: italic"
[style.xs]="{'font-size.px': 10, color: 'blue'}"
[style.sm]="{'font-size.px': 20, color: 'lightblue'}"
[style.md]="{'font-size.px': 30, color: 'orange'}"
[style.xs]="{'font-size.px': 10, 'background-color': '#ddd', color: 'blue'}"
[style.sm]="{'font-size.px': 20, 'background-color': 'grey', color: '#482b00'}"
[style.md]="{'font-size.px': 30, 'background-color': 'black', color: 'orange'}"
[style.lg]="styleLgExp">
Sample Text #2
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/base-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
*/
import {ElementRef, Renderer} from '@angular/core';
import {ElementRef, Renderer2} from '@angular/core';

import {BaseFxDirective} from './base';
import {ResponsiveActivation} from './../responsive/responsive-activation';
Expand Down Expand Up @@ -48,7 +48,7 @@ export class BaseFxDirectiveAdapter extends BaseFxDirective {
constructor(protected _baseKey: string, // non-responsive @Input property name
protected _mediaMonitor: MediaMonitor,
protected _elementRef: ElementRef,
protected _renderer: Renderer) {
protected _renderer: Renderer2) {
super(_mediaMonitor, _elementRef, _renderer);
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/flexbox/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import {
ElementRef, OnDestroy, SimpleChanges, OnChanges,
SimpleChange, Renderer
SimpleChange, Renderer2
} from '@angular/core';
import {ɵgetDOM as getDom} from '@angular/platform-browser';

Expand Down Expand Up @@ -65,7 +65,7 @@ export abstract class BaseFxDirective implements OnDestroy, OnChanges {
*/
constructor(protected _mediaMonitor: MediaMonitor,
protected _elementRef: ElementRef,
protected _renderer: Renderer) {
protected _renderer: Renderer2) {
this._display = this._getDisplayStyle();
}

Expand Down Expand Up @@ -126,8 +126,8 @@ export abstract class BaseFxDirective implements OnDestroy, OnChanges {
let element: HTMLElement = source || this._elementRef.nativeElement;
let value = this._lookupStyle(element, 'display');

return value ? value.trim() :
((element.nodeType === 1) ? 'block' : 'inline-block');
// Note: 'inline' is the default of all elements, unless UA stylesheet overrides
return value ? value.trim() : 'inline';
}

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ export abstract class BaseFxDirective implements OnDestroy, OnChanges {
Object.keys(styles).forEach(key => {
const values = Array.isArray(styles[key]) ? styles[key] : [styles[key]];
for (let value of values) {
this._renderer.setElementStyle(element, key, value);
this._renderer.setStyle(element, key, value);
}
});
}
Expand Down
50 changes: 30 additions & 20 deletions src/lib/flexbox/api/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
*/
import {
Directive,
DoCheck,
ElementRef,
Input,
DoCheck,
IterableDiffers,
KeyValueDiffers,
OnChanges,
OnDestroy,
Optional,
Renderer,
IterableDiffers,
KeyValueDiffers, SimpleChanges, OnChanges
Renderer2,
SimpleChanges,
Self
} from '@angular/core';
import {NgClass} from '@angular/common';

import {BaseFxDirective} from './base';
import {BaseFxDirectiveAdapter} from './base-adapter';
import {MediaChange} from '../../media-query/media-change';
import {MediaMonitor} from '../../media-query/media-monitor';

/** NgClass allowed inputs **/
export type NgClassType = string | string[] | Set<string> | {[klass: string]: any};

/**
* Explicitly export the NgStyle super class type for ngc/AOT compiles
* Workaround for https://github.com/angular/angular/issues/17849
*/
export const _ClassDirectiveBaseClass = NgClass;

/**
* Directive to add responsive support for ngClass.
*/
Expand All @@ -44,7 +44,7 @@ export const _ClassDirectiveBaseClass = NgClass;
[ngClass.gt-xs], [ngClass.gt-sm], [ngClass.gt-md], [ngClass.gt-lg]
`
})
export class ClassDirective extends _ClassDirectiveBaseClass
export class ClassDirective extends BaseFxDirective
implements DoCheck, OnChanges, OnDestroy {

/**
Expand All @@ -55,7 +55,7 @@ export class ClassDirective extends _ClassDirectiveBaseClass
@Input('ngClass')
set ngClassBase(val: NgClassType) {
this._ngClassAdapter.cacheInput('ngClass', val, true);
this.ngClass = val;
this._ngClassInstance.ngClass = val;
}

/* tslint:disable */
Expand Down Expand Up @@ -86,7 +86,7 @@ export class ClassDirective extends _ClassDirectiveBaseClass
@Input('class')
set classBase(val: string) {
this._classAdapter.cacheInput('_rawClass', val, true);
this.klass = val;
this._ngClassInstance.klass = val;
}

@Input('class.xs') set classXs(val: NgClassType) { this._classAdapter.cacheInput('classXs', val, true); }
Expand Down Expand Up @@ -115,13 +115,20 @@ export class ClassDirective extends _ClassDirectiveBaseClass

/* tslint:enable */
constructor(protected monitor: MediaMonitor,
_ngEl: ElementRef, _renderer: Renderer2, _oldRenderer: Renderer,
_iterableDiffers: IterableDiffers, _keyValueDiffers: KeyValueDiffers,
_ngEl: ElementRef, _renderer: Renderer) {
@Optional() @Self() private _ngClassInstance: NgClass) {

super(_iterableDiffers, _keyValueDiffers, _ngEl, _renderer);
super(monitor, _ngEl, _renderer);

this._classAdapter = new BaseFxDirectiveAdapter('class', monitor, _ngEl, _renderer);
this._ngClassAdapter = new BaseFxDirectiveAdapter('ngClass', monitor, _ngEl, _renderer);

// Create an instance NgClass Directive instance only if `ngClass=""` has NOT been defined on
// the same host element; since the responsive variations may be defined...
if ( !this._ngClassInstance ) {
this._ngClassInstance = new NgClass(_iterableDiffers, _keyValueDiffers, _ngEl, _oldRenderer);
}
}

// ******************************************************************
Expand All @@ -147,12 +154,13 @@ export class ClassDirective extends _ClassDirectiveBaseClass
if (!this._classAdapter.hasMediaQueryListener) {
this._configureMQListener();
}
super.ngDoCheck();
this._ngClassInstance.ngDoCheck();
}

ngOnDestroy() {
this._classAdapter.ngOnDestroy();
this._ngClassAdapter.ngOnDestroy();
this._ngClassInstance = null;
}

// ******************************************************************
Expand All @@ -170,7 +178,7 @@ export class ClassDirective extends _ClassDirectiveBaseClass

this._ngClassAdapter.listenForMediaQueryChanges('ngClass', '', (changes: MediaChange) => {
this._updateNgClass(changes.value);
super.ngDoCheck(); // trigger NgClass::_applyIterableChanges()
this._ngClassInstance.ngDoCheck(); // trigger NgClass::_applyIterableChanges()
});
}

Expand All @@ -183,7 +191,7 @@ export class ClassDirective extends _ClassDirectiveBaseClass
if (this._classAdapter.mqActivation) {
klass = this._classAdapter.mqActivation.activatedInput;
}
this.klass = klass || this.initialClasses;
this._ngClassInstance.klass = klass || this.initialClasses;
}

/**
Expand All @@ -194,14 +202,16 @@ export class ClassDirective extends _ClassDirectiveBaseClass
if (this._ngClassAdapter.mqActivation) {
value = this._ngClassAdapter.mqActivation.activatedInput;
}
this.ngClass = value || ''; // Delegate subsequent activity to the NgClass logic

// Delegate subsequent activity to the NgClass logic
this._ngClassInstance.ngClass = value || '';
}

/**
* Special adapter to cross-cut responsive behaviors
* into the ClassDirective
* into the ClassDirective instance
*/
protected _classAdapter: BaseFxDirectiveAdapter; // used for `class.xxx` selectores
protected _classAdapter: BaseFxDirectiveAdapter; // used for `class.xxx` selectors
protected _ngClassAdapter: BaseFxDirectiveAdapter; // used for `ngClass.xxx` selectors
}

4 changes: 2 additions & 2 deletions src/lib/flexbox/api/flex-align.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
OnInit,
OnChanges,
OnDestroy,
Renderer,
Renderer2,
SimpleChanges,
} from '@angular/core';

Expand Down Expand Up @@ -54,7 +54,7 @@ export class FlexAlignDirective extends BaseFxDirective implements OnInit, OnCha
@Input('fxFlexAlign.gt-lg') set alignGtLg(val) { this._cacheInput('alignGtLg', val); };

/* tslint:enable */
constructor(monitor: MediaMonitor, elRef: ElementRef, renderer: Renderer) {
constructor(monitor: MediaMonitor, elRef: ElementRef, renderer: Renderer2) {
super(monitor, elRef, renderer);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/flex-fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
*/
import {Directive, ElementRef, Renderer} from '@angular/core';
import {Directive, ElementRef, Renderer2} from '@angular/core';

import {MediaMonitor} from '../../media-query/media-monitor';
import {BaseFxDirective} from './base';
Expand All @@ -29,7 +29,7 @@ const FLEX_FILL_CSS = {
[fxFlexFill]
`})
export class FlexFillDirective extends BaseFxDirective {
constructor(monitor: MediaMonitor, public elRef: ElementRef, public renderer: Renderer) {
constructor(monitor: MediaMonitor, public elRef: ElementRef, public renderer: Renderer2) {
super(monitor, elRef, renderer);
this._applyStyleToElement(FLEX_FILL_CSS);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/flex-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
OnChanges,
OnDestroy,
Optional,
Renderer,
Renderer2,
SimpleChanges,
SkipSelf
} from '@angular/core';
Expand Down Expand Up @@ -59,7 +59,7 @@ export class FlexOffsetDirective extends BaseFxDirective implements OnInit, OnCh
/* tslint:enable */
constructor(monitor: MediaMonitor,
elRef: ElementRef,
renderer: Renderer,
renderer: Renderer2,
@Optional() @SkipSelf() protected _container: LayoutDirective ) {
super(monitor, elRef, renderer);

Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/flex-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
OnInit,
OnChanges,
OnDestroy,
Renderer,
Renderer2,
SimpleChanges,
} from '@angular/core';

Expand Down Expand Up @@ -52,7 +52,7 @@ export class FlexOrderDirective extends BaseFxDirective implements OnInit, OnCha
@Input('fxFlexOrder.lt-xl') set orderLtXl(val) { this._cacheInput('orderLtXl', val); };

/* tslint:enable */
constructor(monitor: MediaMonitor, elRef: ElementRef, renderer: Renderer) {
constructor(monitor: MediaMonitor, elRef: ElementRef, renderer: Renderer2) {
super(monitor, elRef, renderer);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/flex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
OnDestroy,
OnInit,
Optional,
Renderer,
Renderer2,
SimpleChanges,
SkipSelf,
} from '@angular/core';
Expand Down Expand Up @@ -84,7 +84,7 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
// for the parent flex container for this flex item.
constructor(monitor: MediaMonitor,
elRef: ElementRef,
renderer: Renderer,
renderer: Renderer2,
@Optional() @SkipSelf() protected _container: LayoutDirective,
@Optional() @SkipSelf() protected _wrap: LayoutWrapDirective) {

Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/layout-align.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
OnDestroy,
OnInit,
Optional,
Renderer,
Renderer2,
SimpleChanges, Self,
} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';
Expand Down Expand Up @@ -67,7 +67,7 @@ export class LayoutAlignDirective extends BaseFxDirective implements OnInit, OnC
/* tslint:enable */
constructor(
monitor: MediaMonitor,
elRef: ElementRef, renderer: Renderer,
elRef: ElementRef, renderer: Renderer2,
@Optional() @Self() container: LayoutDirective) {
super(monitor, elRef, renderer);

Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/layout-gap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ElementRef,
Input,
OnChanges,
Renderer,
Renderer2,
SimpleChanges,
Self,
AfterContentInit,
Expand Down Expand Up @@ -65,7 +65,7 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
/* tslint:enable */
constructor(monitor: MediaMonitor,
elRef: ElementRef,
renderer: Renderer,
renderer: Renderer2,
@Optional() @Self() container: LayoutDirective,
private _zone: NgZone) {
super(monitor, elRef, renderer);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/layout-wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
OnChanges,
OnDestroy,
OnInit,
Renderer,
Renderer2,
SimpleChanges, Self, Optional,
} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';
Expand Down Expand Up @@ -64,7 +64,7 @@ export class LayoutWrapDirective extends BaseFxDirective implements OnInit, OnCh
constructor(
monitor: MediaMonitor,
elRef: ElementRef,
renderer: Renderer,
renderer: Renderer2,
@Optional() @Self() container: LayoutDirective) {

super(monitor, elRef, renderer);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/flexbox/api/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
OnInit,
OnChanges,
OnDestroy,
Renderer,
Renderer2,
SimpleChanges,
} from '@angular/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
Expand Down Expand Up @@ -71,7 +71,7 @@ export class LayoutDirective extends BaseFxDirective implements OnInit, OnChange
/**
*
*/
constructor(monitor: MediaMonitor, elRef: ElementRef, renderer: Renderer) {
constructor(monitor: MediaMonitor, elRef: ElementRef, renderer: Renderer2) {
super(monitor, elRef, renderer);
this._announcer = new BehaviorSubject<string>('row');
this.layout$ = this._announcer.asObservable();
Expand Down
Loading

0 comments on commit 695bf37

Please sign in to comment.