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

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