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

271 lines
8.9 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { Utils } from '../../utils/utils';
2018-11-29 09:55:14 +00:00
import { gettext, siteRoot } from '../../utils/constants';
2018-12-12 02:34:58 +00:00
import { seafileAPI } from '../../utils/seafile-api';
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 = {
2018-11-29 09:55:14 +00:00
isViewFile: PropTypes.bool, // just for view file,
permission: PropTypes.string, //just for view file and premission is file permission
path: PropTypes.string.isRequired,
repoID: PropTypes.string.isRequired,
showShareBtn: PropTypes.bool.isRequired,
onAddFile: PropTypes.func.isRequired,
onAddFolder: PropTypes.func.isRequired,
onUploadFile: PropTypes.func.isRequired,
onUploadFolder: PropTypes.func.isRequired,
2018-12-12 02:34:58 +00:00
isDraft: PropTypes.bool,
hasDraft: PropTypes.bool,
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});
}
onEditClick = (e) => {
e.preventDefault();
2018-11-29 09:55:14 +00:00
let { path, repoID } = this.props;
let url = siteRoot + 'lib/' + repoID + '/file' + path + '?mode=edit';
window.open(url);
}
2018-12-12 02:34:58 +00:00
onNewDraft = (e) => {
e.preventDefault();
let { path, repoID } = this.props;
seafileAPI.createDraft(repoID, path).then(res => {
window.location.href = siteRoot + 'lib/' + res.data.origin_repo_id + '/file' + res.data.draft_file_path + '?mode=edit';
});
}
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);
}
2018-12-12 02:34:58 +00:00
onViewReview = () => {
this.props.goReviewPage();
}
onViewDraft = () => {
this.props.goDraftPage();
}
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-01-29 02:06:26 +00:00
let { path, isViewFile, repoName } = this.props;
2018-12-17 13:38:55 +00:00
let itemType = isViewFile ? 'file' : 'dir';
2019-01-29 02:06:26 +00:00
let itemName = isViewFile ? Utils.getFileName(path) : (path == '/' ? repoName : Utils.getFolderName(path));
return (
<Fragment>
<div className="operation">
2018-12-16 12:45:26 +00:00
{(this.props.isViewFile && this.props.permission === 'rw' && !this.props.hasDraft ) && (
2018-12-12 02:34:58 +00:00
<Fragment>
<button className="btn btn-secondary operation-item" title={gettext('Edit File')} onClick={this.onEditClick}>{gettext('Edit')}</button>
2018-12-12 02:34:58 +00:00
</Fragment>
)}
2018-12-12 02:34:58 +00:00
2018-12-16 12:45:26 +00:00
{(this.props.isViewFile && !this.props.isDraft && !this.props.hasDraft) && (
2018-12-12 02:34:58 +00:00
<button className="btn btn-secondary operation-item" title={gettext('New Draft')} onClick={this.onNewDraft}>{gettext('New Draft')}</button>
)}
{!this.props.isViewFile && (
<Fragment>
{Utils.isSupportUploadFolder() ?
<button className="btn btn-secondary operation-item" title={gettext('Upload')} onClick={this.onUploadClick}>{gettext('Upload')}</button> :
<button className="btn btn-secondary operation-item" title={gettext('Upload')} onClick={this.uploadFile}>{gettext('Upload')}</button>
}
<button className="btn btn-secondary operation-item" title={gettext('New')} onClick={this.onCreateClick}>{gettext('New')}</button>
</Fragment>
)}
{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}
2019-01-25 07:44:04 +00:00
/>
</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}
isAdmin={this.props.isAdmin}
isGroupOwnedRepo={this.props.isGroupOwnedRepo}
toggleDialog={this.onShareClick}
/>
</ModalPortal>
}
</Fragment>
);
}
}
DirOperationToolbar.propTypes = propTypes;
export default DirOperationToolbar;