2018-10-25 05:36:06 +00:00
|
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import { Button, Modal, ModalHeader, ModalFooter, ModalBody, Alert } from 'reactstrap';
|
2018-11-28 04:41:49 +00:00
|
|
|
|
import { gettext } from '../../utils/constants';
|
2018-10-25 05:36:06 +00:00
|
|
|
|
import { Utils } from '../../utils/utils';
|
|
|
|
|
import FileChooser from '../file-chooser/file-chooser';
|
|
|
|
|
|
|
|
|
|
const propTypes = {
|
2018-11-23 12:19:42 +00:00
|
|
|
|
path: PropTypes.string.isRequired,
|
2018-11-28 04:41:49 +00:00
|
|
|
|
repoID: PropTypes.string.isRequired,
|
2018-11-23 12:19:42 +00:00
|
|
|
|
dirent: PropTypes.object,
|
2018-11-27 06:47:19 +00:00
|
|
|
|
selectedDirentList: PropTypes.array,
|
2018-11-23 12:19:42 +00:00
|
|
|
|
isMutipleOperation: PropTypes.bool.isRequired,
|
2018-11-27 06:47:19 +00:00
|
|
|
|
onItemCopy: PropTypes.func,
|
|
|
|
|
onItemsCopy: PropTypes.func,
|
2018-10-25 05:36:06 +00:00
|
|
|
|
onCancelCopy: PropTypes.func.isRequired,
|
2019-03-20 03:04:36 +00:00
|
|
|
|
repoEncrypted: PropTypes.bool.isRequired,
|
2018-10-25 05:36:06 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// need dirent file Path;
|
|
|
|
|
class CopyDirent extends React.Component {
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
repo: null,
|
2018-11-23 12:19:42 +00:00
|
|
|
|
selectedPath: '',
|
2018-10-25 05:36:06 +00:00
|
|
|
|
errMessage: '',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
|
if (this.state.errMessage === nextState.errMessage) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
2018-11-23 12:19:42 +00:00
|
|
|
|
if (this.props.isMutipleOperation) {
|
|
|
|
|
this.copyItems();
|
|
|
|
|
} else {
|
|
|
|
|
this.copyItem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copyItems = () => {
|
2019-01-23 08:25:14 +00:00
|
|
|
|
let { repoID } = this.props;
|
|
|
|
|
let { repo, selectedPath } = this.state;
|
2018-11-23 12:19:42 +00:00
|
|
|
|
let message = gettext('Invalid destination path');
|
|
|
|
|
|
2018-11-27 06:47:19 +00:00
|
|
|
|
if (!repo || selectedPath === '') {
|
2018-11-23 12:19:42 +00:00
|
|
|
|
this.setState({errMessage: message});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let selectedDirentList = this.props.selectedDirentList;
|
|
|
|
|
let direntPaths = [];
|
|
|
|
|
selectedDirentList.forEach(dirent => {
|
|
|
|
|
let path = Utils.joinPath(this.props.path, dirent.name);
|
|
|
|
|
direntPaths.push(path);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// copy dirents to one of them. eg: A/B, A/C -> A/B
|
2018-11-27 06:47:19 +00:00
|
|
|
|
if (direntPaths.some(direntPath => { return direntPath === selectedPath;})) {
|
2018-11-23 12:19:42 +00:00
|
|
|
|
this.setState({errMessage: message});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// copy dirents to one of their child. eg: A/B, A/D -> A/B/C
|
|
|
|
|
let copyDirentPath = '';
|
|
|
|
|
let isChildPath = direntPaths.some(direntPath => {
|
|
|
|
|
let flag = selectedPath.length > direntPath.length && selectedPath.indexOf(direntPath) > -1;
|
|
|
|
|
if (flag) {
|
|
|
|
|
copyDirentPath = direntPath;
|
|
|
|
|
}
|
|
|
|
|
return flag;
|
2018-11-27 06:47:19 +00:00
|
|
|
|
});
|
2018-11-23 12:19:42 +00:00
|
|
|
|
|
|
|
|
|
if (isChildPath) {
|
|
|
|
|
message = gettext('Can not move directory %(src)s to its subdirectory %(des)s');
|
|
|
|
|
message = message.replace('%(src)s', copyDirentPath);
|
|
|
|
|
message = message.replace('%(des)s', selectedPath);
|
|
|
|
|
this.setState({errMessage: message});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.props.onItemsCopy(repo, selectedPath);
|
|
|
|
|
this.toggle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copyItem = () => {
|
2018-11-28 04:41:49 +00:00
|
|
|
|
let { repo, repoID, selectedPath } = this.state;
|
2018-11-27 06:47:19 +00:00
|
|
|
|
let direntPath = Utils.joinPath(this.props.path, this.props.dirent.name);
|
2018-10-25 05:36:06 +00:00
|
|
|
|
let message = 'Invalid destination path';
|
|
|
|
|
|
2018-11-23 12:19:42 +00:00
|
|
|
|
if (!repo || (repo.repo_id === repoID && selectedPath === '')) {
|
2018-10-25 05:36:06 +00:00
|
|
|
|
this.setState({errMessage: message});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 12:19:42 +00:00
|
|
|
|
// copy the dirent to itself. eg: A/B -> A/B
|
|
|
|
|
if (selectedPath && direntPath === selectedPath) {
|
2018-10-25 05:36:06 +00:00
|
|
|
|
this.setState({errMessage: message});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-31 04:13:24 +00:00
|
|
|
|
|
2018-11-23 12:19:42 +00:00
|
|
|
|
// copy the dirent to current path
|
2018-11-28 04:41:49 +00:00
|
|
|
|
if (selectedPath && this.props.path === selectedPath && repo.repo_id === repoID) {
|
2018-10-25 05:36:06 +00:00
|
|
|
|
this.setState({errMessage: message});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 12:19:42 +00:00
|
|
|
|
// copy the dirent to it's child. eg: A/B -> A/B/C
|
|
|
|
|
if ( selectedPath && selectedPath.length > direntPath.length && selectedPath.indexOf(direntPath) > -1) {
|
2018-10-25 06:42:53 +00:00
|
|
|
|
message = gettext('Can not copy directory %(src)s to its subdirectory %(des)s');
|
2018-10-25 05:36:06 +00:00
|
|
|
|
message = message.replace('%(src)s', direntPath);
|
2018-11-23 12:19:42 +00:00
|
|
|
|
message = message.replace('%(des)s', selectedPath);
|
2018-10-25 05:36:06 +00:00
|
|
|
|
this.setState({errMessage: message});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 03:04:36 +00:00
|
|
|
|
this.props.onItemCopy(repo, this.props.dirent, selectedPath, this.props.path);
|
2018-10-25 05:36:06 +00:00
|
|
|
|
this.toggle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggle = () => {
|
|
|
|
|
this.props.onCancelCopy();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 12:19:42 +00:00
|
|
|
|
onDirentItemClick = (repo, selectedPath) => {
|
2018-10-25 05:36:06 +00:00
|
|
|
|
this.setState({
|
|
|
|
|
repo: repo,
|
2018-11-23 12:19:42 +00:00
|
|
|
|
selectedPath: selectedPath,
|
2018-10-25 05:36:06 +00:00
|
|
|
|
errMessage: '',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onRepoItemClick = (repo) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
repo: repo,
|
2018-11-23 12:19:42 +00:00
|
|
|
|
selectedPath: '/',
|
2018-10-25 05:36:06 +00:00
|
|
|
|
errMessage: ''
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-05-27 09:13:15 +00:00
|
|
|
|
let title = gettext('Copy {placeholder} to');
|
2018-11-23 12:19:42 +00:00
|
|
|
|
if (!this.props.isMutipleOperation) {
|
2019-02-12 02:50:02 +00:00
|
|
|
|
title = title.replace('{placeholder}', '<span class="op-target">' + Utils.HTMLescape(this.props.dirent.name) + '</span>');
|
2018-11-23 12:19:42 +00:00
|
|
|
|
} else {
|
2018-11-27 06:47:19 +00:00
|
|
|
|
title = gettext('Copy selected item(s) to:');
|
2018-11-23 12:19:42 +00:00
|
|
|
|
}
|
2019-04-17 10:21:00 +00:00
|
|
|
|
let mode = this.props.repoEncrypted ? 'only_current_library':'current_repo_and_other_repos';
|
2018-10-25 05:36:06 +00:00
|
|
|
|
return (
|
|
|
|
|
<Modal isOpen={true} toggle={this.toggle}>
|
|
|
|
|
<ModalHeader toggle={this.toggle}><div dangerouslySetInnerHTML={{__html: title}}></div></ModalHeader>
|
|
|
|
|
<ModalBody>
|
|
|
|
|
<FileChooser
|
2018-11-28 04:41:49 +00:00
|
|
|
|
repoID={this.props.repoID}
|
2018-10-25 05:36:06 +00:00
|
|
|
|
onDirentItemClick={this.onDirentItemClick}
|
|
|
|
|
onRepoItemClick={this.onRepoItemClick}
|
2019-03-08 09:38:28 +00:00
|
|
|
|
mode={mode}
|
2018-10-25 05:36:06 +00:00
|
|
|
|
/>
|
|
|
|
|
{this.state.errMessage && <Alert color="danger" style={{margin: '0.5rem'}}>{this.state.errMessage}</Alert>}
|
|
|
|
|
</ModalBody>
|
|
|
|
|
<ModalFooter>
|
|
|
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
2018-12-22 07:18:53 +00:00
|
|
|
|
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button>
|
2018-10-25 05:36:06 +00:00
|
|
|
|
</ModalFooter>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CopyDirent.propTypes = propTypes;
|
|
|
|
|
|
|
|
|
|
export default CopyDirent;
|