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

173 lines
5.2 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import React from 'react';
2016-07-08 18:57:22 +00:00
import classNames from 'classnames';
import { withTranslation } from 'react-i18next';
import ConnectedFileNode from './FileNode';
2020-04-29 22:34:37 +00:00
import DownArrowIcon from '../../../images/down-filled-triangle.svg';
2016-08-03 19:11:59 +00:00
class Sidebar extends React.Component {
constructor(props) {
super(props);
this.resetSelectedFile = this.resetSelectedFile.bind(this);
this.toggleProjectOptions = this.toggleProjectOptions.bind(this);
this.onBlurComponent = this.onBlurComponent.bind(this);
this.onFocusComponent = this.onFocusComponent.bind(this);
this.state = {
isFocused: false,
};
}
onBlurComponent() {
this.setState({ isFocused: false });
setTimeout(() => {
if (!this.state.isFocused) {
this.props.closeProjectOptions();
}
}, 200);
}
onFocusComponent() {
this.setState({ isFocused: true });
2016-08-03 19:11:59 +00:00
}
2016-07-14 16:47:54 +00:00
2016-08-03 19:11:59 +00:00
resetSelectedFile() {
this.props.setSelectedFile(this.props.files[1].id);
2016-08-03 19:11:59 +00:00
}
toggleProjectOptions(e) {
e.preventDefault();
if (this.props.projectOptionsVisible) {
this.props.closeProjectOptions();
} else {
this.sidebarOptions.focus();
this.props.openProjectOptions();
}
}
userCanEditProject() {
let canEdit;
if (!this.props.owner) {
canEdit = true;
} else if (this.props.user.authenticated && this.props.owner.id === this.props.user.id) {
canEdit = true;
} else {
canEdit = false;
}
return canEdit;
}
2016-08-03 19:11:59 +00:00
render() {
const canEditProject = this.userCanEditProject();
2016-08-03 19:11:59 +00:00
const sidebarClass = classNames({
'sidebar': true,
2016-08-30 03:23:10 +00:00
'sidebar--contracted': !this.props.isExpanded,
'sidebar--project-options': this.props.projectOptionsVisible,
'sidebar--cant-edit': !canEditProject
2016-08-03 19:11:59 +00:00
});
const rootFile = this.props.files.filter(file => file.name === 'root')[0];
2016-08-03 19:11:59 +00:00
return (
<section className={sidebarClass}>
<header className="sidebar__header" onContextMenu={this.toggleProjectOptions}>
<h3 className="sidebar__title">
<span>{this.props.t('Sidebar.Title')}</span>
</h3>
2016-08-03 19:11:59 +00:00
<div className="sidebar__icons">
<button
aria-label={this.props.t('Sidebar.ToggleARIA')}
2016-08-03 19:11:59 +00:00
className="sidebar__add"
2016-10-19 17:03:19 +00:00
tabIndex="0"
ref={(element) => { this.sidebarOptions = element; }}
onClick={this.toggleProjectOptions}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
2016-08-03 19:11:59 +00:00
>
<DownArrowIcon focusable="false" aria-hidden="true" />
2016-08-03 19:11:59 +00:00
</button>
2016-08-30 03:23:10 +00:00
<ul className="sidebar__project-options">
<li>
<button
aria-label={this.props.t('Sidebar.AddFolderARIA')}
onClick={() => {
this.props.newFolder(rootFile.id);
setTimeout(this.props.closeProjectOptions, 0);
}}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
>
{this.props.t('Sidebar.AddFolder')}
</button>
2016-08-30 03:23:10 +00:00
</li>
<li>
<button
aria-label={this.props.t('Sidebar.AddFileARIA')}
onClick={() => {
this.props.newFile(rootFile.id);
setTimeout(this.props.closeProjectOptions, 0);
}}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
>
{this.props.t('Sidebar.AddFile')}
</button>
2016-08-30 03:23:10 +00:00
</li>
{
this.props.user.authenticated &&
<li>
<button
aria-label={this.props.t('Sidebar.UploadFileARIA')}
onClick={() => {
this.props.openUploadFileModal(rootFile.id);
setTimeout(this.props.closeProjectOptions, 0);
}}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
>
{this.props.t('Sidebar.UploadFile')}
</button>
</li>
}
2016-08-30 03:23:10 +00:00
</ul>
2016-08-03 19:11:59 +00:00
</div>
</header>
<ConnectedFileNode
id={rootFile.id}
canEdit={canEditProject}
/>
</section>
2016-08-03 19:11:59 +00:00
);
}
}
Sidebar.propTypes = {
files: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string.isRequired,
id: PropTypes.string.isRequired
})).isRequired,
2016-08-03 19:11:59 +00:00
setSelectedFile: PropTypes.func.isRequired,
isExpanded: PropTypes.bool.isRequired,
2016-08-30 03:23:10 +00:00
projectOptionsVisible: PropTypes.bool.isRequired,
2016-08-03 19:11:59 +00:00
newFile: PropTypes.func.isRequired,
2016-08-30 03:23:10 +00:00
openProjectOptions: PropTypes.func.isRequired,
closeProjectOptions: PropTypes.func.isRequired,
newFolder: PropTypes.func.isRequired,
openUploadFileModal: PropTypes.func.isRequired,
owner: PropTypes.shape({
id: PropTypes.string
}),
user: PropTypes.shape({
id: PropTypes.string,
authenticated: PropTypes.bool.isRequired
}).isRequired,
t: PropTypes.func.isRequired,
};
Sidebar.defaultProps = {
owner: undefined
};
export default withTranslation()(Sidebar);