@@ -5,6 +5,7 @@ import { ContentView } from "tns-core-modules/ui/content-view";
5
5
import { LayoutBase } from "tns-core-modules/ui/layouts/layout-base" ;
6
6
import {
7
7
CommentNode ,
8
+ ElementReference ,
8
9
InvisibleNode ,
9
10
NgElement ,
10
11
NgView ,
@@ -53,35 +54,35 @@ export class ViewUtil {
53
54
this . isAndroid = device . os === platformNames . android ;
54
55
}
55
56
56
- public insertChild ( parent : NgView , child : NgElement , refChild ?: NgElement ) {
57
+ public insertChild ( parent : NgView , child : NgElement , previous ?: NgElement , next ?: NgElement ) {
57
58
// handle invisible nodes..
58
- if ( child instanceof InvisibleNode ) {
59
- child . templateParent = parent ;
59
+ if ( ! parent ) {
60
+ return ;
60
61
}
61
62
62
- // add to queue
63
- const previousView = refChild || ( parent && parent . lastChild ) ;
64
- if ( previousView ) {
65
- previousView . nextSibling = child ;
66
- child . previousSibling = previousView ;
63
+ if ( child instanceof InvisibleNode ) {
64
+ child . templateParent = parent ;
67
65
}
68
66
69
- if ( ! refChild && parent ) {
67
+ // add to end of queue if no next
68
+ if ( parent && ! next ) {
69
+ if ( parent . lastChild ) {
70
+ parent . lastChild . nextSibling = child ;
71
+ }
70
72
parent . lastChild = child ;
71
73
}
72
74
73
- // skip invisible elements ...
74
- while ( isDetachedElement ( refChild ) ) {
75
- refChild = refChild . previousSibling ;
76
- previousView . nextSibling = child ;
77
- child . previousSibling = previousView ;
75
+ // update queue if there is next
76
+ if ( next ) {
77
+ previous . nextSibling = child ;
78
+ child . nextSibling = next ;
78
79
}
79
80
80
- // create actual view
81
- if ( ! parent || isDetachedElement ( child ) ) {
81
+ if ( isDetachedElement ( child ) ) {
82
82
return ;
83
83
}
84
84
85
+ // create actual view
85
86
if ( parent . meta && parent . meta . insertChild ) {
86
87
parent . meta . insertChild ( parent , child ) ;
87
88
} else if ( isLayout ( parent ) ) {
@@ -95,12 +96,13 @@ export class ViewUtil {
95
96
96
97
// insert child
97
98
98
- if ( refChild ) {
99
- const atIndex = parent . getChildIndex ( refChild ) ;
99
+ if ( next ) {
100
+ const atIndex = parent . getChildIndex ( next ) ;
100
101
parent . insertChild ( child , atIndex ) ;
101
102
} else {
102
103
parent . addChild ( child ) ;
103
104
}
105
+
104
106
} else if ( isContentView ( parent ) ) {
105
107
parent . content = child ;
106
108
} else if ( parent && ( < any > parent ) . _addChildFromBuilder ) {
@@ -109,29 +111,34 @@ export class ViewUtil {
109
111
}
110
112
111
113
public removeChild ( parent : NgView , child : NgElement ) {
112
- // remove from qeueue
113
- if ( child . previousSibling ) {
114
- child . previousSibling . nextSibling = child . nextSibling ;
115
- }
116
-
117
- if ( child . nextSibling ) {
118
- child . nextSibling . previousSibling = child . previousSibling ;
119
- }
120
-
121
- // actual desctructuring
122
114
if ( ! parent || isDetachedElement ( child ) ) {
115
+ console . log ( "skip the removal of detached element" )
123
116
return ;
124
117
}
125
118
126
119
if ( parent . meta && parent . meta . removeChild ) {
127
120
parent . meta . removeChild ( parent , child ) ;
128
121
} else if ( isLayout ( parent ) ) {
122
+ console . log ( "remove child from layout" )
123
+ const atIndex = parent . getChildIndex ( child ) ;
124
+ if ( atIndex === - 1 ) {
125
+ return ;
126
+ }
127
+
128
+ if ( atIndex !== 0 ) {
129
+ const previous = parent . getChildAt ( atIndex - 1 ) as NgElement ;
130
+ previous . nextSibling = child . nextSibling ;
131
+ }
132
+
129
133
parent . removeChild ( child ) ;
134
+
130
135
} else if ( isContentView ( parent ) ) {
136
+ console . log ( "remove child from content view" ) ;
131
137
if ( parent . content === child ) {
132
138
parent . content = null ;
133
139
}
134
140
} else if ( isView ( parent ) ) {
141
+ console . log ( "remove child from view element" ) ;
135
142
parent . _removeView ( child ) ;
136
143
}
137
144
}
0 commit comments