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

Commit df4a612

Browse files
committed
feat(core): add centralized media marshal service
1 parent 1205588 commit df4a612

File tree

28 files changed

+896
-846
lines changed

28 files changed

+896
-846
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {Directive, Optional, Self} from '@angular/core';
2-
import {FlexDirective} from '@angular/flex-layout';
1+
import {Directive, ElementRef} from '@angular/core';
32

43
@Directive({
54
selector: '[ngxSplitArea]',
@@ -8,5 +7,5 @@ import {FlexDirective} from '@angular/flex-layout';
87
}
98
})
109
export class SplitAreaDirective {
11-
constructor(@Optional() @Self() public flex: FlexDirective) {}
10+
constructor(public elementRef: ElementRef) {}
1211
}

src/apps/demo-app/src/app/github-issues/split/split.directive.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
OnDestroy,
99
QueryList,
1010
} from '@angular/core';
11-
import {FlexDirective} from '@angular/flex-layout';
1211
import {Subscription} from 'rxjs';
12+
import {MediaMarshaller} from '@angular/flex-layout';
1313

1414
import {SplitHandleDirective} from './split-handle.directive';
1515
import {SplitAreaDirective} from './split-area.directive';
@@ -30,7 +30,7 @@ export class SplitDirective implements AfterContentInit, OnDestroy {
3030
@ContentChild(SplitHandleDirective) handle: SplitHandleDirective;
3131
@ContentChildren(SplitAreaDirective) areas: QueryList<SplitAreaDirective>;
3232

33-
constructor(private elementRef: ElementRef) {}
33+
constructor(private elementRef: ElementRef, private marshal: MediaMarshaller) {}
3434

3535
ngAfterContentInit(): void {
3636
this.watcher = this.handle.drag.subscribe(pos => this.onDrag(pos));
@@ -49,12 +49,16 @@ export class SplitDirective implements AfterContentInit, OnDestroy {
4949

5050
this.areas.forEach((area, i) => {
5151
// get the cur flex and the % in px
52-
const flex = (area.flex as FlexDirective);
52+
const elRef = area.elementRef;
5353
const delta = (i === 0) ? dragAmount : -dragAmount;
54-
const currentValue = flex.activatedValue;
54+
const currentValue = this.marshal.getValue(elRef.nativeElement, 'flex');
55+
56+
const changeValues = currentValue.split(' ');
57+
changeValues[2] = this.calculateSize(changeValues[2], delta) + '';
5558

5659
// Update Flex-Layout value to build/inject new flexbox CSS
57-
flex.activatedValue = this.calculateSize(currentValue, delta);
60+
this.marshal.setValue(elRef.nativeElement, 'flex', changeValues.join(' '),
61+
this.marshal.activatedBreakpoint);
5862
});
5963
}
6064

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {Directive, Optional, Self} from '@angular/core';
2-
import {FlexDirective} from '@angular/flex-layout';
1+
import {Directive, ElementRef} from '@angular/core';
32

43
@Directive({
54
selector: '[ngxSplitArea]',
@@ -8,5 +7,5 @@ import {FlexDirective} from '@angular/flex-layout';
87
}
98
})
109
export class SplitAreaDirective {
11-
constructor(@Optional() @Self() public flex: FlexDirective) { }
10+
constructor(public elementRef: ElementRef) { }
1211
}

src/apps/universal-app/src/app/split/split.directive.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
QueryList
1212
} from '@angular/core';
1313
import {isPlatformBrowser} from '@angular/common';
14-
import {FlexDirective} from '@angular/flex-layout';
14+
import {MediaMarshaller} from '@angular/flex-layout';
1515

1616
import {SplitHandleDirective} from './split-handle.directive';
1717
import {SplitAreaDirective} from './split-area.directive';
@@ -32,7 +32,8 @@ export class SplitDirective implements AfterContentInit, OnDestroy {
3232
@ContentChildren(SplitAreaDirective) areas: QueryList<SplitAreaDirective>;
3333

3434
constructor(private elementRef: ElementRef,
35-
@Inject(PLATFORM_ID) private _platformId: Object) {}
35+
@Inject(PLATFORM_ID) private _platformId: Object,
36+
private marshal: MediaMarshaller) {}
3637

