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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { setToolbarHovered } from '../actions';
  5. import ToolbarButton from './ToolbarButton';
  6. /**
  7. * Implements a toolbar in React/Web. It is a strip that contains a set of
  8. * toolbar items such as buttons. Toolbar is commonly placed inside of a
  9. * Toolbox.
  10. *
  11. * @class Toolbar
  12. * @extends Component
  13. */
  14. class Toolbar extends Component {
  15. _renderToolbarButton: Function;
  16. /**
  17. * Base toolbar component's property types.
  18. *
  19. * @static
  20. */
  21. static propTypes = {
  22. /**
  23. * Handler for mouse out event.
  24. */
  25. _onMouseOut: React.PropTypes.func,
  26. /**
  27. * Handler for mouse over event.
  28. */
  29. _onMouseOver: React.PropTypes.func,
  30. /**
  31. * Children of current React component.
  32. */
  33. children: React.PropTypes.element,
  34. /**
  35. * Toolbar's class name.
  36. */
  37. className: React.PropTypes.string,
  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) {
  54. super(props);
  55. // Bind callbacks to preverse this.
  56. this._renderToolbarButton = this._renderToolbarButton.bind(this);
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render(): ReactElement<*> {
  65. const { className } = this.props;
  66. return (
  67. <div
  68. className = { `toolbar ${className}` }
  69. onMouseOut = { this.props._onMouseOut }
  70. onMouseOver = { this.props._onMouseOver }>
  71. {
  72. [ ...this.props.toolbarButtons.entries() ]
  73. .reduce(this._renderToolbarButton, [])
  74. }
  75. {
  76. this.props.children
  77. }
  78. </div>
  79. );
  80. }
  81. /**
  82. * Renders toolbar button. Method is passed to reduce function.
  83. *
  84. * @param {Array} acc - Toolbar buttons array.
  85. * @param {Array} keyValuePair - Key value pair containing button and its
  86. * key.
  87. * @private
  88. * @returns {Array} Array of toolbar buttons.
  89. */
  90. _renderToolbarButton(acc: Array<*>,
  91. keyValuePair: Array<*>): Array<ReactElement<*>> {
  92. const [ key, button ] = keyValuePair;
  93. if (button.component) {
  94. acc.push(
  95. <button.component
  96. key = { key }
  97. tooltipPosition = { this.props.tooltipPosition } />
  98. );
  99. return acc;
  100. }
  101. const { tooltipPosition } = this.props;
  102. const { onClick, onMount, onUnmount } = button;
  103. acc.push(
  104. <ToolbarButton
  105. button = { button }
  106. key = { key }
  107. onClick = { onClick }
  108. onMount = { onMount }
  109. onUnmount = { onUnmount }
  110. tooltipPosition = { tooltipPosition } />
  111. );
  112. return acc;
  113. }
  114. }
  115. /**
  116. * Maps part of Redux actions to component's props.
  117. *
  118. * @param {Function} dispatch - Redux action dispatcher.
  119. * @private
  120. * @returns {Object}
  121. */
  122. function _mapDispatchToProps(dispatch: Function): Object {
  123. return {
  124. /**
  125. * Dispatches an action signalling that toolbar is no being hovered.
  126. *
  127. * @protected
  128. * @returns {Object} Dispatched action.
  129. */
  130. _onMouseOut() {
  131. dispatch(setToolbarHovered(false));
  132. },
  133. /**
  134. * Dispatches an action signalling that toolbar is now being hovered.
  135. *
  136. * @protected
  137. * @returns {Object} Dispatched action.
  138. */
  139. _onMouseOver() {
  140. dispatch(setToolbarHovered(true));
  141. }
  142. };
  143. }
  144. export default connect(undefined, _mapDispatchToProps)(Toolbar);