Skip to content

Commit 4c8dd79

Browse files
committed
refactor(renderer): set first child to every NgElement
1 parent 0bc7d77 commit 4c8dd79

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

nativescript-angular/element-registry.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ export type NgView = (View & ViewExtensions);
55
export type NgElement = NgView | InvisibleNode;
66

77
export interface ViewExtensions {
8+
meta: ViewClassMeta;
89
nodeType: number;
910
nodeName: string;
1011
templateParent: NgView;
1112
nextSibling: NgElement;
13+
firstChild: NgElement;
1214
lastChild: NgElement;
1315
ngCssClasses: Map<string, boolean>;
14-
meta: ViewClassMeta;
1516
}
1617

1718
export interface ElementReference {
@@ -25,13 +26,13 @@ export interface ViewClass {
2526

2627
export abstract class InvisibleNode extends View implements ViewExtensions {
2728
meta: { skipAddToDom: boolean };
28-
templateParent: NgView;
2929
nodeType: number;
3030
nodeName: string;
31-
ngCssClasses: Map<string, boolean>;
32-
previousSibling: NgElement;
31+
templateParent: NgView;
3332
nextSibling: NgElement;
33+
firstChild: NgElement;
3434
lastChild: NgElement;
35+
ngCssClasses: Map<string, boolean>;
3536

3637
constructor() {
3738
super();

nativescript-angular/view-util.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ export class ViewUtil {
5454
this.isAndroid = device.os === platformNames.android;
5555
}
5656

57-
public insertChild(parent: NgView, child: NgElement, previous?: NgElement, next?: NgElement) {
57+
public insertChild(
58+
parent: NgView,
59+
child: NgElement,
60+
previous: NgElement = parent.lastChild,
61+
next?: NgElement
62+
) {
5863
if (!parent) {
5964
return;
6065
}
@@ -68,23 +73,25 @@ export class ViewUtil {
6873
}
6974
}
7075

71-
private addToQueue(parent: NgElement, child: NgElement, previous: NgElement, next: NgElement) {
76+
private addToQueue(
77+
parent: NgElement,
78+
child: NgElement,
79+
previous: NgElement,
80+
next: NgElement
81+
): void {
82+
if (previous) {
83+
previous.nextSibling = child;
84+
} else {
85+
parent.firstChild = child;
86+
}
87+
7288
if (next) {
73-
this.insertBetween(child, previous, next);
89+
child.nextSibling = next;
7490
} else {
7591
this.appendToQueue(parent, child);
7692
}
7793
}
7894

79-
private insertBetween(
80-
view: NgElement,
81-
previous: NgElement,
82-
next: NgElement
83-
): void {
84-
previous.nextSibling = view;
85-
view.nextSibling = next;
86-
}
87-
8895
private appendToQueue(parent: NgElement, view: NgElement) {
8996
if (parent.lastChild) {
9097
parent.lastChild.nextSibling = view;
@@ -133,35 +140,43 @@ export class ViewUtil {
133140
this.removeLayoutChild(parent, child);
134141
} else if (isContentView(parent) && parent.content === child) {
135142
parent.content = null;
143+
parent.lastChild = null;
144+
parent.firstChild = null;
136145
} else if (isView(parent)) {
137146
parent._removeView(child);
138147
}
139148
}
140149

141150
private removeLayoutChild(parent: NgLayoutBase, child: NgView): void {
142151
const index = parent.getChildIndex(child);
152+
this.removeFromQueue(parent, child, index);
143153
if (index === -1) {
144154
return;
145155
}
146156

147-
this.removeFromQueue(parent, child, index);
148157
parent.removeChild(child);
149158
}
150159

151160
private removeFromQueue(parent: NgLayoutBase, child: NgView, index: number) {
152-
let previous = parent.getChildAt(index - 1) as NgElement;
153-
if (!previous) {
154-
return;
161+
if (parent.firstChild === child) {
162+
parent.firstChild = child.nextSibling;
163+
}
164+
165+
let previous = (parent.getChildAt(index - 1) || parent.firstChild) as NgElement;
166+
if (parent.lastChild === child) {
167+
parent.lastChild = previous;
155168
}
156169

157170
// since detached elements are not added to the visual tree,
158171
// we need to find the actual previous sibling of the view,
159-
// which may be an invisible node
160-
while (previous && previous !== child && isDetachedElement(previous.nextSibling)) {
172+
// which may as well be an invisible node
173+
while ( previous && previous !== child && isDetachedElement(previous.nextSibling)) {
161174
previous = previous.nextSibling;
162175
}
163176

164-
previous.nextSibling = child.nextSibling;
177+
if (previous) {
178+
previous.nextSibling = child.nextSibling;
179+
}
165180
}
166181

167182
public getChildIndex(parent: any, child: NgView) {

tests/app/tests/property-sets.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import {device, platformNames} from "platform";
1313
import {createDevice} from "./test-utils";
1414

1515
class TestView extends View implements ViewExtensions {
16-
public previousSibling: NgElement;
17-
public nextSibling: NgElement;
18-
public lastChild: NgElement;
19-
public nodeName: string = "TestView";
16+
public meta: ViewClassMeta = { skipAddToDom: false };
2017
public nodeType: number = 1;
18+
public nodeName: string = "TestView";
2119
public templateParent: NgView = null;
22-
public meta: ViewClassMeta = { skipAddToDom: false };
20+
public nextSibling: NgElement;
21+
public firstChild: NgElement;
22+
public lastChild: NgElement;
2323
public ngCssClasses: Map<string, boolean> = new Map<string, boolean>();
2424

2525
public stringValue: string = "";

0 commit comments

Comments
 (0)