@@ -6,16 +6,16 @@ import "./polyfills/array";
66import "./polyfills/console" ;
77
88import {
9- Type ,
10- Injector ,
11- CompilerOptions ,
12- PlatformRef ,
13- NgModuleFactory ,
14- NgModuleRef ,
15- EventEmitter ,
16- Provider ,
17- Sanitizer ,
18- OpaqueToken ,
9+ Type ,
10+ Injector ,
11+ CompilerOptions ,
12+ PlatformRef ,
13+ NgModuleFactory ,
14+ NgModuleRef ,
15+ EventEmitter ,
16+ Provider ,
17+ Sanitizer ,
18+ OpaqueToken ,
1919} from "@angular/core" ;
2020
2121// Work around a TS bug requiring an import of OpaqueToken without using it
@@ -42,158 +42,164 @@ let lastBootstrappedModule: WeakRef<NgModuleRef<any>>;
4242type BootstrapperAction = ( ) => Promise < NgModuleRef < any > > ;
4343
4444export interface AppOptions {
45- bootInExistingPage : boolean ;
46- cssFile ?: string ;
47- startPageActionBarHidden ?: boolean ;
45+ bootInExistingPage ? : boolean ;
46+ cssFile ?: string ;
47+ startPageActionBarHidden ?: boolean ;
4848}
4949
5050export type PlatformFactory = ( extraProviders ?: Provider [ ] ) => PlatformRef ;
5151
5252export class NativeScriptSanitizer extends Sanitizer {
53- sanitize ( _context : any , value : string ) : string {
54- return value ;
55- }
53+ sanitize ( _context : any , value : string ) : string {
54+ return value ;
55+ }
5656}
5757
5858export const COMMON_PROVIDERS = [
59- defaultPageFactoryProvider ,
60- { provide : Sanitizer , useClass : NativeScriptSanitizer } ,
59+ defaultPageFactoryProvider ,
60+ { provide : Sanitizer , useClass : NativeScriptSanitizer } ,
6161] ;
6262
6363export class NativeScriptPlatformRef extends PlatformRef {
64- private _bootstrapper : BootstrapperAction ;
64+ private _bootstrapper : BootstrapperAction ;
6565
66- constructor ( private platform : PlatformRef , private appOptions ?: AppOptions ) {
67- super ( ) ;
68- }
66+ constructor ( private platform : PlatformRef , private appOptions ?: AppOptions ) {
67+ super ( ) ;
68+ }
6969
70- bootstrapModuleFactory < M > ( moduleFactory : NgModuleFactory < M > ) : Promise < NgModuleRef < M > > {
71- this . _bootstrapper = ( ) => this . platform . bootstrapModuleFactory ( moduleFactory ) ;
70+ bootstrapModuleFactory < M > ( moduleFactory : NgModuleFactory < M > ) : Promise < NgModuleRef < M > > {
71+ this . _bootstrapper = ( ) => this . platform . bootstrapModuleFactory ( moduleFactory ) ;
7272
73- this . bootstrapApp ( ) ;
73+ this . bootstrapApp ( ) ;
7474
75- return null ; // Make the compiler happy
76- }
75+ return null ; // Make the compiler happy
76+ }
7777
78- bootstrapModule < M > (
79- moduleType : Type < M > ,
80- compilerOptions : CompilerOptions | CompilerOptions [ ] = [ ]
81- ) : Promise < NgModuleRef < M > > {
82- this . _bootstrapper = ( ) => this . platform . bootstrapModule ( moduleType , compilerOptions ) ;
78+ bootstrapModule < M > (
79+ moduleType : Type < M > ,
80+ compilerOptions : CompilerOptions | CompilerOptions [ ] = [ ]
81+ ) : Promise < NgModuleRef < M > > {
82+ this . _bootstrapper = ( ) => this . platform . bootstrapModule ( moduleType , compilerOptions ) ;
8383
84- this . bootstrapApp ( ) ;
84+ this . bootstrapApp ( ) ;
8585
86- return null ; // Make the compiler happy
87- }
86+ return null ; // Make the compiler happy
87+ }
8888
89- private bootstrapApp ( ) {
90- global . __onLiveSyncCore = ( ) => this . livesyncModule ( ) ;
89+ private bootstrapApp ( ) {
90+ global . __onLiveSyncCore = ( ) => this . livesyncModule ( ) ;
9191
92- const mainPageEntry = this . createNavigationEntry ( this . _bootstrapper ) ;
92+ const mainPageEntry = this . createNavigationEntry ( this . _bootstrapper ) ;
9393
94- application . start ( mainPageEntry ) ;
95- }
94+ if ( this . appOptions && typeof this . appOptions . cssFile === "string" ) {
95+ // TODO: All exported filed in ES6 modules should be read-only
96+ // Change the case when tns-core-modules become ES6 compatible and there is a legal way to set cssFile
97+ ( < any > application ) . cssFile = this . appOptions . cssFile ;
98+ }
9699
97- livesyncModule ( ) : void {
98- rendererLog ( "ANGULAR LiveSync Started" ) ;
100+ application . start ( mainPageEntry ) ;
101+ }
99102
100- onBeforeLivesync . next ( lastBootstrappedModule ? lastBootstrappedModule . get ( ) : null ) ;
103+ livesyncModule ( ) : void {
104+ rendererLog ( "ANGULAR LiveSync Started" ) ;
101105
102- const mainPageEntry = this . createNavigationEntry (
103- this . _bootstrapper ,
104- compRef => onAfterLivesync . next ( compRef ) ,
105- error => onAfterLivesync . error ( error ) ,
106- true
107- ) ;
108- mainPageEntry . animated = false ;
109- mainPageEntry . clearHistory = true ;
106+ onBeforeLivesync . next ( lastBootstrappedModule ? lastBootstrappedModule . get ( ) : null ) ;
110107
111- const frame = topmost ( ) ;
112- if ( frame ) {
113- if ( frame . currentPage && frame . currentPage . modal ) {
114- frame . currentPage . modal . closeModal ( ) ;
115- }
116- frame . navigate ( mainPageEntry ) ;
117- }
118- }
119-
120- onDestroy ( callback : ( ) => void ) : void {
121- this . platform . onDestroy ( callback ) ;
122- }
123-
124- get injector ( ) : Injector {
125- return this . platform . injector ;
126- } ;
127-
128- destroy ( ) : void {
129- this . platform . destroy ( ) ;
130- }
131-
132- get destroyed ( ) : boolean {
133- return this . platform . destroyed ;
134- }
135-
136- private createNavigationEntry (
137- bootstrapAction : BootstrapperAction ,
138- resolve ?: ( comp : NgModuleRef < any > ) => void ,
139- reject ?: ( e : Error ) => void ,
140- isLivesync : boolean = false ,
141- isReboot : boolean = false ) : NavigationEntry {
142-
143- const pageFactory : PageFactory = this . platform . injector . get ( PAGE_FACTORY ) ;
144-
145- const navEntry : NavigationEntry = {
146- create : ( ) : Page => {
147- let page = pageFactory ( { isBootstrap : true , isLivesync } ) ;
148- if ( this . appOptions ) {
149- page . actionBarHidden = this . appOptions . startPageActionBarHidden ;
108+ const mainPageEntry = this . createNavigationEntry (
109+ this . _bootstrapper ,
110+ compRef => onAfterLivesync . next ( compRef ) ,
111+ error => onAfterLivesync . error ( error ) ,
112+ true
113+ ) ;
114+ mainPageEntry . animated = false ;
115+ mainPageEntry . clearHistory = true ;
116+
117+ const frame = topmost ( ) ;
118+ if ( frame ) {
119+ if ( frame . currentPage && frame . currentPage . modal ) {
120+ frame . currentPage . modal . closeModal ( ) ;
121+ }
122+ frame . navigate ( mainPageEntry ) ;
150123 }
124+ }
125+
126+ onDestroy ( callback : ( ) => void ) : void {
127+ this . platform . onDestroy ( callback ) ;
128+ }
129+
130+ get injector ( ) : Injector {
131+ return this . platform . injector ;
132+ } ;
151133
152- let onLoadedHandler = function ( ) {
153- page . off ( "loaded" , onLoadedHandler ) ;
154- // profiling.stop("application-start");
155- rendererLog ( "Page loaded" ) ;
134+ destroy ( ) : void {
135+ this . platform . destroy ( ) ;
136+ }
156137
157- // profiling.start("ng-bootstrap");
158- rendererLog ( "BOOTSTRAPPING..." ) ;
159- bootstrapAction ( ) . then ( ( moduleRef ) => {
160- // profiling.stop("ng-bootstrap");
161- rendererLog ( "ANGULAR BOOTSTRAP DONE." ) ;
162- lastBootstrappedModule = new WeakRef ( moduleRef ) ;
138+ get destroyed ( ) : boolean {
139+ return this . platform . destroyed ;
140+ }
163141
164- if ( resolve ) {
165- resolve ( moduleRef ) ;
166- }
167- return moduleRef ;
168- } , ( err ) => {
169- rendererError ( "ERROR BOOTSTRAPPING ANGULAR" ) ;
170- let errorMessage = err . message + "\n\n" + err . stack ;
171- rendererError ( errorMessage ) ;
172-
173- let view = new TextView ( ) ;
174- view . text = errorMessage ;
175- page . content = view ;
176-
177- if ( reject ) {
178- reject ( err ) ;
142+ private createNavigationEntry (
143+ bootstrapAction : BootstrapperAction ,
144+ resolve ?: ( comp : NgModuleRef < any > ) => void ,
145+ reject ?: ( e : Error ) => void ,
146+ isLivesync : boolean = false ,
147+ isReboot : boolean = false ) : NavigationEntry {
148+
149+ const pageFactory : PageFactory = this . platform . injector . get ( PAGE_FACTORY ) ;
150+
151+ const navEntry : NavigationEntry = {
152+ create : ( ) : Page => {
153+ let page = pageFactory ( { isBootstrap : true , isLivesync } ) ;
154+ if ( this . appOptions ) {
155+ page . actionBarHidden = this . appOptions . startPageActionBarHidden ;
156+ }
157+
158+ let onLoadedHandler = function ( ) {
159+ page . off ( "loaded" , onLoadedHandler ) ;
160+ // profiling.stop("application-start");
161+ rendererLog ( "Page loaded" ) ;
162+
163+ // profiling.start("ng-bootstrap");
164+ rendererLog ( "BOOTSTRAPPING..." ) ;
165+ bootstrapAction ( ) . then ( ( moduleRef ) => {
166+ // profiling.stop("ng-bootstrap");
167+ rendererLog ( "ANGULAR BOOTSTRAP DONE." ) ;
168+ lastBootstrappedModule = new WeakRef ( moduleRef ) ;
169+
170+ if ( resolve ) {
171+ resolve ( moduleRef ) ;
172+ }
173+ return moduleRef ;
174+ } , ( err ) => {
175+ rendererError ( "ERROR BOOTSTRAPPING ANGULAR" ) ;
176+ let errorMessage = err . message + "\n\n" + err . stack ;
177+ rendererError ( errorMessage ) ;
178+
179+ let view = new TextView ( ) ;
180+ view . text = errorMessage ;
181+ page . content = view ;
182+
183+ if ( reject ) {
184+ reject ( err ) ;
185+ }
186+ } ) ;
187+ } ;
188+
189+ page . on ( "loaded" , onLoadedHandler ) ;
190+
191+ return page ;
179192 }
180- } ) ;
181193 } ;
182194
183- page . on ( "loaded" , onLoadedHandler ) ;
184-
185- return page ;
186- }
187- } ;
195+ if ( isReboot ) {
196+ navEntry . animated = false ;
197+ navEntry . clearHistory = true ;
198+ }
188199
189- if ( isReboot ) {
190- navEntry . animated = false ;
191- navEntry . clearHistory = true ;
200+ return navEntry ;
192201 }
193202
194- return navEntry ;
195- }
196-
197- liveSyncApp ( ) {
198- }
203+ liveSyncApp ( ) {
204+ }
199205}
0 commit comments