2018-10-25 13:36:06 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import RepoListView from './repo-list-view';
|
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
2018-11-28 12:41:49 +08:00
|
|
|
import { gettext } from '../../utils/constants';
|
2019-01-09 14:40:36 +08:00
|
|
|
import { Utils } from '../../utils/utils';
|
2018-12-18 17:21:01 +08:00
|
|
|
import RepoInfo from '../../models/repo-info';
|
2018-10-25 13:36:06 +08:00
|
|
|
|
|
|
|
import '../../css/file-chooser.css';
|
|
|
|
|
|
|
|
const propTypes = {
|
2018-12-28 14:25:25 +08:00
|
|
|
isShowFile: PropTypes.bool,
|
2019-01-04 18:30:03 +08:00
|
|
|
repoID: PropTypes.string,
|
2018-10-25 13:36:06 +08:00
|
|
|
onDirentItemClick: PropTypes.func,
|
|
|
|
onRepoItemClick: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileChooser extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
hasRequest: false,
|
2018-11-01 18:40:18 +08:00
|
|
|
isCurrentRepoShow: true,
|
2018-10-25 13:36:06 +08:00
|
|
|
isOtherRepoShow: false,
|
|
|
|
repoList: [],
|
2018-12-18 17:21:01 +08:00
|
|
|
currentRepoInfo: null,
|
2018-10-25 13:36:06 +08:00
|
|
|
selectedRepo: null,
|
|
|
|
selectedPath: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-01-04 18:30:03 +08:00
|
|
|
if (this.props.repoID) {
|
|
|
|
let repoID = this.props.repoID;
|
|
|
|
seafileAPI.getRepoInfo(repoID).then(res => {
|
|
|
|
// need to optimized
|
|
|
|
let repoInfo = new RepoInfo(res.data);
|
|
|
|
this.setState({
|
|
|
|
currentRepoInfo: repoInfo,
|
|
|
|
});
|
2018-10-25 13:36:06 +08:00
|
|
|
});
|
2019-01-04 18:30:03 +08:00
|
|
|
}
|
2018-10-25 13:36:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onOtherRepoToggle = () => {
|
|
|
|
if (!this.state.hasRequest) {
|
2019-01-04 18:30:03 +08:00
|
|
|
let that = this;
|
2018-10-25 13:36:06 +08:00
|
|
|
seafileAPI.listRepos().then(res => {
|
2018-12-18 17:21:01 +08:00
|
|
|
// todo optimized code
|
2018-10-25 13:36:06 +08:00
|
|
|
let repos = res.data.repos;
|
|
|
|
let repoList = [];
|
|
|
|
let repoIdList = [];
|
|
|
|
for(let i = 0; i < repos.length; i++) {
|
2019-01-04 18:30:03 +08:00
|
|
|
if (repos[i].permission !== 'rw') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (that.props.repoID && (repos[i].repo_name === that.state.currentRepoInfo.repo_name)) {
|
2018-10-25 13:36:06 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (repoIdList.indexOf(repos[i].repo_id) > -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
repoList.push(repos[i]);
|
|
|
|
repoIdList.push(repos[i].repo_id);
|
|
|
|
}
|
2019-01-09 14:40:36 +08:00
|
|
|
repoList = Utils.sortRepos(repoList, 'name', 'asc');
|
2018-10-25 13:36:06 +08:00
|
|
|
this.setState({
|
|
|
|
repoList: repoList,
|
|
|
|
isOtherRepoShow: !this.state.isOtherRepoShow,
|
|
|
|
});
|
|
|
|
});
|
2019-01-04 18:30:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-10-25 13:36:06 +08:00
|
|
|
this.setState({isOtherRepoShow: !this.state.isOtherRepoShow});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:40:18 +08:00
|
|
|
onCurrentRepoToggle = () => [
|
|
|
|
this.setState({isCurrentRepoShow: !this.state.isCurrentRepoShow})
|
|
|
|
]
|
|
|
|
|
2018-12-28 14:25:25 +08:00
|
|
|
onDirentItemClick = (repo, filePath, dirent) => {
|
|
|
|
this.props.onDirentItemClick(repo, filePath, dirent);
|
2018-10-25 13:36:06 +08:00
|
|
|
this.setState({
|
|
|
|
selectedRepo: repo,
|
|
|
|
selectedPath: filePath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onRepoItemClick = (repo) => {
|
2018-12-28 14:25:25 +08:00
|
|
|
if (this.props.onRepoItemClick) {
|
|
|
|
this.props.onRepoItemClick(repo);
|
|
|
|
}
|
2018-10-25 13:36:06 +08:00
|
|
|
this.setState({
|
|
|
|
selectedRepo: repo,
|
|
|
|
selectedPath: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="file-chooser-container">
|
2019-01-04 18:30:03 +08:00
|
|
|
{
|
|
|
|
this.props.repoID &&
|
|
|
|
<div className="list-view">
|
|
|
|
<div className="list-view-header">
|
|
|
|
<span className={`item-toggle fa ${this.state.isCurrentRepoShow ? 'fa-caret-down' : 'fa-caret-right'}`} onClick={this.onCurrentRepoToggle}></span>
|
|
|
|
<span className="library">{gettext('Current Library')}</span>
|
|
|
|
</div>
|
|
|
|
{
|
|
|
|
this.state.isCurrentRepoShow && this.state.currentRepoInfo &&
|
|
|
|
<RepoListView
|
|
|
|
initToShowChildren={true}
|
|
|
|
currentRepoInfo={this.state.currentRepoInfo}
|
|
|
|
selectedRepo={this.state.selectedRepo}
|
|
|
|
selectedPath={this.state.selectedPath}
|
|
|
|
onRepoItemClick={this.onRepoItemClick}
|
|
|
|
onDirentItemClick={this.onDirentItemClick}
|
|
|
|
isShowFile={this.props.isShowFile}
|
|
|
|
/>
|
|
|
|
}
|
2018-10-25 13:36:06 +08:00
|
|
|
</div>
|
2019-01-04 18:30:03 +08:00
|
|
|
}
|
2018-10-25 13:36:06 +08:00
|
|
|
<div className="list-view">
|
|
|
|
<div className="list-view-header">
|
|
|
|
<span className={`item-toggle fa ${this.state.isOtherRepoShow ? 'fa-caret-down' : 'fa-caret-right'}`} onClick={this.onOtherRepoToggle}></span>
|
|
|
|
<span className="library">{gettext('Other Libraries')}</span>
|
|
|
|
</div>
|
|
|
|
{
|
|
|
|
this.state.isOtherRepoShow &&
|
|
|
|
<RepoListView
|
|
|
|
initToShowChildren={false}
|
|
|
|
repoList={this.state.repoList}
|
|
|
|
selectedRepo={this.state.selectedRepo}
|
|
|
|
selectedPath={this.state.selectedPath}
|
|
|
|
onRepoItemClick={this.onRepoItemClick}
|
|
|
|
onDirentItemClick={this.onDirentItemClick}
|
2018-12-28 14:25:25 +08:00
|
|
|
isShowFile={this.props.isShowFile}
|
2018-10-25 13:36:06 +08:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileChooser.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default FileChooser;
|