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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. const popupConfig = this._getPopupDisplayConfiguration();
  134. if (popupConfig) {
  135. const { dataAttr, dataInterpolate, position } = popupConfig;
  136. return (
  137. <AKInlineDialog
  138. content = { t(dataAttr, dataInterpolate) }
  139. isOpen = { Boolean(popupConfig) }
  140. position = { position }>
  141. { buttonComponent }
  142. </AKInlineDialog>
  143. );
  144. }
  145. return buttonComponent;
  146. }
  147. /**
  148. * Wrapper on on click handler props for current button.
  149. *
  150. * @param {Event} event - Click event object.
  151. * @returns {void}
  152. * @private
  153. */
  154. _onClick(event) {
  155. this.props.onClick(event);
  156. this.setState({ showTooltip: false });
  157. }
  158. /**
  159. * Creates reference to current toolbar button.
  160. *
  161. * @param {HTMLElement} element - HTMLElement representing the toolbar
  162. * button.
  163. * @returns {void}
  164. * @private
  165. */
  166. _createRefToButton(element: HTMLElement): void {
  167. this.button = element;
  168. }
  169. /**
  170. * Parses the props and state to find any popup that should be displayed
  171. * and returns an object describing how the popup should display.
  172. *
  173. * @private
  174. * @returns {Object|null}
  175. */
  176. _getPopupDisplayConfiguration() {
  177. const { button, tooltipPosition } = this.props;
  178. const { popups, popupDisplay } = button;
  179. if (!popups || !popupDisplay) {
  180. return null;
  181. }
  182. const { popupID } = popupDisplay;
  183. const currentPopup = popups.find(popup => popup.id === popupID);
  184. return Object.assign(
  185. {},
  186. currentPopup || {},
  187. {
  188. position: TOOLTIP_TO_POPUP_POSITION[tooltipPosition]
  189. });
  190. }
  191. /**
  192. * If toolbar button should contain children elements
  193. * renders them.
  194. *
  195. * @returns {ReactElement|null}
  196. * @private
  197. */
  198. _renderInnerElementsIfRequired(): ReactElement<*> | null {
  199. if (this.props.button.html) {
  200. return this.props.button.html;
  201. }
  202. return null;
  203. }
  204. /**
  205. * Hides any displayed tooltip.
  206. *
  207. * @private
  208. * @returns {void}
  209. */
  210. _onMouseOut(): void {
  211. this.setState({ showTooltip: false });
  212. }
  213. /**
  214. * Hides any displayed tooltip.
  215. *
  216. * @private
  217. * @returns {void}
  218. */
  219. _onMouseOver(): void {
  220. const { button } = this.props;
  221. this.setState({
  222. showTooltip: isButtonEnabled(button.buttonName)
  223. && !button.unclickable
  224. });
  225. }
  226. /**
  227. * Sets shortcut and tooltip for current toolbar button.
  228. *
  229. * @private
  230. * @returns {void}
  231. */
  232. _setShortcut(): void {
  233. const { button } = this.props;
  234. if (button.shortcut && APP && APP.keyboardshortcut) {
  235. APP.keyboardshortcut.registerShortcut(
  236. button.shortcut,
  237. button.shortcutAttr,
  238. button.shortcutFunc,
  239. button.shortcutDescription
  240. );
  241. }
  242. }
  243. }
  244. export default translate(ToolbarButton);