|
|
|
@ -1,11 +1,10 @@ |
|
|
|
import { ApplicationRef, ComponentRef, inject, Injectable } from '@angular/core'; |
|
|
|
import { |
|
|
|
ContentProjectionService, |
|
|
|
LocalizationParam, |
|
|
|
PROJECTION_STRATEGY, |
|
|
|
Strict, |
|
|
|
} from '@abp/ng.core'; |
|
|
|
import { ComponentRef, inject, Injectable } from '@angular/core'; |
|
|
|
import { ReplaySubject } from 'rxjs'; |
|
|
|
import { ToastContainerComponent } from '../components/toast-container/toast-container.component'; |
|
|
|
import { Toaster } from '../models'; |
|
|
|
|
|
|
|
@ -13,28 +12,33 @@ import { Toaster } from '../models'; |
|
|
|
providedIn: 'root', |
|
|
|
}) |
|
|
|
export class ToasterService implements ToasterContract { |
|
|
|
private toasts$ = new ReplaySubject<Toaster.Toast[]>(1); |
|
|
|
private readonly appRef = inject(ApplicationRef); |
|
|
|
private readonly contentProjectionService = inject(ContentProjectionService); |
|
|
|
|
|
|
|
private lastId = -1; |
|
|
|
|
|
|
|
private toasts = [] as Toaster.Toast[]; |
|
|
|
|
|
|
|
private containerComponentRef!: ComponentRef<ToastContainerComponent>; |
|
|
|
private contentProjectionService: ContentProjectionService; |
|
|
|
|
|
|
|
private setContainer() { |
|
|
|
this.containerComponentRef = this.contentProjectionService.projectContent( |
|
|
|
PROJECTION_STRATEGY.AppendComponentToBody(ToastContainerComponent, { |
|
|
|
toasts$: this.toasts$, |
|
|
|
remove: this.remove, |
|
|
|
}), |
|
|
|
); |
|
|
|
|
|
|
|
this.containerComponentRef.changeDetectorRef.detectChanges(); |
|
|
|
this.syncContainer(); |
|
|
|
} |
|
|
|
|
|
|
|
constructor() { |
|
|
|
this.contentProjectionService = inject(ContentProjectionService); |
|
|
|
private syncContainer() { |
|
|
|
if (!this.containerComponentRef) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.containerComponentRef.instance.setToasts(this.toasts); |
|
|
|
this.containerComponentRef.changeDetectorRef.detectChanges(); |
|
|
|
this.appRef.tick(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -100,23 +104,27 @@ export class ToasterService implements ToasterContract { |
|
|
|
* @param severity Sets color of the toast. "success", "warning" etc. |
|
|
|
* @param options Spesific style or structural options for individual toast |
|
|
|
*/ |
|
|
|
|
|
|
|
show( |
|
|
|
message: LocalizationParam, |
|
|
|
title: LocalizationParam | undefined = undefined, |
|
|
|
severity: Toaster.Severity = 'neutral', |
|
|
|
options = {} as Partial<Toaster.ToastOptions>, |
|
|
|
): Toaster.ToasterId { |
|
|
|
if (!this.containerComponentRef) this.setContainer(); |
|
|
|
if (!this.containerComponentRef) { |
|
|
|
this.setContainer(); |
|
|
|
} |
|
|
|
|
|
|
|
const id = ++this.lastId; |
|
|
|
this.toasts.push({ |
|
|
|
message, |
|
|
|
title, |
|
|
|
severity, |
|
|
|
options: { closable: true, id, ...options }, |
|
|
|
}); |
|
|
|
this.toasts$.next(this.toasts); |
|
|
|
this.toasts = [ |
|
|
|
...this.toasts, |
|
|
|
{ |
|
|
|
message, |
|
|
|
title, |
|
|
|
severity, |
|
|
|
options: { closable: true, id, ...options }, |
|
|
|
}, |
|
|
|
]; |
|
|
|
this.syncContainer(); |
|
|
|
return id; |
|
|
|
} |
|
|
|
|
|
|
|
@ -126,7 +134,7 @@ export class ToasterService implements ToasterContract { |
|
|
|
*/ |
|
|
|
remove = (id: number) => { |
|
|
|
this.toasts = this.toasts.filter(toast => toast.options?.id !== id); |
|
|
|
this.toasts$.next(this.toasts); |
|
|
|
this.syncContainer(); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -136,7 +144,7 @@ export class ToasterService implements ToasterContract { |
|
|
|
this.toasts = !containerKey |
|
|
|
? [] |
|
|
|
: this.toasts.filter(toast => toast.options?.containerKey !== containerKey); |
|
|
|
this.toasts$.next(this.toasts); |
|
|
|
this.syncContainer(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|