2022-03-11 10:53:32 -08:00
|
|
|
import { Injectable } from '@angular/core'
|
2023-08-17 20:09:40 -07:00
|
|
|
import { Subject } from 'rxjs'
|
2020-10-27 01:10:18 +01:00
|
|
|
|
2020-11-07 12:05:15 +01:00
|
|
|
export interface Toast {
|
2020-10-27 01:10:18 +01:00
|
|
|
title: string
|
|
|
|
|
|
|
|
|
|
content: string
|
|
|
|
|
|
2020-12-28 21:52:09 +01:00
|
|
|
delay: number
|
2020-11-07 12:05:15 +01:00
|
|
|
|
|
|
|
|
action?: any
|
|
|
|
|
|
|
|
|
|
actionName?: string
|
2023-02-16 19:41:38 -08:00
|
|
|
|
|
|
|
|
classname?: string
|
2023-05-17 12:46:04 -07:00
|
|
|
|
|
|
|
|
error?: any
|
2020-10-27 01:10:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
2022-03-11 10:53:32 -08:00
|
|
|
providedIn: 'root',
|
2020-10-27 01:10:18 +01:00
|
|
|
})
|
|
|
|
|
export class ToastService {
|
2022-03-11 10:53:32 -08:00
|
|
|
constructor() {}
|
2020-10-27 01:10:18 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-05-17 12:46:04 -07:00
|
|
|
showError(content: string, delay: number = 10000, error: any = null) {
|
2023-02-16 19:41:38 -08:00
|
|
|
this.show({
|
|
|
|
|
title: $localize`Error`,
|
|
|
|
|
content: content,
|
|
|
|
|
delay: delay,
|
|
|
|
|
classname: 'error',
|
2023-05-17 12:46:04 -07:00
|
|
|
error,
|
2023-02-16 19:41:38 -08:00
|
|
|
})
|
2020-11-07 12:05:15 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 21:52:09 +01:00
|
|
|
showInfo(content: string, delay: number = 5000) {
|
2022-03-11 10:53:32 -08:00
|
|
|
this.show({ title: $localize`Information`, content: content, delay: delay })
|
2020-11-07 12:05:15 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-27 01:10:18 +01:00
|
|
|
closeToast(toast: Toast) {
|
2022-03-11 10:53:32 -08:00
|
|
|
let index = this.toasts.findIndex((t) => t == toast)
|
2020-10-27 01:10:18 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|