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';
|
|
|
|
import InlineSVG from 'react-inlinesvg';
|
2017-02-22 20:29:35 +01:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import * as IDEActions from '../actions/ide';
|
|
|
|
import * as FileActions from '../actions/files';
|
|
|
|
|
2016-08-23 06:10:42 +02:00
|
|
|
const downArrowUrl = require('../../../images/down-arrow.svg');
|
2016-08-31 00:46:59 +02:00
|
|
|
const folderRightUrl = require('../../../images/triangle-arrow-right.svg');
|
|
|
|
const folderDownUrl = require('../../../images/triangle-arrow-down.svg');
|
2016-08-25 23:32:27 +02:00
|
|
|
const fileUrl = require('../../../images/file.svg');
|
2016-08-23 06:10:42 +02:00
|
|
|
|
|
|
|
export class FileNode extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.renderChild = this.renderChild.bind(this);
|
|
|
|
this.handleKeyPress = this.handleKeyPress.bind(this);
|
|
|
|
this.handleFileNameChange = this.handleFileNameChange.bind(this);
|
|
|
|
this.validateFileName = this.validateFileName.bind(this);
|
2016-08-23 19:52:31 +02:00
|
|
|
this.handleFileClick = this.handleFileClick.bind(this);
|
2017-01-09 22:11:01 +01:00
|
|
|
this.toggleFileOptions = this.toggleFileOptions.bind(this);
|
2018-09-07 21:48:25 +02:00
|
|
|
this.hideFileOptions = this.hideFileOptions.bind(this);
|
|
|
|
this.showEditFileName = this.showEditFileName.bind(this);
|
|
|
|
this.hideEditFileName = this.hideEditFileName.bind(this);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isOptionsOpen: false,
|
|
|
|
isEditingName: false,
|
|
|
|
};
|
2016-08-23 19:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleFileClick(e) {
|
|
|
|
e.stopPropagation();
|
2016-12-09 23:05:24 +01:00
|
|
|
if (this.props.name !== 'root' && !this.isDeleting) {
|
2016-08-25 00:52:08 +02:00
|
|
|
this.props.setSelectedFile(this.props.id);
|
|
|
|
}
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleFileNameChange(event) {
|
|
|
|
this.props.updateFileName(this.props.id, event.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyPress(event) {
|
|
|
|
if (event.key === 'Enter') {
|
2018-09-07 21:48:25 +02:00
|
|
|
this.hideEditFileName();
|
2016-08-23 06:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validateFileName() {
|
|
|
|
const oldFileExtension = this.originalFileName.match(/\.[0-9a-z]+$/i);
|
|
|
|
const newFileExtension = this.props.name.match(/\.[0-9a-z]+$/i);
|
|
|
|
if (oldFileExtension && !newFileExtension) {
|
|
|
|
this.props.updateFileName(this.props.id, this.originalFileName);
|
|
|
|
}
|
2017-06-06 04:33:32 +02:00
|
|
|
if (
|
|
|
|
oldFileExtension &&
|
|
|
|
newFileExtension &&
|
|
|
|
oldFileExtension[0].toLowerCase() !== newFileExtension[0].toLowerCase()
|
|
|
|
) {
|
2016-08-23 06:10:42 +02:00
|
|
|
this.props.updateFileName(this.props.id, this.originalFileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 22:11:01 +01:00
|
|
|
toggleFileOptions(e) {
|
|
|
|
e.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 21:48:25 +02:00
|
|
|
hideFileOptions() {
|
|
|
|
this.setState({ isOptionsOpen: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
showEditFileName() {
|
|
|
|
this.setState({ isEditingName: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
hideEditFileName() {
|
|
|
|
this.setState({ isEditingName: false });
|
|
|
|
}
|
|
|
|
|
2016-08-23 06:10:42 +02:00
|
|
|
renderChild(childId) {
|
|
|
|
return (
|
|
|
|
<li key={childId}>
|
2019-03-14 21:10:50 +01:00
|
|
|
<ConnectedFileNode id={childId} parentId={this.props.id} canEdit={this.props.canEdit} />
|
2016-08-23 06:10:42 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-02-22 20:29:35 +01:00
|
|
|
const itemClass = classNames({
|
2016-08-30 20:39:37 +02:00
|
|
|
'sidebar__root-item': this.props.name === 'root',
|
2016-08-23 19:52:31 +02:00
|
|
|
'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
|
|
|
});
|
|
|
|
return (
|
2017-02-22 20:29:35 +01:00
|
|
|
<div className={itemClass}>
|
2016-08-23 19:52:31 +02:00
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.name !== 'root') {
|
|
|
|
return (
|
2017-01-09 22:11:01 +01:00
|
|
|
<div className="file-item__content" onContextMenu={this.toggleFileOptions}>
|
2016-08-30 20:39:37 +02:00
|
|
|
<span className="file-item__spacer"></span>
|
2016-08-25 23:32:27 +02:00
|
|
|
{(() => { // eslint-disable-line
|
2016-08-30 05:23:10 +02:00
|
|
|
if (this.props.fileType === 'file') {
|
2016-08-25 23:32:27 +02:00
|
|
|
return (
|
|
|
|
<span className="sidebar__file-item-icon">
|
|
|
|
<InlineSVG src={fileUrl} />
|
|
|
|
</span>
|
|
|
|
);
|
2016-08-31 03:08:50 +02:00
|
|
|
}
|
|
|
|
return (
|
2017-05-10 19:23:10 +02:00
|
|
|
<div className="sidebar__file-item--folder">
|
2017-02-22 20:29:35 +01:00
|
|
|
<button
|
2016-08-31 03:08:50 +02:00
|
|
|
className="sidebar__file-item-closed"
|
2016-08-31 00:46:59 +02:00
|
|
|
onClick={() => this.props.showFolderChildren(this.props.id)}
|
|
|
|
>
|
2016-08-31 03:08:50 +02:00
|
|
|
<InlineSVG className="folder-right" src={folderRightUrl} />
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
|
|
|
<button
|
2016-08-31 03:08:50 +02:00
|
|
|
className="sidebar__file-item-open"
|
|
|
|
onClick={() => this.props.hideFolderChildren(this.props.id)}
|
|
|
|
>
|
|
|
|
<InlineSVG className="folder-down" src={folderDownUrl} />
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-31 03:08:50 +02:00
|
|
|
</div>
|
2016-08-31 00:46:59 +02:00
|
|
|
);
|
2016-08-25 23:32:27 +02:00
|
|
|
})()}
|
2017-02-22 20:29:35 +01:00
|
|
|
<button className="sidebar__file-item-name" onClick={this.handleFileClick}>{this.props.name}</button>
|
2016-08-23 19:52:31 +02:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="sidebar__file-item-input"
|
|
|
|
value={this.props.name}
|
|
|
|
onChange={this.handleFileNameChange}
|
2017-02-22 20:29:35 +01:00
|
|
|
ref={(element) => { this.fileNameInput = element; }}
|
2016-08-23 19:52:31 +02:00
|
|
|
onBlur={() => {
|
|
|
|
this.validateFileName();
|
2018-09-07 21:48:25 +02:00
|
|
|
this.hideEditFileName();
|
2016-08-23 19:52:31 +02:00
|
|
|
}}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
className="sidebar__file-item-show-options"
|
|
|
|
aria-label="view file options"
|
2017-02-22 20:29:35 +01:00
|
|
|
ref={(element) => { this[`fileOptions-${this.props.id}`] = element; }}
|
2016-10-19 19:03:19 +02:00
|
|
|
tabIndex="0"
|
2017-01-09 22:11:01 +01:00
|
|
|
onClick={this.toggleFileOptions}
|
2016-08-23 19:52:31 +02:00
|
|
|
>
|
|
|
|
<InlineSVG src={downArrowUrl} />
|
|
|
|
</button>
|
2017-02-22 20:29:35 +01:00
|
|
|
<div className="sidebar__file-item-options">
|
2016-08-23 19:52:31 +02:00
|
|
|
<ul title="file options">
|
2016-08-30 05:43:02 +02:00
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.fileType === 'folder') {
|
|
|
|
return (
|
|
|
|
<li>
|
2017-02-22 20:29:35 +01:00
|
|
|
<button
|
|
|
|
aria-label="add file"
|
2019-03-21 22:08:46 +01:00
|
|
|
onClick={() => {
|
|
|
|
this.props.newFile();
|
|
|
|
setTimeout(() => this.hideFileOptions(), 0);
|
|
|
|
}}
|
2017-02-22 20:29:35 +01:00
|
|
|
className="sidebar__file-item-option"
|
|
|
|
>
|
2016-08-30 05:43:02 +02:00
|
|
|
Add File
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-30 05:43:02 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.fileType === 'folder') {
|
|
|
|
return (
|
|
|
|
<li>
|
2017-02-22 20:29:35 +01:00
|
|
|
<button
|
|
|
|
aria-label="add folder"
|
2019-03-21 22:08:46 +01:00
|
|
|
onClick={() => {
|
|
|
|
this.props.newFolder();
|
|
|
|
setTimeout(() => this.hideFileOptions(), 0);
|
|
|
|
}}
|
2017-02-22 20:29:35 +01:00
|
|
|
className="sidebar__file-item-option"
|
|
|
|
>
|
2016-08-30 05:43:02 +02:00
|
|
|
Add Folder
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-30 05:43:02 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
2016-08-23 19:52:31 +02:00
|
|
|
<li>
|
2017-02-22 20:29:35 +01:00
|
|
|
<button
|
2016-08-23 19:52:31 +02:00
|
|
|
onClick={() => {
|
|
|
|
this.originalFileName = this.props.name;
|
2018-09-07 21:48:25 +02:00
|
|
|
this.showEditFileName();
|
2017-02-22 20:29:35 +01:00
|
|
|
setTimeout(() => this.fileNameInput.focus(), 0);
|
2019-03-21 22:08:46 +01:00
|
|
|
setTimeout(() => this.hideFileOptions(), 0);
|
2016-08-23 19:52:31 +02:00
|
|
|
}}
|
2017-02-22 20:29:35 +01:00
|
|
|
className="sidebar__file-item-option"
|
2016-08-23 19:52:31 +02:00
|
|
|
>
|
|
|
|
Rename
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-23 19:52:31 +02:00
|
|
|
</li>
|
|
|
|
<li>
|
2017-02-22 20:29:35 +01:00
|
|
|
<button
|
2016-08-23 19:52:31 +02:00
|
|
|
onClick={() => {
|
|
|
|
if (window.confirm(`Are you sure you want to delete ${this.props.name}?`)) {
|
2016-08-25 00:52:08 +02:00
|
|
|
this.isDeleting = true;
|
|
|
|
this.props.resetSelectedFile(this.props.id);
|
2016-11-10 18:26:35 +01:00
|
|
|
setTimeout(() => this.props.deleteFile(this.props.id, this.props.parentId), 100);
|
2016-08-23 19:52:31 +02:00
|
|
|
}
|
|
|
|
}}
|
2019-03-21 22:08:46 +01:00
|
|
|
onBlur={() => {
|
|
|
|
setTimeout(this.hideFileOptions, 200);
|
|
|
|
}}
|
2017-02-22 20:29:35 +01:00
|
|
|
className="sidebar__file-item-option"
|
2016-08-23 19:52:31 +02:00
|
|
|
>
|
|
|
|
Delete
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-23 19:52:31 +02:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
2016-08-23 06:10:42 +02:00
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.children) {
|
|
|
|
return (
|
2016-08-30 20:39:37 +02:00
|
|
|
<ul className="file-item__children">
|
2016-08-23 06:10:42 +02:00
|
|
|
{this.props.children.map(this.renderChild)}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
</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,
|
|
|
|
canEdit: 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,
|
|
|
|
isFolderClosed: false
|
|
|
|
};
|
|
|
|
|
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
|
2019-03-14 21:10:50 +01:00
|
|
|
return state.files.find(file => file.id === ownProps.id) || { name: 'test', fileType: 'file' };
|
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;
|