diff --git a/nativescript-angular/element-registry.ts b/nativescript-angular/element-registry.ts index cb39569d..33925be7 100644 --- a/nativescript-angular/element-registry.ts +++ b/nativescript-angular/element-registry.ts @@ -9,6 +9,7 @@ export interface ViewExtensions { nodeName: string; parentNode: NgView; nextSibling: NgView; + previousSibling: NgView; firstChild: NgView; lastChild: NgView; ngCssClasses: Map; @@ -22,6 +23,7 @@ export abstract class InvisibleNode extends View implements NgView { nodeName: string; parentNode: NgView; nextSibling: NgView; + previousSibling: NgView; firstChild: NgView; lastChild: NgView; ngCssClasses: Map; diff --git a/nativescript-angular/renderer.ts b/nativescript-angular/renderer.ts index c91d0abc..99e5fd7b 100644 --- a/nativescript-angular/renderer.ts +++ b/nativescript-angular/renderer.ts @@ -6,11 +6,6 @@ import { ViewUtil } from './view-util'; import { NgView, InvisibleNode } from './element-registry'; import { NativeScriptDebug } from './trace'; -export interface ElementReference { - previous: NgView; - next: NgView; -} - @Injectable() export class NativeScriptRenderer extends Renderer2 { data: { [key: string]: any } = Object.create(null); @@ -31,8 +26,8 @@ export class NativeScriptRenderer extends Renderer2 { } @profile - insertBefore(parent: NgView, newChild: NgView, refChild: NgView | ElementReference): void { - let { previous, next } = refChild instanceof View ? this.nextSibling(refChild) : refChild; + insertBefore(parent: NgView, newChild: NgView, refChild: NgView): void { + let { previous, next } = refChild instanceof View ? { previous: refChild.previousSibling, next: refChild } : { previous: null, next: null }; if (NativeScriptDebug.isLogEnabled()) { NativeScriptDebug.rendererLog(`NativeScriptRenderer.insertBefore child: ${newChild} ` + `parent: ${parent} previous: ${previous} next: ${next}`); } @@ -68,15 +63,12 @@ export class NativeScriptRenderer extends Renderer2 { } @profile - nextSibling(node: NgView): ElementReference { + nextSibling(node: NgView): NgView { if (NativeScriptDebug.isLogEnabled()) { NativeScriptDebug.rendererLog(`NativeScriptRenderer.nextSibling of ${node} is ${node.nextSibling}`); } - return { - previous: node, - next: node.nextSibling, - }; + return node.nextSibling; } @profile diff --git a/nativescript-angular/view-util.ts b/nativescript-angular/view-util.ts index b0b7fc1c..ea2c3976 100644 --- a/nativescript-angular/view-util.ts +++ b/nativescript-angular/view-util.ts @@ -63,12 +63,14 @@ export class ViewUtil { if (previous) { previous.nextSibling = child; + child.previousSibling = previous; } else { parent.firstChild = child; } if (next) { child.nextSibling = next; + next.previousSibling = child; } else { this.appendToQueue(parent, child); } @@ -81,6 +83,7 @@ export class ViewUtil { if (parent.lastChild) { parent.lastChild.nextSibling = view; + view.previousSibling = parent.lastChild; } parent.lastChild = view; @@ -152,6 +155,7 @@ export class ViewUtil { parent.firstChild = null; parent.lastChild = null; child.nextSibling = null; + child.previousSibling = null; return; } @@ -159,16 +163,20 @@ export class ViewUtil { parent.firstChild = child.nextSibling; } - const previous = this.findPreviousElement(parent, child); + const previous = child.previousSibling; if (parent.lastChild === child) { parent.lastChild = previous; } if (previous) { previous.nextSibling = child.nextSibling; + if (child.nextSibling) { + child.nextSibling.previousSibling = previous; + } } child.nextSibling = null; + child.previousSibling = null; } // NOTE: This one is O(n) - use carefully