1 | import { Directive, ElementRef, Input, HostListener, NgModule } from '@angular/core';
|
2 | import { CommonModule } from '@angular/common';
|
3 | import { DomHandler } from 'primeng/dom';
|
4 |
|
5 | class 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 | }
|
36 | FocusTrap.decorators = [
|
37 | { type: Directive, args: [{
|
38 | selector: '[pFocusTrap]',
|
39 | },] }
|
40 | ];
|
41 | FocusTrap.ctorParameters = () => [
|
42 | { type: ElementRef }
|
43 | ];
|
44 | FocusTrap.propDecorators = {
|
45 | pFocusTrapDisabled: [{ type: Input }],
|
46 | onkeydown: [{ type: HostListener, args: ['keydown.tab', ['$event'],] }, { type: HostListener, args: ['keydown.shift.tab', ['$event'],] }]
|
47 | };
|
48 | class FocusTrapModule {
|
49 | }
|
50 | FocusTrapModule.decorators = [
|
51 | { type: NgModule, args: [{
|
52 | imports: [CommonModule],
|
53 | exports: [FocusTrap],
|
54 | declarations: [FocusTrap]
|
55 | },] }
|
56 | ];
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export { FocusTrap, FocusTrapModule };
|
63 |
|