Guest User

Untitled

a guest
Mar 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { DialogService, DialogSettings, DialogRef } from '@progress/kendo-angular-dialog';
  3.  
  4. declare let $: any;
  5.  
  6. /**
  7. * Wrapper of Kendo Angular DialogService to open dialogs where user cannot climb down from
  8. * the modal window by pushing TAB.
  9. *
  10. * After the dialog closes, the same element will get the focus that had before opening the
  11. * dialog window.
  12. */
  13. @Injectable()
  14. export class MyDialogService {
  15. private eventFuncStack: any[] = [];
  16.  
  17. /**
  18. * Helper function to select the button that should be focused after the dialog is opened
  19. * based on the text that is written onto it
  20. *
  21. /*
  22. static findButtonByText(dialogObj: any, text: string): any | undefined {
  23. const buttons = dialogObj.find('button');
  24. for (const button of buttons) {
  25. const buttonText: string = $(button).text();
  26. if (buttonText && buttonText.trim() === text) {
  27. return button;
  28. }
  29. }
  30. return undefined;
  31. }
  32.  
  33. constructor(private dialogService: DialogService) {}
  34.  
  35. /**
  36. * Same as the original DialogService.open() function with the difference that the user can specify
  37. * with the second parameter which element on the opened dialog should be focused.
  38. */
  39. open(options: DialogSettings, focusedElementAfterOpen?: (dialogJQueryObj: any) => any): DialogRef {
  40. const activeElementBeforeOpen = document.activeElement;
  41.  
  42. const dialog = this.dialogService.open(options);
  43. dialog.dialog.changeDetectorRef.detectChanges();
  44.  
  45. const dialogObj = $(dialog.dialog.location.nativeElement).find('.k-window');
  46.  
  47. let focusedElement: any | undefined;
  48.  
  49. if (focusedElementAfterOpen) {
  50. focusedElement = focusedElementAfterOpen(dialogObj);
  51. }
  52.  
  53. if (!focusedElement) {
  54. const focusableElements = dialogObj.focusable();
  55. if (focusableElements.length > 0) {
  56. focusedElement = focusableElements[0];
  57. }
  58. }
  59.  
  60. if (focusedElement) {
  61. $(focusedElement).focus();
  62. }
  63.  
  64. const eventFuncObj = this.addEvent(dialogObj);
  65. this.eventFuncStack.push(eventFuncObj);
  66.  
  67. dialog.result.subscribe(result => {
  68. $(document).off('keydown.kendoWindow', undefined, eventFuncObj);
  69. this.eventFuncStack.splice(this.eventFuncStack.indexOf(eventFuncObj), 1);
  70. if (activeElementBeforeOpen) {
  71. $(activeElementBeforeOpen).focus();
  72. }
  73. if (this.eventFuncStack.length > 0) {
  74. $(document).on('keydown.kendoWindow', undefined, this.eventFuncStack[this.eventFuncStack.length - 1]);
  75. }
  76. });
  77. return dialog;
  78. }
  79.  
  80. private addEvent(dialogObj: any) {
  81. const eventFuncObj = (e: any) => this.eventFunction(e, dialogObj);
  82. if (this.eventFuncStack.length > 0) {
  83. $(document).off('keydown.kendoWindow', undefined, this.eventFuncStack[this.eventFuncStack.length - 1]);
  84. }
  85. $(document).on('keydown.kendoWindow', undefined, eventFuncObj);
  86. return eventFuncObj;
  87. }
  88.  
  89. private eventFunction(e: any, dialogObj: any) {
  90. if (e.keyCode === 9 || e.keyCode === 37 || e.keyCode === 39) {
  91. const tabableElements = dialogObj.focusable();
  92. const focusedElement = $(document.activeElement);
  93.  
  94. const index = tabableElements.index(focusedElement);
  95.  
  96. /*if (e.keyCode === 37) {
  97. // left key, set new focused element index - 1
  98. if (index > 0) {
  99. tabableElements[index - 1].focus();
  100. } else {
  101. tabableElements.first().focus();
  102. }
  103. } else if (e.keyCode === 39) {
  104. // TODO set new focused element index + 1
  105. if (index !== -1 && index !== tabableElements.length - 1) {
  106. tabableElements[index + 1].focus();
  107. } else {
  108. tabableElements.first().focus();
  109. }
  110. } else*/
  111. if (e.keyCode === 9) {
  112. // original tab override
  113. if (!e.shiftKey) {
  114. if (index === -1 || index === tabableElements.length - 1) {
  115. e.preventDefault();
  116. tabableElements.first().focus();
  117. }
  118. } else {
  119. if (index === -1 || index === 0) {
  120. e.preventDefault();
  121. tabableElements.last().focus();
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
Add Comment
Please, Sign In to add comment