您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

68 行
2.1 KiB

  1. import {DomSanitizer, SafeHtml} from "@angular/platform-browser";
  2. import {Injectable} from "@angular/core";
  3. import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap";
  4. import {ModalStatus} from "@app/_helpers/modal.states";
  5. @Injectable({ providedIn: 'root' })
  6. export class AppHelperService {
  7. constructor(
  8. private sanitizer: DomSanitizer,
  9. private modalService: NgbModal,
  10. ) {}
  11. public extractId(iri: string | undefined | null): string {
  12. if (iri !== undefined && iri !== null) {
  13. const iriRegex = /\/(\d+)$/;
  14. const match = iri.match(iriRegex);
  15. if (match && match[1]) {
  16. return match[1];
  17. }
  18. }
  19. return "";
  20. }
  21. public convertDate(dateString: string | null, withTime = false) {
  22. // number 10 for input date (2024-03-15)
  23. // number 16 for input datetime-local (2024-04-28T03:22)
  24. if (dateString !== null) {
  25. const date = new Date(dateString);
  26. return date.toISOString().slice(0, withTime ? 16 : 10);
  27. }
  28. return "";
  29. }
  30. public getSafeLongtext(longtext: any): SafeHtml {
  31. if (longtext) {
  32. return this.sanitizer.bypassSecurityTrustHtml(longtext.replace(/\n/g, '<br>'));
  33. }
  34. return false;
  35. }
  36. public getModalOptions(): NgbModalOptions {
  37. return { centered: true } as NgbModalOptions;
  38. }
  39. public openModal(component: any, data: any, callback?: () => void): Promise<ModalStatus> {
  40. const modalRef = this.modalService.open(component);
  41. for (const key in data) {
  42. modalRef.componentInstance[key] = data[key];
  43. }
  44. return modalRef.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  45. if (modalStatus === ModalStatus.Submitted) {
  46. modalRef.dismiss();
  47. if (callback) {
  48. callback();
  49. }
  50. }
  51. });
  52. }
  53. public assertType<T>(value: any, type: string): asserts value is T {
  54. if (typeof value !== type) {
  55. throw new Error(`Expected ${type} but received ${typeof value}`);
  56. }
  57. }
  58. }