import {DomSanitizer, SafeHtml} from "@angular/platform-browser"; import {Injectable} from "@angular/core"; import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap"; import {ModalStatus} from "@app/_helpers/modal.states"; @Injectable({ providedIn: 'root' }) export class AppHelperService { constructor( private sanitizer: DomSanitizer, private modalService: NgbModal, ) {} public extractId(iri: string | undefined | null): string { if (iri !== undefined && iri !== null) { const iriRegex = /\/(\d+)$/; const match = iri.match(iriRegex); if (match && match[1]) { return match[1]; } } return ""; } public convertDate(dateString: string | null, withTime = false) { // number 10 for input date (2024-03-15) // number 16 for input datetime-local (2024-04-28T03:22) if (dateString !== null) { const date = new Date(dateString); return date.toISOString().slice(0, withTime ? 16 : 10); } return ""; } public getSafeLongtext(longtext: any): SafeHtml { if (longtext) { return this.sanitizer.bypassSecurityTrustHtml(longtext.replace(/\n/g, '
')); } return false; } public getModalOptions(): NgbModalOptions { return { centered: true } as NgbModalOptions; } public openModal(component: any, data: any, callback?: () => void): Promise { const modalRef = this.modalService.open(component); for (const key in data) { modalRef.componentInstance[key] = data[key]; } return modalRef.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRef.dismiss(); if (callback) { callback(); } } }); } public assertType(value: any, type: string): asserts value is T { if (typeof value !== type) { throw new Error(`Expected ${type} but received ${typeof value}`); } } }