Skip to content
This repository was archived by the owner on Oct 12, 2021. It is now read-only.

Commit ec90574

Browse files
committed
feat(AppShell): shellRender and shellNoRender attributes and implement different stripping strategies
- [x] Make the shell-related directives add `shellRender` and `shellNoRender` attributes. - [x] Encapsulate the "hiding", "showing" element logic into a service which is injected with DI and can its provider can be overridden. - [x] Add visitor which finds App Shell related comments in templates and processes them. - [x] Remove useless imports.
1 parent 42a61bf commit ec90574

29 files changed

+307
-56
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Provider} from '@angular/core';
2+
3+
import { IS_PRERENDER } from './is-prerender.service';
4+
import { TemplateVisibilityStrategy } from './template-visibility-strategy';
5+
import { TemplateCommentStrategy } from './template-comment-strategy';
6+
7+
export const APP_SHELL_RUNTIME_PROVIDERS: Provider[] = [
8+
{
9+
provide: IS_PRERENDER,
10+
useValue: false
11+
},
12+
{
13+
provide: TemplateVisibilityStrategy,
14+
useClass: TemplateCommentStrategy
15+
}
16+
];
17+
18+
export const APP_SHELL_BUILD_PROVIDERS: Provider[] = [
19+
{
20+
provide: IS_PRERENDER,
21+
useValue: true
22+
},
23+
{
24+
provide: TemplateVisibilityStrategy,
25+
useClass: TemplateCommentStrategy
26+
}
27+
];

app-shell/src/app/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './module';
22
export * from './prerender';
33
export * from './shell';
4+

app-shell/src/app/shell.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function () {
2222
expect(fixture.debugElement.childNodes.length).toBe(1);
2323
expect(fixture.debugElement.childNodes[0].nativeNode.data).toBe('template bindings={}');
2424
});
25+
2526
it('should render the element at runtime', () => {
2627
const fixture = TestBed
2728
.configureTestingModule({
@@ -55,6 +56,7 @@ export default function () {
5556
expect(fixture.debugElement.childNodes[0].nativeNode.data).toBe('template bindings={}');
5657
expect(fixture.debugElement.childNodes[1].nativeNode.name).toBe('div');
5758
});
59+
5860
it('should NOT render the element at runtime', () => {
5961
const fixture = TestBed
6062
.configureTestingModule({
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Injectable } from '@angular/core';
2+
import { __platform_browser_private__ as _ } from '@angular/platform-browser';
3+
4+
import { TemplateVisibilityStrategy } from './template-visibility-strategy';
5+
6+
@Injectable()
7+
export class TemplateCommentStrategy extends TemplateVisibilityStrategy {
8+
private _hidden: any[] = [];
9+
private _shown: any[] = [];
10+
11+
show(marker: string) {
12+
(this._rootNodes[marker] || [])
13+
.filter((node: any) => this._shown.indexOf(node) < 0)
14+
.forEach((node: any) => {
15+
_.getDOM().setAttribute(node, marker, '');
16+
this._shown.push(node);
17+
});
18+
}
19+
20+
hide(marker: string) {
21+
const DOM = _.getDOM();
22+
(this._rootNodes[marker] || [])
23+
.filter((node: any) => this._hidden.indexOf(node) < 0)
24+
.forEach((node: any) => {
25+
const comment = DOM.createComment(`${marker}(${DOM.getOuterHTML(node)})`);
26+
const parentNode = DOM.parentElement(node);
27+
DOM.replaceChild(parentNode, comment, node);
28+
this._hidden.push(node);
29+
});
30+
}
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface DirectiveNodes {
2+
[marker: string]: any[];
3+
};
4+
5+
export abstract class TemplateVisibilityStrategy {
6+
protected _rootNodes: DirectiveNodes = {};
7+
8+
abstract show(marker: string): void;
9+
abstract hide(marker: string): void;
10+
11+
setRootNodes(marker: string, ...rootNodes: any[]) {
12+
const nodes = this._rootNodes[marker] || [];
13+
this._rootNodes[marker] = nodes.concat(rootNodes);
14+
}
15+
}

app-shell/src/experimental/shell-parser/ast/ast-node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export interface ASTNode {
99
parentNode?: ASTNode;
1010
nodeName: string;
1111
value?: string;
12+
data?: string;
1213
}
13-
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from './ast-node';
2-

app-shell/src/experimental/shell-parser/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type RouteDefinition = string;
33
const SHELL_PARSER_CACHE_NAME = 'mobile-toolkit:app-shell';
44
const APP_SHELL_URL = './app_shell.html';
55
const NO_RENDER_CSS_SELECTOR = '[shellNoRender]';
6+
const RENDER_MARKER = 'shellRender';
67
const ROUTE_DEFINITIONS: RouteDefinition[] = [];
78
const INLINE_IMAGES: string[] = ['png', 'svg', 'jpg'];
89

@@ -13,6 +14,7 @@ export interface ShellParserConfig {
1314
APP_SHELL_URL?: string;
1415
SHELL_PARSER_CACHE_NAME?: string;
1516
NO_RENDER_CSS_SELECTOR?: string;
17+
RENDER_MARKER?: string;
1618
ROUTE_DEFINITIONS?: RouteDefinition[];
1719
INLINE_IMAGES?: string[];
1820
}
@@ -21,6 +23,7 @@ export const SHELL_PARSER_DEFAULT_CONFIG: ShellParserConfig = {
2123
SHELL_PARSER_CACHE_NAME,
2224
APP_SHELL_URL,
2325
NO_RENDER_CSS_SELECTOR,
26+
RENDER_MARKER,
2427
ROUTE_DEFINITIONS,
2528
INLINE_IMAGES
2629
};

app-shell/src/experimental/shell-parser/node-matcher/css-node-matcher.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import {
2-
inject
3-
} from '@angular/core/testing';
41
import { ASTNode } from '../ast';
52
import { CssSelector } from './css-selector';
63
import { CssNodeMatcher } from './css-node-matcher';

app-shell/src/experimental/shell-parser/node-matcher/css-selector/css-selector.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import {
2-
inject
3-
} from '@angular/core/testing';
41
import { CssSelector } from './css-selector';
52

63
describe('CssSelector', () => {
@@ -52,7 +49,5 @@ describe('CssSelector', () => {
5249
expect(result.elementId).toBe('baz');
5350
expect(result.classNames).toEqual(['foo', 'qux']);
5451
});
55-
5652
});
5753
});
58-

0 commit comments

Comments
 (0)