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