UNPKG

ngx-dynamic-tabindex

Version:
78 lines (72 loc) 2.44 kB
import { Directive, ElementRef, NgModule } from '@angular/core'; class NgxDynamicTabindexDirective { constructor(hostEl) { this.hostEl = hostEl; this.attributeName = 'tabindex'; } ngAfterViewInit() { this.applyIndexes(); this.mutationObserver = new MutationObserver(() => this.applyIndexes()); this.mutationObserver.observe(this.hostEl.nativeElement, { childList: true, subtree: true, }); } ngOnDestroy() { this.mutationObserver.disconnect(); } applyIndexes() { const isTabAheadOffset = !!this.hostEl.nativeElement.querySelector('[tabIndexAheadOffset]'); const elements = this.hostEl.nativeElement.querySelectorAll(`[${this.attributeName}]`); let globalOffset; let tabIndex; elements.forEach((el, index) => { if (isTabAheadOffset) { const offsetValue = parseInt(el.getAttribute('tabIndexAheadOffset'), 10); if (offsetValue) { globalOffset = offsetValue; tabIndex = this.getTabIndex(index + offsetValue + 1); } else if (globalOffset >= 1) { tabIndex = this.getTabIndex(index); globalOffset--; } else { tabIndex = this.getTabIndex(index + 1); } } else { tabIndex = this.getTabIndex(index + 1); } el.setAttribute(this.attributeName, tabIndex); }); } getTabIndex(index) { return index.toString(); } } NgxDynamicTabindexDirective.decorators = [ { type: Directive, args: [{ selector: '[applyTabIndexes]' },] } ]; NgxDynamicTabindexDirective.ctorParameters = () => [ { type: ElementRef } ]; class NgxDynamicTabindexModule { } NgxDynamicTabindexModule.decorators = [ { type: NgModule, args: [{ declarations: [NgxDynamicTabindexDirective], imports: [], exports: [NgxDynamicTabindexDirective] },] } ]; /* * Public API Surface of ngx-dynamic-tabindex */ /** * Generated bundle index. Do not edit. */ export { NgxDynamicTabindexDirective, NgxDynamicTabindexModule }; //# sourceMappingURL=ngx-dynamic-tabindex.js.map