focuscycler.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/focuscycler
  7. */
  8. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  9. /**
  10. * A utility class that helps cycling over focusable {@link module:ui/view~View views} in a
  11. * {@link module:ui/viewcollection~ViewCollection} when the focus is tracked by the
  12. * {@link module:utils/focustracker~FocusTracker} instance. It helps implementing keyboard
  13. * navigation in HTML forms, toolbars, lists and the like.
  14. *
  15. * To work properly it requires:
  16. * * a collection of focusable (HTML `tabindex` attribute) views that implement the `focus()` method,
  17. * * an associated focus tracker to determine which view is focused.
  18. *
  19. * A simple cycler setup can look like this:
  20. *
  21. * const focusables = new ViewCollection();
  22. * const focusTracker = new FocusTracker();
  23. *
  24. * // Add focusable views to the focus tracker.
  25. * focusTracker.add( ... );
  26. *
  27. * Then, the cycler can be used manually:
  28. *
  29. * const cycler = new FocusCycler( { focusables, focusTracker } );
  30. *
  31. * // Will focus the first focusable view in #focusables.
  32. * cycler.focusFirst();
  33. *
  34. * // Will log the next focusable item in #focusables.
  35. * console.log( cycler.next );
  36. *
  37. * Alternatively, it can work side by side with the {@link module:utils/keystrokehandler~KeystrokeHandler}:
  38. *
  39. * const keystrokeHandler = new KeystrokeHandler();
  40. *
  41. * // Activate the keystroke handler.
  42. * keystrokeHandler.listenTo( sourceOfEvents );
  43. *
  44. * const cycler = new FocusCycler( {
  45. * focusables, focusTracker, keystrokeHandler,
  46. * actions: {
  47. * // When arrowup of arrowleft is detected by the #keystrokeHandler,
  48. * // focusPrevious() will be called on the cycler.
  49. * focusPrevious: [ 'arrowup', 'arrowleft' ],
  50. * }
  51. * } );
  52. */
  53. export default class FocusCycler {
  54. /**
  55. * Creates an instance of the focus cycler utility.
  56. *
  57. * @param {Object} options Configuration options.
  58. * @param {module:utils/collection~Collection|Object} options.focusables
  59. * @param {module:utils/focustracker~FocusTracker} options.focusTracker
  60. * @param {module:utils/keystrokehandler~KeystrokeHandler} [options.keystrokeHandler]
  61. * @param {Object} [options.actions]
  62. */
  63. constructor( options ) {
  64. Object.assign( this, options );
  65. /**
  66. * A {@link module:ui/view~View view} collection that the cycler operates on.
  67. *
  68. * @readonly
  69. * @member {module:utils/collection~Collection} #focusables
  70. */
  71. /**
  72. * A focus tracker instance that the cycler uses to determine the current focus
  73. * state in {@link #focusables}.
  74. *
  75. * @readonly
  76. * @member {module:utils/focustracker~FocusTracker} #focusTracker
  77. */
  78. /**
  79. * An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}
  80. * which can respond to certain keystrokes and cycle the focus.
  81. *
  82. * @readonly
  83. * @member {module:utils/keystrokehandler~KeystrokeHandler} #keystrokeHandler
  84. */
  85. /**
  86. * Actions that the cycler can take when a keystroke is pressed. Requires
  87. * `options.keystrokeHandler` to be passed and working. When an action is
  88. * performed, `preventDefault` and `stopPropagation` will be called on the event
  89. * the keystroke fired in the DOM.
  90. *
  91. * actions: {
  92. * // Will call #focusPrevious() when arrowleft or arrowup is pressed.
  93. * focusPrevious: [ 'arrowleft', 'arrowup' ],
  94. *
  95. * // Will call #focusNext() when arrowdown is pressed.
  96. * focusNext: 'arrowdown'
  97. * }
  98. *
  99. * @readonly
  100. * @member {Object} #actions
  101. */
  102. if ( options.actions && options.keystrokeHandler ) {
  103. for ( const methodName in options.actions ) {
  104. let actions = options.actions[ methodName ];
  105. if ( typeof actions == 'string' ) {
  106. actions = [ actions ];
  107. }
  108. for ( const keystroke of actions ) {
  109. options.keystrokeHandler.set( keystroke, ( data, cancel ) => {
  110. this[ methodName ]();
  111. cancel();
  112. } );
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Returns the first focusable view in {@link #focusables}.
  119. * Returns `null` if there is none.
  120. *
  121. * @readonly
  122. * @member {module:ui/view~View|null} #first
  123. */
  124. get first() {
  125. return this.focusables.find( isFocusable ) || null;
  126. }
  127. /**
  128. * Returns the last focusable view in {@link #focusables}.
  129. * Returns `null` if there is none.
  130. *
  131. * @readonly
  132. * @member {module:ui/view~View|null} #last
  133. */
  134. get last() {
  135. return this.focusables.filter( isFocusable ).slice( -1 )[ 0 ] || null;
  136. }
  137. /**
  138. * Returns the next focusable view in {@link #focusables} based on {@link #current}.
  139. * Returns `null` if there is none.
  140. *
  141. * @readonly
  142. * @member {module:ui/view~View|null} #next
  143. */
  144. get next() {
  145. return this._getFocusableItem( 1 );
  146. }
  147. /**
  148. * Returns the previous focusable view in {@link #focusables} based on {@link #current}.
  149. * Returns `null` if there is none.
  150. *
  151. * @readonly
  152. * @member {module:ui/view~View|null} #previous
  153. */
  154. get previous() {
  155. return this._getFocusableItem( -1 );
  156. }
  157. /**
  158. * An index of the view in the {@link #focusables} which is focused according
  159. * to {@link #focusTracker}. Returns `null` when there is no such view.
  160. *
  161. * @readonly
  162. * @member {Number|null} #current
  163. */
  164. get current() {
  165. let index = null;
  166. // There's no focused view in the focusables.
  167. if ( this.focusTracker.focusedElement === null ) {
  168. return null;
  169. }
  170. this.focusables.find( ( view, viewIndex ) => {
  171. const focused = view.element === this.focusTracker.focusedElement;
  172. if ( focused ) {
  173. index = viewIndex;
  174. }
  175. return focused;
  176. } );
  177. return index;
  178. }
  179. /**
  180. * Focuses the {@link #first} item in {@link #focusables}.
  181. */
  182. focusFirst() {
  183. this._focus( this.first );
  184. }
  185. /**
  186. * Focuses the {@link #last} item in {@link #focusables}.
  187. */
  188. focusLast() {
  189. this._focus( this.last );
  190. }
  191. /**
  192. * Focuses the {@link #next} item in {@link #focusables}.
  193. */
  194. focusNext() {
  195. this._focus( this.next );
  196. }
  197. /**
  198. * Focuses the {@link #previous} item in {@link #focusables}.
  199. */
  200. focusPrevious() {
  201. this._focus( this.previous );
  202. }
  203. /**
  204. * Focuses the given view if it exists.
  205. *
  206. * @protected
  207. * @param {module:ui/view~View} view
  208. */
  209. _focus( view ) {
  210. if ( view ) {
  211. view.focus();
  212. }
  213. }
  214. /**
  215. * Returns the next or previous focusable view in {@link #focusables} with respect
  216. * to {@link #current}.
  217. *
  218. * @protected
  219. * @param {Number} step Either `1` for checking forward from {@link #current} or
  220. * `-1` for checking backwards.
  221. * @returns {module:ui/view~View|null}
  222. */
  223. _getFocusableItem( step ) {
  224. // Cache for speed.
  225. const current = this.current;
  226. const collectionLength = this.focusables.length;
  227. if ( !collectionLength ) {
  228. return null;
  229. }
  230. // Start from the beginning if no view is focused.
  231. // https://github.com/ckeditor/ckeditor5-ui/issues/206
  232. if ( current === null ) {
  233. return this[ step === 1 ? 'first' : 'last' ];
  234. }
  235. // Cycle in both directions.
  236. let index = ( current + collectionLength + step ) % collectionLength;
  237. do {
  238. const view = this.focusables.get( index );
  239. // TODO: Check if view is visible.
  240. if ( isFocusable( view ) ) {
  241. return view;
  242. }
  243. // Cycle in both directions.
  244. index = ( index + collectionLength + step ) % collectionLength;
  245. } while ( index !== current );
  246. return null;
  247. }
  248. }
  249. // Checks whether a view is focusable.
  250. //
  251. // @private
  252. // @param {module:ui/view~View} view A view to be checked.
  253. // @returns {Boolean}
  254. function isFocusable( view ) {
  255. return !!( view.focus && global.window.getComputedStyle( view.element ).display != 'none' );
  256. }