UNPKG

@lhn/mat-alert

Version:

Implementation of MatDialog shown as an alert message

123 lines (114 loc) 4.04 kB
import { InjectionToken, Component, Inject, Injectable, NgModule } from '@angular/core'; import { MAT_DIALOG_DATA, MatDialog, MatDialogModule } from '@angular/material/dialog'; import { DomSanitizer } from '@angular/platform-browser'; import { CommonModule } from '@angular/common'; import { MatButtonModule } from '@angular/material/button'; class MatAlertConfig { } const MAT_ALERT_DEFAULT_CONFIG = new InjectionToken('mat-alert.config'); const matAlertDefaultConfig = { buttonText: 'Ok', buttonTheme: null, raisedButton: false, autoFocus: true, }; class MatAlertComponent { constructor(data, sanitizer, defaultConfigs) { this.sanitizer = sanitizer; data.message = this._handleMessage(data.message); this.config = Object.assign({}, defaultConfigs, data); } /** * Handles the message and sanitize */ _handleMessage(message) { message = message.toString().replace(/(\r\n|\r|\n)/g, '<br/>'); return this.sanitizer.bypassSecurityTrustHtml(message.toString()); } } MatAlertComponent.decorators = [ { type: Component, args: [{ selector: 'lhn-mat-alert', template: ` <h1 mat-dialog-title>{{config.title}}</h1> <mat-dialog-content class="mat-typography" *ngIf="config.message"> <h4 [innerHTML]="config.message"></h4> </mat-dialog-content> <mat-dialog-actions align="end"> <button [class.mat-raised-button]="config.raisedButton" [color]="config.buttonTheme" mat-button mat-dialog-close type="button"> {{config.buttonText}} </button> </mat-dialog-actions> ` },] } ]; MatAlertComponent.ctorParameters = () => [ { type: MatAlertConfig, decorators: [{ type: Inject, args: [MAT_DIALOG_DATA,] }] }, { type: DomSanitizer }, { type: MatAlertConfig, decorators: [{ type: Inject, args: [MAT_ALERT_DEFAULT_CONFIG,] }] } ]; class MatAlert { constructor(dlg, defaultConfigs) { this.dlg = dlg; this.defaultConfigs = defaultConfigs; } /** * Opens an alert dialog * * @param title Title for the alert * @param message Message body. Can be an additional description * @param config Configuration for the alert * @param disableClose Flag for disable closing the alert clicking outside or pressing Escape * @return Observable<void> The MatDialog's `afterClosed()` observable */ show(title, message = '', config = {}, disableClose = false) { var _a; config.title = title; config.message = message || ''; const cfg = Object.assign({ minWidth: 300, data: config, role: 'alertdialog', disableClose, hasBackdrop: (_a = config.hasBackdrop) !== null && _a !== void 0 ? _a : true, }, this.defaultConfigs, config); return this.dlg.open(MatAlertComponent, cfg).afterClosed(); } } MatAlert.decorators = [ { type: Injectable } ]; MatAlert.ctorParameters = () => [ { type: MatDialog }, { type: MatAlertConfig, decorators: [{ type: Inject, args: [MAT_ALERT_DEFAULT_CONFIG,] }] } ]; const ɵ0 = matAlertDefaultConfig; class MatAlertModule { } MatAlertModule.decorators = [ { type: NgModule, args: [{ declarations: [MatAlertComponent], entryComponents: [MatAlertComponent], imports: [ CommonModule, MatDialogModule, MatButtonModule, ], providers: [ MatAlert, { provide: MAT_ALERT_DEFAULT_CONFIG, useValue: ɵ0 }, ], },] } ]; /* * Public API Surface of mat-alert */ /** * Generated bundle index. Do not edit. */ export { MatAlert, MatAlertModule, ɵ0, MAT_ALERT_DEFAULT_CONFIG as ɵa, matAlertDefaultConfig as ɵb, MatAlertConfig as ɵc, MatAlertComponent as ɵd }; //# sourceMappingURL=lhn-mat-alert.js.map