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

51 lines
956 B
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
2020-12-28 21:52:09 +01:00
export interface Toast {
2020-10-29 14:35:36 +01:00
2020-10-27 01:10:18 +01:00
title: string
content: string
2020-12-28 21:52:09 +01:00
delay: number
2020-10-27 01:10:18 +01:00
}
@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
2020-12-28 21:52:09 +01:00
show(toast: Toast) {
2020-10-27 01:10:18 +01:00
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
}
2020-12-28 21:52:09 +01:00
showError(content: string, delay: number = 10000) {
this.show({title: $localize`Error`, content: content, delay: delay})
}
showInfo(content: string, delay: number = 5000) {
this.show({title: $localize`Information`, content: content, delay: delay})
}
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
}
}