]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { ApplicationRef, NgModule } from '@angular/core' |
2 | import { BrowserModule } from '@angular/platform-browser' | |
8635a2c7 C |
3 | import { |
4 | removeNgStyles, | |
5 | createNewHosts, | |
6 | createInputTransfer | |
df98563e | 7 | } from '@angularclass/hmr' |
ab32b0fc | 8 | |
df98563e | 9 | import { MetaModule, MetaLoader, MetaStaticLoader, PageTitlePositioning } from '@nglibs/meta' |
ad42bea3 | 10 | // TODO: remove, we need this to avoid error in ng2-smart-table |
df98563e C |
11 | import 'rxjs/add/operator/toPromise' |
12 | import 'bootstrap-loader' | |
b58c69a1 | 13 | |
df98563e C |
14 | import { ENV_PROVIDERS } from './environment' |
15 | import { AppRoutingModule } from './app-routing.module' | |
16 | import { AppComponent } from './app.component' | |
17 | import { AppState, InternalStateType } from './app.service' | |
23bcf666 | 18 | |
df98563e C |
19 | import { AccountModule } from './account' |
20 | import { CoreModule } from './core' | |
21 | import { LoginModule } from './login' | |
22 | import { SignupModule } from './signup' | |
23 | import { SharedModule } from './shared' | |
24 | import { VideosModule } from './videos' | |
693b1aba | 25 | |
df98563e | 26 | export function metaFactory (): MetaLoader { |
758b996d C |
27 | return new MetaStaticLoader({ |
28 | pageTitlePositioning: PageTitlePositioning.PrependPageTitle, | |
29 | pageTitleSeparator: ' - ', | |
30 | applicationName: 'PeerTube', | |
31 | defaults: { | |
32 | title: 'PeerTube', | |
33 | description: 'PeerTube, a decentralized video streaming platform using P2P (BitTorrent) directly in the web browser' | |
34 | } | |
df98563e | 35 | }) |
758b996d | 36 | } |
b58c69a1 | 37 | |
8635a2c7 C |
38 | type StoreType = { |
39 | state: InternalStateType, | |
40 | restoreInputValues: () => void, | |
41 | disposeOldHosts: () => void | |
df98563e | 42 | } |
8635a2c7 | 43 | |
ab32b0fc C |
44 | // Application wide providers |
45 | const APP_PROVIDERS = [ | |
693b1aba | 46 | AppState |
df98563e | 47 | ] |
693b1aba | 48 | |
ab32b0fc C |
49 | @NgModule({ |
50 | bootstrap: [ AppComponent ], | |
51 | declarations: [ | |
50b0c262 | 52 | AppComponent |
ab32b0fc | 53 | ], |
693b1aba | 54 | imports: [ |
ab32b0fc | 55 | BrowserModule, |
23bcf666 | 56 | |
693b1aba C |
57 | CoreModule, |
58 | SharedModule, | |
59 | ||
60 | AppRoutingModule, | |
b58c69a1 | 61 | |
693b1aba | 62 | AccountModule, |
693b1aba C |
63 | CoreModule, |
64 | LoginModule, | |
a184c71b | 65 | SignupModule, |
693b1aba | 66 | SharedModule, |
55b33946 C |
67 | VideosModule, |
68 | ||
69 | MetaModule.forRoot({ | |
70 | provide: MetaLoader, | |
71 | useFactory: (metaFactory) | |
72 | }) | |
ab32b0fc C |
73 | ], |
74 | providers: [ // expose our Services and Providers into Angular's dependency injection | |
75 | ENV_PROVIDERS, | |
76 | APP_PROVIDERS | |
77 | ] | |
78 | }) | |
79 | export class AppModule { | |
df98563e | 80 | constructor ( |
8635a2c7 C |
81 | public appRef: ApplicationRef, |
82 | public appState: AppState | |
83 | ) {} | |
84 | ||
df98563e | 85 | public hmrOnInit (store: StoreType) { |
8635a2c7 | 86 | if (!store || !store.state) { |
df98563e | 87 | return |
8635a2c7 | 88 | } |
df98563e | 89 | console.log('HMR store', JSON.stringify(store, null, 2)) |
8635a2c7 C |
90 | /** |
91 | * Set state | |
92 | */ | |
df98563e | 93 | this.appState._state = store.state |
8635a2c7 C |
94 | /** |
95 | * Set input values | |
96 | */ | |
97 | if ('restoreInputValues' in store) { | |
df98563e C |
98 | let restoreInputValues = store.restoreInputValues |
99 | setTimeout(restoreInputValues) | |
8635a2c7 C |
100 | } |
101 | ||
df98563e C |
102 | this.appRef.tick() |
103 | delete store.state | |
104 | delete store.restoreInputValues | |
ab32b0fc | 105 | } |
8635a2c7 | 106 | |
df98563e C |
107 | public hmrOnDestroy (store: StoreType) { |
108 | const cmpLocation = this.appRef.components.map((cmp) => cmp.location.nativeElement) | |
8635a2c7 C |
109 | /** |
110 | * Save state | |
111 | */ | |
df98563e C |
112 | const state = this.appState._state |
113 | store.state = state | |
8635a2c7 C |
114 | /** |
115 | * Recreate root elements | |
116 | */ | |
df98563e | 117 | store.disposeOldHosts = createNewHosts(cmpLocation) |
8635a2c7 C |
118 | /** |
119 | * Save input values | |
120 | */ | |
df98563e | 121 | store.restoreInputValues = createInputTransfer() |
8635a2c7 C |
122 | /** |
123 | * Remove styles | |
124 | */ | |
df98563e | 125 | removeNgStyles() |
ab32b0fc | 126 | } |
8635a2c7 | 127 | |
df98563e | 128 | public hmrAfterDestroy (store: StoreType) { |
8635a2c7 C |
129 | /** |
130 | * Display new elements | |
131 | */ | |
df98563e C |
132 | store.disposeOldHosts () |
133 | delete store.disposeOldHosts | |
ab32b0fc C |
134 | } |
135 | } |