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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * Children of current React component.
  24. */
  25. children: React.PropTypes.element,
  26. /**
  27. * Toolbar's class name.
  28. */
  29. className: React.PropTypes.string,
  30. /**
  31. * Used to dispatch an action when a button is clicked or on mouse
  32. * out/in event.
  33. */
  34. dispatch: React.PropTypes.func,
  35. /**
  36. * Map with toolbar buttons.
  37. */
  38. toolbarButtons: React.PropTypes.instanceOf(Map),
  39. /**
  40. * Indicates the position of the tooltip.
  41. */
  42. tooltipPosition:
  43. React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
  44. };
  45. /**
  46. * Constructor of Primary toolbar class.
  47. *
  48. * @param {Object} props - Object containing React component properties.
  49. */
  50. constructor(props) {
  51. super(props);
  52. // Bind callbacks to preverse this.
  53. this._renderToolbarButton = this._renderToolbarButton.bind(this);
  54. }
  55. /**
  56. * Implements React's {@link Component#render()}.
  57. *
  58. * @inheritdoc
  59. * @returns {ReactElement}
  60. */
  61. render(): ReactElement<*> {
  62. const { className } = this.props;
  63. return (
  64. <div
  65. className = { `toolbar ${className}` }
  66. onMouseOut = { this._onMouseOut }
  67. onMouseOver = { this._onMouseOver }>
  68. {
  69. [ ...this.props.toolbarButtons.entries() ]
  70. .reduce(this._renderToolbarButton, [])
  71. }
  72. {
  73. this.props.children
  74. }
  75. </div>
  76. );
  77. }
  78. /**
  79. * Dispatches an action signalling that toolbar is no being hovered.
  80. *
  81. * @protected
  82. * @returns {Object} Dispatched action.
  83. */
  84. _onMouseOut() {
  85. this.props.dispatch(setToolbarHovered(false));
  86. }
  87. /**
  88. * Dispatches an action signalling that toolbar is now being hovered.
  89. *
  90. * @protected
  91. * @returns {Object} Dispatched action.
  92. */
  93. _onMouseOver() {
  94. this.props.dispatch(setToolbarHovered(true));
  95. }
  96. /**
  97. * Renders toolbar button. Method is passed to reduce function.
  98. *
  99. * @param {Array} acc - Toolbar buttons array.
  100. * @param {Array} keyValuePair - Key value pair containing button and its
  101. * key.
  102. * @private
  103. * @returns {Array} Array of toolbar buttons.
  104. */
  105. _renderToolbarButton(acc: Array<*>,
  106. keyValuePair: Array<*>): Array<ReactElement<*>> {
  107. const [ key, button ] = keyValuePair;
  108. if (button.component) {
  109. acc.push(
  110. <button.component
  111. key = { key }
  112. tooltipPosition = { this.props.tooltipPosition } />
  113. );
  114. return acc;
  115. }
  116. const { tooltipPosition } = this.props;
  117. const { onClick, onMount, onUnmount } = button;
  118. const onClickHandler
  119. = (...args) =>
  120. onClick(this.props.dispatch, ...args);
  121. acc.push(
  122. <ToolbarButton
  123. button = { button }
  124. key = { key }
  125. onClick = { onClickHandler }
  126. onMount = { onMount }
  127. onUnmount = { onUnmount }
  128. tooltipPosition = { tooltipPosition } />
  129. );
  130. return acc;
  131. }
  132. }
  133. export default connect()(Toolbar);