Skip to content

Instantly share code, notes, and snippets.

@mii9000
Created August 31, 2016 10:57
Show Gist options
  • Save mii9000/2c96ccc507550355c53524e753ab91fa to your computer and use it in GitHub Desktop.
Save mii9000/2c96ccc507550355c53524e753ab91fa to your computer and use it in GitHub Desktop.
import { enableProdMode, NgModule, ApplicationRef, provide, ExceptionHandler } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { BrowserModule } from '@angular/platform-browser';
import { Http, XHRBackend, RequestOptions, HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { routing } from './app/app.routing';
import { removeNgStyles, createNewHosts } from '@angularclass/hmr';
import * as pages from './app';
import * as components from './app/shared/components';
import * as directives from './app/shared/directives';
import * as pipes from './app/shared/pipes';
import * as services from './app/shared/services';
import * as models from './app/shared/models';
declare var Office: any;
// depending on the env mode, enable prod mode or add debugging modules
if (process.env.ENV === 'build') {
enableProdMode();
}
var appContext = new models.AppContext();
var clientService: services.IClientService
//Office based init
if (!(!this.Office)) {
Office.initialize = (reason: any) => {
if (!Office.context.document) {
clientService = new services.OutlookClientService();
clientService.initializeContext(appContext)
.subscribe((res: any) => {
console.log(('Booting using OutlookClientService'));
services.Trace.write('Booting using OutlookClientService');
boot();
}, (error: any) => console.error(error));
}
else {
clientService = new services.OfficeClientService();
clientService.initializeContext(appContext)
.subscribe((res: any) => {
console.log('Booting using OfficeClientService');
services.Trace.write('Booting using OfficeClientService');
boot();
}, (error: any) => console.error(error));
}
};
}
else {
//Browser based init
clientService = new services.BrowserClientService();
clientService.initializeContext(appContext)
.subscribe((res: any) => {
console.log('Booting using BrowserClientService');
services.Trace.write('Booting using BrowserClientService');
boot();
}, (error: any) => console.error(error));
}
@NgModule({
imports: [
BrowserModule,
HttpModule,
RouterModule,
routing
],
declarations: [
pages.AppComponent,
pages.InviteLetterSidebar,
pages.LicenseSidebar,
pages.Login,
components.Agenda,
components.AgendaItem,
components.UserSpan,
directives.Touch,
pipes.CapitalizePipe,
pipes.OrderBy
],
providers: [
models.AuthProvider,
provide(Http, {
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, helperService: services.HelperService, authProvider: models.AuthProvider) =>
new services.CustomHttp(backend, defaultOptions, helperService, authProvider),
deps: [XHRBackend, RequestOptions, services.HelperService, models.AuthProvider]
}),
provide(LocationStrategy, { useClass: HashLocationStrategy }), // can be switched to HashLocationStrategy if you cannot configure your server appropriately for URL rewriting
provide(ExceptionHandler, { useClass: models.FiggExceptionHandler}),
provide("GraphApiService", { useClass: services.GraphApiService }),
provide("OneNoteService", { useClass: services.OneNoteService }),
provide("DocumentService", { useClass: services.WordDocumentService }),
provide("AppContext", { useValue: appContext }),
provide("ClientService", { useValue: clientService }),
services.AgendaService,
services.WordDocumentService
],
bootstrap: [pages.AppComponent]
})
export class AppModule {
constructor(public appRef: ApplicationRef) {}
hmrOnInit(store) {
console.log('HMR store', store);
}
hmrOnDestroy(store) {
let cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
// recreate elements
store.disposeOldHosts = createNewHosts(cmpLocation);
// remove styles
removeNgStyles();
}
hmrAfterDestroy(store) {
// display new elements
store.disposeOldHosts();
delete store.disposeOldHosts;
}
}
export function main() {
return platformBrowserDynamic().bootstrapModule(AppModule);
}
export function boot(){
if (document.readyState === 'complete') {
main();
} else {
document.addEventListener('DOMContentLoaded', main);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment