64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
import { Directive, ElementRef, Input, HostListener, NgModule } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { DomHandler } from 'primeng/dom';
|
|
|
|
class FocusTrap {
|
|
constructor(el) {
|
|
this.el = el;
|
|
}
|
|
onkeydown(e) {
|
|
if (this.pFocusTrapDisabled !== true) {
|
|
e.preventDefault();
|
|
let focusableElements = DomHandler.getFocusableElements(this.el.nativeElement);
|
|
if (focusableElements && focusableElements.length > 0) {
|
|
if (!focusableElements[0].ownerDocument.activeElement) {
|
|
focusableElements[0].focus();
|
|
}
|
|
else {
|
|
let focusedIndex = focusableElements.indexOf(focusableElements[0].ownerDocument.activeElement);
|
|
if (e.shiftKey) {
|
|
if (focusedIndex == -1 || focusedIndex === 0)
|
|
focusableElements[focusableElements.length - 1].focus();
|
|
else
|
|
focusableElements[focusedIndex - 1].focus();
|
|
}
|
|
else {
|
|
if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
|
|
focusableElements[0].focus();
|
|
else
|
|
focusableElements[focusedIndex + 1].focus();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
FocusTrap.decorators = [
|
|
{ type: Directive, args: [{
|
|
selector: '[pFocusTrap]',
|
|
},] }
|
|
];
|
|
FocusTrap.ctorParameters = () => [
|
|
{ type: ElementRef }
|
|
];
|
|
FocusTrap.propDecorators = {
|
|
pFocusTrapDisabled: [{ type: Input }],
|
|
onkeydown: [{ type: HostListener, args: ['keydown.tab', ['$event'],] }, { type: HostListener, args: ['keydown.shift.tab', ['$event'],] }]
|
|
};
|
|
class FocusTrapModule {
|
|
}
|
|
FocusTrapModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
imports: [CommonModule],
|
|
exports: [FocusTrap],
|
|
declarations: [FocusTrap]
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { FocusTrap, FocusTrapModule };
|
|
//# sourceMappingURL=primeng-focustrap.js.map
|