focustracker.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global setTimeout, clearTimeout */
  6. /**
  7. * @module utils/focustracker
  8. */
  9. import DomEmitterMixin from './dom/emittermixin';
  10. import ObservableMixin from './observablemixin';
  11. import CKEditorError from './ckeditorerror';
  12. import mix from './mix';
  13. /**
  14. * Allows observing a group of `HTMLElement`s whether at least one of them is focused.
  15. *
  16. * Used by the {@link module:core/editor/editor~Editor} in order to track whether the focus is still within the application,
  17. * or were used outside of its UI.
  18. *
  19. * **Note** `focus` and `blur` listeners use event capturing, so it is only needed to register wrapper `HTMLElement`
  20. * which contain other `focusable` elements. But note that this wrapper element has to be focusable too
  21. * (have e.g. `tabindex="-1"`).
  22. *
  23. * @mixes module:utils/dom/emittermixin~EmitterMixin
  24. * @mixes module:utils/observablemixin~ObservableMixin
  25. */
  26. export default class FocusTracker {
  27. constructor() {
  28. /**
  29. * True when one of the registered elements is focused.
  30. *
  31. * @readonly
  32. * @observable
  33. * @member {Boolean} #isFocused
  34. */
  35. this.set( 'isFocused', false );
  36. /**
  37. * The currently focused element.
  38. *
  39. * While {@link #isFocused `isFocused`} remains `true`, the focus can
  40. * move between different UI elements. This property tracks those
  41. * elements and tells which one is currently focused.
  42. *
  43. * @readonly
  44. * @observable
  45. * @member {HTMLElement|null}
  46. */
  47. this.set( 'focusedElement', null );
  48. /**
  49. * List of registered elements.
  50. *
  51. * @private
  52. * @member {Set.<HTMLElement>}
  53. */
  54. this._elements = new Set();
  55. /**
  56. * Event loop timeout.
  57. *
  58. * @private
  59. * @member {Number}
  60. */
  61. this._nextEventLoopTimeout = null;
  62. }
  63. /**
  64. * Starts tracking the specified element.
  65. *
  66. * @param {HTMLElement} element
  67. */
  68. add( element ) {
  69. if ( this._elements.has( element ) ) {
  70. throw new CKEditorError( 'focusTracker-add-element-already-exist', this );
  71. }
  72. this.listenTo( element, 'focus', () => this._focus( element ), { useCapture: true } );
  73. this.listenTo( element, 'blur', () => this._blur(), { useCapture: true } );
  74. this._elements.add( element );
  75. }
  76. /**
  77. * Stops tracking the specified element and stops listening on this element.
  78. *
  79. * @param {HTMLElement} element
  80. */
  81. remove( element ) {
  82. if ( element === this.focusedElement ) {
  83. this._blur( element );
  84. }
  85. if ( this._elements.has( element ) ) {
  86. this.stopListening( element );
  87. this._elements.delete( element );
  88. }
  89. }
  90. /**
  91. * Destroys the focus tracker by:
  92. * - Disabling all event listeners attached to tracked elements.
  93. * - Removing all tracked elements that were previously added.
  94. */
  95. destroy() {
  96. this.stopListening();
  97. }
  98. /**
  99. * Stores currently focused element and set {#isFocused} as `true`.
  100. *
  101. * @private
  102. * @param {HTMLElement} element Element which has been focused.
  103. */
  104. _focus( element ) {
  105. clearTimeout( this._nextEventLoopTimeout );
  106. this.focusedElement = element;
  107. this.isFocused = true;
  108. }
  109. /**
  110. * Clears currently focused element and set {@link #isFocused} as `false`.
  111. * This method uses `setTimeout` to change order of fires `blur` and `focus` events.
  112. *
  113. * @private
  114. * @fires blur
  115. */
  116. _blur() {
  117. clearTimeout( this._nextEventLoopTimeout );
  118. this._nextEventLoopTimeout = setTimeout( () => {
  119. this.focusedElement = null;
  120. this.isFocused = false;
  121. }, 0 );
  122. }
  123. /**
  124. * @event focus
  125. */
  126. /**
  127. * @event blur
  128. */
  129. }
  130. mix( FocusTracker, DomEmitterMixin );
  131. mix( FocusTracker, ObservableMixin );