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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* @flow */
  2. import React from 'react';
  3. import { translate } from '../../base/i18n';
  4. import {
  5. setTooltip,
  6. setTooltipText
  7. } from '../../../../modules/UI/util/Tooltip';
  8. import AbstractToolbarButton from './AbstractToolbarButton';
  9. import {
  10. getButtonAttributesByProps,
  11. isButtonEnabled
  12. } from '../functions';
  13. declare var APP: Object;
  14. /**
  15. * Represents a button in Toolbar on React.
  16. *
  17. * @class ToolbarButton
  18. * @extends AbstractToolbarButton
  19. */
  20. class ToolbarButton extends AbstractToolbarButton {
  21. _createRefToButton: Function;
  22. _onClick: Function;
  23. /**
  24. * Toolbar button component's property types.
  25. *
  26. * @static
  27. */
  28. static propTypes = {
  29. ...AbstractToolbarButton.propTypes,
  30. /**
  31. * Object describing button.
  32. */
  33. button: React.PropTypes.object.isRequired,
  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. onClick(event);
  137. }
  138. }
  139. /**
  140. * If toolbar button should contain children elements
  141. * renders them.
  142. *
  143. * @returns {ReactElement|null}
  144. * @private
  145. */
  146. _renderInnerElementsIfRequired(): ReactElement<*> | null {
  147. if (this.props.button.html) {
  148. return this.props.button.html;
  149. }
  150. return null;
  151. }
  152. /**
  153. * Renders popup element for toolbar button.
  154. *
  155. * @param {Array} popups - Array of popup objects.
  156. * @returns {Array}
  157. * @private
  158. */
  159. _renderPopups(popups: Array<*> = []): Array<*> {
  160. return popups.map(popup => {
  161. let gravity = 'n';
  162. if (popup.dataAttrPosition) {
  163. gravity = popup.dataAttrPosition;
  164. }
  165. const title = this.props.t(popup.dataAttr, popup.dataInterpolate);
  166. return (
  167. <div
  168. className = { popup.className }
  169. data-popup = { gravity }
  170. id = { popup.id }
  171. key = { popup.id }
  172. title = { title } />
  173. );
  174. });
  175. }
  176. /**
  177. * Sets shortcut and tooltip for current toolbar button.
  178. *
  179. * @private
  180. * @returns {void}
  181. */
  182. _setShortcutAndTooltip(): void {
  183. const { button, tooltipPosition } = this.props;
  184. const name = button.buttonName;
  185. if (isButtonEnabled(name)) {
  186. if (!button.unclickable) {
  187. if (button.tooltipText) {
  188. setTooltipText(this.button,
  189. button.tooltipText,
  190. tooltipPosition);
  191. } else {
  192. setTooltip(this.button,
  193. button.tooltipKey,
  194. tooltipPosition);
  195. }
  196. }
  197. if (button.shortcut) {
  198. APP.keyboardshortcut.registerShortcut(
  199. button.shortcut,
  200. button.shortcutAttr,
  201. button.shortcutFunc,
  202. button.shortcutDescription
  203. );
  204. }
  205. }
  206. }
  207. }
  208. export default translate(ToolbarButton);