3738
ngAfterContentInit(): void {
3839
if (isPlatformBrowser(this._platformId)) {
@@ -50,17 +51,21 @@ export class SplitDirective implements AfterContentInit, OnDestroy {
5051
* While dragging, continually update the `flex.activatedValue` for each area
5152
* managed by the splitter.
5253
*/
53-
onDrag({x, y}): void {
54+
onDrag({x, y}: {x: number, y: number}): void {
5455
const dragAmount = (this.direction === 'row') ? x : y;
5556

5657
this.areas.forEach((area, i) => {
5758
// get the cur flex and the % in px
58-
const flex = (area.flex as FlexDirective);
59+
const elRef = area.elementRef;
5960
const delta = (i === 0) ? dragAmount : -dragAmount;
60-
const currentValue = flex.activatedValue;
61+
const currentValue = this.marshal.getValue(elRef.nativeElement, 'flex');
62+
63+
const changeValues = currentValue.split(' ');
64+
changeValues[2] = this.calculateSize(changeValues[2], delta) + '';
6165

6266
// Update Flex-Layout value to build/inject new flexbox CSS
63-
flex.activatedValue = this.calculateSize(currentValue, delta);
67+
this.marshal.setValue(elRef.nativeElement, 'flex', changeValues.join(' '),
68+
this.marshal.activatedBreakpoint);
6469
});
6570
}
6671

src/lib/core/add-alias.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {extendObject} from '../utils/object-extend';
1313
* For the specified MediaChange, make sure it contains the breakpoint alias
1414
* and suffix (if available).
1515
*/
16-
export function mergeAlias(dest: MediaChange, source: BreakPoint | null) {
16+
export function mergeAlias(dest: MediaChange, source: BreakPoint | null): MediaChange {
1717
return extendObject(dest, source ? {
1818
mqAlias: source.alias,
1919
suffix: source.suffix

src/lib/core/base/base-adapter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {StyleUtils} from '../style-utils/style-utils';
1717
/**
1818
* Adapter to the BaseDirective abstract class so it can be used via composition.
1919
* @see BaseDirective
20+
* @deprecated
21+
* @deletion-target v7.0.0-beta.21
2022
*/
2123
export class BaseDirectiveAdapter extends BaseDirective {
2224

src/lib/core/base/base.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import {MediaMonitor} from '../media-monitor/media-monitor';
2323
import {MediaQuerySubscriber} from '../media-change';
2424
import {StyleBuilder} from '../style-builder/style-builder';
2525

26-
/** Abstract base class for the Layout API styling directives. */
26+
/**
27+
* Abstract base class for the Layout API styling directives.
28+
* @deprecated
29+
* @deletion-target v7.0.0-beta.21
30+
*/
2731
export abstract class BaseDirective implements OnDestroy, OnChanges {
2832

2933
/**

src/lib/core/base/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
export * from './base';
1010
export * from './base-adapter';
11+
export * from './new-base';

src/lib/core/base/new-base.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {ElementRef} from '@angular/core';
9+
10+
import {StyleDefinition, StyleUtils} from '../style-utils/style-utils';
11+
import {StyleBuilder} from '../style-builder/style-builder';
12+
import {MediaMarshaller} from '../media-marshaller/media-marshaller';
13+
import {buildLayoutCSS} from '../../utils/layout-validator';
14+
15+
export abstract class NewBaseDirective {
16+
17+
protected DIRECTIVE_KEY = '';
18+
19+
/** Access to host element's parent DOM node */
20+
protected get parentElement(): any {
21+
return this.elementRef.nativeElement.parentNode;
22+
}
23+
24+
protected get nativeElement(): HTMLElement {
25+
return this.elementRef.nativeElement;
26+
}
27+
28+
/** Cache map for style computation */
29+
protected styleCache: Map<string, StyleDefinition> = new Map();
30+
31+
protected constructor(protected elementRef: ElementRef,
32+
protected styleBuilder: StyleBuilder,
33+
protected styler: StyleUtils,
34+
protected marshal: MediaMarshaller) {
35+
}
36+
37+
/** Add styles to the element using predefined style builder */
38+
protected addStyles(input: string, parent?: Object) {
39+
const builder = this.styleBuilder;
40+
const useCache = builder.shouldCache;
41+
42+
let genStyles: StyleDefinition | undefined = this.styleCache.get(input);
43+
44+
if (!genStyles || !useCache) {
45+
genStyles = builder.buildStyles(input, parent);
46+
if (useCache) {
47+
this.styleCache.set(input, genStyles);
48+
}
49+
}
50+
51+
this.applyStyleToElement(genStyles);
52+
builder.sideEffect(input, genStyles, parent);
53+
}
54+
55+
/**
56+
* Determine the DOM element's Flexbox flow (flex-direction).
57+
*
58+
* Check inline style first then check computed (stylesheet) style.
59+
* And optionally add the flow value to element's inline style.
60+
*/
61+
protected getFlexFlowDirection(target: HTMLElement, addIfMissing = false): string {
62+
if (target) {
63+
let [value, hasInlineValue] = this.styler.getFlowDirection(target);
64+
65+
if (!hasInlineValue && addIfMissing) {
66+
const style = buildLayoutCSS(value);
67+
const elements = [target];
68+
this.styler.applyStyleToElements(style, elements);
69+
}
70+
71+
return value.trim();
72+
}
73+
74+
return 'row';
75+
}
76+
77+
/** Applies styles given via string pair or object map to the directive element */
78+
protected applyStyleToElement(style: StyleDefinition,
79+
value?: string | number,
80+
element: HTMLElement = this.nativeElement) {
81+
this.styler.applyStyleToElement(element, style, value);
82+
}
83+
84+
protected setValue(val: any, bp: string): void {
85+
this.marshal.setValue(this.nativeElement, this.DIRECTIVE_KEY, val, bp);
86+
}
87+
}

src/lib/core/breakpoints/break-point.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export interface BreakPoint {
1010
alias: string;
1111
suffix?: string;
1212
overlapping?: boolean;
13+
priority?: number;
14+
includes?: string[];
1315
}

0 commit comments

Comments
 (0)