1
1
import {
2
2
Inject , Injectable , Optional , NgZone ,
3
3
RendererV2 , RendererFactoryV2 , RendererTypeV2 ,
4
- // ViewEncapsulation
5
- // ɵAnimationStyles, ɵAnimationKeyframe,
4
+ ViewEncapsulation ,
6
5
} from "@angular/core" ;
7
6
8
7
import { escapeRegexSymbols } from "tns-core-modules/utils/utils" ;
@@ -21,8 +20,12 @@ import { Device } from "tns-core-modules/platform";
21
20
import { getRootPage } from "./platform-providers" ;
22
21
23
22
// CONTENT_ATTR not exported from NativeScript_renderer - we need it for styles application.
23
+ const COMPONENT_REGEX = / % C O M P % / g;
24
24
export const COMPONENT_VARIABLE = "%COMP%" ;
25
+ export const HOST_ATTR = `_nghost-${ COMPONENT_VARIABLE } ` ;
25
26
export const CONTENT_ATTR = `_ngcontent-${ COMPONENT_VARIABLE } ` ;
27
+ const ATTR_REPLACER = new RegExp ( escapeRegexSymbols ( CONTENT_ATTR ) , "g" ) ;
28
+ const ATTR_SANITIZER = / - / g;
26
29
27
30
@Injectable ( )
28
31
export class NativeScriptRendererFactory implements RendererFactoryV2 {
@@ -34,7 +37,7 @@ export class NativeScriptRendererFactory implements RendererFactoryV2 {
34
37
constructor (
35
38
@Optional ( ) @Inject ( APP_ROOT_VIEW ) rootView : View ,
36
39
@Inject ( DEVICE ) device : Device ,
37
- zone : NgZone
40
+ private zone : NgZone
38
41
) {
39
42
this . viewUtil = new ViewUtil ( device ) ;
40
43
this . setRootNgView ( rootView ) ;
@@ -55,30 +58,19 @@ export class NativeScriptRendererFactory implements RendererFactoryV2 {
55
58
}
56
59
57
60
let renderer : NativeScriptRenderer = this . componentRenderers . get ( type . id ) ;
58
- if ( isBlank ( renderer ) ) {
59
- renderer = this . defaultRenderer ;
60
-
61
- let stylesLength = type . styles . length ;
62
- for ( let i = 0 ; i < stylesLength ; i ++ ) {
63
- console . log ( type . styles [ i ] ) ;
64
- // this.hasComponentStyles = true;
65
- let cssString = type . styles [ i ] + "" ;
66
- const realCSS = this . replaceNgAttribute ( cssString , type . id ) ;
67
- addCss ( realCSS ) ;
68
- }
69
- this . componentRenderers . set ( type . id , renderer ) ;
61
+ if ( ! isBlank ( renderer ) ) {
62
+ return renderer ;
70
63
}
71
64
72
- return renderer ;
73
- }
74
-
75
- private attrReplacer = new RegExp ( escapeRegexSymbols ( CONTENT_ATTR ) , "g" ) ;
76
- private attrSanitizer = / - / g ;
77
-
65
+ if ( type . encapsulation === ViewEncapsulation . Emulated ) {
66
+ renderer = new EmulatedRenderer ( type , this . rootNgView , this . zone , this . viewUtil ) ;
67
+ ( < EmulatedRenderer > renderer ) . applyToHost ( element ) ;
68
+ } else {
69
+ renderer = this . defaultRenderer ;
70
+ }
78
71
79
- private replaceNgAttribute ( input : string , componentId : string ) : string {
80
- return input . replace ( this . attrReplacer ,
81
- "_ng_content_" + componentId . replace ( this . attrSanitizer , "_" ) ) ;
72
+ this . componentRenderers . set ( type . id , renderer ) ;
73
+ return renderer ;
82
74
}
83
75
}
84
76
@@ -94,16 +86,21 @@ export class NativeScriptRenderer extends RendererV2 {
94
86
traceLog ( "NativeScriptRenderer created" ) ;
95
87
}
96
88
97
- appendChild ( parent : any , newChild : any ) : void {
89
+ appendChild ( parent : any , newChild : NgView ) : void {
98
90
traceLog ( `NativeScriptRenderer.appendChild child: ${ newChild } parent: ${ parent } ` ) ;
91
+ console . log ( typeof parent )
92
+ console . log ( "appending child" )
93
+ console . log ( newChild . id )
99
94
this . viewUtil . insertChild ( parent , newChild ) ;
100
95
}
101
96
102
97
103
- insertBefore ( parent : any , newChild : any , refChild : any ) : void {
98
+ insertBefore ( parent : any , newChild : any , _refChild : any ) : void {
104
99
traceLog ( `NativeScriptRenderer.insertBefore child: ${ newChild } parent: ${ parent } ` ) ;
105
100
if ( parent ) {
106
- parent . insertBefore ( newChild , refChild ) ;
101
+ // Temporary solution until we implement nextSibling method
102
+ this . appendChild ( parent , newChild ) ;
103
+ // parent.insertBefore(newChild, refChild);
107
104
}
108
105
}
109
106
@@ -117,24 +114,22 @@ export class NativeScriptRenderer extends RendererV2 {
117
114
return this . rootView ;
118
115
}
119
116
120
- parentNode ( node : NgView ) : NgView {
121
- return node . templateParent ;
117
+ parentNode ( node : NgView ) : any {
118
+ return node . parent ;
122
119
}
123
120
124
121
nextSibling ( _node : NgView ) : void {
125
122
traceLog ( `NativeScriptRenderer.nextSibling ${ _node } ` ) ;
126
123
}
127
124
128
125
createViewRoot ( hostElement : NgView ) : NgView {
129
- traceLog ( "CREATE VIEW ROOT: " + hostElement . nodeName ) ;
126
+ traceLog ( `NativeScriptRenderer.createViewRoot ${ hostElement . nodeName } ` )
130
127
return hostElement ;
131
128
}
132
129
133
130
projectNodes ( parentElement : NgView , nodes : NgView [ ] ) : void {
134
131
traceLog ( "NativeScriptRenderer.projectNodes" ) ;
135
- nodes . forEach ( ( node ) => {
136
- this . viewUtil . insertChild ( parentElement , node ) ;
137
- } ) ;
132
+ nodes . forEach ( ( node ) => this . viewUtil . insertChild ( parentElement , node ) ) ;
138
133
}
139
134
140
135
destroy ( ) {
@@ -202,17 +197,7 @@ export class NativeScriptRenderer extends RendererV2 {
202
197
203
198
createElement ( name : any , _namespace : string ) : NgView {
204
199
traceLog ( `NativeScriptRenderer.createElement: ${ name } ` ) ;
205
-
206
- return this . viewUtil . createView ( name , view => {
207
- console . log ( view ) ;
208
- // Set an attribute to the view to scope component-specific css.
209
- // The property name is pre-generated by Angular.
210
-
211
- // if (this.hasComponentStyles) {
212
- // const cssAttribute = this.replaceNgAttribute(CONTENT_ATTR, this.componentProtoId);
213
- // view[cssAttribute] = true;
214
- // }
215
- } ) ;
200
+ return this . viewUtil . createView ( name )
216
201
}
217
202
218
203
createText ( _value : string ) : NgView {
@@ -239,3 +224,63 @@ export class NativeScriptRenderer extends RendererV2 {
239
224
}
240
225
}
241
226
227
+ class EmulatedRenderer extends NativeScriptRenderer {
228
+ private contentAttr : string ;
229
+ private hostAttr : string ;
230
+
231
+ constructor (
232
+ private component : RendererTypeV2 ,
233
+ rootView : NgView ,
234
+ zone : NgZone ,
235
+ viewUtil : ViewUtil ,
236
+ ) {
237
+ super ( rootView , zone , viewUtil ) ;
238
+
239
+ this . addStyles ( ) ;
240
+ this . contentAttr = shimContentAttribute ( component . id ) ;
241
+ this . hostAttr = shimHostAttribute ( component . id ) ;
242
+ }
243
+
244
+ applyToHost ( view : NgView ) {
245
+ super . setAttribute ( view , this . hostAttr , "" ) ;
246
+ }
247
+
248
+ appendChild ( parent : any , newChild : NgView ) : void {
249
+ // Set an attribute to the view to scope component-specific css.
250
+ // The property name is pre-generated by Angular.
251
+ const cssAttribute = this . replaceNgAttribute ( CONTENT_ATTR ) ;
252
+ newChild [ cssAttribute ] = true ;
253
+
254
+ super . appendChild ( parent , newChild ) ;
255
+ }
256
+
257
+ createElement ( parent : any , name : string ) : NgView {
258
+ const view = super . createElement ( parent , name ) ;
259
+ super . setAttribute ( view , this . contentAttr , "" ) ;
260
+
261
+ return view ;
262
+ }
263
+
264
+ private addStyles ( ) {
265
+ this . component . styles
266
+ . map ( s => s . toString ( ) )
267
+ . map ( s => this . replaceNgAttribute ( s ) )
268
+ . forEach ( addCss ) ;
269
+ }
270
+
271
+ private replaceNgAttribute ( input : string ) : string {
272
+ return input . replace ( ATTR_REPLACER , `_ng_content_${ this . componentId } ` ) ;
273
+ }
274
+
275
+ private get componentId ( ) : string {
276
+ return this . component . id . replace ( ATTR_SANITIZER , "_" ) ;
277
+ }
278
+ }
279
+
280
+ function shimContentAttribute ( componentShortId : string ) : string {
281
+ return CONTENT_ATTR . replace ( COMPONENT_REGEX , componentShortId ) ;
282
+ }
283
+
284
+ function shimHostAttribute ( componentShortId : string ) : string {
285
+ return HOST_ATTR . replace ( COMPONENT_REGEX , componentShortId ) ;
286
+ }
0 commit comments