2019-04-18 14:36:07 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { gettext } from '../../utils/constants';
|
2024-09-04 07:11:49 +00:00
|
|
|
import { Utils, validateName } from '../../utils/utils';
|
2024-12-24 03:20:40 +00:00
|
|
|
import { Button, Modal, Input, ModalBody, ModalFooter, Alert } from 'reactstrap';
|
|
|
|
import SeahubModalHeader from '@/components/common/seahub-modal-header';
|
2019-04-18 14:36:07 +00:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
onRename: PropTypes.func.isRequired,
|
|
|
|
toggleCancel: PropTypes.func.isRequired,
|
|
|
|
checkDuplicatedName: PropTypes.func.isRequired,
|
|
|
|
dirent: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Rename extends React.Component {
|
2019-04-19 02:56:37 +00:00
|
|
|
|
2019-04-18 14:36:07 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
newName: '',
|
|
|
|
errMessage: '',
|
2019-05-17 03:09:05 +00:00
|
|
|
isSubmitBtnActive: false,
|
2019-04-18 14:36:07 +00:00
|
|
|
};
|
|
|
|
this.newInput = React.createRef();
|
|
|
|
}
|
|
|
|
|
2023-12-06 09:24:05 +00:00
|
|
|
UNSAFE_componentWillMount() {
|
2024-07-18 03:58:42 +00:00
|
|
|
this.setState({ newName: this.props.dirent.name });
|
2019-04-18 14:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2022-12-24 02:41:34 +00:00
|
|
|
let { dirent } = this.props;
|
2019-04-18 14:36:07 +00:00
|
|
|
this.changeState(dirent);
|
|
|
|
}
|
|
|
|
|
2023-12-06 09:24:05 +00:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2019-04-18 14:36:07 +00:00
|
|
|
this.changeState(nextProps.dirent);
|
|
|
|
}
|
2020-11-02 05:56:35 +00:00
|
|
|
|
2019-04-18 14:36:07 +00:00
|
|
|
handleChange = (e) => {
|
2019-05-17 03:09:05 +00:00
|
|
|
if (!e.target.value.trim()) {
|
2024-07-18 03:58:42 +00:00
|
|
|
this.setState({ isSubmitBtnActive: false });
|
2019-05-17 03:09:05 +00:00
|
|
|
} else {
|
2024-07-18 03:58:42 +00:00
|
|
|
this.setState({ isSubmitBtnActive: true });
|
2019-05-17 03:09:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 03:58:42 +00:00
|
|
|
this.setState({ newName: e.target.value });
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2019-04-18 14:36:07 +00:00
|
|
|
|
|
|
|
handleSubmit = () => {
|
2024-09-04 07:11:49 +00:00
|
|
|
let newName = this.state.newName.trim();
|
|
|
|
let { isValid, errMessage } = validateName(newName);
|
2019-04-18 14:36:07 +00:00
|
|
|
if (!isValid) {
|
2024-09-04 07:11:49 +00:00
|
|
|
this.setState({ errMessage });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let isDuplicated = this.props.checkDuplicatedName(newName);
|
|
|
|
if (isDuplicated) {
|
|
|
|
let errMessage = gettext('The name "{name}" is already taken. Please choose a different name.');
|
|
|
|
errMessage = errMessage.replace('{name}', Utils.HTMLescape(newName));
|
|
|
|
this.setState({ errMessage });
|
|
|
|
return;
|
2019-04-18 14:36:07 +00:00
|
|
|
}
|
2024-09-04 07:11:49 +00:00
|
|
|
this.props.onRename(newName);
|
|
|
|
this.props.toggleCancel();
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2019-04-18 14:36:07 +00:00
|
|
|
|
2023-11-14 12:25:25 +00:00
|
|
|
handleKeyDown = (e) => {
|
2019-04-18 14:36:07 +00:00
|
|
|
if (e.key === 'Enter') {
|
2024-07-17 13:00:10 +00:00
|
|
|
e.preventDefault();
|
2019-04-18 14:36:07 +00:00
|
|
|
this.handleSubmit();
|
2020-11-02 05:56:35 +00:00
|
|
|
}
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2019-04-18 14:36:07 +00:00
|
|
|
|
|
|
|
toggle = () => {
|
|
|
|
this.props.toggleCancel();
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2019-04-18 14:36:07 +00:00
|
|
|
|
|
|
|
changeState = (dirent) => {
|
|
|
|
let name = dirent.name;
|
2024-07-18 03:58:42 +00:00
|
|
|
this.setState({ newName: name });
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2019-04-18 14:36:07 +00:00
|
|
|
|
2022-12-24 02:41:34 +00:00
|
|
|
onAfterModelOpened = () => {
|
2024-07-18 03:54:50 +00:00
|
|
|
const inputElement = this.newInput.current;
|
|
|
|
if (inputElement) {
|
|
|
|
inputElement.focus();
|
|
|
|
const filename = this.state.newName;
|
|
|
|
const lastDotIndex = filename.lastIndexOf('.');
|
|
|
|
if (lastDotIndex > 0) {
|
|
|
|
inputElement.setSelectionRange(0, lastDotIndex);
|
|
|
|
} else {
|
|
|
|
inputElement.select();
|
|
|
|
}
|
2022-12-24 02:41:34 +00:00
|
|
|
}
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2022-12-24 02:41:34 +00:00
|
|
|
|
2019-04-18 14:36:07 +00:00
|
|
|
render() {
|
|
|
|
let type = this.props.dirent.type;
|
|
|
|
return (
|
2022-12-24 02:41:34 +00:00
|
|
|
<Modal isOpen={true} toggle={this.toggle} onOpened={this.onAfterModelOpened}>
|
2024-12-24 03:20:40 +00:00
|
|
|
<SeahubModalHeader toggle={this.toggle}>{type === 'file' ? gettext('Rename File') : gettext('Rename Folder') }</SeahubModalHeader>
|
2019-04-18 14:36:07 +00:00
|
|
|
<ModalBody>
|
2024-07-18 03:58:42 +00:00
|
|
|
<p>{type === 'file' ? gettext('New file name') : gettext('New folder name')}</p>
|
2023-11-14 12:25:25 +00:00
|
|
|
<Input onKeyDown={this.handleKeyDown} innerRef={this.newInput} value={this.state.newName} onChange={this.handleChange} />
|
2019-04-18 14:36:07 +00:00
|
|
|
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
2019-05-17 03:09:05 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
|
2019-04-18 14:36:07 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rename.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default Rename;
|