1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-02 23:48:47 +00:00
Files
seahub/frontend/src/components/file-chooser/file-chooser.js

133 lines
3.8 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';
import Repo from '../../models/repo';
import '../../css/file-chooser.css';
const propTypes = {
2018-11-28 12:41:49 +08:00
repoID: PropTypes.string.isRequired,
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: [],
currentRepo: null,
selectedRepo: null,
selectedPath: '',
};
}
componentDidMount() {
2018-11-28 12:41:49 +08:00
let repoID = this.props.repoID;
seafileAPI.getRepoInfo(repoID).then(res => {
let repo = new Repo(res.data);
this.setState({
currentRepo: repo,
});
});
}
onOtherRepoToggle = () => {
if (!this.state.hasRequest) {
let { currentRepo } = this.state;
seafileAPI.listRepos().then(res => {
let repos = res.data.repos;
let repoList = [];
let repoIdList = [];
for(let i = 0; i < repos.length; i++) {
if (repos[i].repo_name === currentRepo.repo_name || repos[i].permission !== 'rw') {
continue;
}
if (repoIdList.indexOf(repos[i].repo_id) > -1) {
continue;
}
repoList.push(repos[i]);
repoIdList.push(repos[i].repo_id);
}
this.setState({
repoList: repoList,
isOtherRepoShow: !this.state.isOtherRepoShow,
});
});
} else {
this.setState({isOtherRepoShow: !this.state.isOtherRepoShow});
}
}
2018-11-01 18:40:18 +08:00
onCurrentRepoToggle = () => [
this.setState({isCurrentRepoShow: !this.state.isCurrentRepoShow})
]
onDirentItemClick = (repo, filePath) => {
this.props.onDirentItemClick(repo, filePath);
this.setState({
selectedRepo: repo,
selectedPath: filePath
});
}
onRepoItemClick = (repo) => {
this.props.onRepoItemClick(repo);
this.setState({
selectedRepo: repo,
selectedPath: '',
});
}
render() {
return (
<div className="file-chooser-container">
<div className="list-view">
<div className="list-view-header">
2018-11-01 18:40:18 +08:00
<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>
{
2018-11-01 18:40:18 +08:00
this.state.isCurrentRepoShow && this.state.currentRepo &&
<RepoListView
initToShowChildren={true}
repo={this.state.currentRepo}
selectedRepo={this.state.selectedRepo}
selectedPath={this.state.selectedPath}
onRepoItemClick={this.onRepoItemClick}
onDirentItemClick={this.onDirentItemClick}
/>
}
</div>
<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}
/>
}
</div>
</div>
);
}
}
FileChooser.propTypes = propTypes;
export default FileChooser;