File
Methods
registerTemplate
|
registerTemplate(template: TemplateRef<>, index: number)
|
|
Parameters :
Name |
Type |
Optional |
Default value |
template |
TemplateRef<>
|
No
|
|
index |
number
|
No
|
0
|
|
removeTemplate
|
removeTemplate(template: TemplateRef<>)
|
|
Parameters :
Name |
Type |
Optional |
template |
TemplateRef<>
|
No
|
|
templateList$
|
Default value : this.templatesListSubject.asObservable()
|
|
import { TalyPageDataService } from '@allianz/taly-core';
import { DestroyRef, Injectable, TemplateRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { BehaviorSubject } from 'rxjs';
import { skip } from 'rxjs/operators';
@Injectable({ providedIn: 'root' })
export class TalyFrameSmallPrintService {
private registeredTemplates = new Map<TemplateRef<unknown>, number>();
private templatesListSubject = new BehaviorSubject<TemplateRef<unknown>[]>([]);
templateList$ = this.templatesListSubject.asObservable();
private destroyRef = inject(DestroyRef);
constructor(private talyPageDataService: TalyPageDataService) {
this.init();
}
init() {
this.talyPageDataService.pageData$
.pipe(
skip(1), // skip the first as we subscribe to a behavior subject
takeUntilDestroyed(this.destroyRef)
)
.subscribe(() => this.pageChanged());
}
registerTemplate(template: TemplateRef<unknown>, index = 0) {
this.registeredTemplates.set(template, index);
this.update();
}
removeTemplate(template: TemplateRef<unknown>) {
this.registeredTemplates.delete(template);
this.update();
}
private pageChanged() {
this.registeredTemplates.clear();
this.update();
}
private update() {
const sortedTemplates: TemplateRef<unknown>[] = Array.from(this.registeredTemplates.entries())
.sort((a, b) => a[1] - b[1])
.map((entry) => entry[0]);
this.templatesListSubject.next(sortedTemplates);
}
}