fb71787c4cd8d8f75dfb58fe398042b0d08c41f7
[oweals/peertube.git] / client / src / app / app.module.ts
1 import { ApplicationRef, NgModule } from '@angular/core';
2 import { BrowserModule } from '@angular/platform-browser';
3 import { removeNgStyles, createNewHosts } from '@angularclass/hmr';
4
5 import { MetaModule, MetaConfig } from 'ng2-meta/src';
6 import 'bootstrap-loader';
7
8 import { ENV_PROVIDERS } from './environment';
9 import { AppRoutingModule } from './app-routing.module';
10 import { AppComponent } from './app.component';
11 import { AppState } from './app.service';
12
13 import { AccountModule } from './account';
14 import { AdminModule } from './admin';
15 import { CoreModule } from './core';
16 import { LoginModule } from './login';
17 import { SharedModule } from './shared';
18 import { VideosModule } from './videos';
19
20 const metaConfig: MetaConfig = {
21   //Append a title suffix such as a site name to all titles
22   //Defaults to false
23   useTitleSuffix: true,
24   defaults: {
25     title: 'PeerTube'
26   }
27 };
28
29 // Application wide providers
30 const APP_PROVIDERS = [
31   AppState
32 ];
33
34 @NgModule({
35   bootstrap: [ AppComponent ],
36   declarations: [
37     AppComponent
38   ],
39   imports: [
40     BrowserModule,
41
42     CoreModule,
43     SharedModule,
44
45     AppRoutingModule,
46
47     MetaModule.forRoot(metaConfig),
48
49     AccountModule,
50     AdminModule,
51     CoreModule,
52     LoginModule,
53     SharedModule,
54     VideosModule
55   ],
56   providers: [ // expose our Services and Providers into Angular's dependency injection
57     ENV_PROVIDERS,
58     APP_PROVIDERS
59   ]
60 })
61 export class AppModule {
62   constructor(public appRef: ApplicationRef, public appState: AppState) {}
63   hmrOnInit(store) {
64     if (!store || !store.state) return;
65     console.log('HMR store', store);
66     this.appState._state = store.state;
67     this.appRef.tick();
68     delete store.state;
69   }
70   hmrOnDestroy(store) {
71     const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
72     // recreate elements
73     const state = this.appState._state;
74     store.state = state;
75     store.disposeOldHosts = createNewHosts(cmpLocation);
76     // remove styles
77     removeNgStyles();
78   }
79   hmrAfterDestroy(store) {
80     // display new elements
81     store.disposeOldHosts();
82     delete store.disposeOldHosts;
83   }
84 }