2017-02-16 23:02:40 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-07-31 08:02:41 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-02-16 23:02:40 +00:00
|
|
|
|
|
|
|
import { translate } from '../../base/i18n';
|
|
|
|
|
2017-07-26 21:00:10 +00:00
|
|
|
import {
|
|
|
|
setTooltip,
|
|
|
|
setTooltipText
|
|
|
|
} from '../../../../modules/UI/util/Tooltip';
|
2017-02-16 23:02:40 +00:00
|
|
|
|
2017-07-31 08:02:41 +00:00
|
|
|
import StatelessToolbarButton from './StatelessToolbarButton';
|
2017-02-16 23:02:40 +00:00
|
|
|
|
|
|
|
declare var APP: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a button in Toolbar on React.
|
|
|
|
*
|
|
|
|
* @class ToolbarButton
|
|
|
|
* @extends AbstractToolbarButton
|
|
|
|
*/
|
2017-07-31 08:02:41 +00:00
|
|
|
class ToolbarButton extends Component {
|
|
|
|
button: Object;
|
2017-02-16 23:02:40 +00:00
|
|
|
_createRefToButton: Function;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toolbar button component's property types.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static propTypes = {
|
2017-07-31 08:02:41 +00:00
|
|
|
...StatelessToolbarButton.propTypes,
|
2017-02-16 23:02:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Object describing button.
|
|
|
|
*/
|
|
|
|
button: React.PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for component mount.
|
|
|
|
*/
|
|
|
|
onMount: React.PropTypes.func,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for component unmount.
|
|
|
|
*/
|
|
|
|
onUnmount: React.PropTypes.func,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translation helper function.
|
|
|
|
*/
|
2017-04-06 22:40:10 +00:00
|
|
|
t: React.PropTypes.func,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates the position of the tooltip.
|
|
|
|
*/
|
|
|
|
tooltipPosition:
|
|
|
|
React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
|
2017-02-16 23:02:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes new ToolbarButton instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props: Object) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
// Bind methods to save the context
|
|
|
|
this._createRefToButton = this._createRefToButton.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets shortcut/tooltip
|
|
|
|
* after mounting of the component.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentDidMount(): void {
|
|
|
|
this._setShortcutAndTooltip();
|
|
|
|
|
|
|
|
if (this.props.onMount) {
|
|
|
|
this.props.onMount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes on unmount handler if it was passed to the props.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentWillUnmount(): void {
|
|
|
|
if (this.props.onUnmount) {
|
|
|
|
this.props.onUnmount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render(): ReactElement<*> {
|
|
|
|
const { button } = this.props;
|
|
|
|
const popups = button.popups || [];
|
|
|
|
|
2017-07-31 08:02:41 +00:00
|
|
|
const props = {
|
|
|
|
...this.props,
|
|
|
|
createRefToButton: this._createRefToButton
|
|
|
|
};
|
|
|
|
|
2017-02-16 23:02:40 +00:00
|
|
|
return (
|
2017-07-31 08:02:41 +00:00
|
|
|
<StatelessToolbarButton { ...props }>
|
2017-02-16 23:02:40 +00:00
|
|
|
{ this._renderPopups(popups) }
|
2017-07-31 08:02:41 +00:00
|
|
|
</StatelessToolbarButton>
|
2017-02-16 23:02:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates reference to current toolbar button.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element - HTMLElement representing the toolbar
|
|
|
|
* button.
|
|
|
|
* @returns {void}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_createRefToButton(element: HTMLElement): void {
|
|
|
|
this.button = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If toolbar button should contain children elements
|
|
|
|
* renders them.
|
|
|
|
*
|
|
|
|
* @returns {ReactElement|null}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_renderInnerElementsIfRequired(): ReactElement<*> | null {
|
|
|
|
if (this.props.button.html) {
|
|
|
|
return this.props.button.html;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders popup element for toolbar button.
|
|
|
|
*
|
|
|
|
* @param {Array} popups - Array of popup objects.
|
|
|
|
* @returns {Array}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_renderPopups(popups: Array<*> = []): Array<*> {
|
|
|
|
return popups.map(popup => {
|
|
|
|
let gravity = 'n';
|
|
|
|
|
|
|
|
if (popup.dataAttrPosition) {
|
|
|
|
gravity = popup.dataAttrPosition;
|
|
|
|
}
|
|
|
|
|
2017-04-05 15:14:26 +00:00
|
|
|
const title = this.props.t(popup.dataAttr, popup.dataInterpolate);
|
2017-02-16 23:02:40 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className = { popup.className }
|
|
|
|
data-popup = { gravity }
|
|
|
|
id = { popup.id }
|
|
|
|
key = { popup.id }
|
|
|
|
title = { title } />
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets shortcut and tooltip for current toolbar button.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_setShortcutAndTooltip(): void {
|
2017-04-06 22:40:10 +00:00
|
|
|
const { button, tooltipPosition } = this.props;
|
2017-02-16 23:02:40 +00:00
|
|
|
|
2017-07-26 21:21:52 +00:00
|
|
|
if (!button.unclickable) {
|
|
|
|
if (button.tooltipText) {
|
|
|
|
setTooltipText(this.button,
|
|
|
|
button.tooltipText,
|
|
|
|
tooltipPosition);
|
|
|
|
} else {
|
|
|
|
setTooltip(this.button,
|
|
|
|
button.tooltipKey,
|
|
|
|
tooltipPosition);
|
2017-02-16 23:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 21:21:52 +00:00
|
|
|
|
|
|
|
if (button.shortcut && APP && APP.keyboardshortcut) {
|
|
|
|
APP.keyboardshortcut.registerShortcut(
|
|
|
|
button.shortcut,
|
|
|
|
button.shortcutAttr,
|
|
|
|
button.shortcutFunc,
|
|
|
|
button.shortcutDescription
|
|
|
|
);
|
|
|
|
}
|
2017-02-16 23:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:47:53 +00:00
|
|
|
export default translate(ToolbarButton);
|