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

Commit d5b283c

Browse files
ThomasBurlesontinayuangao
authored andcommitted
fix(fxStyle): enable raw input caching (#173)
* update `BaseFxDirectiveAdapter::cacheIput()` to support cacheRaw options * fix failing style.spec.ts tests
1 parent 942939e commit d5b283c

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

src/lib/flexbox/api/base-adapter.ts

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,78 @@
1-
/**
2-
* Adapted BaseFxDirective abtract class version so it can be used via composition.
3-
*
4-
* @see BaseFxDirective
5-
*/
61
import {BaseFxDirective} from './base';
72
import {ResponsiveActivation} from './../responsive/responsive-activation';
83
import {MediaQuerySubscriber} from '../../media-query/media-change';
94

5+
/**
6+
* Adapter to the BaseFxDirective abstract class so it can be used via composition.
7+
* @see BaseFxDirective
8+
*/
109
export class BaseFxDirectiveAdapter extends BaseFxDirective {
1110
get inputMap() {
1211
return this._inputMap;
1312
}
1413

14+
/**
15+
* @see BaseFxDirective._mqActivation
16+
*/
17+
get mqActivation(): ResponsiveActivation {
18+
return this._mqActivation;
19+
}
20+
/**
21+
* @see BaseFxDirective._queryInput
22+
*/
23+
queryInput(key) {
24+
return this._queryInput(key);
25+
}
26+
1527
/**
1628
* Save the property value.
1729
*/
18-
cacheInput(key?: string, source?: any) {
19-
if (Array.isArray(source)) {
30+
cacheInput(key?: string, source?: any, cacheRaw = false) {
31+
if ( cacheRaw ) {
32+
this._cacheInputRaw(key, source);
33+
} else if (Array.isArray(source)) {
2034
this._cacheInputArray(key, source);
2135
} else if (typeof source === 'object') {
2236
this._cacheInputObject(key, source);
2337
} else if (typeof source === 'string') {
2438
this._cacheInputString(key, source);
2539
} else {
26-
throw new Error('Invalid class value provided');
40+
throw new Error('Invalid class value provided. Did you want to cache the raw value?');
2741
}
2842
}
2943

30-
_cacheInputRaw(key?: string, source?: any) {
44+
/**
45+
* @see BaseFxDirective._listenForMediaQueryChanges
46+
*/
47+
listenForMediaQueryChanges(key: string,
48+
defaultValue: any,
49+
onMediaQueryChange: MediaQuerySubscriber): ResponsiveActivation {
50+
return this._listenForMediaQueryChanges(key, defaultValue, onMediaQueryChange);
51+
}
52+
53+
// ************************************************************
54+
// Protected Methods
55+
// ************************************************************
56+
57+
/**
58+
* No implicit transforms of the source.
59+
* Required when caching values expected later for KeyValueDiffers
60+
*/
61+
protected _cacheInputRaw(key?: string, source?: any) {
3162
this._inputMap[key] = source;
3263
}
3364

3465
/**
3566
* Save the property value for Array values.
3667
*/
37-
_cacheInputArray(key?: string, source?: boolean[]) {
68+
protected _cacheInputArray(key?: string, source?: boolean[]) {
3869
this._inputMap[key] = source.join(' ');
3970
}
4071

4172
/**
4273
* Save the property value for key/value pair values.
4374
*/
44-
_cacheInputObject(key?: string, source?: {[key: string]: boolean}) {
75+
protected _cacheInputObject(key?: string, source?: {[key: string]: boolean}) {
4576
let classes = [];
4677
for (let prop in source) {
4778
if (!!source[prop]) {
@@ -54,30 +85,7 @@ export class BaseFxDirectiveAdapter extends BaseFxDirective {
5485
/**
5586
* Save the property value for string values.
5687
*/
57-
_cacheInputString(key?: string, source?: string) {
88+
protected _cacheInputString(key?: string, source?: string) {
5889
this._inputMap[key] = source;
5990
}
60-
61-
/**
62-
* @see BaseFxDirective._listenForMediaQueryChanges
63-
*/
64-
listenForMediaQueryChanges(key: string,
65-
defaultValue: any,
66-
onMediaQueryChange: MediaQuerySubscriber): ResponsiveActivation {
67-
return this._listenForMediaQueryChanges(key, defaultValue, onMediaQueryChange);
68-
}
69-
70-
/**
71-
* @see BaseFxDirective._queryInput
72-
*/
73-
queryInput(key) {
74-
return this._queryInput(key);
75-
}
76-
77-
/**
78-
* @see BaseFxDirective._mqActivation
79-
*/
80-
get mqActivation(): ResponsiveActivation {
81-
return this._mqActivation;
82-
}
8391
}

src/lib/flexbox/api/style.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,47 @@ export class StyleDirective extends NgStyle implements OnInit, OnChanges, OnDest
4747

4848
@Input('style.xs')
4949
set styleXs(val: NgStyleType) {
50-
this._base._cacheInputRaw('styleXs', val as string);
50+
this._base.cacheInput('styleXs', val, true);
5151
}
5252

5353
@Input('style.gt-xs')
5454
set styleGtXs(val: NgStyleType) {
55-
this._base._cacheInputRaw('styleGtXs', val);
55+
this._base.cacheInput('styleGtXs', val, true);
5656
};
5757

5858
@Input('style.sm')
5959
set styleSm(val: NgStyleType) {
60-
this._base._cacheInputRaw('styleSm', val);
60+
this._base.cacheInput('styleSm', val, true);
6161
};
6262

6363
@Input('style.gt-sm')
6464
set styleGtSm(val: NgStyleType) {
65-
this._base._cacheInputRaw('styleGtSm', val);
65+
this._base.cacheInput('styleGtSm', val, true);
6666
};
6767

6868
@Input('style.md')
6969
set styleMd(val: NgStyleType) {
70-
this._base._cacheInputRaw('styleMd', val);
70+
this._base.cacheInput('styleMd', val, true);
7171
};
7272

7373
@Input('style.gt-md')
7474
set styleGtMd(val: NgStyleType) {
75-
this._base._cacheInputRaw('styleGtMd', val);
75+
this._base.cacheInput('styleGtMd', val, true);
7676
};
7777

7878
@Input('style.lg')
7979
set styleLg(val: NgStyleType) {
80-
this._base._cacheInputRaw('styleLg', val);
80+
this._base.cacheInput('styleLg', val, true);
8181
};
8282

8383
@Input('style.gt-lg')
8484
set styleGtLg(val: NgStyleType) {
85-
this._base._cacheInputRaw('styleGtLg', val);
85+
this._base.cacheInput('styleGtLg', val, true);
8686
};
8787

8888
@Input('style.xl')
8989
set styleXl(val: NgStyleType) {
90-
this._base._cacheInputRaw('styleXl', val);
90+
this._base.cacheInput('styleXl', val, true);
9191
};
9292

9393
/**

0 commit comments

Comments
 (0)