|
| 1 | +import 'globals'; |
| 2 | +import "zone.js/dist/zone-node"; |
| 3 | + |
| 4 | +import 'reflect-metadata'; |
| 5 | +import './polyfills/array'; |
| 6 | +import './polyfills/console'; |
| 7 | + |
| 8 | +import { |
| 9 | + ElementSchemaRegistry, |
| 10 | + XHR, |
| 11 | + COMPILER_PROVIDERS, |
| 12 | + CompilerConfig, |
| 13 | + platformCoreDynamic |
| 14 | +} from '@angular/compiler'; |
| 15 | +import {CommonModule} from '@angular/common'; |
| 16 | +import {provide, Provider} from '@angular/core'; |
| 17 | +import {NativeScriptRootRenderer, NativeScriptRenderer} from './renderer'; |
| 18 | +import { |
| 19 | + Injector, |
| 20 | + OpaqueToken, |
| 21 | + ApplicationModule, |
| 22 | + ExceptionHandler, |
| 23 | + NgModule, |
| 24 | + platformCore, |
| 25 | + CompilerOptions, |
| 26 | + COMPILER_OPTIONS, |
| 27 | + CompilerFactory, |
| 28 | + PLATFORM_INITIALIZER, |
| 29 | + Renderer, |
| 30 | + RootRenderer, |
| 31 | + SanitizationService, |
| 32 | + PLATFORM_DIRECTIVES, |
| 33 | + PlatformRef, |
| 34 | + NgModuleFactory, |
| 35 | + NgModuleRef, |
| 36 | + Testability, |
| 37 | + createPlatformFactory |
| 38 | +} from '@angular/core'; |
| 39 | +import { FormsModule } from "@angular/forms"; |
| 40 | +import * as application from "application"; |
| 41 | +import { topmost, NavigationEntry } from "ui/frame"; |
| 42 | +import { Page } from 'ui/page'; |
| 43 | +import { rendererLog, rendererError } from "./trace"; |
| 44 | +import { TextView } from 'ui/text-view'; |
| 45 | +import { PlatformFactory } from "@angular/core/src/application_ref"; |
| 46 | +import { isPresent, Type, ConcreteType, print } from '@angular/core/src/facade/lang'; |
| 47 | +import { defaultPageProvider, defaultFrameProvider, defaultDeviceProvider, defaultAnimationDriverProvider |
| 48 | +} from "./platform-providers"; |
| 49 | +import { NativeScriptDomAdapter, NativeScriptElementSchemaRegistry, NativeScriptSanitizationService |
| 50 | +} from './dom-adapter'; |
| 51 | +import { FileSystemXHR } from './http/xhr'; |
| 52 | +import { NS_DIRECTIVES } from './directives'; |
| 53 | + |
| 54 | +import * as nativescriptIntl from "nativescript-intl"; |
| 55 | +global.Intl = nativescriptIntl; |
| 56 | + |
| 57 | +export interface AppOptions { |
| 58 | + cssFile?: string; |
| 59 | + startPageActionBarHidden?: boolean; |
| 60 | +} |
| 61 | + |
| 62 | +class ConsoleLogger { |
| 63 | + log = print; |
| 64 | + logError = print; |
| 65 | + logGroup = print; |
| 66 | + logGroupEnd() { } |
| 67 | +} |
| 68 | + |
| 69 | +@NgModule({ |
| 70 | + declarations: [ |
| 71 | + NS_DIRECTIVES |
| 72 | + ], |
| 73 | + providers: [ |
| 74 | + provide(ExceptionHandler, { |
| 75 | + useFactory: () => { |
| 76 | + return new ExceptionHandler(new ConsoleLogger(), true) |
| 77 | + }, deps: [] |
| 78 | + }), |
| 79 | + |
| 80 | + defaultFrameProvider, |
| 81 | + defaultPageProvider, |
| 82 | + defaultDeviceProvider, |
| 83 | + defaultAnimationDriverProvider, |
| 84 | + NativeScriptRootRenderer, |
| 85 | + provide(RootRenderer, { useClass: NativeScriptRootRenderer }), |
| 86 | + NativeScriptRenderer, |
| 87 | + provide(Renderer, { useClass: NativeScriptRenderer }), |
| 88 | + provide(SanitizationService, { useClass: NativeScriptSanitizationService }), |
| 89 | + ], |
| 90 | + imports: [ |
| 91 | + CommonModule, |
| 92 | + ApplicationModule, |
| 93 | + FormsModule, //TODO: split to a separate NativeScriptFormsModule |
| 94 | + ], |
| 95 | + exports: [ |
| 96 | + CommonModule, |
| 97 | + ApplicationModule, |
| 98 | + FormsModule, //TODO: split to a separate NativeScriptFormsModule |
| 99 | + NS_DIRECTIVES |
| 100 | + ] |
| 101 | +}) |
| 102 | +export class NativeScriptModule { |
| 103 | +} |
| 104 | + |
| 105 | +export const NS_COMPILER_PROVIDERS = [ |
| 106 | + COMPILER_PROVIDERS, |
| 107 | + //provide(PLATFORM_DIRECTIVES, { useValue: NS_DIRECTIVES, multi: true }), |
| 108 | + { |
| 109 | + provide: COMPILER_OPTIONS, |
| 110 | + useValue: {providers: [ |
| 111 | + {provide: XHR, useClass: FileSystemXHR}, |
| 112 | + provide(ElementSchemaRegistry, { useClass: NativeScriptElementSchemaRegistry }), |
| 113 | + ]}, |
| 114 | + multi: true |
| 115 | + } |
| 116 | +]; |
| 117 | + |
| 118 | +type BootstrapperAction<M> = () => Promise<NgModuleRef<M>>; |
| 119 | + |
| 120 | +let lastBootstrappedApp: WeakRef<NgModuleRef<any>>; |
| 121 | + |
| 122 | +class NativeScriptPlatformRef extends PlatformRef { |
| 123 | + |
| 124 | + constructor(private platform: PlatformRef, private appOptions?: AppOptions) { |
| 125 | + super(); |
| 126 | + } |
| 127 | + |
| 128 | + bootstrapModuleFactory<M>(moduleFactory: NgModuleFactory<M>): Promise<NgModuleRef<M>> { |
| 129 | + throw new Error("Not implemented."); |
| 130 | + } |
| 131 | + |
| 132 | + bootstrapModule<M>(moduleType: ConcreteType<M>, compilerOptions: CompilerOptions|CompilerOptions[] = []): Promise<NgModuleRef<M>> { |
| 133 | + const mainPageEntry = this.createNavigationEntry(() => this.platform.bootstrapModule(moduleType, compilerOptions)); |
| 134 | + application.start(mainPageEntry); |
| 135 | + return null; //Make the compiler happy |
| 136 | + } |
| 137 | + |
| 138 | + registerDisposeListener(dispose: () => void): void { |
| 139 | + this.platform.registerDisposeListener(dispose); |
| 140 | + } |
| 141 | + |
| 142 | + onDestroy(callback: () => void): void { |
| 143 | + this.platform.onDestroy(callback); |
| 144 | + } |
| 145 | + |
| 146 | + get injector(): Injector { |
| 147 | + return this.platform.injector; |
| 148 | + }; |
| 149 | + |
| 150 | + dispose(): void { |
| 151 | + this.platform.dispose(); |
| 152 | + } |
| 153 | + |
| 154 | + destroy(): void { |
| 155 | + this.platform.destroy(); |
| 156 | + } |
| 157 | + |
| 158 | + get disposed(): boolean { |
| 159 | + return this.platform.disposed; |
| 160 | + } |
| 161 | + |
| 162 | + get destroyed(): boolean { |
| 163 | + return this.platform.destroyed; |
| 164 | + } |
| 165 | + |
| 166 | + private createNavigationEntry<M>(bootstrapAction: BootstrapperAction<M>, resolve?: (comp: NgModuleRef<any>) => void, reject?: (e: Error) => void, isReboot: boolean = false): NavigationEntry { |
| 167 | + const navEntry: NavigationEntry = { |
| 168 | + create: (): Page => { |
| 169 | + let page = new Page(); |
| 170 | + if (this.appOptions) { |
| 171 | + page.actionBarHidden = this.appOptions.startPageActionBarHidden; |
| 172 | + } |
| 173 | + |
| 174 | + let onLoadedHandler = function (args) { |
| 175 | + page.off('loaded', onLoadedHandler); |
| 176 | + //profiling.stop('application-start'); |
| 177 | + rendererLog('Page loaded'); |
| 178 | + |
| 179 | + //profiling.start('ng-bootstrap'); |
| 180 | + rendererLog('BOOTSTRAPPING...'); |
| 181 | + bootstrapAction().then((moduleRef) => { |
| 182 | + //profiling.stop('ng-bootstrap'); |
| 183 | + rendererLog('ANGULAR BOOTSTRAP DONE.'); |
| 184 | + lastBootstrappedApp = new WeakRef(moduleRef); |
| 185 | + |
| 186 | + if (resolve) { |
| 187 | + resolve(moduleRef); |
| 188 | + } |
| 189 | + return moduleRef; |
| 190 | + }, (err) => { |
| 191 | + rendererError('ERROR BOOTSTRAPPING ANGULAR'); |
| 192 | + let errorMessage = err.message + "\n\n" + err.stack; |
| 193 | + rendererError(errorMessage); |
| 194 | + |
| 195 | + let view = new TextView(); |
| 196 | + view.text = errorMessage; |
| 197 | + page.content = view; |
| 198 | + |
| 199 | + if (reject) { |
| 200 | + reject(err); |
| 201 | + } |
| 202 | + }); |
| 203 | + }; |
| 204 | + |
| 205 | + page.on('loaded', onLoadedHandler); |
| 206 | + |
| 207 | + return page; |
| 208 | + } |
| 209 | + }; |
| 210 | + |
| 211 | + if (isReboot) { |
| 212 | + navEntry.animated = false; |
| 213 | + navEntry.clearHistory = true; |
| 214 | + } |
| 215 | + |
| 216 | + return navEntry; |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +var _platformNativeScriptDynamic: PlatformFactory = createPlatformFactory( |
| 221 | + platformCoreDynamic, 'nativeScriptDynamic', NS_COMPILER_PROVIDERS); |
| 222 | + |
| 223 | +export function platformNativeScriptDynamic(options?: AppOptions, extraProviders?: any[]): PlatformRef { |
| 224 | + return new NativeScriptPlatformRef(_platformNativeScriptDynamic(extraProviders), options); |
| 225 | +} |
| 226 | + |
0 commit comments