1 import { Injectable, SecurityContext } from '@angular/core';
2 import { DomSanitizer } from '@angular/platform-browser';
3 import { Observable } from 'rxjs/Observable';
4 import { Subject } from 'rxjs/Subject';
7 export type AlertType = "danger" | "warning" | "info" | "success";
9 export interface IAlert {
13 dismissible?: boolean;
14 dismissTimeout?: number; // close alert after this time (in seconds)
19 export class AlertService {
20 public alerts: Observable<IAlert[]>;
22 private _alerts: IAlert[];
23 private alertsSubject = <Subject<IAlert[]>>new Subject();
25 private defaultDissmissTmo = 5; // in seconds
27 constructor(private sanitizer: DomSanitizer) {
28 this.alerts = this.alertsSubject.asObservable();
33 public error(msg: string, dismissTime?: number) {
35 type: "danger", msg: msg, dismissible: true, dismissTimeout: dismissTime
39 public warning(msg: string, dismissible?: boolean) {
40 this.add({ type: "warning", msg: msg, dismissible: true, dismissTimeout: (dismissible ? this.defaultDissmissTmo : 0) });
43 public info(msg: string) {
44 this.add({ type: "info", msg: msg, dismissible: true, dismissTimeout: this.defaultDissmissTmo });
47 public add(al: IAlert) {
51 msg: this.sanitizer.sanitize(SecurityContext.HTML, al.msg),
52 dismissible: al.dismissible || true,
53 dismissTimeout: (al.dismissTimeout * 1000) || 0,
57 this.alertsSubject.next(this._alerts);
60 public del(al: IAlert) {
61 let idx = this._alerts.findIndex((a) => a.id === al.id);
63 this._alerts.splice(idx, 1);