forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_manipulation.ts
More file actions
162 lines (146 loc) Β· 5.01 KB
/
view_manipulation.ts
File metadata and controls
162 lines (146 loc) Β· 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {setActiveConsumer} from '@angular/core/primitives/signals';
import {Injector} from '../di/injector';
import {DehydratedContainerView} from '../hydration/interfaces';
import {hasInSkipHydrationBlockFlag} from '../hydration/skip_hydration';
import {assertDefined} from '../util/assert';
import {assertLContainer, assertLView, assertTNodeForLView} from './assert';
import {renderView} from './instructions/render';
import {createLView} from './instructions/shared';
import {CONTAINER_HEADER_OFFSET, LContainer, NATIVE} from './interfaces/container';
import {TNode} from './interfaces/node';
import {RComment, RElement} from './interfaces/renderer_dom';
import {
DECLARATION_LCONTAINER,
FLAGS,
HYDRATION,
LView,
LViewFlags,
QUERIES,
RENDERER,
T_HOST,
TVIEW,
} from './interfaces/view';
import {
addViewToDOM,
destroyLView,
detachView,
getBeforeNodeForView,
insertView,
nativeParentNode,
} from './node_manipulation';
export function createAndRenderEmbeddedLView<T>(
declarationLView: LView<unknown>,
templateTNode: TNode,
context: T,
options?: {
injector?: Injector;
embeddedViewInjector?: Injector;
dehydratedView?: DehydratedContainerView | null;
},
): LView<T> {
const prevConsumer = setActiveConsumer(null);
try {
const embeddedTView = templateTNode.tView!;
ngDevMode && assertDefined(embeddedTView, 'TView must be defined for a template node.');
ngDevMode && assertTNodeForLView(templateTNode, declarationLView);
// Embedded views follow the change detection strategy of the view they're declared in.
const isSignalView = declarationLView[FLAGS] & LViewFlags.SignalView;
const viewFlags = isSignalView ? LViewFlags.SignalView : LViewFlags.CheckAlways;
const embeddedLView = createLView<T>(
declarationLView,
embeddedTView,
context,
viewFlags,
null,
templateTNode,
null,
null,
options?.injector ?? null,
options?.embeddedViewInjector ?? null,
options?.dehydratedView ?? null,
);
const declarationLContainer = declarationLView[templateTNode.index];
ngDevMode && assertLContainer(declarationLContainer);
embeddedLView[DECLARATION_LCONTAINER] = declarationLContainer;
const declarationViewLQueries = declarationLView[QUERIES];
if (declarationViewLQueries !== null) {
embeddedLView[QUERIES] = declarationViewLQueries.createEmbeddedView(embeddedTView);
}
// execute creation mode of a view
renderView(embeddedTView, embeddedLView, context);
return embeddedLView;
} finally {
setActiveConsumer(prevConsumer);
}
}
export function getLViewFromLContainer<T>(
lContainer: LContainer,
index: number,
): LView<T> | undefined {
const adjustedIndex = CONTAINER_HEADER_OFFSET + index;
// avoid reading past the array boundaries
if (adjustedIndex < lContainer.length) {
const lView = lContainer[adjustedIndex];
ngDevMode && assertLView(lView);
return lView as LView<T>;
}
return undefined;
}
/**
* Returns whether an elements that belong to a view should be
* inserted into the DOM. For client-only cases, DOM elements are
* always inserted. For hydration cases, we check whether serialized
* info is available for a view and the view is not in a "skip hydration"
* block (in which case view contents was re-created, thus needing insertion).
*/
export function shouldAddViewToDom(
tNode: TNode,
dehydratedView?: DehydratedContainerView | null,
): boolean {
return (
!dehydratedView || dehydratedView.firstChild === null || hasInSkipHydrationBlockFlag(tNode)
);
}
export function addLViewToLContainer(
lContainer: LContainer,
lView: LView<unknown>,
index: number,
addToDOM = true,
): void {
const tView = lView[TVIEW];
// Insert into the view tree so the new view can be change-detected
insertView(tView, lView, lContainer, index);
// Insert elements that belong to this view into the DOM tree
if (addToDOM) {
const beforeNode = getBeforeNodeForView(index, lContainer);
const renderer = lView[RENDERER];
const parentRNode = nativeParentNode(renderer, lContainer[NATIVE] as RElement | RComment);
if (parentRNode !== null) {
addViewToDOM(tView, lContainer[T_HOST], renderer, lView, parentRNode, beforeNode);
}
}
// When in hydration mode, reset the pointer to the first child in
// the dehydrated view. This indicates that the view was hydrated and
// further attaching/detaching should work with this view as normal.
const hydrationInfo = lView[HYDRATION];
if (hydrationInfo !== null && hydrationInfo.firstChild !== null) {
hydrationInfo.firstChild = null;
}
}
export function removeLViewFromLContainer(
lContainer: LContainer,
index: number,
): LView<unknown> | undefined {
const lView = detachView(lContainer, index);
if (lView !== undefined) {
destroyLView(lView[TVIEW], lView);
}
return lView;
}