@@ -11,6 +11,15 @@ import {ContainerState, LContainer, LElement, LNode, LNodeFlags, LProjection, LT
1111import { assertNodeType } from './node_assert' ;
1212import { RComment , RElement , RNode , RText , Renderer3Fn } from './renderer' ;
1313
14+ /**
15+ * Finds the closest DOM node above a given container in the hierarchy.
16+ *
17+ * This is necessary to add or remove elements from the DOM when a view
18+ * is added or removed from the container. e.g. parent.removeChild(...)
19+ *
20+ * @param {LContainer } containerNode The container node whose parent must be found
21+ * @returns {RNode }
22+ */
1423export function findNativeParent ( containerNode : LContainer ) : RNode | null {
1524 let container : LContainer | null = containerNode ;
1625 while ( container ) {
@@ -32,6 +41,19 @@ export function findNativeParent(containerNode: LContainer): RNode|null {
3241 return null ;
3342}
3443
44+ /**
45+ * Finds the DOM element before which a certain view should be inserting its
46+ * child elements.
47+ *
48+ * If the view has a next (e.g. for loop), elements should be inserted before
49+ * the next view's first child element. Otherwise, the container's comment
50+ * anchor is the marker.
51+ *
52+ * @param {number } index The index of the view to check
53+ * @param {ContainerState } state ContainerState of the parent container
54+ * @param {RComment } native Comment anchor for container
55+ * @returns {RElement | RText | RComment }
56+ */
3557export function findBeforeNode ( index : number , state : ContainerState , native : RComment ) : RElement |
3658 RText | RComment {
3759 const children = state . children ;
@@ -41,6 +63,18 @@ export function findBeforeNode(index: number, state: ContainerState, native: RCo
4163 native ;
4264}
4365
66+ /**
67+ * Adds or removes all DOM elements associated with a view.
68+ *
69+ * Because some root nodes of the view may be containers, we sometimes need
70+ * to propagate deeply into the nested containers to remove all elements in the
71+ * views beneath it.
72+ *
73+ * @param {LContainer } container - The container to which the root view belongs
74+ * @param {LView } rootNode - The view from which elements should be added or removed
75+ * @param {boolean } insertMode - Whether or not elements should be added (if false, removing)
76+ * @param {RNode } beforeNode - The node before which elements should be added, if insert mode
77+ */
4478export function addRemoveViewFromContainer (
4579 container : LContainer , rootNode : LView , insertMode : true , beforeNode : RNode | null ) : void ;
4680export function addRemoveViewFromContainer (
@@ -98,14 +132,17 @@ export function addRemoveViewFromContainer(
98132}
99133
100134/**
101- * Traverses the tree of component views and containers to remove listeners.
135+ * Traverses the tree of component views and containers to remove listeners and
136+ * call onDestroy callbacks.
102137 *
103138 * Notes:
104- * - Will be used for onDestroy calls later, so needs to be bottom-up.
139+ * - Because it's used for onDestroy calls, it needs to be bottom-up.
105140 * - Must process containers instead of their views to avoid splicing
106141 * when views are destroyed and re-added.
107- * - Using a while loop because it's faster than recursing
142+ * - Using a while loop because it's faster than recursion
108143 * - Destroy only called on movement to sibling or movement to parent (laterally or up)
144+ *
145+ * @param {ViewState } rootView - The view to destroy
109146 */
110147export function destroyViewTree ( rootView : ViewState ) : void {
111148 let viewOrContainerState : ViewOrContainerState | null = rootView ;
@@ -135,6 +172,19 @@ export function destroyViewTree(rootView: ViewState): void {
135172 }
136173}
137174
175+ /**
176+ * Inserts a view into a container.
177+ *
178+ * This adds the view to the container's array of active children in the correct
179+ * position. It also adds the view's elements to the DOM if the container isn't a
180+ * root node of another view (in that case, the view's elements will be added when
181+ * the container's parent view is added later).
182+ *
183+ * @param {LContainer } container - The container into which the view should be inserted
184+ * @param {LView } newView - The view to insert
185+ * @param {number } index - The index at which to insert the view
186+ * @returns {LView } - The inserted view
187+ */
138188export function insertView ( container : LContainer , newView : LView , index : number ) : LView {
139189 const state = container . data ;
140190 const children = state . children ;
@@ -169,7 +219,17 @@ export function insertView(container: LContainer, newView: LView, index: number)
169219 return newView ;
170220}
171221
172-
222+ /**
223+ * Removes a view from a container.
224+ *
225+ * This method splices the view from the container's array of active children. It also
226+ * removes the view's elements from the DOM and conducts cleanup (e.g. removing
227+ * listeners, calling onDestroys).
228+ *
229+ * @param {LContainer } container - The container from which to remove a view
230+ * @param {number } removeIndex - The index of the view to remove
231+ * @returns {LView } - The removed view
232+ */
173233export function removeView ( container : LContainer , removeIndex : number ) : LView {
174234 const children = container . data . children ;
175235 const viewNode = children [ removeIndex ] ;
@@ -184,11 +244,31 @@ export function removeView(container: LContainer, removeIndex: number): LView {
184244 return viewNode ;
185245}
186246
247+ /**
248+ * Sets a next on the view node, so views in for loops can easily jump from
249+ * one view to the next to add/remove elements. Also adds the ViewState (view.data)
250+ * to the view tree for easy traversal when cleaning up the view.
251+ *
252+ * @param {LView } view - The view to set up
253+ * @param {LView } next - The view's new next
254+ */
187255export function setViewNext ( view : LView , next : LView | null ) : void {
188256 view . next = next ;
189257 view . data . next = next ? next . data : null ;
190258}
191259
260+ /**
261+ * Determines which ViewOrContainerState to jump to when traversing back up the
262+ * tree in destroyViewTree.
263+ *
264+ * Normally, the view's parent ViewState should be checked, but in the case of
265+ * embedded views, the container (which is the view node's parent, but not the
266+ * ViewState's parent) needs to be checked for a possible next property.
267+ *
268+ * @param {ViewOrContainerState } state - The ViewOrContainerState for which we need a parent state
269+ * @param {ViewState } rootView - The rootView, so we don't propagate too far up the view tree
270+ * @returns {ViewOrContainerState }
271+ */
192272export function getParentState (
193273 state : ViewOrContainerState , rootView : ViewState ) : ViewOrContainerState | null {
194274 let node ;
@@ -203,7 +283,11 @@ export function getParentState(
203283 }
204284}
205285
206- /** Removes all listeners and call all onDestroys in a given view. */
286+ /**
287+ * Removes all listeners and call all onDestroys in a given view.
288+ *
289+ * @param {ViewState } viewState - The ViewState of the view to clean up
290+ */
207291function cleanUpView ( viewState : ViewState ) : void {
208292 if ( ! viewState . cleanup ) return ;
209293 const cleanup = viewState . cleanup ! ;
@@ -218,6 +302,19 @@ function cleanUpView(viewState: ViewState): void {
218302 viewState . cleanup = null ;
219303}
220304
305+ /**
306+ * Appends the provided child element to the provided parent, if appropriate.
307+ *
308+ * If the parent is a view, the element will be appended as part of viewEnd(), so
309+ * the element should not be appended now. Similarly, if the child is a content child
310+ * of a parent component, the child will be appended to the right position later by
311+ * the content projection system. Otherwise, append normally.
312+ *
313+ * @param {LNode } parent - The parent to which to append the child
314+ * @param {RNode } child - The child that should be appended
315+ * @param {ViewState } currentView - The current view's ViewState
316+ * @returns {boolean } - Whether or not the child was appended
317+ */
221318export function appendChild ( parent : LNode , child : RNode | null , currentView : ViewState ) : boolean {
222319 // Only add native child element to parent element if the parent element is regular Element.
223320 // If parent is:
@@ -242,7 +339,18 @@ export function appendChild(parent: LNode, child: RNode | null, currentView: Vie
242339 return false ;
243340}
244341
245- export function insertChild ( node : LNode , currentView : ViewState ) {
342+ /**
343+ * Inserts the provided node before the correct element in the DOM, if appropriate.
344+ *
345+ * If the parent is a view, the element will be inserted as part of viewEnd(), so
346+ * the element should not be inserted now. Similarly, if the child is a content child
347+ * of a parent component, the child will be inserted to the right position later by
348+ * the content projection system. Otherwise, insertBefore normally.
349+ *
350+ * @param {LNode } node - Node to insert
351+ * @param {ViewState } currentView - The current view's ViewState
352+ */
353+ export function insertChild ( node : LNode , currentView : ViewState ) : void {
246354 const parent = node . parent ! ;
247355 // Only add child element to parent element if the parent element is regular Element.
248356 // If parent is:
@@ -270,9 +378,19 @@ export function insertChild(node: LNode, currentView: ViewState) {
270378 }
271379}
272380
381+ /**
382+ * Appends a projected node to the DOM, or in the case of a projected container,
383+ * appends the nodes from all of the container's active views to the DOM. Also stores the
384+ * node in the given projectedNodes array.
385+ *
386+ * @param {ProjectionState } projectedNodes - Array to store the projected node
387+ * @param {LElement | LText | LContainer } node - The node to process
388+ * @param {LView | LElement } currentParent - The last parent element to be processed
389+ * @param {ViewState } currentView - The current view's ViewState
390+ */
273391export function processProjectedNode (
274392 projectedNodes : ProjectionState , node : LElement | LText | LContainer ,
275- currentParent : LView | LElement , currentView : ViewState ) {
393+ currentParent : LView | LElement , currentView : ViewState ) : void {
276394 if ( ( node . flags & LNodeFlags . TYPE_MASK ) === LNodeFlags . Container &&
277395 ( currentParent . flags & LNodeFlags . TYPE_MASK ) === LNodeFlags . Element &&
278396 ( currentParent . data === null || currentParent . data === currentView ) ) {
0 commit comments