2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-07-08 18:57:22 +00:00
|
|
|
import classNames from 'classnames';
|
2020-08-19 09:40:05 +00:00
|
|
|
import { withTranslation } from 'react-i18next';
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
import ConnectedFileNode from './FileNode';
|
|
|
|
|
2020-04-29 22:34:37 +00:00
|
|
|
import DownArrowIcon from '../../../images/down-filled-triangle.svg';
|
2016-07-06 19:09:05 +00:00
|
|
|
|
2016-08-03 19:11:59 +00:00
|
|
|
class Sidebar extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.resetSelectedFile = this.resetSelectedFile.bind(this);
|
2017-01-09 21:11:01 +00:00
|
|
|
this.toggleProjectOptions = this.toggleProjectOptions.bind(this);
|
2019-03-27 19:52:57 +00:00
|
|
|
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() {
|
2016-08-23 17:52:31 +00:00
|
|
|
this.props.setSelectedFile(this.props.files[1].id);
|
2016-08-03 19:11:59 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 21:11:01 +00:00
|
|
|
toggleProjectOptions(e) {
|
|
|
|
e.preventDefault();
|
2017-01-09 17:47:32 +00:00
|
|
|
if (this.props.projectOptionsVisible) {
|
|
|
|
this.props.closeProjectOptions();
|
|
|
|
} else {
|
2017-02-22 19:29:35 +00:00
|
|
|
this.sidebarOptions.focus();
|
2017-01-09 17:47:32 +00:00
|
|
|
this.props.openProjectOptions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 00:06:04 +00:00
|
|
|
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() {
|
2019-03-14 20:10:50 +00:00
|
|
|
const canEditProject = this.userCanEditProject();
|
2016-08-03 19:11:59 +00:00
|
|
|
const sidebarClass = classNames({
|
2017-02-22 19:29:35 +00:00
|
|
|
'sidebar': true,
|
2016-08-30 03:23:10 +00:00
|
|
|
'sidebar--contracted': !this.props.isExpanded,
|
2017-04-12 00:06:04 +00:00
|
|
|
'sidebar--project-options': this.props.projectOptionsVisible,
|
2019-03-14 20:10:50 +00:00
|
|
|
'sidebar--cant-edit': !canEditProject
|
2016-08-03 19:11:59 +00:00
|
|
|
});
|
2019-10-08 20:39:47 +00:00
|
|
|
const rootFile = this.props.files.filter(file => file.name === 'root')[0];
|
2016-08-03 19:11:59 +00:00
|
|
|
|
|
|
|
return (
|
2020-05-19 19:34:00 +00:00
|
|
|
<section className={sidebarClass}>
|
|
|
|
<header className="sidebar__header" onContextMenu={this.toggleProjectOptions}>
|
2016-08-25 21:32:27 +00:00
|
|
|
<h3 className="sidebar__title">
|
2020-08-19 09:40:05 +00:00
|
|
|
<span>{this.props.t('Sidebar.Title')}</span>
|
2016-08-25 21:32:27 +00:00
|
|
|
</h3>
|
2016-08-03 19:11:59 +00:00
|
|
|
<div className="sidebar__icons">
|
|
|
|
<button
|
2020-08-19 09:40:05 +00:00
|
|
|
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"
|
2017-02-22 19:29:35 +00:00
|
|
|
ref={(element) => { this.sidebarOptions = element; }}
|
2017-01-09 21:11:01 +00:00
|
|
|
onClick={this.toggleProjectOptions}
|
2019-03-27 19:52:57 +00:00
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
2016-08-03 19:11:59 +00:00
|
|
|
>
|
2020-05-05 23:03:58 +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>
|
2019-03-21 21:08:46 +00:00
|
|
|
<button
|
2020-08-19 09:40:05 +00:00
|
|
|
aria-label={this.props.t('Sidebar.AddFolderARIA')}
|
2019-03-21 21:08:46 +00:00
|
|
|
onClick={() => {
|
2019-10-08 20:39:47 +00:00
|
|
|
this.props.newFolder(rootFile.id);
|
2019-03-21 21:08:46 +00:00
|
|
|
setTimeout(this.props.closeProjectOptions, 0);
|
|
|
|
}}
|
2019-03-27 19:52:57 +00:00
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
2019-03-21 21:08:46 +00:00
|
|
|
>
|
2020-08-19 09:40:05 +00:00
|
|
|
{this.props.t('Sidebar.AddFolder')}
|
2017-02-22 19:29:35 +00:00
|
|
|
</button>
|
2016-08-30 03:23:10 +00:00
|
|
|
</li>
|
|
|
|
<li>
|
2019-03-21 21:08:46 +00:00
|
|
|
<button
|
2020-08-19 09:40:05 +00:00
|
|
|
aria-label={this.props.t('Sidebar.AddFileARIA')}
|
2019-03-21 21:08:46 +00:00
|
|
|
onClick={() => {
|
2019-10-08 20:39:47 +00:00
|
|
|
this.props.newFile(rootFile.id);
|
2019-03-21 21:08:46 +00:00
|
|
|
setTimeout(this.props.closeProjectOptions, 0);
|
|
|
|
}}
|
2019-03-27 19:52:57 +00:00
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
2019-03-21 21:08:46 +00:00
|
|
|
>
|
2020-08-19 09:40:05 +00:00
|
|
|
{this.props.t('Sidebar.AddFile')}
|
2017-02-22 19:29:35 +00:00
|
|
|
</button>
|
2016-08-30 03:23:10 +00:00
|
|
|
</li>
|
2020-04-16 20:16:19 +00:00
|
|
|
{
|
|
|
|
this.props.user.authenticated &&
|
|
|
|
<li>
|
|
|
|
<button
|
2020-08-19 09:40:05 +00:00
|
|
|
aria-label={this.props.t('Sidebar.UploadFileARIA')}
|
2020-04-16 20:16:19 +00:00
|
|
|
onClick={() => {
|
|
|
|
this.props.openUploadFileModal(rootFile.id);
|
|
|
|
setTimeout(this.props.closeProjectOptions, 0);
|
|
|
|
}}
|
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
|
|
|
>
|
2020-08-19 09:40:05 +00:00
|
|
|
{this.props.t('Sidebar.UploadFile')}
|
2020-04-16 20:16:19 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
}
|
2016-08-30 03:23:10 +00:00
|
|
|
</ul>
|
2016-08-03 19:11:59 +00:00
|
|
|
</div>
|
2020-05-19 19:34:00 +00:00
|
|
|
</header>
|
2019-03-14 20:10:50 +00:00
|
|
|
<ConnectedFileNode
|
2019-10-08 20:39:47 +00:00
|
|
|
id={rootFile.id}
|
2019-03-14 20:10:50 +00:00
|
|
|
canEdit={canEditProject}
|
|
|
|
/>
|
2020-05-19 19:34:00 +00:00
|
|
|
</section>
|
2016-08-03 19:11:59 +00:00
|
|
|
);
|
|
|
|
}
|
2016-07-06 19:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Sidebar.propTypes = {
|
2017-02-22 19:29:35 +00:00
|
|
|
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,
|
2017-04-12 00:06:04 +00:00
|
|
|
newFolder: PropTypes.func.isRequired,
|
2019-08-12 15:21:42 +00:00
|
|
|
openUploadFileModal: PropTypes.func.isRequired,
|
2017-04-12 00:06:04 +00:00
|
|
|
owner: PropTypes.shape({
|
|
|
|
id: PropTypes.string
|
|
|
|
}),
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
authenticated: PropTypes.bool.isRequired
|
2020-08-19 09:40:05 +00:00
|
|
|
}).isRequired,
|
|
|
|
t: PropTypes.func.isRequired,
|
2017-04-12 00:06:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Sidebar.defaultProps = {
|
|
|
|
owner: undefined
|
2016-07-06 19:09:05 +00:00
|
|
|
};
|
|
|
|
|
2020-08-19 09:40:05 +00:00
|
|
|
export default withTranslation()(Sidebar);
|