focustrap.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var domhandler_1 = require("../dom/domhandler");
  15. var FocusTrap = /** @class */ (function () {
  16. function FocusTrap(el) {
  17. this.el = el;
  18. }
  19. FocusTrap.prototype.onkeydown = function (e) {
  20. if (e.which === 9) {
  21. e.preventDefault();
  22. var focusableElements = domhandler_1.DomHandler.getFocusableElements(this.el.nativeElement);
  23. if (focusableElements && focusableElements.length > 0) {
  24. if (!document.activeElement) {
  25. focusableElements[0].focus();
  26. }
  27. else {
  28. var focusedIndex = focusableElements.indexOf(document.activeElement);
  29. if (e.shiftKey) {
  30. if (focusedIndex == -1 || focusedIndex === 0)
  31. focusableElements[focusableElements.length - 1].focus();
  32. else
  33. focusableElements[focusedIndex - 1].focus();
  34. }
  35. else {
  36. if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
  37. focusableElements[0].focus();
  38. else
  39. focusableElements[focusedIndex + 1].focus();
  40. }
  41. }
  42. }
  43. }
  44. };
  45. __decorate([
  46. core_1.HostListener('keydown', ['$event']),
  47. __metadata("design:type", Function),
  48. __metadata("design:paramtypes", [Object]),
  49. __metadata("design:returntype", void 0)
  50. ], FocusTrap.prototype, "onkeydown", null);
  51. FocusTrap = __decorate([
  52. core_1.Directive({
  53. selector: '[pFocusTrap]',
  54. }),
  55. __metadata("design:paramtypes", [core_1.ElementRef])
  56. ], FocusTrap);
  57. return FocusTrap;
  58. }());
  59. exports.FocusTrap = FocusTrap;
  60. var FocusTrapModule = /** @class */ (function () {
  61. function FocusTrapModule() {
  62. }
  63. FocusTrapModule = __decorate([
  64. core_1.NgModule({
  65. imports: [common_1.CommonModule],
  66. exports: [FocusTrap],
  67. declarations: [FocusTrap]
  68. })
  69. ], FocusTrapModule);
  70. return FocusTrapModule;
  71. }());
  72. exports.FocusTrapModule = FocusTrapModule;
  73. //# sourceMappingURL=focustrap.js.map