@@ -19,8 +19,8 @@ import {
19
19
import { platformNames , Device } from "tns-core-modules/platform" ;
20
20
import { rendererLog as traceLog } from "./trace" ;
21
21
22
- const XML_ATTRIBUTES = Object . freeze ( [ "style" , "rows" , "columns" , "fontAttributes" ] ) ;
23
22
const ELEMENT_NODE_TYPE = 1 ;
23
+ const XML_ATTRIBUTES = Object . freeze ( [ "style" , "rows" , "columns" , "fontAttributes" ] ) ;
24
24
const whiteSpaceSplitter = / \s + / ;
25
25
26
26
export type ViewExtensions = ViewExtensions ;
@@ -53,37 +53,65 @@ export class ViewUtil {
53
53
this . isAndroid = device . os === platformNames . android ;
54
54
}
55
55
56
- public insertChild ( parent : any , child : NgElement , atIndex : number = - 1 ) {
56
+ public insertChild ( parent : NgView , child : NgElement , refChild ?: NgElement ) {
57
+ // handle invisible nodes..
57
58
if ( child instanceof InvisibleNode ) {
58
59
child . templateParent = parent ;
59
60
}
60
61
62
+ // add to queue
63
+ if ( ! parent ) {
64
+ return ;
65
+ }
66
+ const previousView = refChild || parent . lastChild ;
67
+ if ( previousView ) {
68
+ previousView . nextSibling = child ;
69
+ child . previousSibling = previousView ;
70
+ } else {
71
+ parent . lastChild = child ;
72
+ }
73
+
74
+ // create actual view
61
75
if ( ! parent || isDetachedElement ( child ) ) {
62
76
return ;
63
77
}
64
78
65
79
if ( parent . meta && parent . meta . insertChild ) {
66
- parent . meta . insertChild ( parent , child , atIndex ) ;
80
+ parent . meta . insertChild ( parent , child ) ;
67
81
} else if ( isLayout ( parent ) ) {
82
+ // remove child if already exists
68
83
if ( child . parent === parent ) {
69
- const index = ( < LayoutBase > parent ) . getChildIndex ( child ) ;
84
+ const index = parent . getChildIndex ( child ) ;
70
85
if ( index !== - 1 ) {
71
86
parent . removeChild ( child ) ;
72
87
}
73
88
}
74
- if ( atIndex !== - 1 ) {
89
+
90
+ // insert child
91
+ if ( refChild ) {
92
+ const atIndex = parent . getChildIndex ( refChild ) ;
75
93
parent . insertChild ( child , atIndex ) ;
76
94
} else {
77
95
parent . addChild ( child ) ;
78
96
}
79
97
} else if ( isContentView ( parent ) ) {
80
98
parent . content = child ;
81
- } else if ( parent && parent . _addChildFromBuilder ) {
82
- parent . _addChildFromBuilder ( child . nodeName , child ) ;
99
+ } else if ( parent && ( < any > parent ) . _addChildFromBuilder ) {
100
+ ( < any > parent ) . _addChildFromBuilder ( child . nodeName , child ) ;
83
101
}
84
102
}
85
103
86
- public removeChild ( parent : any , child : NgElement ) {
104
+ public removeChild ( parent : NgView , child : NgElement ) {
105
+ // remove from qeueue
106
+ if ( child . previousSibling ) {
107
+ child . previousSibling . nextSibling = child . nextSibling ;
108
+ }
109
+
110
+ if ( child . nextSibling ) {
111
+ child . nextSibling . previousSibling = child . previousSibling ;
112
+ }
113
+
114
+ // actual desctructuring
87
115
if ( ! parent || isDetachedElement ( child ) ) {
88
116
return ;
89
117
}
@@ -131,9 +159,7 @@ export class ViewUtil {
131
159
132
160
// we're setting the node type of the view
133
161
// to 'element' because of checks done in the
134
- // dom animation engine:
135
- // tslint:disable-next-line:max-line-length
136
- // https://github.com/angular/angular/blob/master/packages/animations/browser/src/render/dom_animation_engine.ts#L70-L81
162
+ // dom animation engine
137
163
view . nodeType = ELEMENT_NODE_TYPE ;
138
164
139
165
return view ;
@@ -170,32 +196,31 @@ export class ViewUtil {
170
196
171
197
// finds the node in the parent's views and returns the next index
172
198
// returns -1 if the node has no parent or next sibling
173
- public nextSiblingIndex ( node : NgView ) : number {
174
- const parent = node . parent ;
175
- if ( ! parent ) {
176
- return - 1 ;
177
- }
178
-
179
- let index = 0 ;
180
- let found = false ;
181
- parent . eachChild ( child => {
182
- if ( child === node ) {
183
- found = true ;
184
- }
185
-
186
- index += 1 ;
187
- return ! found ;
188
- } ) ;
189
-
190
- return found ? index : - 1 ;
191
- }
199
+ // public nextSiblingIndex(node: NgView): number {
200
+ // const parent = node.parent;
201
+ // if (!parent) {
202
+ // return -1;
203
+ // }
204
+
205
+ // let index = 0;
206
+ // let found = false;
207
+ // parent.eachChild(child => {
208
+ // if (child === node) {
209
+ // found = true;
210
+ // }
211
+
212
+ // index += 1;
213
+ // return !found;
214
+ // });
215
+
216
+ // return found ? index : -1;
217
+ // }
192
218
193
219
private runsIn ( platform : string ) : boolean {
194
220
return ( platform === "ios" && this . isIos ) ||
195
221
( platform === "android" && this . isAndroid ) ;
196
222
}
197
223
198
-
199
224
private setPropertyInternal ( view : NgView , attributeName : string , value : any ) : void {
200
225
traceLog ( `Setting attribute: ${ attributeName } =${ value } to ${ view } ` ) ;
201
226
0 commit comments