import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { AlertConfig } from './alert.config';
import { OnChange } from '../utils/decorators';
@Component({
selector: 'alert,ngx-alert',
template: `
`
})
export class AlertComponent implements OnInit {
/** Alert type. Provides one of four bootstrap supported contextual classes: `success`, `info`, `warning` and `danger` */
@Input() public type: string = 'warning';
/** If set, displays an inline "Close" button */
@OnChange()
@Input() public dismissible: boolean = false;
/** Number in milliseconds, after which alert will be closed */
@Input() public dismissOnTimeout: number | string;
/** This event fires immediately after close instance method is called, $event is an instance of Alert component. */
@Output() public onClose: EventEmitter = new EventEmitter();
/** This event fires when alert closed, $event is an instance of Alert component */
@Output() public onClosed: EventEmitter = new EventEmitter();
public isClosed: boolean = false;
public classes: string = '';
public dismissibleChange: EventEmitter = new EventEmitter();
public constructor(_config: AlertConfig) {
Object.assign(this, _config);
this.dismissibleChange.subscribe((dismissible: boolean) => {
this.classes = this.dismissible ? 'alert-dismissible' : '';
});
}
public ngOnInit(): void {
if (this.dismissOnTimeout) {
// if dismissOnTimeout used as attr without binding, it will be a string
setTimeout(() => this.close(),
parseInt(this.dismissOnTimeout as string, 10));
}
}
// todo: animation ` If the .fade and .in classes are present on the element,
// the alert will fade out before it is removed`
/**
* Closes an alert by removing it from the DOM.
*/
public close(): void {
if (this.isClosed) {
return;
}
this.onClose.emit(this);
this.isClosed = true;
this.onClosed.emit(this);
}
}