f9a9ae7603174a7f0b70777a65a277c6993cb94d
[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 { FormsModule, ReactiveFormsModule } from '@angular/forms';
4 import { HttpModule, RequestOptions, XHRBackend } from '@angular/http';
5 import { RouterModule } from '@angular/router';
6 import { removeNgStyles, createNewHosts } from '@angularclass/hmr';
7
8 import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
9 import { ProgressbarModule } from 'ng2-bootstrap/components/progressbar';
10 import { PaginationModule } from 'ng2-bootstrap/components/pagination';
11 import { FileSelectDirective } from 'ng2-file-upload/ng2-file-upload';
12
13 /*
14  * Platform and Environment providers/directives/pipes
15  */
16 import { ENV_PROVIDERS } from './environment';
17 import { routes } from './app.routes';
18 // App is our top level component
19 import { AppComponent } from './app.component';
20 import { AppState } from './app.service';
21
22 import {
23   AdminComponent,
24   FriendsComponent,
25   FriendAddComponent,
26   FriendListComponent,
27   FriendService,
28   MenuAdminComponent,
29   RequestsComponent,
30   RequestStatsComponent,
31   RequestService,
32   UsersComponent,
33   UserAddComponent,
34   UserListComponent,
35   UserService
36 } from './admin';
37 import { AccountComponent, AccountService } from './account';
38 import { LoginComponent } from './login';
39 import { MenuComponent } from './menu.component';
40 import { AuthService, AuthHttp, RestExtractor, RestService, SearchComponent, SearchService } from './shared';
41 import {
42   LoaderComponent,
43   VideosComponent,
44   VideoAddComponent,
45   VideoListComponent,
46   VideoMiniatureComponent,
47   VideoSortComponent,
48   VideoWatchComponent,
49   VideoService,
50   WebTorrentService
51 } from './videos';
52
53 // Application wide providers
54 const APP_PROVIDERS = [
55   AppState,
56
57   {
58     provide: AuthHttp,
59     useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, authService: AuthService) => {
60       return new AuthHttp(backend, defaultOptions, authService);
61     },
62     deps: [ XHRBackend, RequestOptions, AuthService ]
63   },
64
65   AuthService,
66   RestExtractor,
67   RestService,
68
69   VideoService,
70   SearchService,
71   FriendService,
72   RequestService,
73   UserService,
74   AccountService,
75   WebTorrentService
76 ];
77 /**
78  * `AppModule` is the main entry point into Angular2's bootstraping process
79  */
80 @NgModule({
81   bootstrap: [ AppComponent ],
82   declarations: [
83     AccountComponent,
84     AdminComponent,
85     AppComponent,
86     BytesPipe,
87     FileSelectDirective,
88     FriendAddComponent,
89     FriendListComponent,
90     FriendsComponent,
91     LoaderComponent,
92     LoginComponent,
93     MenuAdminComponent,
94     MenuComponent,
95     RequestsComponent,
96     RequestStatsComponent,
97     SearchComponent,
98     UserAddComponent,
99     UserListComponent,
100     UsersComponent,
101     VideoAddComponent,
102     VideoListComponent,
103     VideoMiniatureComponent,
104     VideosComponent,
105     VideoSortComponent,
106     VideoWatchComponent,
107   ],
108   imports: [ // import Angular's modules
109     BrowserModule,
110     FormsModule,
111     ReactiveFormsModule,
112     HttpModule,
113     RouterModule.forRoot(routes),
114
115     ProgressbarModule,
116     PaginationModule
117   ],
118   providers: [ // expose our Services and Providers into Angular's dependency injection
119     ENV_PROVIDERS,
120     APP_PROVIDERS
121   ]
122 })
123 export class AppModule {
124   constructor(public appRef: ApplicationRef, public appState: AppState) {}
125   hmrOnInit(store) {
126     if (!store || !store.state) return;
127     console.log('HMR store', store);
128     this.appState._state = store.state;
129     this.appRef.tick();
130     delete store.state;
131   }
132   hmrOnDestroy(store) {
133     const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
134     // recreate elements
135     const state = this.appState._state;
136     store.state = state;
137     store.disposeOldHosts = createNewHosts(cmpLocation);
138     // remove styles
139     removeNgStyles();
140   }
141   hmrAfterDestroy(store) {
142     // display new elements
143     store.disposeOldHosts();
144     delete store.disposeOldHosts;
145   }
146 }