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

Commit cd13487

Browse files
update(flexbox): refactor fxStyle and fxClass code
* refactor code to `base-adapter.ts`
1 parent 0a5fc71 commit cd13487

File tree

6 files changed

+74
-229
lines changed

6 files changed

+74
-229
lines changed

src/demo-app/app/demo-app/demo-app.css

+4-4
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,20 @@ md-card-content pre {
207207
color: blue!important;
208208
}
209209
.fxClass-sm {
210-
font-size: 12px;
210+
font-size: 20px;
211211
color: lightblue!important;
212212
}
213213
.fxClass-md {
214-
font-size: 14px;
214+
font-size: 30px;
215215
color: orange!important;
216216
}
217217
.fxClass-md2 {
218218
font-weight: bold;
219219
}
220220
.fxClass-lg {
221-
font-size: 16px;
221+
font-size: 40px;
222222
color: lightgreen!important;
223223
}
224224
.fxClass-lg2 {
225-
font-style: normal;
225+
text-shadow: #5c5c5c;
226226
}

src/demo-app/app/docs-layout-responsive/DemosResponsiveLayouts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { Component } from '@angular/core';
33
@Component({
44
selector: 'demos-docs-layout',
55
template: `
6-
<demo-responsive-style class="small-demo"> </demo-responsive-style>
76
<demo-responsive-layout-direction class="small-demo"> </demo-responsive-layout-direction>
87
<demo-responsive-row-column class="small-demo"> </demo-responsive-row-column>
98
<demo-responsive-flex-directive class="small-demo"> </demo-responsive-flex-directive>
109
<demo-responsive-flex-order class="small-demo"> </demo-responsive-flex-order>
1110
<demo-responsive-show-hide class="small-demo"> </demo-responsive-show-hide>
11+
<demo-responsive-style class="small-demo"> </demo-responsive-style>
1212
`
1313
})
1414
export class DemosResponsiveLayout { }

