buttonview.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/button/buttonview
  7. */
  8. import View from '../view';
  9. import IconView from '../icon/iconview';
  10. import TooltipView from '../tooltip/tooltipview';
  11. import uid from '@ckeditor/ckeditor5-utils/src/uid';
  12. import { getEnvKeystrokeText } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import '../../theme/components/button/button.css';
  14. /**
  15. * The button view class.
  16. *
  17. * const view = new ButtonView();
  18. *
  19. * view.set( {
  20. * label: 'A button',
  21. * keystroke: 'Ctrl+B',
  22. * tooltip: true,
  23. * withText: true
  24. * } );
  25. *
  26. * view.render();
  27. *
  28. * document.body.append( view.element );
  29. *
  30. * @extends module:ui/view~View
  31. * @implements module:ui/button/button~Button
  32. */
  33. export default class ButtonView extends View {
  34. /**
  35. * @inheritDoc
  36. */
  37. constructor( locale ) {
  38. super( locale );
  39. const bind = this.bindTemplate;
  40. const ariaLabelUid = uid();
  41. // Implement the Button interface.
  42. this.set( 'class' );
  43. this.set( 'labelStyle' );
  44. this.set( 'icon' );
  45. this.set( 'isEnabled', true );
  46. this.set( 'isOn', false );
  47. this.set( 'isVisible', true );
  48. this.set( 'keystroke' );
  49. this.set( 'label' );
  50. this.set( 'tabindex', -1 );
  51. this.set( 'tooltip' );
  52. this.set( 'tooltipPosition', 's' );
  53. this.set( 'type', 'button' );
  54. this.set( 'withText', false );
  55. /**
  56. * Collection of the child views inside of the button {@link #element}.
  57. *
  58. * @readonly
  59. * @member {module:ui/viewcollection~ViewCollection}
  60. */
  61. this.children = this.createCollection();
  62. /**
  63. * Tooltip of the button view. It is configurable using the {@link #tooltip tooltip attribute}.
  64. *
  65. * @readonly
  66. * @member {module:ui/tooltip/tooltipview~TooltipView} #tooltipView
  67. */
  68. this.tooltipView = this._createTooltipView();
  69. /**
  70. * Label of the button view. It is configurable using the {@link #label label attribute}.
  71. *
  72. * @readonly
  73. * @member {module:ui/view~View} #labelView
  74. */
  75. this.labelView = this._createLabelView( ariaLabelUid );
  76. /**
  77. * The icon view of the button. Will be added to {@link #children} when the
  78. * {@link #icon icon attribute} is defined.
  79. *
  80. * @readonly
  81. * @member {module:ui/icon/iconview~IconView} #iconView
  82. */
  83. this.iconView = new IconView();
  84. this.iconView.extendTemplate( {
  85. attributes: {
  86. class: 'ck-button__icon'
  87. }
  88. } );
  89. /**
  90. * Tooltip of the button bound to the template.
  91. *
  92. * @see #tooltip
  93. * @see #_getTooltipString
  94. * @private
  95. * @observable
  96. * @member {Boolean} #_tooltipString
  97. */
  98. this.bind( '_tooltipString' ).to(
  99. this, 'tooltip',
  100. this, 'label',
  101. this, 'keystroke',
  102. this._getTooltipString.bind( this )
  103. );
  104. this.setTemplate( {
  105. tag: 'button',
  106. attributes: {
  107. class: [
  108. 'ck',
  109. 'ck-button',
  110. bind.to( 'class' ),
  111. bind.if( 'isEnabled', 'ck-disabled', value => !value ),
  112. bind.if( 'isVisible', 'ck-hidden', value => !value ),
  113. bind.to( 'isOn', value => value ? 'ck-on' : 'ck-off' ),
  114. bind.if( 'withText', 'ck-button_with-text' )
  115. ],
  116. type: bind.to( 'type', value => value ? value : 'button' ),
  117. tabindex: bind.to( 'tabindex' ),
  118. 'aria-labelledby': `ck-editor__aria-label_${ ariaLabelUid }`,
  119. 'aria-disabled': bind.if( 'isEnabled', true, value => !value ),
  120. 'aria-pressed': bind.if( 'isOn', true )
  121. },
  122. children: this.children,
  123. on: {
  124. mousedown: bind.to( evt => {
  125. evt.preventDefault();
  126. } ),
  127. click: bind.to( evt => {
  128. // We can't make the button disabled using the disabled attribute, because it won't be focusable.
  129. // Though, shouldn't this condition be moved to the button controller?
  130. if ( this.isEnabled ) {
  131. this.fire( 'execute' );
  132. } else {
  133. // Prevent the default when button is disabled, to block e.g.
  134. // automatic form submitting. See ckeditor/ckeditor5-link#74.
  135. evt.preventDefault();
  136. }
  137. } )
  138. }
  139. } );
  140. }
  141. /**
  142. * @inheritDoc
  143. */
  144. render() {
  145. super.render();
  146. if ( this.icon ) {
  147. this.iconView.bind( 'content' ).to( this, 'icon' );
  148. this.children.add( this.iconView );
  149. }
  150. this.children.add( this.tooltipView );
  151. this.children.add( this.labelView );
  152. }
  153. /**
  154. * Focuses the {@link #element} of the button.
  155. */
  156. focus() {
  157. this.element.focus();
  158. }
  159. /**
  160. * Creates a {@link module:ui/tooltip/tooltipview~TooltipView} instance and binds it with button
  161. * attributes.
  162. *
  163. * @private
  164. * @returns {module:ui/tooltip/tooltipview~TooltipView}
  165. */
  166. _createTooltipView() {
  167. const tooltipView = new TooltipView();
  168. tooltipView.bind( 'text' ).to( this, '_tooltipString' );
  169. tooltipView.bind( 'position' ).to( this, 'tooltipPosition' );
  170. return tooltipView;
  171. }
  172. /**
  173. * Creates a label view instance and binds it with button attributes.
  174. *
  175. * @private
  176. * @param {String} ariaLabelUid The aria label UID.
  177. * @returns {module:ui/view~View}
  178. */
  179. _createLabelView( ariaLabelUid ) {
  180. const labelView = new View();
  181. const bind = this.bindTemplate;
  182. labelView.setTemplate( {
  183. tag: 'span',
  184. attributes: {
  185. class: [
  186. 'ck',
  187. 'ck-button__label'
  188. ],
  189. style: bind.to( 'labelStyle' ),
  190. id: `ck-editor__aria-label_${ ariaLabelUid }`,
  191. },
  192. children: [
  193. {
  194. text: this.bindTemplate.to( 'label' )
  195. }
  196. ]
  197. } );
  198. return labelView;
  199. }
  200. /**
  201. * Gets the text for the {@link #tooltipView} from the combination of
  202. * {@link #tooltip}, {@link #label} and {@link #keystroke} attributes.
  203. *
  204. * @private
  205. * @see #tooltip
  206. * @see #_tooltipString
  207. * @param {Boolean|String|Function} tooltip Button tooltip.
  208. * @param {String} label Button label.
  209. * @param {String} keystroke Button keystroke.
  210. * @returns {String}
  211. */
  212. _getTooltipString( tooltip, label, keystroke ) {
  213. if ( tooltip ) {
  214. if ( typeof tooltip == 'string' ) {
  215. return tooltip;
  216. } else {
  217. if ( keystroke ) {
  218. keystroke = getEnvKeystrokeText( keystroke );
  219. }
  220. if ( tooltip instanceof Function ) {
  221. return tooltip( label, keystroke );
  222. } else {
  223. return `${ label }${ keystroke ? ` (${ keystroke })` : '' }`;
  224. }
  225. }
  226. }
  227. return '';
  228. }
  229. }