1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-21 08:25:21 +00:00
seahub/frontend/src/components/dropdown-menu/item-dropdown-menu.js

166 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-04-21 02:43:34 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import listener from '../context-menu/globalEventListener';
2019-04-21 05:57:58 +00:00
import { Dropdown, ButtonDropdown, DropdownMenu, DropdownToggle, DropdownItem } from 'reactstrap';
2019-04-21 02:43:34 +00:00
import { gettext } from '../../utils/constants';
const propTypes = {
tagName: PropTypes.string,
opItem: PropTypes.object.isRequired,
2019-04-21 05:57:58 +00:00
menuType: PropTypes.oneOf(['pc', 'mobile']),
menuClass: PropTypes.string,
2019-04-21 02:43:34 +00:00
isHandleContextMenuEvent: PropTypes.bool,
getOpItemMenuList: PropTypes.func.isRequired,
onMenuItemClick: PropTypes.func.isRequired,
onFrezeedItem: PropTypes.func,
onUnfrezeedItem: PropTypes.func,
};
class ItemDropDownMenu extends React.Component {
2019-04-21 02:43:34 +00:00
static defaultProps = {
2019-04-21 05:57:58 +00:00
isHandleContextMenuEvent: true,
2019-04-21 02:43:34 +00:00
menuType: 'pc',
2019-04-21 05:57:58 +00:00
menuClass: 'sf2-icon-caret-down'
2019-04-21 02:43:34 +00:00
};
constructor(props) {
super(props);
this.state = {
menuList: [],
isItemMenuShow: false,
};
}
componentDidMount() {
if (this.props.isHandleContextMenuEvent) {
this.listenerId = listener.register(this.onShowMenu, this.onHideMenu);
}
let { opItem, menuType } = this.props;
// scene 1: menuType === 'pc', Get some menu operations
// scene 2: menuType === 'mobile', Get all menu operations
let isAllOperations = menuType === 'pc' ? false : true;
let menuList = this.props.getOpItemMenuList(opItem, isAllOperations);
this.setState({menuList: menuList});
}
2019-04-21 05:57:58 +00:00
componentWillReceiveProps(nextProps) { // for toolbar opItem operation
let { opItem, menuType } = nextProps;
if (opItem.name !== this.props.opItem.name) {
let isAllOperations = menuType === 'pc' ? false : true;
let menuList = this.props.getOpItemMenuList(opItem, isAllOperations);
this.setState({menuList: menuList});
}
}
2019-04-21 02:43:34 +00:00
componentWillUnmount() {
if (this.props.isHandleContextMenuEvent && this.listenerId) {
listener.unregister(this.listenerId);
}
}
onShowMenu = () => {
// nothing todo
}
onHideMenu = () => {
if (this.state.isItemMenuShow) {
this.setState({isItemMenuShow: false});
if (typeof(this.props.onUnfreezedItem) === 'function') {
this.props.onUnfreezedItem();
}
}
}
onDropdownToggleClick = (e) => {
e.preventDefault();
e.stopPropagation();
this.toggleOperationMenu();
}
toggleOperationMenu = () => {
this.setState(
{isItemMenuShow: !this.state.isItemMenuShow},
() => {
if (this.state.isItemMenuShow && typeof(this.props.onFreezedItem) === 'function') {
this.props.onFreezedItem();
} else if (!this.state.isItemMenuShow && typeof(this.props.onUnfreezedItem) === 'function') {
this.props.onUnfreezedItem();
}
}
);
}
onMenuItemClick = (event) => {
let operation = event.target.dataset.toggle;
2019-04-21 09:31:28 +00:00
let opItem = this.props.opItem;
this.props.onMenuItemClick(operation, opItem, event);
2019-04-21 02:43:34 +00:00
}
render() {
let menuList = this.state.menuList;
let { menuClass, tagName } = this.props;
menuClass = 'sf-dropdown-toggle ' + menuClass;
if (!menuList.length) {
return '';
}
2019-04-21 05:57:58 +00:00
if (tagName && tagName === 'button') {
return (
<ButtonDropdown isOpen={this.state.isItemMenuShow} toggle={this.onDropdownToggleClick} title={gettext('More Operations')}>
<DropdownToggle
className={menuClass}
data-toggle="dropdown"
title={gettext('More Operations')}
aria-expanded={this.state.isItemMenuShow}
// onClick={this.onDropdownToggleClick}
>
</DropdownToggle>
<DropdownMenu>
{menuList.map((menuItem, index) => {
if (menuItem === 'Divider') {
return <DropdownItem key={index} divider />;
} else {
return (
<DropdownItem key={index} data-toggle={menuItem.key} onClick={this.onMenuItemClick}>{menuItem.value}</DropdownItem>
);
}
})}
</DropdownMenu>
</ButtonDropdown>
);
}
2019-04-21 02:43:34 +00:00
return (
2019-04-21 09:31:28 +00:00
<Dropdown isOpen={this.state.isItemMenuShow} toggle={this.onDropdownToggleClick}>
2019-04-21 02:43:34 +00:00
<DropdownToggle
tag={tagName || 'i'}
className={menuClass}
title={gettext('More Operations')}
data-toggle="dropdown"
aria-expanded={this.state.isItemMenuShow}
// onClick={this.onDropdownToggleClick}
/>
<DropdownMenu>
{menuList.map((menuItem, index) => {
if (menuItem === 'Divider') {
2019-04-21 05:57:58 +00:00
return <DropdownItem key={index} divider />;
2019-04-21 02:43:34 +00:00
} else {
return (
<DropdownItem key={index} data-toggle={menuItem.key} onClick={this.onMenuItemClick}>{menuItem.value}</DropdownItem>
);
}
})}
</DropdownMenu>
</Dropdown>
);
}
}
ItemDropDownMenu.propTypes = propTypes;
2019-04-21 02:43:34 +00:00
export default ItemDropDownMenu;