src/demo-app/app/docs-layout-responsive/responsiveStyle.demo.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ObservableMedia } from "../../../lib/media-query/observable-media-servi
1515
<md-card class="card-demo" >
1616
<md-card-title>Responsive Style</md-card-title>
1717
<md-card-subtitle>
18-
Use the class and style APIs to responsively apply styles to elements:
18+
Use the fxClass and fxStyle APIs to responsively apply styles to elements:
1919
</md-card-subtitle>
2020
2121
<md-card-content>
@@ -87,7 +87,7 @@ import { ObservableMedia } from "../../../lib/media-query/observable-media-servi
8787
</md-card>
8888
`
8989
})
90-
export class DemoResponsiveStyle implements OnInit, OnDestroy {
90+
export class DemoResponsiveStyle implements OnDestroy {
9191
private _watcher: Subscription;
9292
public activeMediaQuery = "";
9393
public activeMediaQueryAlias = "";
@@ -97,21 +97,14 @@ export class DemoResponsiveStyle implements OnInit, OnDestroy {
9797
color: 'lightgreen'
9898
};
9999

100-
constructor( @Inject(ObservableMedia) private _media$) { }
101-
102-
ngOnInit() {
103-
this._watcher = this.watchMQChanges();
100+
constructor( private _media$:ObservableMedia ) {
101+
this._watcher = this._media$.subscribe((change: MediaChange) => {
102+
this.activeMediaQuery = change ? `'${change.mqAlias}' = ${change.mediaQuery} )` : "";
103+
this.activeMediaQueryAlias = change.mqAlias;
104+
});
104105
}
105106

106107
ngOnDestroy() {
107108
this._watcher.unsubscribe();
108109
}
109-
110-
watchMQChanges() {
111-
return this._media$.subscribe((change: MediaChange) => {
112-
let value = change ? `'${change.mqAlias}' = ${change.mediaQuery} )` : "";
113-
this.activeMediaQuery = value;
114-
this.activeMediaQueryAlias = change.mqAlias;
115-
});
116-
}
117110
}

src/lib/flexbox/api/class.spec.ts

+24-61
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {
9-
Component, OnInit, Inject, ElementRef
9+
Component, OnInit
1010
} from '@angular/core';
11-
import { CommonModule } from '@angular/common';
12-
import { ComponentFixture, TestBed } from '@angular/core/testing';
11+
import {CommonModule} from '@angular/common';
12+
import {ComponentFixture, TestBed} from '@angular/core/testing';
1313

14-
import { MockMatchMedia } from '../../media-query/mock/mock-match-media';
15-
import { MatchMedia } from '../../media-query/match-media';
16-
import { ObservableMedia } from '../../media-query/observable-media-service';
17-
import { BreakPointsProvider } from '../../media-query/breakpoints/break-points';
18-
import { BreakPointRegistry } from '../../media-query/breakpoints/break-point-registry';
19-
import { BaseFxDirectiveAdapter } from './class';
14+
import {MockMatchMedia} from '../../media-query/mock/mock-match-media';
15+
import {MatchMedia} from '../../media-query/match-media';
16+
import {ObservableMedia} from '../../media-query/observable-media-service';
17+
import {BreakPointsProvider} from '../../media-query/breakpoints/break-points';
18+
import {BreakPointRegistry} from '../../media-query/breakpoints/break-point-registry';
2019

21-
import { customMatchers, expect } from '../../utils/testing/custom-matchers';
20+
import {customMatchers} from '../../utils/testing/custom-matchers';
2221
import {
2322
makeCreateTestComponent, expectNativeEl
2423
} from '../../utils/testing/helpers';
25-
import { ClassDirective } from './class';
26-
import { MediaQueriesModule } from '../../media-query/_module';
24+
import {ClassDirective} from './class';
25+
import {MediaQueriesModule} from '../../media-query/_module';
2726

2827
describe('class directive', () => {
2928
let fixture: ComponentFixture<any>;
@@ -42,7 +41,7 @@ describe('class directive', () => {
4241
declarations: [TestClassComponent, ClassDirective],
4342
providers: [
4443
BreakPointRegistry, BreakPointsProvider,
45-
{ provide: MatchMedia, useClass: MockMatchMedia }
44+
{provide: MatchMedia, useClass: MockMatchMedia}
4645
]
4746
});
4847
});
@@ -54,17 +53,17 @@ describe('class directive', () => {
5453
});
5554

5655
['xs', 'sm', 'md', 'lg']
57-
.forEach(mq => {
58-
const selector = `class-${mq}`;
59-
it(`should apply '${selector}' with '${mq}' media query`, () => {
60-
fixture = createTestComponent(`
56+
.forEach(mq => {
57+
const selector = `class-${mq}`;
58+
it(`should apply '${selector}' with '${mq}' media query`, () => {
59+
fixture = createTestComponent(`
6160
<div class.${mq}="${selector}">
6261
</div>
6362
`);
64-
activateMediaQuery(mq, true);
65-
expectNativeEl(fixture).toHaveCssClass(selector);
63+
activateMediaQuery(mq, true);
64+
expectNativeEl(fixture).toHaveCssClass(selector);
65+
});
6666
});
67-
});
6867

