Skip to content

Commit

Permalink
feat(renderer): implement simple nextSibling method using parent's _s…
Browse files Browse the repository at this point in the history
…ubViews
  • Loading branch information
sis0k0 committed Mar 16, 2017
1 parent 25f5111 commit 98d9d20
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
27 changes: 13 additions & 14 deletions nativescript-angular/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class NativeScriptRendererFactory implements RendererFactoryV2 {
}

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

constructor(
private rootView: NgView,
Expand All @@ -83,25 +83,23 @@ export class NativeScriptRenderer extends RendererV2 {

appendChild(parent: any, newChild: NgView): void {
traceLog(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`);
console.log(typeof parent)
console.log("appending child")
console.log(newChild.id)
this.viewUtil.insertChild(parent, newChild);
}


insertBefore(parent: any, newChild: any, _refChild: any): void {
insertBefore(parent: NgView, newChild: NgView, refChildIndex: number): void {
traceLog(`NativeScriptRenderer.insertBefore child: ${newChild} parent: ${parent}`);

if (parent) {
// Temporary solution until we implement nextSibling method
this.appendChild(parent, newChild);
// parent.insertBefore(newChild, refChild);
this.viewUtil.insertChild(parent, newChild, refChildIndex);
}
}

removeChild(parent: any, oldChild: NgView): void {
traceLog(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`);
this.viewUtil.removeChild(parent, oldChild);

if (parent) {
this.viewUtil.removeChild(parent, oldChild);
}
}

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

nextSibling(_node: NgView): void {
traceLog(`NativeScriptRenderer.nextSibling ${_node}`);
nextSibling(node: NgView): number {
traceLog(`NativeScriptRenderer.nextSibling ${node}`);
return this.viewUtil.nextSibling(node);
}

createViewRoot(hostElement: NgView): NgView {
traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`)
traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`);
return hostElement;
}

Expand Down Expand Up @@ -192,7 +191,7 @@ export class NativeScriptRenderer extends RendererV2 {

createElement(name: any, _namespace: string): NgView {
traceLog(`NativeScriptRenderer.createElement: ${name}`);
return this.viewUtil.createView(name)
return this.viewUtil.createView(name);
}

createText(_value: string): NgView {
Expand Down
14 changes: 13 additions & 1 deletion nativescript-angular/view-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ViewUtil {
}

if (parent.meta && parent.meta.insertChild) {
parent.meta.insertChild(parent, child, atIndex);
parent.meta.insertChild(parent, child, atIndex);
} else if (isLayout(parent)) {
if (child.parent === parent) {
let index = (<LayoutBase>parent).getChildIndex(child);
Expand Down Expand Up @@ -195,6 +195,18 @@ export class ViewUtil {
}
}

// finds the node in the parent's views and returns the next index
// returns -1 if the node has no parent or next sibling
public nextSibling(node: NgView): number {
const parent = node.parent;
if (!parent || typeof (<any>parent)._subViews === "undefined") {
return -1;
} else {
const index = (<any>parent)._subViews.indexOf(node);
return index === -1 ? index : index + 1;
}
}

private setPropertyInternal(view: NgView, attributeName: string, value: any): void {
traceLog("Setting attribute: " + attributeName);

Expand Down

0 comments on commit 98d9d20

Please sign in to comment.