您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Toolbar.web.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  55. * Constructor of Primary toolbar class.
  56. *
  57. * @param {Object} props - Object containing React component properties.
  58. */
  59. constructor(props) {
  60. super(props);
  61. this._setButtonHandlers();
  62. // Bind methods to save the context
  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. const { splitterIndex } = this.props;
  102. if (splitterIndex && index === splitterIndex) {
  103. const splitter = <span className = 'toolbar__splitter' />;
  104. acc.push(splitter);
  105. }
  106. const { onClick, onMount, onUnmount } = button;
  107. acc.push(
  108. <ToolbarButton
  109. button = { button }
  110. key = { key }
  111. onClick = { onClick }
  112. onMount = { onMount }
  113. onUnmount = { onUnmount } />
  114. );
  115. return acc;
  116. }
  117. /**
  118. * Sets handlers for some of the buttons.
  119. *
  120. * @private
  121. * @returns {void}
  122. */
  123. _setButtonHandlers(): void {
  124. const {
  125. buttonHandlers,
  126. toolbarButtons
  127. } = this.props;
  128. // Only a few buttons have custom button handlers defined, so this
  129. // list may be undefined or empty depending on the buttons we're
  130. // rendering.
  131. // TODO: merge the buttonHandlers and onClick properties and come up
  132. // with a consistent event handling property.
  133. if (!buttonHandlers) {
  134. return;
  135. }
  136. Object.keys(buttonHandlers).forEach(key => {
  137. let button = toolbarButtons.get(key);
  138. if (button) {
  139. button = {
  140. ...button,
  141. ...buttonHandlers[key]
  142. };
  143. toolbarButtons.set(key, button);
  144. }
  145. });
  146. }
  147. }
  148. /**
  149. * Maps part of Redux actions to component's props.
  150. *
  151. * @param {Function} dispatch - Redux action dispatcher.
  152. * @returns {Object}
  153. * @private
  154. */
  155. function _mapDispatchToProps(dispatch: Function): Object {
  156. return {
  157. /**
  158. * Dispatches an action signalling that toolbar is no being hovered.
  159. *
  160. * @protected
  161. * @returns {Object} Dispatched action.
  162. */
  163. _onMouseOut() {
  164. return dispatch(setToolbarHovered(false));
  165. },
  166. /**
  167. * Dispatches an action signalling that toolbar is now being hovered.
  168. *
  169. * @protected
  170. * @returns {Object} Dispatched action.
  171. */
  172. _onMouseOver() {
  173. return dispatch(setToolbarHovered(true));
  174. }
  175. };
  176. }
  177. export default connect(null, _mapDispatchToProps)(Toolbar);