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.

BaseToolbar.web.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. declare var APP: Object;
  9. declare var config: Object;
  10. declare var interfaceConfig: Object;
  11. /**
  12. * Class implementing Primary Toolbar React component.
  13. *
  14. * @class PrimaryToolbar
  15. * @extends Component
  16. */
  17. class BaseToolbar extends Component {
  18. _renderToolbarButton: Function;
  19. /**
  20. * Base toolbar component's property types.
  21. *
  22. * @static
  23. */
  24. static propTypes = {
  25. /**
  26. * Handler for mouse out event.
  27. */
  28. _onMouseOut: React.PropTypes.func,
  29. /**
  30. * Handler for mouse over event.
  31. */
  32. _onMouseOver: React.PropTypes.func,
  33. /**
  34. * Contains button handlers.
  35. */
  36. buttonHandlers: React.PropTypes.object,
  37. /**
  38. * Children of current React component.
  39. */
  40. children: React.PropTypes.element,
  41. /**
  42. * Toolbar's class name.
  43. */
  44. className: React.PropTypes.string,
  45. /**
  46. * If the toolbar requires splitter this property defines splitter
  47. * index.
  48. */
  49. splitterIndex: React.PropTypes.number,
  50. /**
  51. * Map with toolbar buttons.
  52. */
  53. toolbarButtons: React.PropTypes.instanceOf(Map)
  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. this._setButtonHandlers();
  63. // Bind methods to save the context
  64. this._renderToolbarButton = this._renderToolbarButton.bind(this);
  65. }
  66. /**
  67. * Implements React's {@link Component#render()}.
  68. *
  69. * @inheritdoc
  70. * @returns {ReactElement}
  71. */
  72. render(): ReactElement<*> {
  73. const { className } = this.props;
  74. return (
  75. <div
  76. className = { `toolbar ${className}` }
  77. onMouseOut = { this.props._onMouseOut }
  78. onMouseOver = { this.props._onMouseOver }>
  79. {
  80. [ ...this.props.toolbarButtons.entries() ]
  81. .reduce(this._renderToolbarButton, [])
  82. }
  83. {
  84. this.props.children
  85. }
  86. </div>
  87. );
  88. }
  89. /**
  90. * Renders toolbar button. Method is passed to reduce function.
  91. *
  92. * @param {Array} acc - Toolbar buttons array.
  93. * @param {Array} keyValuePair - Key value pair containing button and its
  94. * key.
  95. * @param {number} index - Index of the key value pair in the array.
  96. * @returns {Array} Array of toolbar buttons and splitter if it's on.
  97. * @private
  98. */
  99. _renderToolbarButton(acc: Array<*>, keyValuePair: Array<*>,
  100. index: number): Array<ReactElement<*>> {
  101. const [ key, button ] = keyValuePair;
  102. const { splitterIndex } = this.props;
  103. if (splitterIndex && index === splitterIndex) {
  104. const splitter = <span className = 'toolbar__splitter' />;
  105. acc.push(splitter);
  106. }
  107. const { onClick, onMount, onUnmount } = button;
  108. acc.push(
  109. <ToolbarButton
  110. button = { button }
  111. key = { key }
  112. onClick = { onClick }
  113. onMount = { onMount }
  114. onUnmount = { onUnmount } />
  115. );
  116. return acc;
  117. }
  118. /**
  119. * Sets handlers for some of the buttons.
  120. *
  121. * @private
  122. * @returns {void}
  123. */
  124. _setButtonHandlers(): void {
  125. const {
  126. buttonHandlers,
  127. toolbarButtons
  128. } = this.props;
  129. Object.keys(buttonHandlers).forEach(key => {
  130. let button = toolbarButtons.get(key);
  131. if (button) {
  132. button = {
  133. ...button,
  134. ...buttonHandlers[key]
  135. };
  136. toolbarButtons.set(key, button);
  137. }
  138. });
  139. }
  140. }
  141. /**
  142. * Maps part of Redux actions to component's props.
  143. *
  144. * @param {Function} dispatch - Redux action dispatcher.
  145. * @returns {Object}
  146. * @private
  147. */
  148. function _mapDispatchToProps(dispatch: Function): Object {
  149. return {
  150. /**
  151. * Dispatches an action signalling that toolbar is no being hovered.
  152. *
  153. * @protected
  154. * @returns {Object} Dispatched action.
  155. */
  156. _onMouseOut() {
  157. return dispatch(setToolbarHovered(false));
  158. },
  159. /**
  160. * Dispatches an action signalling that toolbar is now being hovered.
  161. *
  162. * @protected
  163. * @returns {Object} Dispatched action.
  164. */
  165. _onMouseOver() {
  166. return dispatch(setToolbarHovered(true));
  167. }
  168. };
  169. }
  170. export default connect(null, _mapDispatchToProps)(BaseToolbar);