8
0

buttonview.js 5.4 KB

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