You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ToolbarButton.web.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* @flow */
  2. import AKInlineDialog from '@atlaskit/inline-dialog';
  3. import { Tooltip } from '@atlaskit/tooltip';
  4. import React, { Component } from 'react';
  5. import { translate } from '../../base/i18n';
  6. import { isButtonEnabled } from '../functions';
  7. import StatelessToolbarButton from './StatelessToolbarButton';
  8. declare var APP: Object;
  9. /**
  10. * Mapping of tooltip positions to equivalent {@code AKInlineDialog} positions.
  11. *
  12. * @private
  13. */
  14. const TOOLTIP_TO_POPUP_POSITION = {
  15. bottom: 'bottom center',
  16. left: 'left middle',
  17. top: 'top center',
  18. right: 'right middle'
  19. };
  20. /**
  21. * Represents a button in Toolbar on React.
  22. *
  23. * @class ToolbarButton
  24. * @extends AbstractToolbarButton
  25. */
  26. class ToolbarButton extends Component {
  27. button: Object;
  28. _createRefToButton: Function;
  29. _onClick: Function;
  30. _onMouseOut: Function;
  31. _onMouseOver: Function;
  32. state: {
  33. /**
  34. * Whether or not the tooltip for the button should be displayed.
  35. *
  36. * @type {boolean}
  37. */
  38. showTooltip: boolean
  39. }
  40. /**
  41. * Toolbar button component's property types.
  42. *
  43. * @static
  44. */
  45. static propTypes = {
  46. ...StatelessToolbarButton.propTypes,
  47. /**
  48. * Object describing button.
  49. */
  50. button: React.PropTypes.object.isRequired,
  51. /**
  52. * Handler for component mount.
  53. */
  54. onMount: React.PropTypes.func,
  55. /**
  56. * Handler for component unmount.
  57. */
  58. onUnmount: React.PropTypes.func,
  59. /**
  60. * Translation helper function.
  61. */
  62. t: React.PropTypes.func,
  63. /**
  64. * Indicates the position of the tooltip.
  65. */
  66. tooltipPosition:
  67. React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
  68. };
  69. /**
  70. * Initializes new ToolbarButton instance.
  71. *
  72. * @param {Object} props - The read-only properties with which the new
  73. * instance is to be initialized.
  74. */
  75. constructor(props: Object) {
  76. super(props);
  77. this.state = {
  78. showTooltip: false
  79. };
  80. // Bind methods to save the context
  81. this._createRefToButton = this._createRefToButton.bind(this);
  82. this._onClick = this._onClick.bind(this);
  83. this._onMouseOut = this._onMouseOut.bind(this);
  84. this._onMouseOver = this._onMouseOver.bind(this);
  85. }
  86. /**
  87. * Sets shortcut/tooltip
  88. * after mounting of the component.
  89. *
  90. * @inheritdoc
  91. * @returns {void}
  92. */
  93. componentDidMount(): void {
  94. this._setShortcut();
  95. if (this.props.onMount) {
  96. this.props.onMount();
  97. }
  98. }
  99. /**
  100. * Invokes on unmount handler if it was passed to the props.
  101. *
  102. * @inheritdoc
  103. * @returns {void}
  104. */
  105. componentWillUnmount(): void {
  106. if (this.props.onUnmount) {
  107. this.props.onUnmount();
  108. }
  109. }
  110. /**
  111. * Implements React's {@link Component#render()}.
  112. *
  113. * @inheritdoc
  114. * @returns {ReactElement}
  115. */
  116. render(): ReactElement<*> {
  117. const { button, t, tooltipPosition } = this.props;
  118. const props = {
  119. ...this.props,
  120. onClick: this._onClick,
  121. createRefToButton: this._createRefToButton
  122. };
  123. const buttonComponent = ( // eslint-disable-line no-extra-parens
  124. <Tooltip
  125. description = { button.tooltipText || t(button.tooltipKey) }
  126. onMouseOut = { this._onMouseOut }
  127. onMouseOver = { this._onMouseOver }
  128. position = { tooltipPosition }
  129. visible = { this.state.showTooltip }>
  130. <StatelessToolbarButton { ...props } />
  131. </Tooltip>
  132. );
  133. let children = buttonComponent;
  134. const popupConfig = this._getPopupDisplayConfiguration();
  135. if (popupConfig) {
  136. const { dataAttr, dataInterpolate, position } = popupConfig;
  137. children = ( // eslint-disable-line no-extra-parens
  138. <AKInlineDialog
  139. content = { t(dataAttr, dataInterpolate) }
  140. isOpen = { Boolean(popupConfig) }
  141. position = { position }>
  142. { buttonComponent }
  143. </AKInlineDialog>
  144. );
  145. }
  146. return (
  147. <div className = { `toolbar-button-wrapper ${button.id}-wrapper` }>
  148. { children }
  149. </div>
  150. );
  151. }
  152. /**
  153. * Wrapper on on click handler props for current button.
  154. *
  155. * @param {Event} event - Click event object.
  156. * @returns {void}
  157. * @private
  158. */
  159. _onClick(event) {
  160. this.props.onClick(event);
  161. this.setState({ showTooltip: false });
  162. }
  163. /**
  164. * Creates reference to current toolbar button.
  165. *
  166. * @param {HTMLElement} element - HTMLElement representing the toolbar
  167. * button.
  168. * @returns {void}
  169. * @private
  170. */
  171. _createRefToButton(element: HTMLElement): void {
  172. this.button = element;
  173. }
  174. /**
  175. * Parses the props and state to find any popup that should be displayed
  176. * and returns an object describing how the popup should display.
  177. *
  178. * @private
  179. * @returns {Object|null}
  180. */
  181. _getPopupDisplayConfiguration() {
  182. const { button, tooltipPosition } = this.props;
  183. const { popups, popupDisplay } = button;
  184. if (!popups || !popupDisplay) {
  185. return null;
  186. }
  187. const { popupID } = popupDisplay;
  188. const currentPopup = popups.find(popup => popup.id === popupID);
  189. return Object.assign(
  190. {},
  191. currentPopup || {},
  192. {
  193. position: TOOLTIP_TO_POPUP_POSITION[tooltipPosition]
  194. });
  195. }
  196. /**
  197. * If toolbar button should contain children elements
  198. * renders them.
  199. *
  200. * @returns {ReactElement|null}
  201. * @private
  202. */
  203. _renderInnerElementsIfRequired(): ReactElement<*> | null {
  204. if (this.props.button.html) {
  205. return this.props.button.html;
  206. }
  207. return null;
  208. }
  209. /**
  210. * Hides any displayed tooltip.
  211. *
  212. * @private
  213. * @returns {void}
  214. */
  215. _onMouseOut(): void {
  216. this.setState({ showTooltip: false });
  217. }
  218. /**
  219. * Hides any displayed tooltip.
  220. *
  221. * @private
  222. * @returns {void}
  223. */
  224. _onMouseOver(): void {
  225. const { button } = this.props;
  226. this.setState({
  227. showTooltip: isButtonEnabled(button.buttonName)
  228. && !button.unclickable
  229. });
  230. }
  231. /**
  232. * Sets shortcut and tooltip for current toolbar button.
  233. *
  234. * @private
  235. * @returns {void}
  236. */
  237. _setShortcut(): void {
  238. const { button } = this.props;
  239. if (button.shortcut && APP && APP.keyboardshortcut) {
  240. APP.keyboardshortcut.registerShortcut(
  241. button.shortcut,
  242. button.shortcutAttr,
  243. button.shortcutFunc,
  244. button.shortcutDescription
  245. );
  246. }
  247. }
  248. }
  249. export default translate(ToolbarButton);