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