2018-02-07 19:06:07 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-08-23 06:10:42 +02:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2017-02-22 20:29:35 +01:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import * as IDEActions from '../actions/ide';
|
|
|
|
import * as FileActions from '../actions/files';
|
2020-04-30 00:34:37 +02:00
|
|
|
import DownArrowIcon from '../../../images/down-filled-triangle.svg';
|
|
|
|
import FolderRightIcon from '../../../images/triangle-arrow-right.svg';
|
|
|
|
import FolderDownIcon from '../../../images/triangle-arrow-down.svg';
|
|
|
|
import FileIcon from '../../../images/file.svg';
|
2016-08-23 06:10:42 +02:00
|
|
|
|
|
|
|
export class FileNode extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-09-07 21:48:25 +02:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isOptionsOpen: false,
|
|
|
|
isEditingName: false,
|
2019-03-27 20:52:57 +01:00
|
|
|
isFocused: false,
|
2020-04-16 21:53:32 +02:00
|
|
|
isDeleting: false,
|
2020-04-10 18:42:33 +02:00
|
|
|
updatedName: this.props.name
|
2018-09-07 21:48:25 +02:00
|
|
|
};
|
2016-08-23 19:52:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
onFocusComponent = () => {
|
2019-03-27 20:52:57 +01:00
|
|
|
this.setState({ isFocused: true });
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
onBlurComponent = () => {
|
2019-03-27 20:52:57 +01:00
|
|
|
this.setState({ isFocused: false });
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!this.state.isFocused) {
|
|
|
|
this.hideFileOptions();
|
|
|
|
}
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
|
|
|
|
setUpdatedName = (updatedName) => {
|
|
|
|
this.setState({ updatedName });
|
2020-04-10 18:42:33 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
saveUpdatedFileName = () => {
|
2020-04-10 18:42:33 +02:00
|
|
|
const { updatedName } = this.state;
|
|
|
|
const { name, updateFileName, id } = this.props;
|
|
|
|
|
|
|
|
if (updatedName !== name) {
|
|
|
|
updateFileName(id, updatedName);
|
|
|
|
}
|
2020-04-08 00:47:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
handleFileClick = (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
const { isDeleting } = this.state;
|
|
|
|
const { id, setSelectedFile, name } = this.props;
|
|
|
|
if (name !== 'root' && !isDeleting) {
|
|
|
|
setSelectedFile(id);
|
2016-08-25 00:52:08 +02:00
|
|
|
}
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
handleFileNameChange = (event) => {
|
|
|
|
const newName = event.target.value;
|
|
|
|
this.setUpdatedName(newName);
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
handleFileNameBlur = () => {
|
|
|
|
this.validateFileName();
|
|
|
|
this.hideEditFileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClickRename = () => {
|
|
|
|
this.setUpdatedName(this.props.name);
|
|
|
|
this.showEditFileName();
|
|
|
|
setTimeout(() => this.fileNameInput.focus(), 0);
|
|
|
|
setTimeout(() => this.hideFileOptions(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClickAddFile = () => {
|
|
|
|
this.props.newFile(this.props.id);
|
|
|
|
setTimeout(() => this.hideFileOptions(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClickAddFolder = () => {
|
|
|
|
this.props.newFolder(this.props.id);
|
|
|
|
setTimeout(() => this.hideFileOptions(), 0);
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:08:51 +02:00
|
|
|
handleClickUploadFile = () => {
|
|
|
|
this.props.openUploadFileModal(this.props.id);
|
|
|
|
setTimeout(this.hideFileOptions, 0);
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
handleClickDelete = () => {
|
|
|
|
if (window.confirm(`Are you sure you want to delete ${this.props.name}?`)) {
|
|
|
|
this.setState({ isDeleting: true });
|
|
|
|
this.props.resetSelectedFile(this.props.id);
|
|
|
|
setTimeout(() => this.props.deleteFile(this.props.id, this.props.parentId), 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyPress = (event) => {
|
2016-08-23 06:10:42 +02:00
|
|
|
if (event.key === 'Enter') {
|
2018-09-07 21:48:25 +02:00
|
|
|
this.hideEditFileName();
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
validateFileName = () => {
|
|
|
|
const currentName = this.props.name;
|
|
|
|
const { updatedName } = this.state;
|
|
|
|
const oldFileExtension = currentName.match(/\.[0-9a-z]+$/i);
|
|
|
|
const newFileExtension = updatedName.match(/\.[0-9a-z]+$/i);
|
|
|
|
const hasPeriod = updatedName.match(/\.+/);
|
2019-07-11 08:05:43 +02:00
|
|
|
const hasNoExtension = oldFileExtension && !newFileExtension;
|
2019-07-17 18:30:26 +02:00
|
|
|
const hasExtensionIfFolder = this.props.fileType === 'folder' && hasPeriod;
|
|
|
|
const notSameExtension = oldFileExtension && newFileExtension
|
|
|
|
&& oldFileExtension[0].toLowerCase() !== newFileExtension[0].toLowerCase();
|
2020-04-16 22:22:59 +02:00
|
|
|
const hasEmptyFilename = updatedName.trim() === '';
|
|
|
|
const hasOnlyExtension = newFileExtension && updatedName.trim() === newFileExtension[0];
|
2019-07-17 18:30:26 +02:00
|
|
|
if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension || hasExtensionIfFolder) {
|
2020-04-16 21:53:32 +02:00
|
|
|
this.setUpdatedName(currentName);
|
|
|
|
} else {
|
|
|
|
this.saveUpdatedFileName();
|
|
|
|
}
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
toggleFileOptions = (event) => {
|
|
|
|
event.preventDefault();
|
2019-03-14 21:10:50 +01:00
|
|
|
if (!this.props.canEdit) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-07 21:48:25 +02:00
|
|
|
if (this.state.isOptionsOpen) {
|
|
|
|
this.setState({ isOptionsOpen: false });
|
2017-01-09 18:47:32 +01:00
|
|
|
} else {
|
2017-02-22 20:29:35 +01:00
|
|
|
this[`fileOptions-${this.props.id}`].focus();
|
2018-09-07 21:48:25 +02:00
|
|
|
this.setState({ isOptionsOpen: true });
|
2017-01-09 18:47:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
hideFileOptions = () => {
|
2018-09-07 21:48:25 +02:00
|
|
|
this.setState({ isOptionsOpen: false });
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
showEditFileName = () => {
|
2018-09-07 21:48:25 +02:00
|
|
|
this.setState({ isEditingName: true });
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
hideEditFileName = () => {
|
2018-09-07 21:48:25 +02:00
|
|
|
this.setState({ isEditingName: false });
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
showFolderChildren = () => {
|
|
|
|
this.props.showFolderChildren(this.props.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
hideFolderChildren = () => {
|
|
|
|
this.props.hideFolderChildren(this.props.id);
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
renderChild = childId => (
|
|
|
|
<li key={childId}>
|
|
|
|
<ConnectedFileNode id={childId} parentId={this.props.id} canEdit={this.props.canEdit} />
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
|
2016-08-23 06:10:42 +02:00
|
|
|
render() {
|
2017-02-22 20:29:35 +01:00
|
|
|
const itemClass = classNames({
|
2020-04-16 21:53:32 +02:00
|
|
|
'sidebar__root-item': this.props.name === 'root',
|
|
|
|
'sidebar__file-item': this.props.name !== 'root',
|
2016-09-14 21:57:52 +02:00
|
|
|
'sidebar__file-item--selected': this.props.isSelectedFile,
|
2018-09-07 21:48:25 +02:00
|
|
|
'sidebar__file-item--open': this.state.isOptionsOpen,
|
|
|
|
'sidebar__file-item--editing': this.state.isEditingName,
|
2016-08-31 00:46:59 +02:00
|
|
|
'sidebar__file-item--closed': this.props.isFolderClosed
|
2016-08-23 06:10:42 +02:00
|
|
|
});
|
2019-03-27 20:52:57 +01:00
|
|
|
|
2020-04-16 21:53:32 +02:00
|
|
|
const isFile = this.props.fileType === 'file';
|
|
|
|
const isFolder = this.props.fileType === 'folder';
|
|
|
|
const isRoot = this.props.name === 'root';
|
|
|
|
|
2016-08-23 06:10:42 +02:00
|
|
|
return (
|
2017-02-22 20:29:35 +01:00
|
|
|
<div className={itemClass}>
|
2020-04-16 21:53:32 +02:00
|
|
|
{ !isRoot &&
|
|
|
|
<div className="file-item__content" onContextMenu={this.toggleFileOptions}>
|
|
|
|
<span className="file-item__spacer"></span>
|
|
|
|
{ isFile &&
|
|
|
|
<span className="sidebar__file-item-icon">
|
2020-05-06 01:03:58 +02:00
|
|
|
<FileIcon focusable="false" aria-hidden="true" />
|
2020-04-16 21:53:32 +02:00
|
|
|
</span>
|
|
|
|
}
|
|
|
|
{ isFolder &&
|
|
|
|
<div className="sidebar__file-item--folder">
|
|
|
|
<button
|
|
|
|
className="sidebar__file-item-closed"
|
|
|
|
onClick={this.showFolderChildren}
|
2020-05-06 01:03:58 +02:00
|
|
|
aria-label="Open folder contents"
|
2020-04-16 21:53:32 +02:00
|
|
|
>
|
2020-05-06 01:03:58 +02:00
|
|
|
<FolderRightIcon className="folder-right" focusable="false" aria-hidden="true" />
|
2020-04-16 21:53:32 +02:00
|
|
|
</button>
|
2016-08-23 19:52:31 +02:00
|
|
|
<button
|
2020-04-16 21:53:32 +02:00
|
|
|
className="sidebar__file-item-open"
|
|
|
|
onClick={this.hideFolderChildren}
|
2020-05-06 01:03:58 +02:00
|
|
|
aria-label="Close file contents"
|
2016-08-23 19:52:31 +02:00
|
|
|
>
|
2020-05-06 01:03:58 +02:00
|
|
|
<FolderDownIcon className="folder-down" focusable="false" aria-hidden="true" />
|
2016-08-23 19:52:31 +02:00
|
|
|
</button>
|
2020-04-16 21:53:32 +02:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
<button
|
|
|
|
className="sidebar__file-item-name"
|
|
|
|
onClick={this.handleFileClick}
|
|
|
|
>
|
|
|
|
{this.state.updatedName}
|
|
|
|
</button>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="sidebar__file-item-input"
|
|
|
|
value={this.state.updatedName}
|
|
|
|
maxLength="128"
|
|
|
|
onChange={this.handleFileNameChange}
|
|
|
|
ref={(element) => { this.fileNameInput = element; }}
|
|
|
|
onBlur={this.handleFileNameBlur}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
className="sidebar__file-item-show-options"
|
2020-05-06 01:03:58 +02:00
|
|
|
aria-label="Toggle open/close file options"
|
2020-04-16 21:53:32 +02:00
|
|
|
ref={(element) => { this[`fileOptions-${this.props.id}`] = element; }}
|
|
|
|
tabIndex="0"
|
|
|
|
onClick={this.toggleFileOptions}
|
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
|
|
|
>
|
2020-05-06 01:03:58 +02:00
|
|
|
<DownArrowIcon focusable="false" aria-hidden="true" />
|
2020-04-16 21:53:32 +02:00
|
|
|
</button>
|
|
|
|
<div className="sidebar__file-item-options">
|
|
|
|
<ul title="file options">
|
|
|
|
{ isFolder &&
|
|
|
|
<React.Fragment>
|
2016-08-23 19:52:31 +02:00
|
|
|
<li>
|
2017-02-22 20:29:35 +01:00
|
|
|
<button
|
2020-04-16 21:59:09 +02:00
|
|
|
aria-label="add folder"
|
|
|
|
onClick={this.handleClickAddFolder}
|
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
|
|
|
className="sidebar__file-item-option"
|
|
|
|
>
|
|
|
|
Create folder
|
|
|
|
</button>
|
|
|
|
</li>
|
2016-08-23 19:52:31 +02:00
|
|
|
<li>
|
2017-02-22 20:29:35 +01:00
|
|
|
<button
|
2020-04-16 21:53:32 +02:00
|
|
|
aria-label="add file"
|
|
|
|
onClick={this.handleClickAddFile}
|
2019-03-27 20:52:57 +01:00
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
2017-02-22 20:29:35 +01:00
|
|
|
className="sidebar__file-item-option"
|
2016-08-23 19:52:31 +02:00
|
|
|
>
|
2020-04-16 21:59:09 +02:00
|
|
|
Create file
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-23 19:52:31 +02:00
|
|
|
</li>
|
2020-04-16 22:08:51 +02:00
|
|
|
{ this.props.authenticated &&
|
|
|
|
<li>
|
|
|
|
<button
|
|
|
|
aria-label="upload file"
|
|
|
|
onClick={this.handleClickUploadFile}
|
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
|
|
|
>
|
|
|
|
Upload file
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
}
|
2020-04-16 21:53:32 +02:00
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
<li>
|
|
|
|
<button
|
|
|
|
onClick={this.handleClickRename}
|
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
|
|
|
className="sidebar__file-item-option"
|
|
|
|
>
|
|
|
|
Rename
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<button
|
|
|
|
onClick={this.handleClickDelete}
|
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
|
|
|
className="sidebar__file-item-option"
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</button>
|
|
|
|
</li>
|
2016-08-23 06:10:42 +02:00
|
|
|
</ul>
|
2020-04-16 21:53:32 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
{ this.props.children &&
|
|
|
|
<ul className="file-item__children">
|
|
|
|
{this.props.children.map(this.renderChild)}
|
|
|
|
</ul>
|
|
|
|
}
|
2016-08-23 06:10:42 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileNode.propTypes = {
|
|
|
|
id: PropTypes.string.isRequired,
|
2016-08-24 22:06:28 +02:00
|
|
|
parentId: PropTypes.string,
|
2017-02-22 20:29:35 +01:00
|
|
|
children: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
|
2016-08-23 06:10:42 +02:00
|
|
|
name: PropTypes.string.isRequired,
|
2016-08-30 05:23:10 +02:00
|
|
|
fileType: PropTypes.string.isRequired,
|
2016-09-14 21:57:52 +02:00
|
|
|
isSelectedFile: PropTypes.bool,
|
2016-08-31 00:46:59 +02:00
|
|
|
isFolderClosed: PropTypes.bool,
|
2016-08-23 06:10:42 +02:00
|
|
|
setSelectedFile: PropTypes.func.isRequired,
|
|
|
|
deleteFile: PropTypes.func.isRequired,
|
2016-08-23 19:52:31 +02:00
|
|
|
updateFileName: PropTypes.func.isRequired,
|
2016-08-30 05:43:02 +02:00
|
|
|
resetSelectedFile: PropTypes.func.isRequired,
|
|
|
|
newFile: PropTypes.func.isRequired,
|
2016-08-31 00:46:59 +02:00
|
|
|
newFolder: PropTypes.func.isRequired,
|
|
|
|
showFolderChildren: PropTypes.func.isRequired,
|
2019-03-14 21:10:50 +01:00
|
|
|
hideFolderChildren: PropTypes.func.isRequired,
|
2020-04-08 22:18:10 +02:00
|
|
|
canEdit: PropTypes.bool.isRequired,
|
2020-04-16 22:08:51 +02:00
|
|
|
openUploadFileModal: PropTypes.func.isRequired,
|
|
|
|
authenticated: PropTypes.bool.isRequired
|
2016-08-23 06:10:42 +02:00
|
|
|
};
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
FileNode.defaultProps = {
|
|
|
|
parentId: '0',
|
|
|
|
isSelectedFile: false,
|
2020-04-08 00:47:51 +02:00
|
|
|
isFolderClosed: false,
|
2017-02-22 20:29:35 +01:00
|
|
|
};
|
|
|
|
|
2016-08-23 06:10:42 +02:00
|
|
|
function mapStateToProps(state, ownProps) {
|
2018-05-05 02:22:39 +02:00
|
|
|
// this is a hack, state is updated before ownProps
|
2020-04-16 22:08:51 +02:00
|
|
|
const fileNode = state.files.find(file => file.id === ownProps.id) || { name: 'test', fileType: 'file' };
|
|
|
|
return Object.assign({}, fileNode, { authenticated: state.user.authenticated });
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
2016-08-23 19:52:31 +02:00
|
|
|
return bindActionCreators(Object.assign(FileActions, IDEActions), dispatch);
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ConnectedFileNode = connect(mapStateToProps, mapDispatchToProps)(FileNode);
|
|
|
|
export default ConnectedFileNode;
|