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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * Contains button handlers.
  34. */
  35. buttonHandlers: React.PropTypes.object,
  36. /**
  37. * Children of current React component.
  38. */
  39. children: React.PropTypes.element,
  40. /**
  41. * Toolbar's class name.
  42. */
  43. className: React.PropTypes.string,
  44. /**
  45. * If the toolbar requires splitter this property defines splitter
  46. * index.
  47. */
  48. splitterIndex: React.PropTypes.number,
  49. /**
  50. * Map with toolbar buttons.
  51. */
  52. toolbarButtons: React.PropTypes.instanceOf(Map),
  53. /**
  54. * Indicates the position of the tooltip.
  55. */
  56. tooltipPosition:
  57. React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
  58. };
  59. /**
  60. * Constructor of Primary toolbar class.
  61. *
  62. * @param {Object} props - Object containing React component properties.
  63. */
  64. constructor(props) {
  65. super(props);
  66. this._setButtonHandlers();
  67. // Bind callbacks to preverse this.
  68. this._renderToolbarButton = this._renderToolbarButton.bind(this);
  69. }
  70. /**
  71. * Implements React's {@link Component#render()}.
  72. *
  73. * @inheritdoc
  74. * @returns {ReactElement}
  75. */
  76. render(): ReactElement<*> {
  77. const { className } = this.props;
  78. return (
  79. <div
  80. className = { `toolbar ${className}` }
  81. onMouseOut = { this.props._onMouseOut }
  82. onMouseOver = { this.props._onMouseOver }>
  83. {
  84. [ ...this.props.toolbarButtons.entries() ]
  85. .reduce(this._renderToolbarButton, [])
  86. }
  87. {
  88. this.props.children
  89. }
  90. </div>
  91. );
  92. }
  93. /**
  94. * Renders toolbar button. Method is passed to reduce function.
  95. *
  96. * @param {Array} acc - Toolbar buttons array.
  97. * @param {Array} keyValuePair - Key value pair containing button and its
  98. * key.
  99. * @param {number} index - Index of the key value pair in the array.
  100. * @returns {Array} Array of toolbar buttons and splitter if it's on.
  101. * @private
  102. */
  103. _renderToolbarButton(acc: Array<*>, keyValuePair: Array<*>,
  104. index: number): Array<ReactElement<*>> {
  105. const [ key, button ] = keyValuePair;
  106. const { splitterIndex, tooltipPosition } = this.props;
  107. if (splitterIndex && index === splitterIndex) {
  108. const splitter = <span className = 'toolbar__splitter' />;
  109. acc.push(splitter);
  110. }
  111. const { onClick, onMount, onUnmount } = button;
  112. acc.push(
  113. <ToolbarButton
  114. button = { button }
  115. key = { key }
  116. onClick = { onClick }
  117. onMount = { onMount }
  118. onUnmount = { onUnmount }
  119. tooltipPosition = { tooltipPosition } />
  120. );
  121. return acc;
  122. }
  123. /**
  124. * Sets handlers for some of the buttons.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _setButtonHandlers(): void {
  130. const {
  131. buttonHandlers,
  132. toolbarButtons
  133. } = this.props;
  134. // Only a few buttons have buttonHandlers defined, so it may be
  135. // undefined or empty depending on the buttons rendered.
  136. // TODO Merge the buttonHandlers and onClick properties and come up with
  137. // a consistent event handling property.
  138. buttonHandlers && Object.keys(buttonHandlers).forEach(key => {
  139. let button = toolbarButtons.get(key);
  140. if (button) {
  141. button = {
  142. ...button,
  143. ...buttonHandlers[key]
  144. };
  145. toolbarButtons.set(key, button);
  146. }
  147. });
  148. }
  149. }
  150. /**
  151. * Maps part of Redux actions to component's props.
  152. *
  153. * @param {Function} dispatch - Redux action dispatcher.
  154. * @returns {Object}
  155. * @private
  156. */
  157. function _mapDispatchToProps(dispatch: Function): Object {
  158. return {
  159. /**
  160. * Dispatches an action signalling that toolbar is no being hovered.
  161. *
  162. * @protected
  163. * @returns {Object} Dispatched action.
  164. */
  165. _onMouseOut() {
  166. return dispatch(setToolbarHovered(false));
  167. },
  168. /**
  169. * Dispatches an action signalling that toolbar is now being hovered.
  170. *
  171. * @protected
  172. * @returns {Object} Dispatched action.
  173. */
  174. _onMouseOver() {
  175. return dispatch(setToolbarHovered(true));
  176. }
  177. };
  178. }
  179. export default connect(null, _mapDispatchToProps)(Toolbar);