2017-04-27 15:32:46 -07:00
|
|
|
import { Component, ElementRef, OnInit } from '@angular/core';
|
|
|
|
|
|
|
|
import { TocItem, TocService } from 'app/shared/toc.service';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'aio-toc',
|
|
|
|
templateUrl: 'toc.component.html',
|
|
|
|
styles: []
|
|
|
|
})
|
|
|
|
export class TocComponent implements OnInit {
|
|
|
|
|
|
|
|
hasSecondary = false;
|
|
|
|
hasToc = true;
|
|
|
|
isClosed = true;
|
|
|
|
isEmbedded = false;
|
|
|
|
private primaryMax = 4;
|
|
|
|
tocList: TocItem[];
|
|
|
|
|
|
|
|
constructor(
|
2017-04-27 15:33:50 -07:00
|
|
|
elementRef: ElementRef,
|
2017-04-27 15:32:46 -07:00
|
|
|
private tocService: TocService) {
|
2017-04-27 15:33:50 -07:00
|
|
|
const hostElement = elementRef.nativeElement;
|
2017-04-27 15:32:46 -07:00
|
|
|
this.isEmbedded = hostElement.className.indexOf('embedded') !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
const tocList = this.tocList = this.tocService.tocList;
|
|
|
|
const count = tocList.length;
|
|
|
|
this.hasToc = count > 0;
|
|
|
|
if (this.isEmbedded && this.hasToc) {
|
|
|
|
// If TOC is embedded in doc, mark secondary (sometimes hidden) items
|
|
|
|
this.hasSecondary = tocList.length > this.primaryMax;
|
|
|
|
for (let i = this.primaryMax; i < count; i++) {
|
|
|
|
tocList[i].isSecondary = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
this.isClosed = !this.isClosed;
|
|
|
|
}
|
|
|
|
}
|