2017-05-02 10:13:10 -07:00
|
|
|
import { Component, ElementRef, OnDestroy, OnInit } from '@angular/core';
|
|
|
|
import { Subject } from 'rxjs/Subject';
|
|
|
|
import 'rxjs/add/operator/takeUntil';
|
2017-04-27 15:32:46 -07:00
|
|
|
|
|
|
|
import { TocItem, TocService } from 'app/shared/toc.service';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'aio-toc',
|
|
|
|
templateUrl: 'toc.component.html',
|
|
|
|
styles: []
|
|
|
|
})
|
2017-05-02 10:13:10 -07:00
|
|
|
export class TocComponent implements OnInit, OnDestroy {
|
2017-04-27 15:32:46 -07:00
|
|
|
|
|
|
|
hasSecondary = false;
|
2017-05-06 09:30:18 +01:00
|
|
|
hasToc = false;
|
2017-04-27 15:32:46 -07:00
|
|
|
isClosed = true;
|
|
|
|
isEmbedded = false;
|
|
|
|
private primaryMax = 4;
|
2017-05-02 10:13:10 -07:00
|
|
|
private onDestroy = new Subject();
|
2017-04-27 15:32:46 -07:00
|
|
|
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() {
|
2017-05-02 10:13:10 -07:00
|
|
|
this.tocService.tocList
|
|
|
|
.takeUntil(this.onDestroy)
|
|
|
|
.subscribe((tocList: TocItem[]) => {
|
|
|
|
const count = tocList.length;
|
|
|
|
|
|
|
|
this.hasToc = count > 0;
|
|
|
|
this.hasSecondary = this.isEmbedded && this.hasToc && (count > this.primaryMax);
|
|
|
|
this.tocList = tocList;
|
|
|
|
|
|
|
|
if (this.hasSecondary) {
|
|
|
|
for (let i = this.primaryMax; i < count; i++) {
|
|
|
|
tocList[i].isSecondary = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.onDestroy.next();
|
2017-04-27 15:32:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
this.isClosed = !this.isClosed;
|
|
|
|
}
|
|
|
|
}
|