2017-12-01 21:02:07 +00:00
|
|
|
// @flow
|
2017-02-16 23:02:40 +00:00
|
|
|
|
2017-09-27 21:23:31 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-02-16 23:02:40 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2017-05-31 05:24:34 +00:00
|
|
|
import { setToolbarHovered } from '../actions';
|
2017-07-31 08:02:41 +00:00
|
|
|
|
|
|
|
import StatelessToolbar from './StatelessToolbar';
|
2017-02-16 23:02:40 +00:00
|
|
|
import ToolbarButton from './ToolbarButton';
|
|
|
|
|
|
|
|
/**
|
2017-04-01 05:52:40 +00:00
|
|
|
* Implements a toolbar in React/Web. It is a strip that contains a set of
|
|
|
|
* toolbar items such as buttons. Toolbar is commonly placed inside of a
|
|
|
|
* Toolbox.
|
2017-02-16 23:02:40 +00:00
|
|
|
*
|
2017-04-01 05:52:40 +00:00
|
|
|
* @class Toolbar
|
2017-02-16 23:02:40 +00:00
|
|
|
* @extends Component
|
|
|
|
*/
|
2017-10-24 22:26:56 +00:00
|
|
|
class Toolbar extends Component<*> {
|
2017-02-16 23:02:40 +00:00
|
|
|
/**
|
|
|
|
* Base toolbar component's property types.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static propTypes = {
|
|
|
|
/**
|
|
|
|
* Children of current React component.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
children: PropTypes.element,
|
2017-02-16 23:02:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Toolbar's class name.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
className: PropTypes.string,
|
2017-02-16 23:02:40 +00:00
|
|
|
|
2017-07-26 20:47:53 +00:00
|
|
|
/**
|
|
|
|
* Used to dispatch an action when a button is clicked or on mouse
|
|
|
|
* out/in event.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
dispatch: PropTypes.func,
|
2017-07-26 20:47:53 +00:00
|
|
|
|
2017-02-16 23:02:40 +00:00
|
|
|
/**
|
|
|
|
* Map with toolbar buttons.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
toolbarButtons: PropTypes.instanceOf(Map),
|
2017-04-06 22:40:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates the position of the tooltip.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
tooltipPosition: PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
|
2017-02-16 23:02:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor of Primary toolbar class.
|
|
|
|
*
|
|
|
|
* @param {Object} props - Object containing React component properties.
|
|
|
|
*/
|
2017-07-31 08:02:41 +00:00
|
|
|
constructor(props: Object) {
|
2017-02-16 23:02:40 +00:00
|
|
|
super(props);
|
|
|
|
|
2017-04-06 22:40:10 +00:00
|
|
|
// Bind callbacks to preverse this.
|
2017-07-31 08:02:41 +00:00
|
|
|
this._onMouseOut = this._onMouseOut.bind(this);
|
|
|
|
this._onMouseOver = this._onMouseOver.bind(this);
|
2017-02-16 23:02:40 +00:00
|
|
|
this._renderToolbarButton = this._renderToolbarButton.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
2017-10-24 22:26:56 +00:00
|
|
|
render(): React$Element<*> {
|
2017-07-31 08:02:41 +00:00
|
|
|
const props = {
|
2017-08-04 08:15:11 +00:00
|
|
|
className: this.props.className,
|
2017-07-31 08:02:41 +00:00
|
|
|
onMouseOut: this._onMouseOut,
|
2017-08-04 08:15:11 +00:00
|
|
|
onMouseOver: this._onMouseOver
|
2017-07-31 08:02:41 +00:00
|
|
|
};
|
2017-02-16 23:02:40 +00:00
|
|
|
|
|
|
|
return (
|
2017-07-31 08:02:41 +00:00
|
|
|
<StatelessToolbar { ...props }>
|
2017-02-16 23:02:40 +00:00
|
|
|
{
|
|
|
|
[ ...this.props.toolbarButtons.entries() ]
|
2017-12-01 21:02:07 +00:00
|
|
|
.map(this._renderToolbarButton)
|
2017-02-16 23:02:40 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
this.props.children
|
|
|
|
}
|
2017-07-31 08:02:41 +00:00
|
|
|
</StatelessToolbar>
|
2017-02-16 23:02:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:02:07 +00:00
|
|
|
_onMouseOut: () => void;
|
|
|
|
|
2017-07-26 20:47:53 +00:00
|
|
|
/**
|
|
|
|
* Dispatches an action signalling that toolbar is no being hovered.
|
|
|
|
*
|
|
|
|
* @protected
|
2017-12-01 21:02:07 +00:00
|
|
|
* @returns {void}
|
2017-07-26 20:47:53 +00:00
|
|
|
*/
|
|
|
|
_onMouseOut() {
|
|
|
|
this.props.dispatch(setToolbarHovered(false));
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:02:07 +00:00
|
|
|
_onMouseOver: () => void;
|
|
|
|
|
2017-07-26 20:47:53 +00:00
|
|
|
/**
|
|
|
|
* Dispatches an action signalling that toolbar is now being hovered.
|
|
|
|
*
|
|
|
|
* @protected
|
2017-12-01 21:02:07 +00:00
|
|
|
* @returns {void}
|
2017-07-26 20:47:53 +00:00
|
|
|
*/
|
|
|
|
_onMouseOver() {
|
|
|
|
this.props.dispatch(setToolbarHovered(true));
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:02:07 +00:00
|
|
|
_renderToolbarButton: (Array<*>) => React$Element<*>;
|
|
|
|
|
2017-02-16 23:02:40 +00:00
|
|
|
/**
|
2017-07-31 08:02:41 +00:00
|
|
|
* Renders toolbar button. Method is passed to map function.
|
2017-02-16 23:02:40 +00:00
|
|
|
*
|
|
|
|
* @param {Array} keyValuePair - Key value pair containing button and its
|
|
|
|
* key.
|
|
|
|
* @private
|
2017-07-31 08:02:41 +00:00
|
|
|
* @returns {ReactElement} A toolbar button.
|
2017-02-16 23:02:40 +00:00
|
|
|
*/
|
2017-12-01 21:02:07 +00:00
|
|
|
_renderToolbarButton([ key, button ]): React$Element<*> {
|
|
|
|
const { tooltipPosition } = this.props;
|
2017-04-05 15:14:26 +00:00
|
|
|
|
|
|
|
if (button.component) {
|
2017-07-31 08:02:41 +00:00
|
|
|
return (
|
2017-04-05 15:14:26 +00:00
|
|
|
<button.component
|
|
|
|
key = { key }
|
2017-09-22 22:02:34 +00:00
|
|
|
toggled = { button.toggled }
|
2017-12-01 21:02:07 +00:00
|
|
|
tooltipPosition = { tooltipPosition } />
|
2017-04-05 15:14:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-22 22:02:34 +00:00
|
|
|
const {
|
|
|
|
childComponent: ChildComponent,
|
|
|
|
onClick,
|
|
|
|
onMount,
|
|
|
|
onUnmount
|
|
|
|
} = button;
|
2017-08-04 08:15:11 +00:00
|
|
|
const onClickWithDispatch = (...args) =>
|
|
|
|
onClick && onClick(this.props.dispatch, ...args);
|
2017-02-16 23:02:40 +00:00
|
|
|
|
2017-07-31 08:02:41 +00:00
|
|
|
return (
|
2017-02-16 23:02:40 +00:00
|
|
|
<ToolbarButton
|
|
|
|
button = { button }
|
|
|
|
key = { key }
|
2017-12-01 21:02:07 +00:00
|
|
|
|
|
|
|
// TODO The following disables an eslint error alerting about a
|
|
|
|
// known potential/theoretical performance pernalty:
|
|
|
|
//
|
|
|
|
// A bind call or arrow function in a JSX prop will create a
|
|
|
|
// brand new function on every single render. This is bad for
|
|
|
|
// performance, as it will result in the garbage collector being
|
|
|
|
// invoked way more than is necessary. It may also cause
|
|
|
|
// unnecessary re-renders if a brand new function is passed as a
|
|
|
|
// prop to a component that uses reference equality check on the
|
|
|
|
// prop to determine if it should update.
|
|
|
|
//
|
|
|
|
// I'm not addressing the potential/theoretical performance
|
|
|
|
// penalty at the time of this writing because I don't know for
|
|
|
|
// a fact that it's a practical performance penalty in the case.
|
|
|
|
//
|
|
|
|
// eslint-disable-next-line react/jsx-no-bind
|
2017-08-04 08:15:11 +00:00
|
|
|
onClick = { onClickWithDispatch }
|
2017-02-16 23:02:40 +00:00
|
|
|
onMount = { onMount }
|
2017-04-06 22:40:10 +00:00
|
|
|
onUnmount = { onUnmount }
|
2017-09-22 22:02:34 +00:00
|
|
|
tooltipPosition = { tooltipPosition }>
|
|
|
|
{ button.html || null }
|
|
|
|
{ ChildComponent ? <ChildComponent /> : null }
|
|
|
|
</ToolbarButton>
|
2017-02-16 23:02:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:47:53 +00:00
|
|
|
export default connect()(Toolbar);
|