argos/dmp-frontend/src/app/ui/supportive-material-editor/supportive-material-editor....

168 lines
6.4 KiB
TypeScript
Raw Normal View History

2020-02-13 17:15:34 +01:00
import { Component, OnInit } from '@angular/core';
2023-10-05 15:39:17 +02:00
import { UntypedFormGroup, UntypedFormBuilder } from '@angular/forms';
2020-02-13 17:15:34 +01:00
import { BaseComponent } from '@common/base/base.component';
import { takeUntil } from 'rxjs/operators';
import { UiNotificationService, SnackBarNotificationLevel } from '@app/core/services/notification/ui-notification-service';
import { TranslateService } from '@ngx-translate/core';
import { Router } from '@angular/router';
2021-09-24 20:52:14 +02:00
import { isNullOrUndefined } from '@app/utilities/enhancers/utils';
2020-02-13 17:15:34 +01:00
import { environment } from 'environments/environment';
import { ConfigurationService } from '@app/core/services/configuration/configuration.service';
2020-12-10 10:34:01 +01:00
import { AuthService } from '@app/core/services/auth/auth.service';
import { LanguageService } from '@app/core/services/language/language.service';
import { MatomoService } from '@app/core/services/matomo/matomo-service';
import { HttpClient } from '@angular/common/http';
2023-10-18 09:31:57 +02:00
import { SupportiveMaterialService } from '@app/core/services/supportive-material/supportive-material.service';
import { SupportiveMaterialFieldType } from '@app/core/common/enum/supportive-material-field-type';
2023-11-03 12:30:11 +01:00
import { SupportiveMaterialPersist } from '@app/core/model/supportive-material/supportive-material';
interface VisibleMaterialType{
name: string;
type: SupportiveMaterialFieldType;
}
interface VisibleLangType{
name: string;
type: string;
}
2020-02-13 17:15:34 +01:00
@Component({
2023-11-03 12:30:11 +01:00
selector: 'app-supportive-material-editor',
templateUrl: './supportive-material-editor.component.html',
styleUrls: ['./supportive-material-editor.component.scss']
2020-02-13 17:15:34 +01:00
})
2023-11-03 12:30:11 +01:00
export class SupportiveMaterialEditorComponent extends BaseComponent implements OnInit {
2023-10-05 15:39:17 +02:00
public formGroup: UntypedFormGroup;
private formBuilder: UntypedFormBuilder;
2020-02-13 17:15:34 +01:00
2023-10-18 09:31:57 +02:00
constructor(private supportiveMaterialService: SupportiveMaterialService,
2020-02-13 17:15:34 +01:00
private uiNotificationService: UiNotificationService,
private translate: TranslateService,
private router: Router,
2020-12-10 10:34:01 +01:00
private configurationService: ConfigurationService,
private languageService: LanguageService,
private httpClient: HttpClient,
private matomoService: MatomoService
2020-02-13 17:15:34 +01:00
) { super(); }
2023-11-03 12:30:11 +01:00
visiblesMaterialsTypes: VisibleMaterialType[] = [
{name: "FOOTER.FAQ", type: SupportiveMaterialFieldType.Faq},
{name: "FOOTER.ABOUT", type: SupportiveMaterialFieldType.About},
{name: "FOOTER.GLOSSARY", type: SupportiveMaterialFieldType.Glossary},
{name: "FOOTER.TERMS-OF-SERVICE", type: SupportiveMaterialFieldType.TermsOfService},
{name: "FOOTER.GUIDE", type: SupportiveMaterialFieldType.UserGuide}
]
visiblesLangTypes: VisibleLangType[] = [
{name: "GENERAL.LANGUAGES.ENGLISH", type: "en"},
{name: "GENERAL.LANGUAGES.GREEK", type: "gr"},
{name: "GENERAL.LANGUAGES.SPANISH", type: "es"},
{name: "GENERAL.LANGUAGES.GERMAN", type: "de"},
{name: "GENERAL.LANGUAGES.TURKISH", type: "tr"},
{name: "GENERAL.LANGUAGES.SLOVAK", type: "sk"},
{name: "GENERAL.LANGUAGES.SERBIAN", type: "sr"},
{name: "GENERAL.LANGUAGES.PORTUGUESE", type: "pt"},
{name: "GENERAL.LANGUAGES.CROATIAN", type: "hr"},
{name: "GENERAL.LANGUAGES.POLISH", type: "pl"}
]
selectedMaterial: VisibleMaterialType;
selectedLang: VisibleLangType;
2020-02-13 17:15:34 +01:00
ngOnInit() {
2023-11-03 12:30:11 +01:00
this.selectedMaterial = this.visiblesMaterialsTypes.find(x => x.type == SupportiveMaterialFieldType.Faq);
this.selectedLang = this.visiblesLangTypes.find(x => x.type == this.languageService.getCurrentLanguage());
this.getSupportiveMaterialData();
}
public selectedMaterialChanged(item: VisibleMaterialType){
this.selectedMaterial = item;
this.getSupportiveMaterialData();
}
public selectedLangChanged(item: VisibleLangType){
this.selectedLang = item;
this.getSupportiveMaterialData();
}
private getSupportiveMaterialData(){
this.matomoService.trackPageView(`Admin: ${this.selectedMaterial.name} Editor`);
2023-10-05 15:39:17 +02:00
this.formBuilder = new UntypedFormBuilder();
2020-02-13 17:15:34 +01:00
this.formGroup = this.formBuilder.group({
name: [''],
html: ['']
});
2023-11-03 12:30:11 +01:00
this.supportiveMaterialService.getMaterial(this.selectedLang.type, this.selectedMaterial.type).pipe(takeUntil(this._destroyed)).subscribe(data => {
2020-02-13 17:15:34 +01:00
const contentDispositionHeader = data.headers.get('Content-Disposition');
const filename = contentDispositionHeader.split(';')[1].trim().split('=')[1].replace(/"/g, '');
this.formGroup.get('name').patchValue(filename);
this.loadFile(data.body);
});
}
private parseText(source: string): string {
source = source.replace(/src="images/g, `src="${this.configurationService.guideAssets}`);
2020-02-17 16:43:12 +01:00
source = source.replace(/\r\n +>/g, '>\n');
2020-02-13 17:15:34 +01:00
const brokenElements = Array.from(new Set(source.match(/<\/\w+\d* >/g)));
if (!isNullOrUndefined(brokenElements)) {
brokenElements.forEach((brokenElement) => {
const tempvalue = brokenElement.match(/\/\w+\d*/)[0];
2020-02-17 16:43:12 +01:00
source = source.replace(new RegExp(brokenElement, 'g'), `<${tempvalue}>\n`);
2020-02-13 17:15:34 +01:00
});
}
return source;
}
loadFile(data: Blob) {
const reader = new FileReader();
reader.addEventListener('load', () => {
let result = this.parseText(reader.result as string);
2020-12-31 15:30:35 +01:00
//result = result.replace(/class="href" path="/g, 'href="#');
this.formGroup.get('html').patchValue(result);
2020-02-13 17:15:34 +01:00
}, false);
reader.readAsText(data);
}
submit() {
let result = this.parseText(this.formGroup.get('html').value);
2020-12-31 15:30:35 +01:00
//result = result.replace(/href="#/g, 'class="href" path="');
2020-02-13 17:15:34 +01:00
this.formGroup.get('html').patchValue(result);
//const item = {name: this.formGroup.value['name'], type: this.selectedMaterial.type ,html: this.formGroup.value['html']} as SupportiveMaterialPersist
const item = {id: null,
type: this.selectedMaterial.type,
languageCode: this.selectedLang.type,
payload: this.formGroup.value['html']} as SupportiveMaterialPersist;
// if(item.name.endsWith("_en.html") && this.selectedLang.type != "en" ){
// item.name = item.name.replace("_en.html", `_${this.selectedLang.type}.html`);
// }
2023-11-03 12:30:11 +01:00
this.supportiveMaterialService.persist(item).pipe(takeUntil(this._destroyed))
2020-02-13 17:15:34 +01:00
.subscribe(
complete => {
this.onCallbackSuccess();
2020-02-13 17:15:34 +01:00
},
error => {
this.onCallbackError(error);
}
);
}
onCallbackSuccess(): void {
2020-02-13 17:15:34 +01:00
this.uiNotificationService.snackBarNotification( this.translate.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
this.router.navigate(['/reload']).then(() => this.router.navigate(['/supportive-material']));
2020-02-13 17:15:34 +01:00
}
onCallbackError(error: any) {
this.uiNotificationService.snackBarNotification( error, SnackBarNotificationLevel.Error);
//this.validateAllFormFields(this.formGroup);
}
}