8
0

splitbuttonview.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/dropdown/button/splitbuttonview
  7. */
  8. import View from '../../view';
  9. import ButtonView from '../../button/buttonview';
  10. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  11. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  12. import dropdownArrowIcon from '../../../theme/icons/dropdown-arrow.svg';
  13. import '../../../theme/components/dropdown/splitbutton.css';
  14. /**
  15. * The split button view class.
  16. *
  17. * const view = new SplitButtonView();
  18. *
  19. * view.set( {
  20. * label: 'A button',
  21. * keystroke: 'Ctrl+B',
  22. * tooltip: true
  23. * } );
  24. *
  25. * view.render();
  26. *
  27. * document.body.append( view.element );
  28. *
  29. * Also see {@link module:ui/dropdown/utils~createDropdown}.
  30. *
  31. * @extends module:ui/view~View
  32. * @implements module:ui/dropdown/button/dropdownbuttoninterface~DropdownButtonInterface
  33. */
  34. export default class SplitButtonView extends View {
  35. /**
  36. * @inheritDoc
  37. */
  38. constructor( locale ) {
  39. super( locale );
  40. /**
  41. * Controls whether the button view is enabled, i.e. it can be clicked and execute an action.
  42. *
  43. * To change the "on" state of the button, use {@link #isOn} instead.
  44. *
  45. * @observable
  46. * @member {Boolean} #isEnabled
  47. */
  48. this.set( 'isEnabled', true );
  49. /**
  50. * Used to create a {@link #tooltip}.
  51. *
  52. * @observable
  53. * @member {String} #label
  54. */
  55. this.set( 'label' );
  56. /**
  57. * (Optional) The keystroke associated with the button, i.e. <kbd>Ctrl</kbd>+<kbd>B</kbd>,
  58. * in the string format compatible with {@link module:utils/keyboard}.
  59. *
  60. * @observable
  61. * @member {Boolean} #keystroke
  62. */
  63. this.set( 'keystroke' );
  64. /**
  65. * (Optional) An XML {@link module:ui/icon/iconview~IconView#content content} of the icon.
  66. * When defined, an {@link module:ui/button/buttonview~ButtonView#iconView} will be added to the {@link #actionView} button.
  67. *
  68. * @observable
  69. * @member {String} #icon
  70. */
  71. this.set( 'icon' );
  72. /**
  73. * (Optional) Tooltip of the button, i.e. displayed when hovering the button with the mouse cursor.
  74. *
  75. * * If defined as a `Boolean` (e.g. `true`), then combination of `label` and `keystroke` will be set as a tooltip.
  76. * * If defined as a `String`, tooltip will equal the exact text of that `String`.
  77. * * If defined as a `Function`, `label` and `keystroke` will be passed to that function, which is to return
  78. * a string with the tooltip text.
  79. *
  80. * const view = new ButtonView( locale );
  81. * view.tooltip = ( label, keystroke ) => `A tooltip for ${ label } and ${ keystroke }.`
  82. *
  83. * @observable
  84. * @default false
  85. * @member {Boolean|String|Function} #tooltip
  86. */
  87. this.set( 'tooltip' );
  88. /**
  89. * Controls whether the button view is "on". It makes sense when a feature it represents
  90. * is currently active, e.g. a bold button is "on" when the selection is in the bold text.
  91. *
  92. * To disable the button, use {@link #isEnabled} instead.
  93. *
  94. * @observable
  95. * @member {Boolean} #isOn
  96. */
  97. this.set( 'isOn', false );
  98. /**
  99. * Controls whether the button view is enabled, i.e. it can be clicked and execute an action.
  100. *
  101. * To change the "on" state of the button, use {@link #isOn} instead.
  102. *
  103. * @observable
  104. * @member {Boolean} #isEnabled
  105. */
  106. this.set( 'isEnabled', true );
  107. /**
  108. * Collection of the child views inside of the split button {@link #element}.
  109. *
  110. * @readonly
  111. * @member {module:ui/viewcollection~ViewCollection}
  112. */
  113. this.children = this.createCollection();
  114. /**
  115. * A main button of split button.
  116. *
  117. * @readonly
  118. * @member {module:ui/button/buttonview~ButtonView}
  119. */
  120. this.actionView = this._createActionView();
  121. /**
  122. * A secondary button of split button that opens dropdown.
  123. *
  124. * @readonly
  125. * @member {module:ui/button/buttonview~ButtonView}
  126. */
  127. this.arrowView = this._createArrowView();
  128. /**
  129. * Instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}. It manages
  130. * keystrokes of the split button:
  131. *
  132. * * <kbd>▶</kbd> moves focus to arrow view when action view is focused,
  133. * * <kbd>◀</kbd> moves focus to action view when arrow view is focused.
  134. *
  135. * @readonly
  136. * @member {module:utils/keystrokehandler~KeystrokeHandler}
  137. */
  138. this.keystrokes = new KeystrokeHandler();
  139. /**
  140. * Tracks information about DOM focus in the dropdown.
  141. *
  142. * @readonly
  143. * @member {module:utils/focustracker~FocusTracker}
  144. */
  145. this.focusTracker = new FocusTracker();
  146. this.setTemplate( {
  147. tag: 'div',
  148. attributes: {
  149. class: 'ck-splitbutton'
  150. },
  151. children: this.children
  152. } );
  153. }
  154. /**
  155. * @inheritDoc
  156. */
  157. render() {
  158. super.render();
  159. this.children.add( this.actionView );
  160. this.children.add( this.arrowView );
  161. this.focusTracker.add( this.actionView.element );
  162. this.focusTracker.add( this.arrowView.element );
  163. this.keystrokes.listenTo( this.element );
  164. // Overrides toolbar focus cycling behavior.
  165. this.keystrokes.set( 'arrowright', ( evt, cancel ) => {
  166. if ( this.focusTracker.focusedElement === this.actionView.element ) {
  167. this.arrowView.focus();
  168. cancel();
  169. }
  170. } );
  171. // Overrides toolbar focus cycling behavior.
  172. this.keystrokes.set( 'arrowleft', ( evt, cancel ) => {
  173. if ( this.focusTracker.focusedElement === this.arrowView.element ) {
  174. this.actionView.focus();
  175. cancel();
  176. }
  177. } );
  178. }
  179. /**
  180. * Focuses the {@link #actionView#element} of the action part of split button.
  181. */
  182. focus() {
  183. this.actionView.focus();
  184. }
  185. /**
  186. * Creates a {@link module:ui/button/buttonview~ButtonView} instance as {@link #actionView} and binds it with main split button
  187. * attributes.
  188. *
  189. * @private
  190. * @returns {module:ui/button/buttonview~ButtonView}
  191. */
  192. _createActionView() {
  193. const buttonView = new ButtonView();
  194. buttonView.bind( 'icon', 'isEnabled', 'label', 'isOn', 'tooltip', 'keystroke' ).to( this );
  195. buttonView.delegate( 'execute' ).to( this );
  196. return buttonView;
  197. }
  198. /**
  199. * Creates a {@link module:ui/button/buttonview~ButtonView} instance as {@link #arrowView} and binds it with main split button
  200. * attributes.
  201. *
  202. * @private
  203. * @returns {module:ui/button/buttonview~ButtonView}
  204. */
  205. _createArrowView() {
  206. const arrowView = new ButtonView();
  207. arrowView.icon = dropdownArrowIcon;
  208. arrowView.extendTemplate( {
  209. attributes: {
  210. class: 'ck-splitbutton-arrow'
  211. }
  212. } );
  213. arrowView.bind( 'isEnabled' ).to( this );
  214. arrowView.delegate( 'execute' ).to( this, 'open' );
  215. return arrowView;
  216. }
  217. }