8
0

utils.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/toolbar/utils
  7. */
  8. /**
  9. * An utility which expands a plain toolbar configuration into a collection
  10. * of {@link module:ui/view~View views} using a given factory.
  11. *
  12. * @param {Object} config The toolbar config.
  13. * @param {module:utils/collection~Collection} collection A collection into which the config
  14. * is expanded.
  15. * @param {module:ui/componentfactory~ComponentFactory} factory A factory producing toolbar items.
  16. * @returns {Promise} A promise resolved when all toolbar items are initialized.
  17. */
  18. export function expandToolbarConfig( config, collection, factory ) {
  19. let promises = [];
  20. if ( config ) {
  21. promises = config.map( name => collection.add( factory.create( name ) ) );
  22. }
  23. return Promise.all( promises );
  24. }
  25. /**
  26. * Enables focus/blur toolbar navigation using `Alt+F10` and `Esc` keystrokes.
  27. *
  28. * @param {Object} options Options of the utility.
  29. * @param {*} options.origin A view to which the focus will return when `Esc` is pressed and
  30. * `options.toolbar` is focused.
  31. * @param {module:core/keystrokehandler~KeystrokeHandler} options.originKeystrokeHandler A keystroke
  32. * handler to register `Alt+F10` keystroke.
  33. * @param {module:utils/focustracker~FocusTracker} options.originFocusTracker A focus tracker
  34. * for `options.origin`.
  35. * @param {module:ui/toolbar/toolbarview~ToolbarView} options.toolbar A toolbar which is to gain
  36. * focus when `Alt+F10` is pressed.
  37. */
  38. export function enableToolbarKeyboardFocus( {
  39. origin,
  40. originKeystrokeHandler,
  41. originFocusTracker,
  42. toolbar
  43. } ) {
  44. // Because toolbar items can get focus, the overall state of the toolbar must
  45. // also be tracked.
  46. originFocusTracker.add( toolbar.element );
  47. // Focus the toolbar on the keystroke, if not already focused.
  48. originKeystrokeHandler.set( 'Alt+F10', ( data, cancel ) => {
  49. if ( originFocusTracker.isFocused && !toolbar.focusTracker.isFocused ) {
  50. toolbar.focus();
  51. cancel();
  52. }
  53. } );
  54. // Blur the toolbar and bring the focus back to origin.
  55. toolbar.keystrokes.set( 'Esc', ( data, cancel ) => {
  56. if ( toolbar.focusTracker.isFocused ) {
  57. origin.focus();
  58. cancel();
  59. }
  60. } );
  61. }