red-ui/apps/red-ui/src/app/app.module.ts
2022-02-07 18:21:22 +02:00

228 lines
8.4 KiB
TypeScript

import { BrowserModule, Title } from '@angular/platform-browser';
import { APP_INITIALIZER, ErrorHandler, NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ActivatedRoute, Router } from '@angular/router';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HTTP_INTERCEPTORS, HttpClient, HttpClientModule } from '@angular/common/http';
import { BaseScreenComponent } from '@components/base-screen/base-screen.component';
import { ApiPathInterceptor } from '@utils/api-path-interceptor';
import { MissingTranslationHandler, TranslateCompiler, TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { LanguageService } from '@i18n/language.service';
import { ToastrModule } from 'ngx-toastr';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '@environments/environment';
import { AuthModule } from './modules/auth/auth.module';
import { AuthErrorComponent } from '@components/auth-error/auth-error.component';
import { HttpCacheInterceptor } from '@redaction/red-cache';
import { NotificationsComponent } from '@components/notifications/notifications.component';
import { DownloadsListScreenComponent } from '@components/downloads-list-screen/downloads-list-screen.component';
import { AppRoutingModule } from './app-routing.module';
import { SharedModule } from '@shared/shared.module';
import { FileUploadDownloadModule } from '@upload-download/file-upload-download.module';
import { PlatformLocation } from '@angular/common';
import { BASE_HREF } from './tokens';
import { MonacoEditorModule } from '@materia-ui/ngx-monaco-editor';
import { GlobalErrorHandler } from '@utils/global-error-handler.service';
import { REDMissingTranslationHandler } from '@utils/missing-translations-handler';
import { TranslateMessageFormatCompiler } from 'ngx-translate-messageformat-compiler';
import { configurationInitializer } from '@utils/configuration.initializer';
import { ConfigService } from '@services/config.service';
import { SpotlightSearchComponent } from '@components/spotlight-search/spotlight-search.component';
import { PruningTranslationLoader } from '@utils/pruning-translation-loader';
import { DatePipe } from '@shared/pipes/date.pipe';
import * as links from '../assets/help-mode/links.json';
import {
HELP_DOCS,
IqserHelpModeModule,
MANUAL_BASE_URL,
MAX_RETRIES_ON_SERVER_ERROR,
ServerErrorInterceptor,
ToastComponent,
} from '@iqser/common-ui';
import { KeycloakService } from 'keycloak-angular';
import { GeneralSettingsService } from '@services/general-settings.service';
import { BreadcrumbsComponent } from '@components/breadcrumbs/breadcrumbs.component';
import { UserPreferenceService } from '@services/user-preference.service';
import { UserService } from '@services/user.service';
export function httpLoaderFactory(httpClient: HttpClient, configService: ConfigService): PruningTranslationLoader {
return new PruningTranslationLoader(httpClient, '/assets/i18n/', `.json?version=${configService.values.FRONTEND_APP_VERSION}`);
}
function cleanupBaseUrl(baseUrl: string) {
if (!baseUrl) {
return '';
} else if (baseUrl[baseUrl.length - 1] === '/') {
return baseUrl.substring(0, baseUrl.length - 1);
} else {
return baseUrl;
}
}
const screens = [BaseScreenComponent, DownloadsListScreenComponent];
const components = [AppComponent, AuthErrorComponent, NotificationsComponent, SpotlightSearchComponent, BreadcrumbsComponent, ...screens];
@NgModule({
declarations: [...components],
imports: [
BrowserModule,
BrowserAnimationsModule,
SharedModule,
FileUploadDownloadModule,
HttpClientModule,
AuthModule,
AppRoutingModule,
MonacoEditorModule,
IqserHelpModeModule,
ToastrModule.forRoot({
closeButton: true,
enableHtml: true,
toastComponent: ToastComponent,
preventDuplicates: true,
resetTimeoutOnDuplicate: true,
}),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: httpLoaderFactory,
deps: [HttpClient, ConfigService],
},
compiler: {
provide: TranslateCompiler,
useClass: TranslateMessageFormatCompiler,
},
}),
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
],
providers: [
{
provide: HTTP_INTERCEPTORS,
multi: true,
useClass: ServerErrorInterceptor,
},
{
provide: ErrorHandler,
useClass: GlobalErrorHandler,
},
{
provide: BASE_HREF,
useFactory: (s: PlatformLocation) => cleanupBaseUrl(s.getBaseHrefFromDOM()),
deps: [PlatformLocation],
},
{
provide: HTTP_INTERCEPTORS,
multi: true,
useClass: ApiPathInterceptor,
},
{
provide: HTTP_INTERCEPTORS,
multi: true,
useClass: HttpCacheInterceptor,
},
{
provide: APP_INITIALIZER,
multi: true,
useFactory: configurationInitializer,
deps: [KeycloakService, Title, ConfigService, GeneralSettingsService, LanguageService, UserService, UserPreferenceService],
},
{
provide: MissingTranslationHandler,
useClass: REDMissingTranslationHandler,
},
{
provide: HELP_DOCS,
useValue: links,
},
{
provide: MANUAL_BASE_URL,
useFactory: (configService: ConfigService) => configService.values.MANUAL_BASE_URL,
deps: [ConfigService],
},
{
provide: MAX_RETRIES_ON_SERVER_ERROR,
useFactory: (configService: ConfigService) => configService.values.MAX_RETRIES_ON_SERVER_ERROR,
deps: [ConfigService],
},
DatePipe,
],
bootstrap: [AppComponent],
})
export class AppModule {
constructor(private readonly _router: Router, private readonly _route: ActivatedRoute) {
this._configureKeyCloakRouteHandling();
// this._test();
}
private _configureKeyCloakRouteHandling() {
this._route.queryParamMap.subscribe(queryParams => {
if (queryParams.has('code') || queryParams.has('state') || queryParams.has('session_state')) {
this._router.navigate([], {
queryParams: {
state: null,
session_state: null,
code: null,
},
queryParamsHandling: 'merge',
});
}
});
}
// private _test(){
//
// const flatGerman = flatten(german);
//
//
// const flatEnglish = flatten(english);
//
// const tmfc = new TranslateMessageFormatCompiler();
//
//
//
// for (const key of Object.keys(flatGerman)) {
// try {
// const result = tmfc.compile(flatGerman[key], 'de');
// //console.log(result);
// } catch (e) {
// console.error('ERROR AT: ', flatGerman[key]);
// }
// }
//
// for (const key of Object.keys(flatEnglish)) {
// try {
// const result = tmfc.compile(flatEnglish[key], 'de');
// //console.log(result);
// } catch (e) {
// console.error('ERROR AT: ', flatEnglish[key]);
// }
// }
// console.log('done');
// }
}
//
// function flatten(data: any) {
// const result: any = {};
//
// function recurse(cur: any, prop: any) {
// if (Object(cur) !== cur) {
// result[prop] = cur;
// } else if (Array.isArray(cur)) {
// let l = 0;
// for (let i = 0, l = cur.length; i < l; i++) recurse(cur[i], prop + '[' + i + ']');
// if (l === 0) result[prop] = [];
// } else {
// let isEmpty = true;
// for (const p in cur) {
// isEmpty = false;
// recurse(cur[p], prop ? prop + '.' + p : p);
// }
// if (isEmpty && prop) result[prop] = {};
// }
// }
//
// recurse(data, '');
// return result;
// }