forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.service.ts
More file actions
241 lines (223 loc) · 7.41 KB
/
Copy pathinit.service.ts
File metadata and controls
241 lines (223 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
import {
APP_INITIALIZER,
Inject,
makeStateKey,
Provider,
TransferState,
Type,
} from '@angular/core';
import {
DYNAMIC_FORM_CONTROL_MAP_FN,
DYNAMIC_VALIDATORS,
} from '@ng-dynamic-forms/core';
import {
select,
Store,
} from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import isEqual from 'lodash/isEqual';
import { Observable } from 'rxjs';
import {
distinctUntilChanged,
find,
} from 'rxjs/operators';
import {
APP_CONFIG,
APP_DATA_SERVICES_MAP,
AppConfig,
} from '../config/app-config.interface';
import { environment } from '../environments/environment';
import { AppState } from './app.reducer';
import { BreadcrumbsService } from './breadcrumbs/breadcrumbs.service';
import { CheckAuthenticationTokenAction } from './core/auth/auth.actions';
import { isAuthenticationBlocking } from './core/auth/selectors';
import { LAZY_DATA_SERVICES } from './core/data-services-map';
import { LocaleService } from './core/locale/locale.service';
import { HeadTagService } from './core/metadata/head-tag.service';
import { CorrelationIdService } from './correlation-id/correlation-id.service';
import { dsDynamicFormControlMapFn } from './shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control-map-fn';
import { CUSTOM_VALIDATORS } from './shared/form/builder/parsers/field-parser';
import { MenuService } from './shared/menu/menu.service';
import { MenuProviderService } from './shared/menu/menu-provider.service';
import { ThemeService } from './shared/theme-support/theme.service';
import { Angulartics2DSpace } from './statistics/angulartics/dspace-provider';
/**
* Performs the initialization of the app.
*
* Should be extended to implement server- & browser-specific functionality.
* Initialization steps shared between the server and browser implementations
* can be included in this class.
*
* Note that the service cannot (indirectly) depend on injection tokens that are only available _after_ APP_INITIALIZER.
* For example, NgbModal depends on ApplicationRef and can therefore not be used during initialization.
*/
export abstract class InitService {
/**
* The state transfer key to use for the NgRx store state
* @protected
*/
protected static NGRX_STATE = makeStateKey('NGRX_STATE');
protected constructor(
protected store: Store<AppState>,
protected correlationIdService: CorrelationIdService,
@Inject(APP_CONFIG) protected appConfig: AppConfig,
protected translate: TranslateService,
protected localeService: LocaleService,
protected angulartics2DSpace: Angulartics2DSpace,
protected headTagService: HeadTagService,
protected breadcrumbsService: BreadcrumbsService,
protected themeService: ThemeService,
protected menuService: MenuService,
protected menuProviderService: MenuProviderService,
) {
}
/**
* The initialization providers to use in `*AppModule`
* - this concrete {@link InitService}
* - {@link APP_CONFIG} with optional pre-initialization hook
* - {@link APP_INITIALIZER}
* <br>
* Should only be called on concrete subclasses of InitService for the initialization hooks to work
*/
public static providers(): Provider[] {
if (!InitService.isPrototypeOf(this)) {
throw new Error(
'Initalization providers should only be generated from concrete subclasses of InitService',
);
}
return [
{
provide: InitService,
useClass: this as unknown as Type<InitService>,
},
{
provide: APP_CONFIG,
useFactory: (transferState: TransferState) => {
this.resolveAppConfig(transferState);
return environment;
},
deps: [ TransferState ],
},
{
provide: APP_INITIALIZER,
useFactory: (initService: InitService) => initService.init(),
deps: [ InitService ],
multi: true,
},
{
provide: APP_DATA_SERVICES_MAP,
useValue: LAZY_DATA_SERVICES,
},
{
provide: DYNAMIC_FORM_CONTROL_MAP_FN,
useValue: dsDynamicFormControlMapFn,
},
{
provide: DYNAMIC_VALIDATORS,
useValue: CUSTOM_VALIDATORS,
},
];
}
/**
* Optional pre-initialization method to ensure that {@link APP_CONFIG} is fully resolved before {@link init} is called.
*
* For example, Router depends on APP_BASE_HREF, which in turn depends on APP_CONFIG.
* In production mode, APP_CONFIG is resolved from the TransferState when the app is initialized.
* If we want to use Router within APP_INITIALIZER, we have to make sure APP_BASE_HREF is resolved beforehand.
* In this case that means that we must transfer the configuration from the SSR state during pre-initialization.
* @protected
*/
protected static resolveAppConfig(
transferState: TransferState,
): void {
// overridden in subclasses if applicable
}
/**
* Main initialization method.
* @protected
*/
protected abstract init(): () => Promise<boolean>;
// Common initialization steps
/**
* Dispatch a {@link CheckAuthenticationTokenAction} to start off the chain of
* actions used to determine whether a user is already logged in.
* @protected
*/
protected checkAuthenticationToken(): void {
this.store.dispatch(new CheckAuthenticationTokenAction());
}
/**
* Initialize the correlation ID (from cookie, NgRx store or random)
* @protected
*/
protected initCorrelationId(): void {
this.correlationIdService.initCorrelationId();
}
/**
* Make sure the {@link environment} matches {@link APP_CONFIG} and print
* some information about it to the console
* @protected
*/
protected checkEnvironment(): void {
if (!isEqual(environment, this.appConfig)) {
throw new Error('environment does not match app config!');
}
if (environment.debug) {
console.info(environment);
}
}
/**
* Initialize internationalization services
* - Specify the active languages
* - Set the current locale
* @protected
*/
protected initI18n(): void {
// Load all the languages that are defined as active from the config file
this.translate.addLangs(
environment.languages
.filter((LangConfig) => LangConfig.active === true)
.map((a) => a.code),
);
// Load the default language from the config file
// translate.setDefaultLang(environment.defaultLanguage);
this.localeService.setCurrentLanguageCode();
}
/**
* Initialize Angulartics
* @protected
*/
protected initAngulartics(): void {
this.angulartics2DSpace.startTracking();
}
/**
* Start route-listening subscriptions
* - {@link HeadTagService.listenForRouteChange}
* - {@link BreadcrumbsService.listenForRouteChanges}
* - {@link ThemeService.listenForRouteChanges}
* @protected
*/
protected initRouteListeners(): void {
this.headTagService.listenForRouteChange();
this.breadcrumbsService.listenForRouteChanges();
this.themeService.listenForRouteChanges();
}
/**
* Emits once authentication is ready (no longer blocking)
* @protected
*/
protected authenticationReady$(): Observable<boolean> {
return this.store.pipe(
select(isAuthenticationBlocking),
distinctUntilChanged(),
find((b: boolean) => b === false),
);
}
}