paperless-ngx/src-ui/src/app/services/toast.service.ts

60 lines
1 KiB
TypeScript
Raw Normal View History

2020-10-27 01:10:18 +01:00
import { Injectable } from '@angular/core';
2020-10-29 14:35:36 +01:00
import { Subject, zip } from 'rxjs';
2020-10-27 01:10:18 +01:00
export class Toast {
2020-10-29 14:35:36 +01:00
static make(title: string, content: string, classname?: string, delay?: number): Toast {
2020-10-27 01:10:18 +01:00
let t = new Toast()
t.title = title
t.content = content
2020-10-29 14:35:36 +01:00
t.classname = classname
2020-10-27 01:10:18 +01:00
if (delay) {
t.delay = delay
}
return t
}
2020-10-29 14:35:36 +01:00
static makeError(content: string) {
return Toast.make("Error", content, null, 10000)
}
2020-10-27 01:10:18 +01:00
title: string
2020-10-29 14:35:36 +01:00
classname: string
2020-10-27 01:10:18 +01:00
content: string
delay: number = 5000
}
@Injectable({
providedIn: 'root'
})
export class ToastService {
constructor() { }
private toasts: Toast[] = []
2020-10-29 14:35:36 +01:00
private toastsSubject: Subject<Toast[]> = new Subject()
2020-10-27 01:10:18 +01:00
showToast(toast: Toast) {
this.toasts.push(toast)
2020-10-29 14:35:36 +01:00
this.toastsSubject.next(this.toasts)
2020-10-27 01:10:18 +01:00
}
closeToast(toast: Toast) {
let index = this.toasts.findIndex(t => t == toast)
if (index > -1) {
this.toasts.splice(index, 1)
2020-10-29 14:35:36 +01:00
this.toastsSubject.next(this.toasts)
2020-10-27 01:10:18 +01:00
}
}
getToasts() {
2020-10-29 14:35:36 +01:00
return this.toastsSubject
2020-10-27 01:10:18 +01:00
}
}