8
0

buttonview.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 Template from '../template';
  10. import IconView from '../icon/iconview';
  11. import { getEnvKeystrokeText } from 'ckeditor5-utils/src/keyboard';
  12. /**
  13. * The button view class.
  14. *
  15. * @extends module:ui/view~View
  16. */
  17. export default class ButtonView extends View {
  18. /**
  19. * @inheritDoc
  20. */
  21. constructor( locale ) {
  22. super( locale );
  23. /**
  24. * The label of the button view visible to the user.
  25. *
  26. * @observable
  27. * @member {String} #label
  28. */
  29. this.set( 'label' );
  30. /**
  31. * (Optional) The keystroke associated with the button, i.e. <kbd>CTRL+B</kbd>,
  32. * in the string format compatible with {@link module:utils/keyboard}.
  33. *
  34. * @observable
  35. * @member {Boolean} #keystroke
  36. */
  37. this.set( 'keystroke' );
  38. /**
  39. * (Optional) Tooltip of the button, i.e. displayed when hovering the button with the mouse cursor.
  40. *
  41. * * If defined as a `Boolean` (e.g. `true`), then combination of `label` and `keystroke` will be set as a tooltip.
  42. * * If defined as a `String`, tooltip will equal the exact text of that `String`.
  43. * * If defined as a `Function`, `label` and `keystroke` will be passed to that function, which is to return
  44. * a string with the tooltip text.
  45. *
  46. * const view = new ButtonView( locale );
  47. * view.tooltip = ( label, keystroke ) => `A tooltip for ${ label } and ${ keystroke }.`
  48. *
  49. * @observable
  50. * @default false
  51. * @member {Boolean|String|Function} #tooltip
  52. */
  53. this.set( 'tooltip' );
  54. /**
  55. * The HTML type of the button. Default `button`.
  56. *
  57. * @observable
  58. * @member {'button'|'submit'|'reset'|'menu'} #type
  59. */
  60. this.set( 'type', 'button' );
  61. /**
  62. * Controls whether the button view is "on", e.g. some feature which it represents
  63. * is currently enabled.
  64. *
  65. * @observable
  66. * @member {Boolean} #isOn
  67. */
  68. this.set( 'isOn', false );
  69. /**
  70. * Controls whether the button view is enabled (can be clicked).
  71. *
  72. * @observable
  73. * @member {Boolean} #isEnabled
  74. */
  75. this.set( 'isEnabled', true );
  76. /**
  77. * (Optional) Whether the label of the button is hidden (e.g. button with icon only).
  78. *
  79. * @observable
  80. * @member {Boolean} #withText
  81. */
  82. this.set( 'withText', false );
  83. /**
  84. * (Optional) Source of the icon. See {@link module:ui/icon/iconview~IconView#content}.
  85. *
  86. * @observable
  87. * @member {String} #icon
  88. */
  89. this.set( 'icon' );
  90. /**
  91. * Tooltip of the button bound to the template.
  92. *
  93. * @see #tooltip
  94. * @see #_getTooltipString
  95. * @private
  96. * @observable
  97. * @member {Boolean} #_tooltipString
  98. */
  99. this.bind( '_tooltipString' ).to(
  100. this, 'tooltip',
  101. this, 'label',
  102. this, 'keystroke',
  103. this._getTooltipString.bind( this )
  104. );
  105. /**
  106. * Icon of the button view.
  107. *
  108. * @readonly
  109. * @member {module:ui/icon/iconview~IconView} #iconView
  110. */
  111. const bind = this.bindTemplate;
  112. this.template = new Template( {
  113. tag: 'button',
  114. attributes: {
  115. class: [
  116. 'ck-button',
  117. bind.if( '_tooltipString', 'ck-tooltip_s' ),
  118. bind.to( 'isEnabled', value => value ? 'ck-enabled' : 'ck-disabled' ),
  119. bind.to( 'isOn', value => value ? 'ck-on' : 'ck-off' ),
  120. bind.if( 'withText', 'ck-button_with-text' )
  121. ],
  122. type: bind.to( 'type', value => value ? value : 'button' ),
  123. 'data-ck-tooltip': [
  124. bind.to( '_tooltipString' )
  125. ]
  126. },
  127. children: [
  128. {
  129. tag: 'span',
  130. attributes: {
  131. class: [ 'ck-button__label' ]
  132. },
  133. children: [
  134. {
  135. text: bind.to( 'label' )
  136. }
  137. ]
  138. }
  139. ],
  140. on: {
  141. mousedown: bind.to( evt => {
  142. evt.preventDefault();
  143. } ),
  144. click: bind.to( evt => {
  145. // We can't make the button disabled using the disabled attribute, because it won't be focusable.
  146. // Though, shouldn't this condition be moved to the button controller?
  147. if ( this.isEnabled ) {
  148. this.fire( 'execute' );
  149. } else {
  150. // Prevent the default when button is disabled, to block e.g.
  151. // automatic form submitting. See ckeditor/ckeditor5-link#74.
  152. evt.preventDefault();
  153. }
  154. } )
  155. }
  156. } );
  157. /**
  158. * Fired when the button view is clicked. It won't be fired when the button is disabled.
  159. *
  160. * @event #execute
  161. */
  162. }
  163. /**
  164. * @inheritDoc
  165. */
  166. init() {
  167. let promise = Promise.resolve();
  168. if ( this.icon && !this.iconView ) {
  169. const iconView = this.iconView = new IconView();
  170. iconView.bind( 'content' ).to( this, 'icon' );
  171. this.element.insertBefore( iconView.element, this.element.firstChild );
  172. // Make sure the icon view will be destroyed along with button.
  173. promise = promise.then( () => this.addChildren( iconView ) );
  174. }
  175. return promise.then( () => super.init() );
  176. }
  177. /**
  178. * Gets value for the `data-ck-tooltip` attribute from the combination of
  179. * {@link #tooltip}, {@link #label} and {@link #keystroke} attributes.
  180. *
  181. * @private
  182. * @see #tooltip
  183. * @see #_tooltipString
  184. * @param {Boolean|String|Function} tooltip Button tooltip.
  185. * @param {String} label Button label.
  186. * @param {String} keystroke Button keystroke.
  187. * @returns {String}
  188. */
  189. _getTooltipString( tooltip, label, keystroke ) {
  190. if ( tooltip ) {
  191. if ( typeof tooltip == 'string' ) {
  192. return tooltip;
  193. } else {
  194. if ( keystroke ) {
  195. keystroke = getEnvKeystrokeText( keystroke );
  196. }
  197. if ( tooltip instanceof Function ) {
  198. return tooltip( label, keystroke );
  199. } else if ( tooltip === true ) {
  200. return `${ label }${ keystroke ? ` (${ keystroke })` : '' }`;
  201. }
  202. }
  203. }
  204. return false;
  205. }
  206. }