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

Commit e6bc451

Browse files
ThomasBurlesontinayuangao
authored andcommitted
feat(flexbox): use protected access to allow API directives to be easily extended (#163)
* change internal methods to `protected` to enable subclass access > Useful when extending directives with custom breakpoints.
1 parent 645a2dc commit e6bc451

12 files changed

+54
-57
lines changed

src/lib/flexbox/api/base.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export abstract class BaseFxDirective implements OnDestroy {
4242
/**
4343
*
4444
*/
45-
constructor(private _mediaMonitor: MediaMonitor,
45+
constructor(protected _mediaMonitor: MediaMonitor,
4646
protected _elementRef: ElementRef,
47-
private _renderer: Renderer) {
47+
protected _renderer: Renderer) {
4848
this._display = this._getDisplayStyle();
4949
}
5050

src/lib/flexbox/api/flex-align.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class FlexAlignDirective extends BaseFxDirective implements OnInit, OnCha
114114
// Protected methods
115115
// *********************************************
116116

117-
private _updateWithValue(value?: string|number) {
117+
protected _updateWithValue(value?: string|number) {
118118
value = value || this._queryInput("align") || 'stretch';
119119
if (this._mqActivation) {
120120
value = this._mqActivation.activatedInput;
@@ -123,7 +123,7 @@ export class FlexAlignDirective extends BaseFxDirective implements OnInit, OnCha
123123
this._applyStyleToElement(this._buildCSS(value));
124124
}
125125

126-
private _buildCSS(align) {
126+
protected _buildCSS(align) {
127127
let css = {};
128128

129129
// Cross-axis

src/lib/flexbox/api/flex-offset.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class FlexOffsetDirective extends BaseFxDirective implements OnInit, OnCh
8383
// *********************************************
8484

8585

86-
private _updateWithValue(value?: string|number) {
86+
protected _updateWithValue(value?: string|number) {
8787
value = value || this._queryInput("offset") || 0;
8888
if (this._mqActivation) {
8989
value = this._mqActivation.activatedInput;
@@ -92,7 +92,7 @@ export class FlexOffsetDirective extends BaseFxDirective implements OnInit, OnCh
9292
this._applyStyleToElement(this._buildCSS(value));
9393
}
9494

95-
private _buildCSS(offset) {
95+
protected _buildCSS(offset) {
9696
let isPercent = String(offset).indexOf('%') > -1;
9797
let isPx = String(offset).indexOf('px') > -1;
9898
if (!isPx && !isPercent && !isNaN(offset)) {

src/lib/flexbox/api/flex-order.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class FlexOrderDirective extends BaseFxDirective implements OnInit, OnCha
8282
// Protected methods
8383
// *********************************************
8484

85-
private _updateWithValue(value?: string) {
85+
protected _updateWithValue(value?: string) {
8686
value = value || this._queryInput("order") || '0';
8787
if (this._mqActivation) {
8888
value = this._mqActivation.activatedInput;
@@ -92,7 +92,7 @@ export class FlexOrderDirective extends BaseFxDirective implements OnInit, OnCha
9292
}
9393

9494

95-
private _buildCSS(value) {
95+
protected _buildCSS(value) {
9696
value = parseInt(value, 10);
9797
return {order: isNaN(value) ? 0 : value};
9898
}

src/lib/flexbox/api/flex.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ export type FlexBasisAlias = 'grow' | 'initial' | 'auto' | 'none' | 'nogrow' | '
3838
*
3939
* @see https://css-tricks.com/snippets/css/a-guide-to-flexbox/
4040
*/
41-
@Directive({
42-
selector: `
41+
@Directive({selector: `
4342
[fxFlex],
4443
[fxFlex.xs],
4544
[fxFlex.gt-xs],
@@ -55,13 +54,13 @@ export type FlexBasisAlias = 'grow' | 'initial' | 'auto' | 'none' | 'nogrow' | '
5554
export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges, OnDestroy {
5655

5756
/** The flex-direction of this element's flex container. Defaults to 'row'. */
58-
private _layout = 'row';
57+
protected _layout = 'row';
5958

6059
/**
6160
* Subscription to the parent flex container's layout changes.
6261
* Stored so we can unsubscribe when this directive is destroyed.
6362
*/
64-
private _layoutWatcher: Subscription;
63+
protected _layoutWatcher: Subscription;
6564

6665
@Input('fxFlex') set flex(val) {
6766
this._cacheInput("flex", val);
@@ -117,8 +116,8 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
117116
constructor(monitor: MediaMonitor,
118117
elRef: ElementRef,
119118
renderer: Renderer,
120-
@Optional() @SkipSelf() private _container: LayoutDirective,
121-
@Optional() @SkipSelf() private _wrap: LayoutWrapDirective) {
119+
@Optional() @SkipSelf() protected _container: LayoutDirective,
120+
@Optional() @SkipSelf() protected _wrap: LayoutWrapDirective) {
122121

123122
super(monitor, elRef, renderer);
124123

@@ -168,12 +167,12 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
168167
* Caches the parent container's 'flex-direction' and updates the element's style.
169168
* Used as a handler for layout change events from the parent flex container.
170169
*/
171-
private _onLayoutChange(direction?: string) {
170+
protected _onLayoutChange(direction?: string) {
172171
this._layout = direction || this._layout || "row";
173172
this._updateStyle();
174173
}
175174

176-
private _updateStyle(value?: string|number) {
175+
protected _updateStyle(value?: string|number) {
177176
let flexBasis = value || this._queryInput("flex") || '';
178177
if (this._mqActivation) {
179178
flexBasis = this._mqActivation.activatedInput;
@@ -186,7 +185,7 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
186185
/**
187186
* If the used the short-form `fxFlex="1 0 37%"`, then parse the parts
188187
*/
189-
private _parseFlexParts(basis: string) {
188+
protected _parseFlexParts(basis: string) {
190189
basis = basis.replace(";", "");
191190

192191
let hasCalc = basis && basis.indexOf("calc") > -1;
@@ -200,7 +199,7 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
200199
* e.g.
201200
* fxFlex="3 3 calc(15em + 20px)"
202201
*/
203-
private _getPartsWithCalc(value: string) {
202+
protected _getPartsWithCalc(value: string) {
204203
let parts = [this._queryInput("grow"), this._queryInput("shrink"), value];
205204
let j = value.indexOf('calc');
206205

@@ -219,7 +218,7 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
219218
* Validate the value to be one of the acceptable value options
220219
* Use default fallback of "row"
221220
*/
222-
private _validateValue(grow: number|string,
221+
protected _validateValue(grow: number|string,
223222
shrink: number|string,
224223
basis: string|number|FlexBasisAlias) {
225224
let css, isValue;

src/lib/flexbox/api/hide.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class HideDirective extends BaseFxDirective implements OnInit, OnChanges,
5050
* Subscription to the parent flex container's layout changes.
5151
* Stored so we can unsubscribe when this directive is destroyed.
5252
*/
53-
private _layoutWatcher: Subscription;
53+
protected _layoutWatcher: Subscription;
5454

5555
@Input('fxHide') set hide(val) {
5656
this._cacheInput("hide", val);
@@ -96,7 +96,7 @@ export class HideDirective extends BaseFxDirective implements OnInit, OnChanges,
9696
*
9797
*/
9898
constructor(monitor: MediaMonitor,
99-
@Optional() @Self() private _layout: LayoutDirective,
99+
@Optional() @Self() protected _layout: LayoutDirective,
100100
protected elRef: ElementRef,
101101
protected renderer: Renderer) {
102102
super(monitor, elRef, renderer);
@@ -169,7 +169,7 @@ export class HideDirective extends BaseFxDirective implements OnInit, OnChanges,
169169
/**
170170
* Validate the visibility value and then update the host's inline display style
171171
*/
172-
private _updateWithValue(value?: string|number|boolean) {
172+
protected _updateWithValue(value?: string|number|boolean) {
173173
value = value || this._getDefaultVal("hide", false);
174174
if (this._mqActivation) {
175175
value = this._mqActivation.activatedInput;
@@ -183,14 +183,14 @@ export class HideDirective extends BaseFxDirective implements OnInit, OnChanges,
183183
/**
184184
* Build the CSS that should be assigned to the element instance
185185
*/
186-
private _buildCSS(value) {
186+
protected _buildCSS(value) {
187187
return {'display': value ? 'none' : this._display};
188188
}
189189

190190
/**
191191
* Validate the value to NOT be FALSY
192192
*/
193-
private _validateTruthy(value) {
193+
protected _validateTruthy(value) {
194194
return FALSY.indexOf(value) === -1;
195195
}
196196
}

src/lib/flexbox/api/layout-align.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ import {LAYOUT_VALUES, LayoutDirective} from './layout';
4949
`})
5050
export class LayoutAlignDirective extends BaseFxDirective implements OnInit, OnChanges, OnDestroy {
5151

52-
private _layout = 'row'; // default flex-direction
53-
private _layoutWatcher: Subscription;
52+
protected _layout = 'row'; // default flex-direction
53+
protected _layoutWatcher: Subscription;
5454

5555
@Input('fxLayoutAlign') set align(val) { this._cacheInput('align', val); }
5656
@Input('fxLayoutAlign.xs') set alignXs(val) { this._cacheInput('alignXs', val); }
@@ -110,7 +110,7 @@ export class LayoutAlignDirective extends BaseFxDirective implements OnInit, OnC
110110
/**
111111
*
112112
*/
113-
private _updateWithValue(value?: string) {
113+
protected _updateWithValue(value?: string) {
114114
value = value || this._queryInput("align") || 'start stretch';
115115
if (this._mqActivation) {
116116
value = this._mqActivation.activatedInput;
@@ -123,7 +123,7 @@ export class LayoutAlignDirective extends BaseFxDirective implements OnInit, OnC
123123
/**
124124
* Cache the parent container 'flex-direction' and update the 'flex' styles
125125
*/
126-
private _onLayoutChange(direction) {
126+
protected _onLayoutChange(direction) {
127127
this._layout = (direction || '').toLowerCase();
128128
if (!LAYOUT_VALUES.find(x => x === this._layout)) {
129129
this._layout = 'row';
@@ -136,7 +136,7 @@ export class LayoutAlignDirective extends BaseFxDirective implements OnInit, OnC
136136
this._allowStretching(value, this._layout || "row");
137137
}
138138

139-
private _buildCSS(align) {
139+
protected _buildCSS(align) {
140140
let css = {}, [main_axis, cross_axis] = align.split(' '); // tslint:disable-line:variable-name
141141

142142
css['justify-content'] = 'flex-start'; // default main axis
@@ -188,7 +188,7 @@ export class LayoutAlignDirective extends BaseFxDirective implements OnInit, OnC
188188
* Update container element to 'stretch' as needed...
189189
* NOTE: this is only done if the crossAxis is explicitly set to 'stretch'
190190
*/
191-
private _allowStretching(align, layout) {
191+
protected _allowStretching(align, layout) {
192192
let [, cross_axis] = align.split(' '); // tslint:disable-line:variable-name
193193

194194
if (cross_axis == 'stretch') {

src/lib/flexbox/api/layout-gap.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ import {LayoutDirective, LAYOUT_VALUES} from './layout';
2828
* 'layout-padding' styling directive
2929
* Defines padding of child elements in a layout container
3030
*/
31-
@Directive({
32-
selector: `
31+
@Directive({selector: `
3332
[fxLayoutGap],
3433
[fxLayoutGap.xs],
3534
[fxLayoutGap.gt-xs],
@@ -44,9 +43,9 @@ import {LayoutDirective, LAYOUT_VALUES} from './layout';
4443
})
4544
export class LayoutGapDirective extends BaseFxDirective implements AfterContentInit, OnChanges,
4645
OnDestroy {
47-
private _layout = 'row'; // default flex-direction
48-
private _layoutWatcher: Subscription;
49-
private _observer: MutationObserver;
46+
protected _layout = 'row'; // default flex-direction
47+
protected _layoutWatcher: Subscription;
48+
protected _observer: MutationObserver;
5049

5150
@Input('fxLayoutGap') set gap(val) {
5251
this._cacheInput('gap', val);
@@ -139,7 +138,7 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
139138
* Watch for child nodes to be added... and apply the layout gap styles to each.
140139
* NOTE: this does NOT! differentiate between viewChildren and contentChildren
141140
*/
142-
private _watchContentChanges() {
141+
protected _watchContentChanges() {
143142
let onMutationCallback = (mutations) => {
144143
let validatedChanges = (it: MutationRecord) => {
145144
return (it.addedNodes && it.addedNodes.length) ||
@@ -159,7 +158,7 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
159158
/**
160159
* Cache the parent container 'flex-direction' and update the 'margin' styles
161160
*/
162-
private _onLayoutChange(direction) {
161+
protected _onLayoutChange(direction) {
163162
this._layout = (direction || '').toLowerCase();
164163
if (!LAYOUT_VALUES.find(x => x === this._layout)) {
165164
this._layout = 'row';
@@ -170,7 +169,7 @@ export class LayoutGapDirective extends BaseFxDirective implements AfterContentI
170169
/**
171170
*
172171
*/
173-
private _updateWithValue(value?: string) {
172+
protected _updateWithValue(value?: string) {
174173
value = value || this._queryInput("gap") || '0';
175174
if (this._mqActivation) {
176175
value = this._mqActivation.activatedInput;

src/lib/flexbox/api/layout-wrap.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ import {LayoutDirective, LAYOUT_VALUES} from './layout';
4242
[fxLayoutWrap.xl]
4343
`})
4444
export class LayoutWrapDirective extends BaseFxDirective implements OnInit, OnChanges, OnDestroy {
45-
private _layout = 'row'; // default flex-direction
46-
private _layoutWatcher: Subscription;
45+
protected _layout = 'row'; // default flex-direction
46+
protected _layoutWatcher: Subscription;
4747

4848
@Input('fxLayoutWrap') set wrap(val) { this._cacheInput("wrap", val); }
4949
@Input('fxLayoutWrap.xs') set wrapXs(val) { this._cacheInput('wrapXs', val); }
@@ -98,7 +98,7 @@ export class LayoutWrapDirective extends BaseFxDirective implements OnInit, OnCh
9898
/**
9999
* Cache the parent container 'flex-direction' and update the 'flex' styles
100100
*/
101-
private _onLayoutChange(direction) {
101+
protected _onLayoutChange(direction) {
102102
this._layout = (direction || '').toLowerCase().replace('-reverse', '');
103103
if (!LAYOUT_VALUES.find(x => x === this._layout)) {
104104
this._layout = 'row';
@@ -107,7 +107,7 @@ export class LayoutWrapDirective extends BaseFxDirective implements OnInit, OnCh
107107
this._updateWithValue();
108108
}
109109

110-
private _updateWithValue(value?: string) {
110+
protected _updateWithValue(value?: string) {
111111
value = value || this._queryInput("wrap") || 'wrap';
112112
if (this._mqActivation) {
113113
value = this._mqActivation.activatedInput;
@@ -121,7 +121,7 @@ export class LayoutWrapDirective extends BaseFxDirective implements OnInit, OnCh
121121
/**
122122
* Build the CSS that should be assigned to the element instance
123123
*/
124-
private _buildCSS(value) {
124+
protected _buildCSS(value) {
125125
return extendObject({ 'flex-wrap': value }, {
126126
'display' : 'flex',
127127
'flex-direction' : this._layout || 'row'
@@ -131,7 +131,7 @@ export class LayoutWrapDirective extends BaseFxDirective implements OnInit, OnCh
131131
/**
132132
* Convert layout-wrap="<value>" to expected flex-wrap style
133133
*/
134-
private _validateValue(value) {
134+
protected _validateValue(value) {
135135
switch (value.toLowerCase()) {
136136
case 'reverse':
137137
case 'wrap-reverse':

src/lib/flexbox/api/layout.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ export class LayoutDirective extends BaseFxDirective implements OnInit, OnChange
4949
* Create Observable for nested/child 'flex' directives. This allows
5050
* child flex directives to subscribe/listen for flexbox direction changes.
5151
*/
52-
private _announcer: BehaviorSubject<string>;
52+
protected _announcer: BehaviorSubject<string>;
5353

5454
/**
5555
* Publish observer to enabled nested, dependent directives to listen
5656
* to parent "layout" direction changes
5757
*/
5858
public layout$: Observable<string>;
5959

60-
61-
@Input('fxLayout') set layout(val) { this._cacheInput("layout", val); }
62-
@Input('fxLayout.xs') set layoutXs(val) { this._cacheInput('layoutXs', val); }
60+
@Input('fxLayout') set layout(val) { this._cacheInput("layout", val); };
61+
@Input('fxLayout.xs') set layoutXs(val) { this._cacheInput('layoutXs', val); };
6362
@Input('fxLayout.gt-xs') set layoutGtXs(val) { this._cacheInput('layoutGtXs', val); };
6463
@Input('fxLayout.sm') set layoutSm(val) { this._cacheInput('layoutSm', val); };
6564
@Input('fxLayout.gt-sm') set layoutGtSm(val) { this._cacheInput('layoutGtSm', val); };
@@ -111,7 +110,7 @@ export class LayoutDirective extends BaseFxDirective implements OnInit, OnChange
111110
/**
112111
* Validate the direction value and then update the host's inline flexbox styles
113112
*/
114-
private _updateWithDirection(direction?: string) {
113+
protected _updateWithDirection(direction?: string) {
115114
direction = direction || this._queryInput("layout") || 'row';
116115
if (this._mqActivation) {
117116
direction = this._mqActivation.activatedInput;
@@ -136,15 +135,15 @@ export class LayoutDirective extends BaseFxDirective implements OnInit, OnChange
136135
* laid out and drawn inside that element's specified width and height.
137136
*
138137
*/
139-
private _buildCSS(value) {
138+
protected _buildCSS(value) {
140139
return {'display': 'flex', 'box-sizing': 'border-box', 'flex-direction': value};
141140
}
142141

143142
/**
144143
* Validate the value to be one of the acceptable value options
145144
* Use default fallback of "row"
146145
*/
147-
private _validateValue(value) {
146+
protected _validateValue(value) {
148147
value = value ? value.toLowerCase() : '';
149148
return LAYOUT_VALUES.find(x => x === value) ? value : LAYOUT_VALUES[0]; // "row"
150149
}

0 commit comments

Comments
 (0)