Skip to content

Commit 98d9d20

Browse files
committed
feat(renderer): implement simple nextSibling method using parent's _subViews
1 parent 25f5111 commit 98d9d20

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

Diff for: nativescript-angular/renderer.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class NativeScriptRendererFactory implements RendererFactoryV2 {
7070
}
7171

7272
export class NativeScriptRenderer extends RendererV2 {
73-
data: {[key: string]: any} = Object.create(null);
73+
data: { [key: string]: any } = Object.create(null);
7474

7575
constructor(
7676
private rootView: NgView,
@@ -83,25 +83,23 @@ export class NativeScriptRenderer extends RendererV2 {
8383

8484
appendChild(parent: any, newChild: NgView): void {
8585
traceLog(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`);
86-
console.log(typeof parent)
87-
console.log("appending child")
88-
console.log(newChild.id)
8986
this.viewUtil.insertChild(parent, newChild);
9087
}
9188

92-
93-
insertBefore(parent: any, newChild: any, _refChild: any): void {
89+
insertBefore(parent: NgView, newChild: NgView, refChildIndex: number): void {
9490
traceLog(`NativeScriptRenderer.insertBefore child: ${newChild} parent: ${parent}`);
91+
9592
if (parent) {
96-
// Temporary solution until we implement nextSibling method
97-
this.appendChild(parent, newChild);
98-
// parent.insertBefore(newChild, refChild);
93+
this.viewUtil.insertChild(parent, newChild, refChildIndex);
9994
}
10095
}
10196

10297
removeChild(parent: any, oldChild: NgView): void {
10398
traceLog(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`);
104-
this.viewUtil.removeChild(parent, oldChild);
99+
100+
if (parent) {
101+
this.viewUtil.removeChild(parent, oldChild);
102+
}
105103
}
106104

107105
selectRootElement(selector: string): NgView {
@@ -113,12 +111,13 @@ export class NativeScriptRenderer extends RendererV2 {
113111
return node.parent;
114112
}
115113

116-
nextSibling(_node: NgView): void {
117-
traceLog(`NativeScriptRenderer.nextSibling ${_node}`);
114+
nextSibling(node: NgView): number {
115+
traceLog(`NativeScriptRenderer.nextSibling ${node}`);
116+
return this.viewUtil.nextSibling(node);
118117
}
119118

120119
createViewRoot(hostElement: NgView): NgView {
121-
traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`)
120+
traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`);
122121
return hostElement;
123122
}
124123

@@ -192,7 +191,7 @@ export class NativeScriptRenderer extends RendererV2 {
192191

193192
createElement(name: any, _namespace: string): NgView {
194193
traceLog(`NativeScriptRenderer.createElement: ${name}`);
195-
return this.viewUtil.createView(name)
194+
return this.viewUtil.createView(name);
196195
}
197196

198197
createText(_value: string): NgView {

Diff for: nativescript-angular/view-util.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class ViewUtil {
5656
}
5757

5858
if (parent.meta && parent.meta.insertChild) {
59-
parent.meta.insertChild(parent, child, atIndex);
59+
parent.meta.insertChild(parent, child, atIndex);
6060
} else if (isLayout(parent)) {
6161
if (child.parent === parent) {
6262
let index = (<LayoutBase>parent).getChildIndex(child);
@@ -195,6 +195,18 @@ export class ViewUtil {
195195
}
196196
}
197197

198+
// finds the node in the parent's views and returns the next index
199+
// returns -1 if the node has no parent or next sibling
200+
public nextSibling(node: NgView): number {
201+
const parent = node.parent;
202+
if (!parent || typeof (<any>parent)._subViews === "undefined") {
203+
return -1;
204+
} else {
205+
const index = (<any>parent)._subViews.indexOf(node);
206+
return index === -1 ? index : index + 1;
207+
}
208+
}
209+
198210
private setPropertyInternal(view: NgView, attributeName: string, value: any): void {
199211
traceLog("Setting attribute: " + attributeName);
200212

0 commit comments

Comments
 (0)