1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-03 16:10:26 +00:00
Files
seahub/frontend/src/components/file-chooser/file-chooser.js

151 lines
4.4 KiB
JavaScript
Raw Normal View History

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';
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,
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,
isOtherRepoShow: false,
repoList: [],
2018-12-18 17:21:01 +08:00
currentRepoInfo: null,
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,
});
});
2019-01-04 18:30:03 +08:00
}
}
onOtherRepoToggle = () => {
if (!this.state.hasRequest) {
2019-01-04 18:30:03 +08:00
let that = this;
seafileAPI.listRepos().then(res => {
2018-12-18 17:21:01 +08:00
// todo optimized code
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)) {
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');
this.setState({
repoList: repoList,
isOtherRepoShow: !this.state.isOtherRepoShow,
});
});
2019-01-04 18:30:03 +08:00
}
else {
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);
this.setState({
selectedRepo: repo,
selectedPath: filePath
});
}
onRepoItemClick = (repo) => {
2018-12-28 14:25:25 +08:00
if (this.props.onRepoItemClick) {
this.props.onRepoItemClick(repo);
}
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}
/>
}
</div>
2019-01-04 18:30:03 +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}
/>
}
</div>
</div>
);
}
}
FileChooser.propTypes = propTypes;
export default FileChooser;