import { Component, Input, Output, ContentChildren, HostListener, EventEmitter, ElementRef, ViewChild, DoCheck, AfterContentInit, } from '@angular/core'; import { NgTabComponent } from './ng-tab/ng-tab.component'; @Component({ selector: 'ta-ng-tabs', templateUrl: './ng-tabs.component.html', styleUrls: ['./ng-tabs.component.scss'], }) export class NgTabsComponent implements DoCheck, AfterContentInit { @ContentChildren(NgTabComponent) public tabs: any; @Output() public onSelect: EventEmitter = new EventEmitter(); @Input() public variant: string = ''; @Input() public minHeight: string = ''; @Input() public background: string = ''; public userTabbing: boolean = false; public showLeftArrow: boolean = false; // appears if and only if direction's scroll is available public showRightArrow: boolean = false; // appears if and only if direction's scroll is available public screenWidth: number = window.innerWidth; public tabsTotalLimit: number = 7; public tabKey: number = 9; public tabsTotal: number; get hasBackground(): boolean { return this.background === 'none'; } @ViewChild('scrollTabs', { static: true }) public scrollTabs: ElementRef; @HostListener('window:resize', ['$event']) public onResize(event?: any): void { this.screenWidth = window.innerWidth; } @HostListener('document:keyup', ['$event']) public tabKeyEvent(event: KeyboardEvent): void { if (event.keyCode === this.tabKey) { this.userTabbing = true; } } public ngDoCheck(): void { const scrollWidth: number = this.scrollTabs.nativeElement.scrollWidth; const clientWidth: number = this.scrollTabs.nativeElement.clientWidth; const leftScrollRemainder: number = (scrollWidth - 1) - (clientWidth); const scrollLeft: number = this.scrollTabs.nativeElement.scrollLeft; this.viewTab(scrollWidth, clientWidth, scrollLeft, leftScrollRemainder); } public ngAfterContentInit(): void { const tabs: NgTabComponent[] = Array.from(this.tabs); const actives: any[] = this.tabs.filter(tab => { return tab.active }); if(actives.length > 1) { console.error("Multiple active tabs set 'active'"); } else if(!actives.length && tabs.length) { tabs[0].active = true; } } public tabClicked(tab: any): void { const tabs: any[] = Array.from(this.tabs); tabs.forEach(tab => tab.active = false); tab.active = true; this.onSelect.emit(tab); } public viewTab(scrollWidth: number, clientWidth: number, scrollLeft: number, leftScrollRemainder: number): void { if (this.screenWidth < 600 && scrollWidth > clientWidth) { this.showLeftArrow = true; this.showRightArrow = true; } if (scrollWidth > clientWidth) { this.showLeftArrow = true; this.showRightArrow = true; } if (scrollLeft > 0) { this.showLeftArrow = true; } if (scrollLeft === 0) { this.showLeftArrow = false; } if (scrollWidth <= clientWidth) { this.showRightArrow = false } if (scrollLeft >= leftScrollRemainder) { this.showRightArrow = false; } } public scrollRight(): void { const offset: number = this.scrollTabs.nativeElement.offsetParent.offsetLeft; this.scrollTabs.nativeElement.scrollTo({ left: (this.scrollTabs.nativeElement.scrollLeft + offset), behavior: 'smooth' }); } public scrollLeft(): void { const clientWidth: number = this.scrollTabs.nativeElement.clientWidth; this.scrollTabs.nativeElement.scrollTo({ left: (this.scrollTabs.nativeElement.scrollLeft - clientWidth), behavior: 'smooth', }); } public getVariant(): string { let className: string = ''; if(this.variant === 'secondary') { className = '-secondaryTabs'; } else if(this.variant === 'tertiary') { className = '-tertiaryTabs'; } return className; } }