UNPKG

2.23 kBJavaScriptView Raw
1import { Directive, ElementRef, Input, HostListener, NgModule } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { DomHandler } from 'primeng/dom';
4
5class FocusTrap {
6 constructor(el) {
7 this.el = el;
8 }
9 onkeydown(e) {
10 if (this.pFocusTrapDisabled !== true) {
11 e.preventDefault();
12 let focusableElements = DomHandler.getFocusableElements(this.el.nativeElement);
13 if (focusableElements && focusableElements.length > 0) {
14 if (!focusableElements[0].ownerDocument.activeElement) {
15 focusableElements[0].focus();
16 }
17 else {
18 let focusedIndex = focusableElements.indexOf(focusableElements[0].ownerDocument.activeElement);
19 if (e.shiftKey) {
20 if (focusedIndex == -1 || focusedIndex === 0)
21 focusableElements[focusableElements.length - 1].focus();
22 else
23 focusableElements[focusedIndex - 1].focus();
24 }
25 else {
26 if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
27 focusableElements[0].focus();
28 else
29 focusableElements[focusedIndex + 1].focus();
30 }
31 }
32 }
33 }
34 }
35}
36FocusTrap.decorators = [
37 { type: Directive, args: [{
38 selector: '[pFocusTrap]',
39 },] }
40];
41FocusTrap.ctorParameters = () => [
42 { type: ElementRef }
43];
44FocusTrap.propDecorators = {
45 pFocusTrapDisabled: [{ type: Input }],
46 onkeydown: [{ type: HostListener, args: ['keydown.tab', ['$event'],] }, { type: HostListener, args: ['keydown.shift.tab', ['$event'],] }]
47};
48class FocusTrapModule {
49}
50FocusTrapModule.decorators = [
51 { type: NgModule, args: [{
52 imports: [CommonModule],
53 exports: [FocusTrap],
54 declarations: [FocusTrap]
55 },] }
56];
57
58/**
59 * Generated bundle index. Do not edit.
60 */
61
62export { FocusTrap, FocusTrapModule };
63//# sourceMappingURL=primeng-focustrap.js.map