Skip to content

Commit 809bf10

Browse files
sis0k0hdeshev
authored andcommitted
feat(renderer): implement simple nextSibling method using parent's _subViews
1 parent b7f5da2 commit 809bf10

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
@@ -75,7 +75,7 @@ export class NativeScriptRendererFactory implements RendererFactoryV2 {
7575
}
7676

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

8080
constructor(
8181
private rootView: NgView,
@@ -88,25 +88,23 @@ export class NativeScriptRenderer extends RendererV2 {
8888

8989
appendChild(parent: any, newChild: NgView): void {
9090
traceLog(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`);
91-
console.log(typeof parent)
92-
console.log("appending child")
93-
console.log(newChild.id)
9491
this.viewUtil.insertChild(parent, newChild);
9592
}
9693

97-
98-
insertBefore(parent: any, newChild: any, _refChild: any): void {
94+
insertBefore(parent: NgView, newChild: NgView, refChildIndex: number): void {
9995
traceLog(`NativeScriptRenderer.insertBefore child: ${newChild} parent: ${parent}`);
96+
10097
if (parent) {
101-
// Temporary solution until we implement nextSibling method
102-
this.appendChild(parent, newChild);
103-
// parent.insertBefore(newChild, refChild);
98+
this.viewUtil.insertChild(parent, newChild, refChildIndex);
10499
}
105100
}
106101

107102
removeChild(parent: any, oldChild: NgView): void {
108103
traceLog(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`);
109-
this.viewUtil.removeChild(parent, oldChild);
104+
105+
if (parent) {
106+
this.viewUtil.removeChild(parent, oldChild);
107+
}
110108
}
111109

112110
selectRootElement(selector: string): NgView {
@@ -118,12 +116,13 @@ export class NativeScriptRenderer extends RendererV2 {
118116
return node.parent;
119117
}
120118

121-
nextSibling(_node: NgView): void {
122-
traceLog(`NativeScriptRenderer.nextSibling ${_node}`);
119+
nextSibling(node: NgView): number {
120+
traceLog(`NativeScriptRenderer.nextSibling ${node}`);
121+
return this.viewUtil.nextSibling(node);
123122
}
124123

125124
createViewRoot(hostElement: NgView): NgView {
126-
traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`)
125+
traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`);
127126
return hostElement;
128127
}
129128

@@ -197,7 +196,7 @@ export class NativeScriptRenderer extends RendererV2 {
197196

198197
createElement(name: any, _namespace: string): NgView {
199198
traceLog(`NativeScriptRenderer.createElement: ${name}`);
200-
return this.viewUtil.createView(name)
199+
return this.viewUtil.createView(name);
201200
}
202201

203202
createText(_value: string): NgView {

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class ViewUtil {
5353
}
5454

5555
if (parent.meta && parent.meta.insertChild) {
56-
parent.meta.insertChild(parent, child, atIndex);
56+
parent.meta.insertChild(parent, child, atIndex);
5757
} else if (isLayout(parent)) {
5858
if (child.parent === parent) {
5959
let index = (<LayoutBase>parent).getChildIndex(child);
@@ -192,6 +192,18 @@ export class ViewUtil {
192192
}
193193
}
194194

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

0 commit comments

Comments
 (0)