-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathmodal-message.component.ts
More file actions
80 lines (70 loc) · 2.6 KB
/
modal-message.component.ts
File metadata and controls
80 lines (70 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Inject, Component, OnInit } from '@angular/core';
import {
MAT_DIALOG_DATA,
MatDialogRef,
MatDialog,
MatDialogConfig,
} from '@angular/material/dialog';
import * as md from 'markdown-it';
import { MarkdownText } from 'src/app/model/markdown-text';
import { NotificationService } from 'src/app/service/notification.service';
@Component({
selector: 'app-modal-message',
templateUrl: './modal-message.component.html',
styleUrls: ['./modal-message.component.css'],
})
export class ModalMessageComponent implements OnInit {
data: DialogInfo;
markdown: md = md();
DSOMM_host: string = 'https://github.com/devsecopsmaturitymodel';
DSOMM_url: string = `${this.DSOMM_host}/DevSecOps-MaturityModel-data`;
meassageTemplates: Record<string, DialogInfo> = {};
constructor(
public dialog: MatDialog,
public dialogRef: MatDialogRef<ModalMessageComponent>,
@Inject(MAT_DIALOG_DATA) data: DialogInfo,
private notificationService: NotificationService
) {
this.data = data;
}
// eslint-disable-next-line @angular-eslint/no-empty-lifecycle-method
ngOnInit(): void {
this.notificationService.message$.subscribe(({ title, message }) => {
this.openDialog(new DialogInfo(message, title));
});
}
openDialog(dialogInfo: DialogInfo | string): MatDialogRef<ModalMessageComponent> {
// Remove focus from the button that becomes aria unavailable (avoids ugly console error message)
const buttonElement = document.activeElement as HTMLElement;
if (buttonElement) buttonElement.blur();
if (typeof dialogInfo === 'string') {
dialogInfo = new DialogInfo(dialogInfo);
}
if (dialogInfo.template && this.meassageTemplates.hasOwnProperty(dialogInfo.template)) {
let template: DialogInfo = this.meassageTemplates[dialogInfo.template];
dialogInfo.title = dialogInfo.title || template?.title;
dialogInfo.message = template?.message?.replace('{message}', dialogInfo.message);
}
const dialogConfig = new MatDialogConfig();
dialogConfig.id = 'modal-message';
dialogConfig.disableClose = true;
dialogConfig.data = dialogInfo;
dialogConfig.autoFocus = false;
this.dialogRef = this.dialog.open(ModalMessageComponent, dialogConfig);
return this.dialogRef;
}
closeDialog(buttonName: string) {
this.dialogRef?.close(buttonName);
}
}
export class DialogInfo {
title: string = '';
template: string | null = '';
message: string = '';
buttons: string[] = ['OK'];
constructor(msg: string = '', title: string = '') {
let md: MarkdownText = new MarkdownText(msg);
this.message = md.render();
this.title = title;
}
}