/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Input,
OnDestroy,
} from '@angular/core';
import { takeWhile, map } from 'rxjs/operators';
import { ActivatedRoute } from '@angular/router';
import { combineLatest, Observable } from 'rxjs';
@Component({
selector: 'ngx-page-toc',
styleUrls: ['./page-toc.component.scss'],
template: `
0">
Overview
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NgxPageTocComponent implements OnDestroy {
items: any[];
@Input()
set toc(value: Observable) {
combineLatest([
value,
this.activatedRoute.fragment,
])
.pipe(
takeWhile(() => this.alive),
map(([toc, fragment]) => {
toc = toc.map((item: any) => ({ ...item, selected: fragment === item.fragment }));
if (toc.length && !toc.find(item => item.selected)) {
toc[0].selected = true;
}
return toc;
}),
)
.subscribe((toc) => {
this.items = toc;
this.cd.detectChanges();
});
}
private alive = true;
constructor(private activatedRoute: ActivatedRoute, private cd: ChangeDetectorRef) {
}
ngOnDestroy() {
this.alive = false;
}
}