|
| 1 | +// Initial imports and polyfills |
| 2 | +import 'globals'; |
| 3 | +import './zone.js/dist/zone-nativescript'; |
| 4 | +import 'reflect-metadata'; |
| 5 | +import './polyfills/array'; |
| 6 | +import './polyfills/console'; |
| 7 | + |
| 8 | +import { |
| 9 | + Type, |
| 10 | + Injector, |
| 11 | + CompilerOptions, |
| 12 | + PlatformRef, |
| 13 | + NgModuleFactory, |
| 14 | + NgModuleRef, |
| 15 | + EventEmitter, |
| 16 | + Provider, |
| 17 | + Sanitizer, |
| 18 | + OpaqueToken, |
| 19 | +} from '@angular/core'; |
| 20 | + |
| 21 | +import { rendererLog, rendererError } from "./trace"; |
| 22 | +import { PAGE_FACTORY, PageFactory, defaultPageFactoryProvider } from './platform-providers'; |
| 23 | + |
| 24 | +import * as application from "application"; |
| 25 | +import { topmost, NavigationEntry } from "ui/frame"; |
| 26 | +import { Page } from 'ui/page'; |
| 27 | +import { TextView } from 'ui/text-view'; |
| 28 | + |
| 29 | +import * as nativescriptIntl from "nativescript-intl"; |
| 30 | +global.Intl = nativescriptIntl; |
| 31 | + |
| 32 | +export const onBeforeLivesync = new EventEmitter<NgModuleRef<any>>(); |
| 33 | +export const onAfterLivesync = new EventEmitter<NgModuleRef<any>>(); |
| 34 | +let lastBootstrappedModule: WeakRef<NgModuleRef<any>>; |
| 35 | +type BootstrapperAction = () => Promise<NgModuleRef<any>>; |
| 36 | + |
| 37 | +interface BootstrapParams { |
| 38 | + appModuleType: Type<any>; |
| 39 | + appOptions?: AppOptions; |
| 40 | +} |
| 41 | + |
| 42 | +export interface AppOptions { |
| 43 | + bootInExistingPage: boolean; |
| 44 | + cssFile?: string; |
| 45 | + startPageActionBarHidden?: boolean; |
| 46 | +} |
| 47 | + |
| 48 | +export type PlatformFactory = (extraProviders?: Provider[]) => PlatformRef; |
| 49 | + |
| 50 | +export class NativeScriptSanitizer extends Sanitizer { |
| 51 | + sanitize(context: any, value: string): string { |
| 52 | + return value; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export const COMMON_PROVIDERS = [ |
| 57 | + defaultPageFactoryProvider, |
| 58 | + { provide: Sanitizer, useClass: NativeScriptSanitizer }, |
| 59 | +]; |
| 60 | + |
| 61 | +export class NativeScriptPlatformRef extends PlatformRef { |
| 62 | + private _bootstrapper: BootstrapperAction; |
| 63 | + |
| 64 | + constructor(private platform: PlatformRef, private appOptions?: AppOptions) { |
| 65 | + super(); |
| 66 | + } |
| 67 | + |
| 68 | + bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>): Promise<NgModuleRef<M>> { |
| 69 | + this._bootstrapper = () => this.platform.bootstrapModuleFactory(moduleFactory); |
| 70 | + |
| 71 | + this.bootstrapApp(); |
| 72 | + |
| 73 | + return null; //Make the compiler happy |
| 74 | + } |
| 75 | + |
| 76 | + bootstrapModule<M>(moduleType: Type<M>, compilerOptions: CompilerOptions | CompilerOptions[] = []): Promise<NgModuleRef<M>> { |
| 77 | + this._bootstrapper = () => this.platform.bootstrapModule(moduleType, compilerOptions); |
| 78 | + |
| 79 | + this.bootstrapApp(); |
| 80 | + |
| 81 | + return null; //Make the compiler happy |
| 82 | + } |
| 83 | + |
| 84 | + private bootstrapApp() { |
| 85 | + global.__onLiveSyncCore = () => this.livesyncModule(); |
| 86 | + |
| 87 | + const mainPageEntry = this.createNavigationEntry(this._bootstrapper); |
| 88 | + |
| 89 | + application.start(mainPageEntry); |
| 90 | + } |
| 91 | + |
| 92 | + livesyncModule(): void { |
| 93 | + rendererLog("ANGULAR LiveSync Started"); |
| 94 | + |
| 95 | + onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null); |
| 96 | + |
| 97 | + const mainPageEntry = this.createNavigationEntry( |
| 98 | + this._bootstrapper, |
| 99 | + compRef => onAfterLivesync.next(compRef), |
| 100 | + error => onAfterLivesync.error(error), |
| 101 | + true |
| 102 | + ); |
| 103 | + mainPageEntry.animated = false; |
| 104 | + mainPageEntry.clearHistory = true; |
| 105 | + |
| 106 | + const frame = topmost(); |
| 107 | + if (frame) { |
| 108 | + if (frame.currentPage && frame.currentPage.modal) { |
| 109 | + frame.currentPage.modal.closeModal(); |
| 110 | + } |
| 111 | + frame.navigate(mainPageEntry); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + onDestroy(callback: () => void): void { |
| 116 | + this.platform.onDestroy(callback); |
| 117 | + } |
| 118 | + |
| 119 | + get injector(): Injector { |
| 120 | + return this.platform.injector; |
| 121 | + }; |
| 122 | + |
| 123 | + destroy(): void { |
| 124 | + this.platform.destroy(); |
| 125 | + } |
| 126 | + |
| 127 | + get destroyed(): boolean { |
| 128 | + return this.platform.destroyed; |
| 129 | + } |
| 130 | + |
| 131 | + private createNavigationEntry( |
| 132 | + bootstrapAction: BootstrapperAction, |
| 133 | + resolve?: (comp: NgModuleRef<any>) => void, |
| 134 | + reject?: (e: Error) => void, |
| 135 | + isLivesync: boolean = false, |
| 136 | + isReboot: boolean = false): NavigationEntry { |
| 137 | + |
| 138 | + const pageFactory: PageFactory = this.platform.injector.get(PAGE_FACTORY); |
| 139 | + |
| 140 | + const navEntry: NavigationEntry = { |
| 141 | + create: (): Page => { |
| 142 | + let page = pageFactory({ isBootstrap: true, isLivesync }); |
| 143 | + if (this.appOptions) { |
| 144 | + page.actionBarHidden = this.appOptions.startPageActionBarHidden; |
| 145 | + } |
| 146 | + |
| 147 | + let onLoadedHandler = function (args) { |
| 148 | + page.off('loaded', onLoadedHandler); |
| 149 | + //profiling.stop('application-start'); |
| 150 | + rendererLog('Page loaded'); |
| 151 | + |
| 152 | + //profiling.start('ng-bootstrap'); |
| 153 | + rendererLog('BOOTSTRAPPING...'); |
| 154 | + bootstrapAction().then((moduleRef) => { |
| 155 | + //profiling.stop('ng-bootstrap'); |
| 156 | + rendererLog('ANGULAR BOOTSTRAP DONE.'); |
| 157 | + lastBootstrappedModule = new WeakRef(moduleRef); |
| 158 | + |
| 159 | + if (resolve) { |
| 160 | + resolve(moduleRef); |
| 161 | + } |
| 162 | + return moduleRef; |
| 163 | + }, (err) => { |
| 164 | + rendererError('ERROR BOOTSTRAPPING ANGULAR'); |
| 165 | + let errorMessage = err.message + "\n\n" + err.stack; |
| 166 | + rendererError(errorMessage); |
| 167 | + |
| 168 | + let view = new TextView(); |
| 169 | + view.text = errorMessage; |
| 170 | + page.content = view; |
| 171 | + |
| 172 | + if (reject) { |
| 173 | + reject(err); |
| 174 | + } |
| 175 | + }); |
| 176 | + }; |
| 177 | + |
| 178 | + page.on('loaded', onLoadedHandler); |
| 179 | + |
| 180 | + return page; |
| 181 | + } |
| 182 | + }; |
| 183 | + |
| 184 | + if (isReboot) { |
| 185 | + navEntry.animated = false; |
| 186 | + navEntry.clearHistory = true; |
| 187 | + } |
| 188 | + |
| 189 | + return navEntry; |
| 190 | + } |
| 191 | + |
| 192 | + liveSyncApp() { |
| 193 | + } |
| 194 | +} |
0 commit comments