|
| 1 | +import { Inject, Component, OnInit } from '@angular/core'; |
| 2 | +import { |
| 3 | + MAT_DIALOG_DATA, |
| 4 | + MatDialogRef, |
| 5 | + MatDialog, |
| 6 | + MatDialogConfig, |
| 7 | +} from '@angular/material/dialog'; |
| 8 | +import * as md from 'markdown-it'; |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: 'app-modal-message', |
| 12 | + templateUrl: './modal-message.component.html', |
| 13 | + styleUrls: ['./modal-message.component.css'], |
| 14 | +}) |
| 15 | +export class ModalMessageComponent implements OnInit { |
| 16 | + data: DialogInfo; |
| 17 | + markdown: md = md(); |
| 18 | + |
| 19 | + DSOMM_host: string = 'https://github.com/devsecopsmaturitymodel'; |
| 20 | + DSOMM_url: string = `${this.DSOMM_host}/DevSecOps-MaturityModel-data`; |
| 21 | + meassageTemplates: Record<string, DialogInfo> = { |
| 22 | + generated_yaml: new DialogInfo( |
| 23 | + `{message}\n\n` + |
| 24 | + `Please download the activity template \`generated.yaml\` ` + |
| 25 | + `from [DSOMM-data](${this.DSOMM_url}) on GitHub.\n\n` + |
| 26 | + 'The DSOMM activities are maintained and distributed ' + |
| 27 | + 'separately from the software.', |
| 28 | + 'DSOMM startup problems' |
| 29 | + ), |
| 30 | + }; |
| 31 | + |
| 32 | + constructor( |
| 33 | + public dialog: MatDialog, |
| 34 | + public dialogRef: MatDialogRef<ModalMessageComponent>, |
| 35 | + @Inject(MAT_DIALOG_DATA) data: DialogInfo |
| 36 | + ) { |
| 37 | + this.data = data; |
| 38 | + } |
| 39 | + |
| 40 | + // eslint-disable-next-line @angular-eslint/no-empty-lifecycle-method |
| 41 | + ngOnInit(): void {} |
| 42 | + |
| 43 | + openDialog( |
| 44 | + dialogInfo: DialogInfo | string |
| 45 | + ): MatDialogRef<ModalMessageComponent> { |
| 46 | + if (typeof dialogInfo === 'string') { |
| 47 | + dialogInfo = new DialogInfo(dialogInfo); |
| 48 | + } |
| 49 | + if ( |
| 50 | + dialogInfo.template && |
| 51 | + this.meassageTemplates.hasOwnProperty(dialogInfo.template) |
| 52 | + ) { |
| 53 | + let template: DialogInfo = this.meassageTemplates[dialogInfo.template]; |
| 54 | + dialogInfo.title = dialogInfo.title || template?.title; |
| 55 | + dialogInfo.message = template?.message?.replace( |
| 56 | + '{message}', |
| 57 | + dialogInfo.message |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + dialogInfo.message = this.markdown.render(dialogInfo.message); |
| 62 | + |
| 63 | + const dialogConfig = new MatDialogConfig(); |
| 64 | + dialogConfig.id = 'modal-message'; |
| 65 | + dialogConfig.disableClose = true; |
| 66 | + dialogConfig.data = dialogInfo; |
| 67 | + dialogConfig.autoFocus = false; |
| 68 | + this.dialogRef = this.dialog.open(ModalMessageComponent, dialogConfig); |
| 69 | + return this.dialogRef; |
| 70 | + } |
| 71 | + |
| 72 | + closeDialog(buttonName: string) { |
| 73 | + this.dialogRef?.close(buttonName); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export class DialogInfo { |
| 78 | + title: string = ''; |
| 79 | + template: string | null = ''; |
| 80 | + message: string = ''; |
| 81 | + buttons: string[] = ['OK']; |
| 82 | + |
| 83 | + constructor(msg: string = '', title: string = '') { |
| 84 | + this.message = msg; |
| 85 | + this.title = title; |
| 86 | + } |
| 87 | +} |
0 commit comments