6968
it('should keep existing class selector', () => {
7069
fixture = createTestComponent(`
@@ -96,11 +95,11 @@ describe('class directive', () => {
9695
</div>
9796
`);
9897
activateMediaQuery('xs', true);
99-
expectNativeEl(fixture, { hasXs1: true, hasXs2: false }).toHaveCssClass('xs-1');
100-
expectNativeEl(fixture, { hasXs1: true, hasXs2: false }).not.toHaveCssClass('xs-2');
98+
expectNativeEl(fixture, {hasXs1: true, hasXs2: false}).toHaveCssClass('xs-1');
99+
expectNativeEl(fixture, {hasXs1: true, hasXs2: false}).not.toHaveCssClass('xs-2');
101100

102-
expectNativeEl(fixture, { hasXs1: false, hasXs2: true }).toHaveCssClass('xs-2');
103-
expectNativeEl(fixture, { hasXs1: false, hasXs2: true }).not.toHaveCssClass('xs-1');
101+
expectNativeEl(fixture, {hasXs1: false, hasXs2: true}).toHaveCssClass('xs-2');
102+
expectNativeEl(fixture, {hasXs1: false, hasXs2: true}).not.toHaveCssClass('xs-1');
104103
});
105104

106105
it('should work with ngClass array notation', () => {
@@ -114,42 +113,6 @@ describe('class directive', () => {
114113
});
115114
});
116115

