blob: d75049b3603c734afe6b4f1b12fb05da10842084 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
import { Component, OnInit, ViewChild } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { SelectItem } from 'primeng/api';
import { User, Role } from './authentication/authentication.service';
import { AuthenticationService } from './authentication/authentication.service';
import { NodeService } from '@navigator/node.service';
import { NodeproviderService } from '@navigator/nodeprovider.service';
import ISO6391 from 'iso-639-1';
import { MdmLocalizationService } from '@localization/mdm-localization.service';
import { AvailableDatasourceService } from './available-datasource/available-datasource.service';
import { Preference, PreferenceService, Scope } from './core/preference.service';
import { MultiSelect } from 'primeng/primeng';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
public static readonly DEFAULT_LANG_CODE = 'en';
public languages: SelectItem[] = [];
public selectedLanguage: string;
public datasources: SelectItem[] = [];
public selectedDatasources: string[] = [];
public standardDatasources: String[];
private preferenceName: string = 'app.components.standard_datasource';
public selectedPreferenceRow: SelectItem[] = [];
public displayDatasourceDialog = false;
public canShowApplication: boolean = false;
public links = [
{ name: 'Administration', path: '/administration', roles: [ Role.Admin ] }
];
public displayAboutDialog = false;
public user: User = null;
public rolesTooltip: string;
@ViewChild('datasourcemultiselect')
public datasourceMultiselect : MultiSelect;
constructor(private translate: TranslateService,
private localizationService: MdmLocalizationService,
private authService: AuthenticationService,
private nodeService: NodeService,
private preferenceService: PreferenceService,
private nodeproviderService: NodeproviderService,
private dsService: AvailableDatasourceService) {}
ngOnInit() {
this.initLanguageSettings();
this.initStandarDatasource(this.loadPreference(this.preferenceName)).subscribe(value => {
this.standardDatasources = value;
})
this.authService.getLoginUser().subscribe(value => {
this.user = value;
this.rolesTooltip = value.roles.join(', ');
});
this.dsService.getAvailableDatasource().subscribe(value => {
this.datasources = value.map(ds => { return { label: ds, value: ds }});
if (this.standardDatasources === null || this.standardDatasources.length === 0) {
this.displayDatasourceDialog = true
} else {
this.datasources.filter(sourceName => this.standardDatasources.forEach(sp => (sp === sourceName.label) ? this.selectedDatasources.push(sourceName.value) : null));
this.selectedPreferenceRow = this.selectedDatasources.map(sourceName => {return { label: sourceName, value: sourceName }});
this.onSelectDatasources();
}
});
}
private initLanguageSettings() {
this.localizationService.getLanguages().subscribe(langs => {
this.languages = Array.from(langs).map(l => {return { label: ISO6391.getNativeName(l), value: l }});
this.translate.langs = this.languages.map(i => i.value);
this.translate.setDefaultLang(AppComponent.DEFAULT_LANG_CODE);
let browserLang = this.translate.getBrowserLang();
if (this.translate.langs.findIndex(l => l === browserLang) > -1) {
this.selectedLanguage = browserLang;
} else {
this.selectedLanguage = this.translate.defaultLang;
}
this.translate.use(this.selectedLanguage);
});
}
public closeDatasourceDropdown(event: any) {
this.datasourceMultiselect.close(event);
}
public onSelectDatasources() {
this.dsService.setDatasource(this.selectedDatasources).subscribe(value => {
this.nodeproviderService.loadNodeproviders().subscribe(np => {
this.nodeproviderService.setActiveNodeprovider(np[0].id);
if (this.languages === undefined || this.languages.length === 0) {
this.initLanguageSettings();
}
this.nodeService.setActiveDatasource(this.selectedDatasources.join(',')); // TODO
this.standardDatasources = [];
this.selectedPreferenceRow.forEach(pp => this.standardDatasources.push(pp.label));
this.canShowApplication = true;
})
});
}
saveDatasourcePreference($event) {
let werte: string = '';
this.selectedPreferenceRow.forEach(pp => werte += pp.label + '\n');
this.preferenceService.savePreference(this.datasourceToPreference(werte.substr(0, werte.length - 1))).subscribe(value => {
this.selectedDatasources = [];
this.selectedPreferenceRow.forEach(pp => this.selectedDatasources.push(pp.value));
this.onSelectDatasources();
this.displayDatasourceDialog = false;
});
}
private datasourceToPreference(datasource: string) {
let pref = new Preference();
pref.value = datasource;
pref.key = this.preferenceName;
pref.scope = Scope.USER;
return pref;
}
public onSelectLanguage($event: any) {
this.translate.use(this.selectedLanguage);
}
public onShowAboutDialog() {
this.displayAboutDialog = true;
}
private initStandarDatasource(pref: Observable<Preference>): Observable<string[]> {
return pref.pipe(map(p => (p === undefined ? null : p.value.split('\n'))));
}
private loadPreference(preferenceName: string): Observable<Preference> {
return this.preferenceService.getPreferenceForScope('USER', preferenceName)
.pipe(map(p => p[0]));
}
}