1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-03 08:11:08 +00:00
seahub/frontend/src/components/toolbar/dir-operation-toolbar.js

237 lines
7.6 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { Utils } from '../../utils/utils';
2019-02-20 03:54:25 +00:00
import { gettext } from '../../utils/constants';
import ModalPortal from '../modal-portal';
import CreateFolder from '../../components/dialog/create-folder-dialog';
import CreateFile from '../../components/dialog/create-file-dialog';
import ShareDialog from '../../components/dialog/share-dialog';
const propTypes = {
path: PropTypes.string.isRequired,
repoID: PropTypes.string.isRequired,
repoName: PropTypes.string.isRequired,
repoEncrypted: PropTypes.bool.isRequired,
enableDirPrivateShare: PropTypes.bool.isRequired,
userPerm: PropTypes.string.isRequired,
isGroupOwnedRepo: PropTypes.bool.isRequired,
showShareBtn: PropTypes.bool.isRequired,
onAddFile: PropTypes.func.isRequired,
onAddFolder: PropTypes.func.isRequired,
onUploadFile: PropTypes.func.isRequired,
onUploadFolder: PropTypes.func.isRequired,
2019-01-25 07:44:04 +00:00
direntList: PropTypes.array.isRequired,
};
class DirOperationToolbar extends React.Component {
constructor(props) {
super(props);
this.state = {
fileType: '.md',
isCreateFileDialogShow: false,
isCreateFolderDialogShow: false,
isUploadMenuShow: false,
isCreateMenuShow: false,
isShareDialogShow: false,
operationMenuStyle: '',
};
}
componentDidMount() {
document.addEventListener('click', this.hideOperationMenu);
}
componentWillUnmount() {
document.removeEventListener('click', this.hideOperationMenu);
}
hideOperationMenu = () => {
this.setState({
isUploadMenuShow: false,
isCreateMenuShow: false,
});
}
toggleOperationMenu = (e) => {
e.nativeEvent.stopImmediatePropagation();
let targetRect = e.target.getClientRects()[0];
let left = targetRect.x;
let top = targetRect.y + targetRect.height;
let style = {position: 'fixed', display: 'block', left: left, top: top};
this.setState({operationMenuStyle: style});
}
onUploadClick = (e) => {
this.toggleOperationMenu(e);
this.setState({
isUploadMenuShow: true,
isCreateMenuShow: false,
});
}
2018-12-25 02:46:49 +00:00
onUploadFile = (e) => {
this.setState({isUploadMenuShow: false});
this.props.onUploadFile(e);
}
onUploadFolder = (e) => {
this.setState({isUploadMenuShow: false});
this.props.onUploadFolder(e);
}
onCreateClick = (e) => {
this.toggleOperationMenu(e);
this.setState({
isCreateMenuShow: true,
isUploadMenuShow: false,
});
}
onShareClick = (e) => {
e.nativeEvent.stopImmediatePropagation(); //for document event
this.setState({
isShareDialogShow: !this.state.isShareDialogShow
});
}
onCreateFolderToggle = () => {
this.setState({isCreateFolderDialogShow: !this.state.isCreateFolderDialogShow});
}
onCreateFileToggle = () => {
this.setState({
isCreateFileDialogShow: !this.state.isCreateFileDialogShow,
fileType: '',
});
}
onCreateMarkdownToggle = () => {
this.setState({
isCreateFileDialogShow: !this.state.isCreateFileDialogShow,
fileType: '.md'
});
}
onCreateExcelToggle = () => {
this.setState({
isCreateFileDialogShow: !this.state.isCreateFileDialogShow,
fileType: '.xlsx'
});
}
onCreatePPTToggle = () => {
this.setState({
isCreateFileDialogShow: !this.state.isCreateFileDialogShow,
fileType: '.pptx'
});
}
onCreateWordToggle = () => {
this.setState({
isCreateFileDialogShow: !this.state.isCreateFileDialogShow,
fileType: '.docx'
});
}
onAddFile = (filePath, isDraft) => {
this.setState({isCreateFileDialogShow: false});
this.props.onAddFile(filePath, isDraft);
}
onAddFolder = (dirPath) => {
this.setState({isCreateFolderDialogShow: false});
this.props.onAddFolder(dirPath);
}
2019-01-25 07:44:04 +00:00
checkDuplicatedName = (newName) => {
let direntList = this.props.direntList;
let isDuplicated = direntList.some(object => {
return object.name === newName;
});
return isDuplicated;
}
2018-12-17 13:38:55 +00:00
render() {
2019-03-07 09:31:52 +00:00
let { path, repoName, userPerm } = this.props;
if (userPerm !== 'rw' && userPerm !== 'admin') {
return '';
}
let itemType = path === '/' ? 'library' : 'dir';
2019-02-20 03:54:25 +00:00
let itemName = path == '/' ? repoName : Utils.getFolderName(path);
return (
<Fragment>
<div className="operation">
2019-02-20 03:54:25 +00:00
{Utils.isSupportUploadFolder() ?
<button className="btn btn-secondary operation-item" title={gettext('Upload')} onClick={this.onUploadClick}>{gettext('Upload')}</button> :
2019-04-02 09:38:08 +00:00
<button className="btn btn-secondary operation-item" title={gettext('Upload')} onClick={this.onUploadFile}>{gettext('Upload')}</button>
2019-02-20 03:54:25 +00:00
}
<button className="btn btn-secondary operation-item" title={gettext('New')} onClick={this.onCreateClick}>{gettext('New')}</button>
{this.props.showShareBtn &&
2018-12-16 12:45:26 +00:00
<button className="btn btn-secondary operation-item" title={gettext('Share')} onClick={this.onShareClick}>{gettext('Share')}</button>
}
</div>
{this.state.isUploadMenuShow && (
<ul className="menu dropdown-menu" style={this.state.operationMenuStyle}>
2019-01-28 08:35:45 +00:00
<li className="dropdown-item" onClick={this.onUploadFile}>{gettext('Upload Files')}</li>
<li className="dropdown-item" onClick={this.onUploadFolder}>{gettext('Upload Folder')}</li>
</ul>
)}
{this.state.isCreateMenuShow && (
<ul className="menu dropdown-menu" style={this.state.operationMenuStyle}>
<li className="dropdown-item" onClick={this.onCreateFolderToggle}>{gettext('New Folder')}</li>
<li className="dropdown-item" onClick={this.onCreateFileToggle}>{gettext('New File')}</li>
<li className="dropdown-divider"></li>
<li className="dropdown-item" onClick={this.onCreateMarkdownToggle}>{gettext('New Markdown File')}</li>
<li className="dropdown-item" onClick={this.onCreateExcelToggle}>{gettext('New Excel File')}</li>
<li className="dropdown-item" onClick={this.onCreatePPTToggle}>{gettext('New PowerPoint File')}</li>
<li className="dropdown-item" onClick={this.onCreateWordToggle}>{gettext('New Word File')}</li>
</ul>
)}
{this.state.isCreateFileDialogShow && (
<ModalPortal>
<CreateFile
parentPath={this.props.path}
fileType={this.state.fileType}
onAddFile={this.onAddFile}
2019-01-25 07:44:04 +00:00
checkDuplicatedName={this.checkDuplicatedName}
addFileCancel={this.onCreateFileToggle}
/>
</ModalPortal>
)}
{this.state.isCreateFolderDialogShow && (
<ModalPortal>
<CreateFolder
parentPath={this.props.path}
onAddFolder={this.onAddFolder}
2019-01-25 07:44:04 +00:00
checkDuplicatedName={this.checkDuplicatedName}
addFolderCancel={this.onCreateFolderToggle}
/>
</ModalPortal>
)}
{this.state.isShareDialogShow &&
<ModalPortal>
<ShareDialog
2018-12-17 13:38:55 +00:00
itemType={itemType}
2018-12-12 02:34:58 +00:00
itemName={itemName}
itemPath={this.props.path}
repoID={this.props.repoID}
2019-01-29 02:06:26 +00:00
repoEncrypted={this.props.repoEncrypted}
enableDirPrivateShare={this.props.enableDirPrivateShare}
userPerm={this.props.userPerm}
isGroupOwnedRepo={this.props.isGroupOwnedRepo}
toggleDialog={this.onShareClick}
/>
</ModalPortal>
}
</Fragment>
);
}
}
DirOperationToolbar.propTypes = propTypes;
export default DirOperationToolbar;