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.

Toolbar.web.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { setToolbarHovered } from '../actions';
  5. import StatelessToolbar from './StatelessToolbar';
  6. import ToolbarButton from './ToolbarButton';
  7. /**
  8. * Implements a toolbar in React/Web. It is a strip that contains a set of
  9. * toolbar items such as buttons. Toolbar is commonly placed inside of a
  10. * Toolbox.
  11. *
  12. * @class Toolbar
  13. * @extends Component
  14. */
  15. class Toolbar extends Component {
  16. _onMouseOut: Function;
  17. _onMouseOver: Function;
  18. _renderToolbarButton: Function;
  19. /**
  20. * Base toolbar component's property types.
  21. *
  22. * @static
  23. */
  24. static propTypes = {
  25. /**
  26. * Children of current React component.
  27. */
  28. children: React.PropTypes.element,
  29. /**
  30. * Toolbar's class name.
  31. */
  32. className: React.PropTypes.string,
  33. /**
  34. * Used to dispatch an action when a button is clicked or on mouse
  35. * out/in event.
  36. */
  37. dispatch: React.PropTypes.func,
  38. /**
  39. * Map with toolbar buttons.
  40. */
  41. toolbarButtons: React.PropTypes.instanceOf(Map),
  42. /**
  43. * Indicates the position of the tooltip.
  44. */
  45. tooltipPosition:
  46. React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
  47. };
  48. /**
  49. * Constructor of Primary toolbar class.
  50. *
  51. * @param {Object} props - Object containing React component properties.
  52. */
  53. constructor(props: Object) {
  54. super(props);
  55. // Bind callbacks to preverse this.
  56. this._onMouseOut = this._onMouseOut.bind(this);
  57. this._onMouseOver = this._onMouseOver.bind(this);
  58. this._renderToolbarButton = this._renderToolbarButton.bind(this);
  59. }
  60. /**
  61. * Implements React's {@link Component#render()}.
  62. *
  63. * @inheritdoc
  64. * @returns {ReactElement}
  65. */
  66. render(): ReactElement<*> {
  67. const props = {
  68. className: this.props.className,
  69. onMouseOut: this._onMouseOut,
  70. onMouseOver: this._onMouseOver
  71. };
  72. return (
  73. <StatelessToolbar { ...props }>
  74. {
  75. [ ...this.props.toolbarButtons.entries() ]
  76. .map(this._renderToolbarButton)
  77. }
  78. {
  79. this.props.children
  80. }
  81. </StatelessToolbar>
  82. );
  83. }
  84. /**
  85. * Dispatches an action signalling that toolbar is no being hovered.
  86. *
  87. * @protected
  88. * @returns {Object} Dispatched action.
  89. */
  90. _onMouseOut() {
  91. this.props.dispatch(setToolbarHovered(false));
  92. }
  93. /**
  94. * Dispatches an action signalling that toolbar is now being hovered.
  95. *
  96. * @protected
  97. * @returns {Object} Dispatched action.
  98. */
  99. _onMouseOver() {
  100. this.props.dispatch(setToolbarHovered(true));
  101. }
  102. /**
  103. * Renders toolbar button. Method is passed to map function.
  104. *
  105. * @param {Array} keyValuePair - Key value pair containing button and its
  106. * key.
  107. * @private
  108. * @returns {ReactElement} A toolbar button.
  109. */
  110. _renderToolbarButton(
  111. keyValuePair: Array<*>): ReactElement<*> {
  112. const [ key, button ] = keyValuePair;
  113. if (button.component) {
  114. return (
  115. <button.component
  116. key = { key }
  117. tooltipPosition = { this.props.tooltipPosition } />
  118. );
  119. }
  120. const { tooltipPosition } = this.props;
  121. const { onClick, onMount, onUnmount } = button;
  122. const onClickWithDispatch = (...args) =>
  123. onClick && onClick(this.props.dispatch, ...args);
  124. return (
  125. <ToolbarButton
  126. button = { button }
  127. key = { key }
  128. onClick = { onClickWithDispatch }
  129. onMount = { onMount }
  130. onUnmount = { onUnmount }
  131. tooltipPosition = { tooltipPosition } />
  132. );
  133. }
  134. }
  135. export default connect()(Toolbar);