2024-07-23 16:50:12 +00:00
|
|
|
/*!
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
|
|
|
|
*/
|
|
|
|
|
|
2024-10-01 18:31:03 +03:00
|
|
|
import {
|
|
|
|
|
ChangeDetectionStrategy,
|
|
|
|
|
Component,
|
|
|
|
|
DestroyRef,
|
2024-10-30 10:54:44 +05:30
|
|
|
input,
|
2024-10-01 18:31:03 +03:00
|
|
|
inject,
|
2025-03-01 03:21:33 +01:00
|
|
|
afterNextRender,
|
2024-10-01 18:31:03 +03:00
|
|
|
} from '@angular/core';
|
2024-11-12 09:35:24 +02:00
|
|
|
import {Location} from '@angular/common';
|
2024-07-23 16:50:12 +00:00
|
|
|
import {TableOfContentsLevel} from '../../interfaces/index';
|
|
|
|
|
import {TableOfContentsLoader} from '../../services/table-of-contents-loader.service';
|
|
|
|
|
import {TableOfContentsScrollSpy} from '../../services/table-of-contents-scroll-spy.service';
|
|
|
|
|
import {IconComponent} from '../icon/icon.component';
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'docs-table-of-contents',
|
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
|
templateUrl: './table-of-contents.component.html',
|
|
|
|
|
styleUrls: ['./table-of-contents.component.scss'],
|
2024-11-12 09:35:24 +02:00
|
|
|
imports: [IconComponent],
|
2024-07-23 16:50:12 +00:00
|
|
|
})
|
|
|
|
|
export class TableOfContents {
|
|
|
|
|
// Element that contains the content from which the Table of Contents is built
|
2024-10-30 10:54:44 +05:30
|
|
|
readonly contentSourceElement = input.required<HTMLElement>();
|
2024-11-12 09:35:24 +02:00
|
|
|
readonly location = inject(Location);
|
2024-07-23 16:50:12 +00:00
|
|
|
|
|
|
|
|
private readonly scrollSpy = inject(TableOfContentsScrollSpy);
|
|
|
|
|
private readonly tableOfContentsLoader = inject(TableOfContentsLoader);
|
2024-10-01 18:31:03 +03:00
|
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
|
|
|
|
2024-07-23 16:50:12 +00:00
|
|
|
tableOfContentItems = this.tableOfContentsLoader.tableOfContentItems;
|
|
|
|
|
|
|
|
|
|
activeItemId = this.scrollSpy.activeItemId;
|
|
|
|
|
TableOfContentsLevel = TableOfContentsLevel;
|
|
|
|
|
|
2025-03-01 03:21:33 +01:00
|
|
|
constructor() {
|
|
|
|
|
afterNextRender({
|
|
|
|
|
read: () => {
|
|
|
|
|
this.tableOfContentsLoader.buildTableOfContent(this.contentSourceElement());
|
|
|
|
|
this.scrollSpy.setupActiveItemListener(this.contentSourceElement(), this.destroyRef);
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-07-23 16:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrollToTop(): void {
|
|
|
|
|
this.scrollSpy.scrollToTop();
|
|
|
|
|
}
|
|
|
|
|
}
|