New dashboard look & feel
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / alert.service.ts
1 import { Injectable, SecurityContext } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { Subject } from 'rxjs/Subject';
4
5
6 export type AlertType = 'error' | 'warning' | 'info' | 'success';
7
8 export interface IAlert {
9   type: AlertType;
10   msg: string;
11   show?: boolean;
12   dismissible?: boolean;
13   dismissTimeout?: number;     // close alert after this time (in seconds)
14   id?: number;
15 }
16
17 @Injectable()
18 export class AlertService {
19   public alerts: Observable<IAlert[]>;
20
21   private _alerts: IAlert[];
22   private alertsSubject = <Subject<IAlert[]>>new Subject();
23   private uid = 0;
24   private defaultDismissTmo = 5; // in seconds
25
26   constructor() {
27     this.alerts = this.alertsSubject.asObservable();
28     this._alerts = [];
29     this.uid = 0;
30   }
31
32   public error(msg: string, dismissTime?: number) {
33     this.add({
34       type: 'error', msg: msg, dismissible: true, dismissTimeout: dismissTime
35     });
36   }
37
38   public warning(msg: string, dismissible?: boolean) {
39     this.add({ type: 'warning', msg: msg, dismissible: true, dismissTimeout: (dismissible ? this.defaultDismissTmo : 0) });
40   }
41
42   public info(msg: string) {
43     this.add({ type: 'info', msg: msg, dismissible: true, dismissTimeout: this.defaultDismissTmo });
44   }
45
46   public add(al: IAlert) {
47     const msg = String(al.msg).replace('\n', '<br>');
48     // this._alerts.push({
49     this._alerts = [{
50       show: true,
51       type: al.type,
52       msg: msg,
53       dismissible: al.dismissible || true,
54       dismissTimeout: (al.dismissTimeout * 1000) || 0,
55       id: this.uid,
56     }];
57     this.uid += 1;
58     this.alertsSubject.next(this._alerts);
59
60   }
61
62   public del(al: IAlert) {
63     /*
64     const idx = this._alerts.findIndex((a) => a.id === al.id);
65     if (idx > -1) {
66       this._alerts.splice(idx, 1);
67       this.alertsSubject.next(this._alerts);
68     }
69     */
70     this._alerts = [];
71     this.alertsSubject.next(this._alerts);
72   }
73 }