Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
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.
jitsi-meet/react/features/toolbox/components/ToolbarButton.web.js

265 lines
6.1 KiB

9 years ago
/* @flow */
import { Tooltip } from '@atlaskit/tooltip';
import React, { Component } from 'react';
9 years ago
import { translate } from '../../base/i18n';
import { isButtonEnabled } from '../functions';
9 years ago
import StatelessToolbarButton from './StatelessToolbarButton';
9 years ago
declare var APP: Object;
/**
* Represents a button in Toolbar on React.
*
* @class ToolbarButton
* @extends AbstractToolbarButton
*/
class ToolbarButton extends Component {
button: Object;
9 years ago
_createRefToButton: Function;
_onClick: Function;
_onMouseOut: Function;
_onMouseOver: Function;
state: {
/**
* Whether or not the tooltip for the button should be displayed.
*
* @type {boolean}
*/
showTooltip: boolean
}
9 years ago
/**
* Toolbar button component's property types.
*
* @static
*/
static propTypes = {
...StatelessToolbarButton.propTypes,
9 years ago
/**
* 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.
*/
t: React.PropTypes.func,
/**
* Indicates the position of the tooltip.
*/
tooltipPosition:
React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
9 years ago
};
/**
* 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);
this.state = {
showTooltip: false
};
9 years ago
// Bind methods to save the context
this._createRefToButton = this._createRefToButton.bind(this);
this._onClick = this._onClick.bind(this);
this._onMouseOut = this._onMouseOut.bind(this);
this._onMouseOver = this._onMouseOver.bind(this);
9 years ago
}
/**
* Sets shortcut/tooltip
* after mounting of the component.
*
* @inheritdoc
* @returns {void}
*/
componentDidMount(): void {
this._setShortcut();
9 years ago
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, t, tooltipPosition } = this.props;
9 years ago
const popups = button.popups || [];
const props = {
...this.props,
onClick: this._onClick,
createRefToButton: this._createRefToButton
};
9 years ago
return (
<Tooltip
description = { button.tooltipText || t(button.tooltipKey) }
onMouseOut = { this._onMouseOut }
onMouseOver = { this._onMouseOver }
position = { tooltipPosition }
visible = { this.state.showTooltip }>
<StatelessToolbarButton { ...props }>
{ this._renderPopups(popups) }
</StatelessToolbarButton>
</Tooltip>
9 years ago
);
}
/**
* Wrapper on on click handler props for current button.
*
* @param {Event} event - Click event object.
* @returns {void}
* @private
*/
_onClick(event) {
this.props.onClick(event);
this.setState({ showTooltip: false });
}
9 years ago
/**
* 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;
}
const title = this.props.t(popup.dataAttr, popup.dataInterpolate);
9 years ago
return (
<div
className = { popup.className }
data-popup = { gravity }
id = { popup.id }
key = { popup.id }
title = { title } />
);
});
}
/**
* Hides any displayed tooltip.
*
* @private
* @returns {void}
*/
_onMouseOut(): void {
this.setState({ showTooltip: false });
}
/**
* Hides any displayed tooltip.
*
* @private
* @returns {void}
*/
_onMouseOver(): void {
const { button } = this.props;
this.setState({
showTooltip: isButtonEnabled(button.buttonName)
&& !button.unclickable
});
}
9 years ago
/**
* Sets shortcut and tooltip for current toolbar button.
*
* @private
* @returns {void}
*/
_setShortcut(): void {
const { button } = this.props;
if (button.shortcut && APP && APP.keyboardshortcut) {
APP.keyboardshortcut.registerShortcut(
button.shortcut,
button.shortcutAttr,
button.shortcutFunc,
button.shortcutDescription
);
}
9 years ago
}
}
export default translate(ToolbarButton);