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