117-
export class MockElementRef extends ElementRef {
118-
constructor() {
119-
const nEl = document.createElement('DIV');
120-
super(nEl);
121-
this.nativeElement = nEl;
122-
}
123-
}
124-
125-
describe('BaseFxDirectiveAdapter class', () => {
126-
let component;
127-
beforeEach(() => {
128-
component = new BaseFxDirectiveAdapter(null, new MockElementRef(), null);
129-
});
130-
describe('cacheInput', () => {
131-
it('should call _cacheInputArray when source is an array', () => {
132-
spyOn(component, '_cacheInputArray');
133-
component.cacheInput('key', []);
134-
expect(component._cacheInputArray).toHaveBeenCalled();
135-
});
136-
it('should call _cacheInputObject when source is an object', () => {
137-
spyOn(component, '_cacheInputObject');
138-
component.cacheInput('key', {});
139-
expect(component._cacheInputObject).toHaveBeenCalled();
140-
});
141-
it('should call _cacheInputString when source is a string', () => {
142-
spyOn(component, '_cacheInputString');
143-
component.cacheInput('key', '');
144-
expect(component._cacheInputString).toHaveBeenCalled();
145-
});
146-
it('should throw when source is not an object, array or string', () => {
147-
expect(component.cacheInput.bind(null, true)).toThrow();
148-
});
149-
});
150-
151-
});
152-
153116
// *****************************************************************
154117
// Template Component
155118
// *****************************************************************
@@ -162,7 +125,7 @@ export class TestClassComponent implements OnInit {
162125
hasXs1: boolean;
163126
hasXs2: boolean;
164127

165-
constructor( @Inject(ObservableMedia) private media) {
128+
constructor(private media: ObservableMedia) {
166129
}
167130

168131
ngOnInit() {

src/lib/flexbox/api/class.ts

+14-90
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ import {
1717
IterableDiffers,
1818
KeyValueDiffers
1919
} from '@angular/core';
20-
import { NgClass } from '@angular/common';
20+
import {NgClass} from '@angular/common';
2121

22-
import { BreakPointRegistry } from './../../media-query/breakpoints/break-point-registry';
23-
import { ResponsiveActivation } from './../responsive/responsive-activation';
24-
import { BaseFxDirective } from './base';
25-
import { MediaChange } from '../../media-query/media-change';
26-
import { MediaMonitor } from '../../media-query/media-monitor';
22+
import {BaseFxDirectiveAdapter} from './base-adapter';
23+
import {BreakPointRegistry} from './../../media-query/breakpoints/break-point-registry';
24+
import {MediaChange} from '../../media-query/media-change';
25+
import {MediaMonitor} from '../../media-query/media-monitor';
2726

2827
/** NgClass allowed inputs **/
29-
export type NgClassType = string | string[] | Set<string> | { [klass: string]: any };
28+
export type NgClassType = string | string[] | Set<string> | {[klass: string]: any};
3029

3130
/**
3231
* Directive to add responsive support for ngClass.
@@ -91,15 +90,11 @@ export class ClassDirective extends NgClass implements OnInit, OnChanges, OnDest
9190
this._base.cacheInput('classXl', val);
9291
};
9392

94-
private _base: BaseFxDirectiveAdapter;
95-
9693
constructor(private monitor: MediaMonitor,
97-
private _bpRegistry: BreakPointRegistry,
98-
_iterableDiffers: IterableDiffers, _keyValueDiffers: KeyValueDiffers,
99-
_ngEl: ElementRef, _renderer: Renderer) {
100-
94+
private _bpRegistry: BreakPointRegistry,
95+
_iterableDiffers: IterableDiffers, _keyValueDiffers: KeyValueDiffers,
96+
_ngEl: ElementRef, _renderer: Renderer) {
10197
super(_iterableDiffers, _keyValueDiffers, _ngEl, _renderer);
102-
10398
this._base = new BaseFxDirectiveAdapter(monitor, _ngEl, _renderer);
10499
}
105100

@@ -133,85 +128,14 @@ export class ClassDirective extends NgClass implements OnInit, OnChanges, OnDest
133128
if (this._base.mqActivation) {
134129
clazz = this._base.mqActivation.activatedInput;
135130
}
136-
131+
// Delegate subsequent activity to the NgClass logic
137132
this.ngClass = clazz;
138133
}
139-
}
140-
141-
/**
142-
* Adapted BaseFxDirective abtract class version so it can be used via composition.
143-
*
144-
* @see BaseFxDirective
145-
*/
146-
import { MediaQuerySubscriber } from '../../media-query/media-change';
147-
148-
export class BaseFxDirectiveAdapter extends BaseFxDirective {
149-
get inputMap() {
150-
return this._inputMap;
151-
}
152-
153-
/**
154-
* Save the property value.
155-
*/
156-
cacheInput(key?: string, source?: any) {
157-
if (Array.isArray(source)) {
158-
this._cacheInputArray(key, source);
159-
} else if (typeof source === 'object') {
160-
this._cacheInputObject(key, source);
161-
} else if (typeof source === 'string') {
162-
this._cacheInputString(key, source);
163-
} else {
164-
throw new Error('Invalid class value provided');
165-
}
166-
}
167-
168-
/**
169-
* Save the property value for Array values.
170-
*/
171-
_cacheInputArray(key?: string, source?: boolean[]) {
172-
this._inputMap[key] = source.join(' ');
173-
}
174134

175135
/**
176-
* Save the property value for key/value pair values.
136+
* Special adapter to cross-cut responsive behaviors
137+
* into the ClassDirective
177138
*/
178-
_cacheInputObject(key?: string, source?: { [key: string]: boolean }) {
179-
let classes = [];
180-
for (let prop in source) {
181-
if (!!source[prop]) {
182-
classes.push(prop);
183-
}
184-
}
185-
this._inputMap[key] = classes.join(' ');
186-
}
187-
188-
/**
189-
* Save the property value for string values.
190-
*/
191-
_cacheInputString(key?: string, source?: string) {
192-
this._inputMap[key] = source;
193-
}
194-
195-
/**
196-
* @see BaseFxDirective._listenForMediaQueryChanges
197-
*/
198-
listenForMediaQueryChanges(key: string,
199-
defaultValue: any,
200-
onMediaQueryChange: MediaQuerySubscriber): ResponsiveActivation {
201-
return this._listenForMediaQueryChanges(key, defaultValue, onMediaQueryChange);
202-
}
203-
204-
/**
205-
* @see BaseFxDirective._queryInput
206-
*/
207-
queryInput(key) {
208-
return this._queryInput(key);
209-
}
210-
211-
/**
212-
* @see BaseFxDirective._mqActivation
213-
*/
214-
get mqActivation(): ResponsiveActivation {
215-
return this._mqActivation;
216-
}
139+
private _base: BaseFxDirectiveAdapter;
217140
}
141+

0 commit comments

Comments
 (0)