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

ToolbarButton.web.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* @flow */
  2. import React from 'react';
  3. import { connect } from 'react-redux';
  4. import { translate } from '../../base/i18n';
  5. import UIUtil from '../../../../modules/UI/util/UIUtil';
  6. import AbstractToolbarButton from './AbstractToolbarButton';
  7. import { getButtonAttributesByProps } from '../functions';
  8. declare var APP: Object;
  9. declare var interfaceConfig: Object;
  10. /**
  11. * Represents a button in Toolbar on React.
  12. *
  13. * @class ToolbarButton
  14. * @extends AbstractToolbarButton
  15. */
  16. class ToolbarButton extends AbstractToolbarButton {
  17. _createRefToButton: Function;
  18. _onClick: Function;
  19. /**
  20. * Toolbar button component's property types.
  21. *
  22. * @static
  23. */
  24. static propTypes = {
  25. ...AbstractToolbarButton.propTypes,
  26. /**
  27. * Object describing button.
  28. */
  29. button: React.PropTypes.object.isRequired,
  30. /**
  31. * Used to dispatch an action when the button is clicked.
  32. */
  33. dispatch: React.PropTypes.func,
  34. /**
  35. * Handler for component mount.
  36. */
  37. onMount: React.PropTypes.func,
  38. /**
  39. * Handler for component unmount.
  40. */
  41. onUnmount: React.PropTypes.func,
  42. /**
  43. * Translation helper function.
  44. */
  45. t: React.PropTypes.func,
  46. /**
  47. * Indicates the position of the tooltip.
  48. */
  49. tooltipPosition:
  50. React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
  51. };
  52. /**
  53. * Initializes new ToolbarButton instance.
  54. *
  55. * @param {Object} props - The read-only properties with which the new
  56. * instance is to be initialized.
  57. */
  58. constructor(props: Object) {
  59. super(props);
  60. // Bind methods to save the context
  61. this._createRefToButton = this._createRefToButton.bind(this);
  62. this._onClick = this._onClick.bind(this);
  63. }
  64. /**
  65. * Sets shortcut/tooltip
  66. * after mounting of the component.
  67. *
  68. * @inheritdoc
  69. * @returns {void}
  70. */
  71. componentDidMount(): void {
  72. this._setShortcutAndTooltip();
  73. if (this.props.onMount) {
  74. this.props.onMount();
  75. }
  76. }
  77. /**
  78. * Invokes on unmount handler if it was passed to the props.
  79. *
  80. * @inheritdoc
  81. * @returns {void}
  82. */
  83. componentWillUnmount(): void {
  84. if (this.props.onUnmount) {
  85. this.props.onUnmount();
  86. }
  87. }
  88. /**
  89. * Implements React's {@link Component#render()}.
  90. *
  91. * @inheritdoc
  92. * @returns {ReactElement}
  93. */
  94. render(): ReactElement<*> {
  95. const { button } = this.props;
  96. const attributes = getButtonAttributesByProps(button);
  97. const popups = button.popups || [];
  98. return (
  99. <a
  100. { ...attributes }
  101. onClick = { this._onClick }
  102. ref = { this._createRefToButton }>
  103. { this._renderInnerElementsIfRequired() }
  104. { this._renderPopups(popups) }
  105. </a>
  106. );
  107. }
  108. /**
  109. * Creates reference to current toolbar button.
  110. *
  111. * @param {HTMLElement} element - HTMLElement representing the toolbar
  112. * button.
  113. * @returns {void}
  114. * @private
  115. */
  116. _createRefToButton(element: HTMLElement): void {
  117. this.button = element;
  118. }
  119. /**
  120. * Wrapper on on click handler props for current button.
  121. *
  122. * @param {Event} event - Click event object.
  123. * @returns {void}
  124. * @private
  125. */
  126. _onClick(event: Event): void {
  127. const {
  128. button,
  129. onClick
  130. } = this.props;
  131. const {
  132. enabled,
  133. unclickable
  134. } = button;
  135. if (enabled && !unclickable && onClick) {
  136. const action = onClick(event);
  137. if (action) {
  138. this.props.dispatch(action);
  139. }
  140. }
  141. }
  142. /**
  143. * If toolbar button should contain children elements
  144. * renders them.
  145. *
  146. * @returns {ReactElement|null}
  147. * @private
  148. */
  149. _renderInnerElementsIfRequired(): ReactElement<*> | null {
  150. if (this.props.button.html) {
  151. return this.props.button.html;
  152. }
  153. return null;
  154. }
  155. /**
  156. * Renders popup element for toolbar button.
  157. *
  158. * @param {Array} popups - Array of popup objects.
  159. * @returns {Array}
  160. * @private
  161. */
  162. _renderPopups(popups: Array<*> = []): Array<*> {
  163. return popups.map(popup => {
  164. let gravity = 'n';
  165. if (popup.dataAttrPosition) {
  166. gravity = popup.dataAttrPosition;
  167. }
  168. const title = this.props.t(popup.dataAttr, popup.dataInterpolate);
  169. return (
  170. <div
  171. className = { popup.className }
  172. data-popup = { gravity }
  173. id = { popup.id }
  174. key = { popup.id }
  175. title = { title } />
  176. );
  177. });
  178. }
  179. /**
  180. * Sets shortcut and tooltip for current toolbar button.
  181. *
  182. * @private
  183. * @returns {void}
  184. */
  185. _setShortcutAndTooltip(): void {
  186. const { button, tooltipPosition } = this.props;
  187. const name = button.buttonName;
  188. if (UIUtil.isButtonEnabled(name)) {
  189. if (!button.unclickable) {
  190. UIUtil.setTooltip(this.button,
  191. button.tooltipKey,
  192. tooltipPosition);
  193. }
  194. if (button.shortcut) {
  195. APP.keyboardshortcut.registerShortcut(
  196. button.shortcut,
  197. button.shortcutAttr,
  198. button.shortcutFunc,
  199. button.shortcutDescription
  200. );
  201. }
  202. }
  203. }
  204. }
  205. export default translate(connect()(ToolbarButton));