p5.js-web-editor/client/modules/IDE/components/SidebarItem.jsx

124 lines
4 KiB
React
Raw Normal View History

2016-08-03 19:11:59 +00:00
import React, { PropTypes } from 'react';
import InlineSVG from 'react-inlinesvg';
import classNames from 'classnames';
const downArrowUrl = require('../../../images/down-arrow.svg');
class SidebarItem extends React.Component {
2016-08-03 21:10:03 +00:00
constructor(props) {
super(props);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.handleFileNameChange = this.handleFileNameChange.bind(this);
2016-08-03 21:42:58 +00:00
this.validateFileName = this.validateFileName.bind(this);
2016-08-03 21:10:03 +00:00
}
handleFileNameChange(event) {
this.props.updateFileName(this.props.file.id, event.target.value);
}
2016-08-03 19:11:59 +00:00
2016-08-03 21:10:03 +00:00
handleKeyPress(event) {
if (event.key === 'Enter') {
this.props.hideEditFileName(this.props.file.id);
}
2016-08-03 19:11:59 +00:00
}
2016-08-03 21:42:58 +00:00
validateFileName() {
const oldFileExtension = this.originalFileName.match(/\.[0-9a-z]+$/i);
const newFileExtension = this.props.file.name.match(/\.[0-9a-z]+$/i);
if (oldFileExtension && !newFileExtension) {
this.props.updateFileName(this.props.file.id, this.originalFileName);
}
if (oldFileExtension && newFileExtension && oldFileExtension[0] !== newFileExtension[0]) {
2016-08-03 21:42:58 +00:00
this.props.updateFileName(this.props.file.id, this.originalFileName);
}
}
2016-08-03 19:11:59 +00:00
render() {
let itemClass = classNames({
'sidebar__file-item': true,
'sidebar__file-item--selected': this.props.file.isSelectedFile,
2016-08-03 21:10:03 +00:00
'sidebar__file-item--open': this.props.file.isOptionsOpen,
'sidebar__file-item--editing': this.props.file.isEditingName
2016-08-03 19:11:59 +00:00
});
2016-08-03 21:10:03 +00:00
2016-08-03 19:11:59 +00:00
return (
<li
className={itemClass}
onBlur={() => setTimeout(() => this.props.hideFileOptions(this.props.file.id), 100)}
2016-08-03 19:11:59 +00:00
tabIndex={this.props.fileIndex}
onClick={() => this.props.setSelectedFile(this.props.file.id)}
2016-08-03 19:11:59 +00:00
>
<a
2016-08-03 21:10:03 +00:00
className="sidebar__file-item-name"
2016-08-03 19:11:59 +00:00
>{this.props.file.name}</a>
2016-08-03 21:10:03 +00:00
<input
type="text"
className="sidebar__file-item-input"
value={this.props.file.name}
onChange={this.handleFileNameChange}
ref="fileNameInput"
2016-08-03 21:42:58 +00:00
onBlur={() => {
this.validateFileName();
this.props.hideEditFileName(this.props.file.id);
}}
2016-08-03 21:10:03 +00:00
onKeyPress={this.handleKeyPress}
/>
2016-08-10 20:20:15 +00:00
<button
2016-08-03 19:11:59 +00:00
className="sidebar__file-item-show-options"
2016-08-10 20:20:15 +00:00
aria-label="view file options"
2016-08-03 19:11:59 +00:00
onClick={() => this.props.showFileOptions(this.props.file.id)}
>
<InlineSVG src={downArrowUrl} />
2016-08-10 20:20:15 +00:00
</button>
2016-08-03 19:11:59 +00:00
<div ref="fileOptions" className="sidebar__file-item-options">
2016-08-10 20:20:15 +00:00
<ul title="file options">
2016-08-03 19:11:59 +00:00
<li>
2016-08-03 21:10:03 +00:00
<a
onClick={() => {
2016-08-03 21:42:58 +00:00
this.originalFileName = this.props.file.name;
2016-08-03 21:10:03 +00:00
this.props.showEditFileName(this.props.file.id);
setTimeout(() => this.refs.fileNameInput.focus(), 0);
}}
>
2016-08-03 19:11:59 +00:00
Rename
</a>
</li>
<li>
<a
onClick={() => {
if (window.confirm(`Are you sure you want to delete ${this.props.file.name}?`)) {
this.props.deleteFile(this.props.file.id);
this.props.resetSelectedFile();
}
}}
>
Delete
</a>
</li>
</ul>
</div>
</li>
);
}
}
SidebarItem.propTypes = {
file: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
isSelectedFile: PropTypes.bool,
2016-08-03 21:10:03 +00:00
isOptionsOpen: PropTypes.bool,
isEditingName: PropTypes.bool
2016-08-03 19:11:59 +00:00
}).isRequired,
setSelectedFile: PropTypes.func.isRequired,
fileIndex: PropTypes.number.isRequired,
showFileOptions: PropTypes.func.isRequired,
hideFileOptions: PropTypes.func.isRequired,
deleteFile: PropTypes.func.isRequired,
2016-08-03 21:10:03 +00:00
resetSelectedFile: PropTypes.func.isRequired,
showEditFileName: PropTypes.func.isRequired,
hideEditFileName: PropTypes.func.isRequired,
updateFileName: PropTypes.func.isRequired
2016-08-03 19:11:59 +00:00
};
export default SidebarItem;