2018-10-25 13:36:06 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-11-01 18:40:18 +08:00
|
|
|
import { isPro, enableFileComment, fileAuditEnabled, folderPermEnabled} from '../../utils/constants';
|
2018-10-25 13:36:06 +08:00
|
|
|
import DirentMenuItem from './dirent-menu-item';
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
dirent: PropTypes.object.isRequired,
|
|
|
|
menuPosition: PropTypes.object.isRequired,
|
|
|
|
onMenuItemClick: PropTypes.func.isRequired,
|
2018-11-01 18:40:18 +08:00
|
|
|
currentRepo: PropTypes.object.isRequired,
|
|
|
|
isRepoOwner: PropTypes.bool.isRequired,
|
2018-10-25 13:36:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DirentMenu extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
menuList: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-11-01 18:40:18 +08:00
|
|
|
let repo = this.props.currentRepo;
|
|
|
|
let menuList = this.calculateMenuList(repo);
|
|
|
|
this.setState({
|
|
|
|
menuList: menuList
|
2018-10-25 13:36:06 +08:00
|
|
|
});
|
2018-11-01 18:40:18 +08:00
|
|
|
|
2018-10-25 13:36:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
calculateMenuList(repoInfo) {
|
|
|
|
let dirent = this.props.dirent;
|
2018-11-01 18:40:18 +08:00
|
|
|
let isRepoOwner = this.props.isRepoOwner;
|
2018-10-25 13:36:06 +08:00
|
|
|
let type = dirent.type;
|
|
|
|
let permission = dirent.permission;
|
2018-11-01 18:40:18 +08:00
|
|
|
let can_set_folder_perm = folderPermEnabled && ((isRepoOwner && repoInfo.has_been_shared_out) || repoInfo.is_admin);
|
2018-10-25 13:36:06 +08:00
|
|
|
if (type === 'dir' && permission === 'rw') {
|
|
|
|
let menuList = [];
|
|
|
|
if (can_set_folder_perm) {
|
|
|
|
menuList = ['Rename', 'Move', 'Copy', 'Divider', 'Permission', 'Details', 'Divider', 'Open via Client'];
|
|
|
|
} else {
|
|
|
|
menuList = ['Rename', 'Move', 'Copy', 'Divider', 'Details', 'Divider', 'Open via Client'];
|
|
|
|
}
|
|
|
|
return menuList;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'dir' && permission === 'r') {
|
|
|
|
let menuList = repoInfo.encrypted ? ['Copy', 'Details'] : ['Details'];
|
|
|
|
return menuList;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'file' && permission === 'rw') {
|
|
|
|
let menuList = [];
|
|
|
|
if (!dirent.is_locked || (dirent.is_locked && dirent.locked_by_me)) {
|
|
|
|
menuList.push('Rename');
|
|
|
|
menuList.push('Move');
|
|
|
|
}
|
|
|
|
menuList.push('Copy');
|
|
|
|
if (isPro) {
|
|
|
|
if (dirent.is_locked && dirent.locked_by_me) {
|
|
|
|
menuList.push('Unlock');
|
|
|
|
} else {
|
|
|
|
menuList.push('Lock');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menuList.push('New Draft');
|
|
|
|
menuList.push('Divider');
|
|
|
|
if (enableFileComment) {
|
|
|
|
menuList.push('Comment');
|
|
|
|
}
|
|
|
|
menuList.push('History');
|
|
|
|
if (fileAuditEnabled) {
|
|
|
|
menuList.push('Access Log');
|
|
|
|
}
|
|
|
|
menuList.push('Details');
|
|
|
|
menuList.push('Divider');
|
|
|
|
menuList.push('Open via Client');
|
|
|
|
return menuList;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'file' && permission === 'r') {
|
|
|
|
let menuList = [];
|
|
|
|
if (!repoInfo.encrypted) {
|
|
|
|
menuList.push('Copy');
|
|
|
|
}
|
|
|
|
if (enableFileComment) {
|
|
|
|
menuList.push('Comment');
|
|
|
|
}
|
|
|
|
menuList.push('History');
|
|
|
|
menuList.push('Details');
|
|
|
|
return menuList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMenuItemClick = (operation) => {
|
|
|
|
this.props.onMenuItemClick(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let position = this.props.menuPosition;
|
|
|
|
let style = {position: 'fixed', left: position.left, top: position.top, display: 'block'};
|
|
|
|
if (this.state.menuList.length) {
|
|
|
|
return (
|
|
|
|
<ul className="dropdown-menu operation-menu" style={style}>
|
|
|
|
{this.state.menuList.map((item, index) => {
|
|
|
|
return (
|
|
|
|
<DirentMenuItem key={index} item={item} onItemClick={this.onMenuItemClick}/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DirentMenu.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default DirentMenu;
|