import { Component, OnDestroy, OnInit, ContentChild, TemplateRef } from '@angular/core'; import { ToastrConfig, ToastrService, Toast, ToastData, ToastRef } from 'ngx-toastr'; import * as _ from 'lodash'; import { CfCoreComponent } from '../core/core.component'; import { CfAlertComponent } from '../alert/alert.component'; import { INFO_SETTINGS, WARNING_SETTINGS, ERROR_SETTINGS } from '../../../assets/settings/alerts/settings'; @Component({ selector: 'cf-alerts', template: "", styles: ["/deep/ #toast-container { pointer-events: none; position: fixed; z-index: 1000000; display: flex; flex-direction: column; } "] }) export class CfAlertsComponent implements OnInit { @ContentChild(TemplateRef) cfAlertsTemplate; options: ToastrConfig; private lastInserted: number[] = []; constructor(public toastrService: ToastrService) { this.options = this.toastrService.toastrConfig; } ngxToastrShow(alert, type): any { var opt = _.cloneDeep(this.options); if(type === 'info') { opt = Object.assign(opt, INFO_SETTINGS, alert.options); } else if (type === 'warn') { opt = Object.assign(opt, WARNING_SETTINGS, alert.options); } else if (type === 'error') { opt = Object.assign(opt, ERROR_SETTINGS, alert.options); } opt.toastClass = 'cf-alert'; opt.toastComponent = CfAlertComponent; const inserted = this.toastrService.show(alert.message, alert.title, opt); if (inserted) { this.lastInserted.push(inserted.toastId); } return inserted; } showMessage(alert) { this.ngxToastrShow(alert, 'info'); } showWarning(alert) { this.ngxToastrShow(alert, 'warn'); } showError(alert) { this.ngxToastrShow(alert, 'error'); } clearAll() { this.toastrService.clear(); } clearLast() { this.toastrService.clear(this.lastInserted.pop()); } ngOnInit() {} }