您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ToolbarButton.web.js 6.3KB

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