1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-05 09:10:05 +00:00
seahub/frontend/src/file-history-old.js

284 lines
9.1 KiB
JavaScript
Raw Normal View History

2019-04-19 10:23:02 +00:00
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom';
2019-04-24 03:55:48 +00:00
import { Button } from 'reactstrap';
2019-04-19 10:23:02 +00:00
import { Utils } from './utils/utils';
import { seafileAPI } from './utils/seafile-api';
2019-05-11 12:38:17 +00:00
import { gettext, PER_PAGE, filePath, fileName, historyRepoID, useNewAPI, canDownload, canCompare } from './utils/constants';
import editUtilities from './utils/editor-utilities';
2019-04-19 10:23:02 +00:00
import Loading from './components/loading';
import Logo from './components/logo';
import CommonToolbar from './components/toolbar/common-toolbar';
import HistoryItem from './pages/file-history-old/history-item';
import './assets/css/fa-solid.css';
import './assets/css/fa-regular.css';
import './assets/css/fontawesome.css';
import './css/layout.css';
import './css/toolbar.css';
import './css/search.css';
import './css/file-history-old.css';
class FileHistory extends React.Component {
constructor(props) {
super(props);
this.state = {
historyList: [],
currentPage: 1,
hasMore: false,
nextCommit: undefined,
filePath: '',
oldFilePath: '',
2019-04-19 10:23:02 +00:00
isLoading: true,
isReloadingData: false,
};
}
componentDidMount() {
if (useNewAPI) {
this.listNewHistoryRecords(filePath, PER_PAGE);
2019-04-19 10:23:02 +00:00
} else {
this.listOldHistoryRecords(historyRepoID, filePath);
2019-04-19 10:23:02 +00:00
}
}
listNewHistoryRecords = (filePath, PER_PAGE) => {
editUtilities.listFileHistoryRecords(filePath, 1, PER_PAGE).then(res => {
2019-04-23 02:45:42 +00:00
let historyData = res.data;
if (!historyData) {
this.setState({isLoading: false});
2019-04-23 02:55:00 +00:00
throw Error('There is an error in server.');
}
2019-04-24 06:49:39 +00:00
this.initNewRecords(res.data);
});
}
listOldHistoryRecords = (repoID, filePath) => {
seafileAPI.listOldFileHistoryRecords(repoID, filePath).then((res) => {
2019-04-23 02:45:42 +00:00
let historyData = res.data;
if (!historyData) {
this.setState({isLoading: false});
2019-04-23 02:55:00 +00:00
throw Error('There is an error in server.');
}
2019-04-24 06:49:39 +00:00
this.initOldRecords(res.data);
2019-04-19 10:23:02 +00:00
});
}
2019-04-24 06:49:39 +00:00
initNewRecords(result) {
2019-05-28 10:21:47 +00:00
if (result.total_count < 5) {
if (result.data.length) {
let commitID = result.data[result.data.length-1].commit_id;
2019-10-10 09:53:28 +00:00
let path = result.data[result.data.length-1].path;
let oldPath = result.data[result.data.length-1].old_path;
path = oldPath ? oldPath : path;
seafileAPI.listOldFileHistoryRecords(historyRepoID, path, commitID).then((res) => {
2019-05-28 10:21:47 +00:00
if (!res.data) {
this.setState({isLoading: false});
throw Error('There is an error in server.');
}
this.setState({
historyList: result.data.concat(res.data.data.slice(1, res.data.data.length)),
isLoading: false,
});
});
} else {
seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath).then((res) => {
if (!res.data) {
this.setState({isLoading: false});
throw Error('There is an error in server.');
}
this.setState({
historyList: res.data.data,
isLoading: false,
});
});
}
} else {
this.setState({
historyList: result.data,
currentPage: result.page,
hasMore: result.total_count > (PER_PAGE * this.state.currentPage),
isLoading: false,
});
}
2019-04-24 06:49:39 +00:00
}
initOldRecords(result) {
if (result.data.length) {
2019-04-23 02:18:30 +00:00
this.setState({
2019-04-24 06:49:39 +00:00
historyList: result.data,
2019-04-23 02:18:30 +00:00
nextCommit: result.next_start_commit,
2019-04-24 06:49:39 +00:00
filePath: result.data[result.data.length-1].path,
oldFilePath: result.data[result.data.length-1].rev_renamed_old_path,
isLoading: false,
2019-04-23 02:18:30 +00:00
});
2019-04-24 06:49:39 +00:00
} else {
this.setState({nextCommit: result.next_start_commit,});
2019-04-23 02:18:30 +00:00
if (this.state.nextCommit) {
seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath, this.state.nextCommit).then((res) => {
2019-04-24 06:49:39 +00:00
this.initOldRecords(res.data);
2019-04-23 02:18:30 +00:00
});
} else {
this.setState({isLoading: false});
2019-04-23 02:18:30 +00:00
}
2019-04-19 10:23:02 +00:00
}
}
onScrollHandler = (event) => {
2019-04-24 06:49:39 +00:00
const clientHeight = event.target.clientHeight;
const scrollHeight = event.target.scrollHeight;
const scrollTop = event.target.scrollTop;
const isBottom = (clientHeight + scrollTop + 1 >= scrollHeight);
let hasMore = this.state.hasMore;
if (isBottom && hasMore) {
this.reloadMore();
2019-04-19 10:23:02 +00:00
}
}
reloadMore = () => {
if (!this.state.isReloadingData) {
if (useNewAPI) {
let currentPage = this.state.currentPage + 1;
this.setState({
currentPage: currentPage,
isReloadingData: true,
});
editUtilities.listFileHistoryRecords(filePath, currentPage, PER_PAGE).then(res => {
2019-04-24 06:49:39 +00:00
this.updateNewRecords(res.data);
});
} else {
let commitID = this.state.nextCommit;
let filePath = this.state.filePath;
let oldFilePath = this.state.oldFilePath;
this.setState({isReloadingData: true});
if (oldFilePath) {
seafileAPI.listOldFileHistoryRecords(historyRepoID, oldFilePath, commitID).then((res) => {
2019-04-26 06:35:30 +00:00
this.updateOldRecords(res.data, oldFilePath);
});
} else {
seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath, commitID).then((res) => {
2019-04-26 06:35:30 +00:00
this.updateOldRecords(res.data, filePath);
});
}
}
2019-04-19 10:23:02 +00:00
}
}
2019-04-24 06:49:39 +00:00
updateNewRecords(result) {
this.setState({
historyList: [...this.state.historyList, ...result.data],
currentPage: result.page,
hasMore: result.total_count > (PER_PAGE * this.state.currentPage),
2019-04-26 07:34:22 +00:00
isReloadingData: false,
2019-04-24 06:49:39 +00:00
});
}
2019-04-26 06:35:30 +00:00
updateOldRecords(result, filePath) {
if (result.data.length) {
this.setState({
2019-04-24 06:49:39 +00:00
historyList: [...this.state.historyList, ...result.data],
nextCommit: result.next_start_commit,
2019-04-24 06:49:39 +00:00
filePath: result.data[result.data.length-1].path,
oldFilePath: result.data[result.data.length-1].rev_renamed_old_path,
2019-04-26 07:34:22 +00:00
isReloadingData: false,
});
2019-04-24 06:49:39 +00:00
} else {
this.setState({nextCommit: result.next_start_commit,});
2019-04-23 02:18:30 +00:00
if (this.state.nextCommit) {
seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath, this.state.nextCommit).then((res) => {
2019-04-26 06:35:30 +00:00
this.updateOldRecords(res.data, filePath);
2019-04-23 02:18:30 +00:00
});
}
2019-04-19 10:23:02 +00:00
}
}
onItemRestore = (item) => {
let commitId = item.commit_id;
let filePath = item.path;
editUtilities.revertFile(filePath, commitId).then(res => {
2019-04-19 10:23:02 +00:00
if (res.data.success) {
this.setState({isLoading: true});
this.refershFileList();
}
});
}
refershFileList() {
if (useNewAPI) {
editUtilities.listFileHistoryRecords(filePath, 1, PER_PAGE).then((res) => {
2019-04-24 06:49:39 +00:00
this.initNewRecords(res.data);
});
} else {
seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath).then((res) => {
2019-04-24 06:49:39 +00:00
this.initOldRecords(res.data);
});
}
}
2019-04-26 03:34:11 +00:00
onSearchedClick = (searchedItem) => {
Utils.handleSearchedItemClick(searchedItem);
}
2019-04-19 10:23:02 +00:00
render() {
return (
<Fragment>
<div id="header" className="old-history-header">
<div className="logo">
2019-04-25 02:50:06 +00:00
<Logo showCloseSidePanelIcon={false}/>
2019-04-19 10:23:02 +00:00
</div>
<div className='toolbar'>
<CommonToolbar onSearchedClick={this.onSearchedClick} />
</div>
</div>
<div id="main" onScroll={this.onScrollHandler}>
<div className="old-history-main">
2019-04-26 07:33:11 +00:00
<Fragment>
2019-04-19 10:23:02 +00:00
<a href="javascript:window.history.back()" className="go-back" title="Back">
<span className="fas fa-chevron-left"></span>
</a>
<h2><span className="file-name">{fileName}</span>{' '}{gettext('History Versions')}</h2>
2019-04-26 07:33:11 +00:00
</Fragment>
<Fragment>
<table className="commit-list">
<thead>
<tr>
<th width="40%" >{gettext('Time')}</th>
<th width="30%" >{gettext('Modifier')}</th>
<th width="25%" >{gettext('Size')}</th>
<th width="5%" ></th>
</tr>
</thead>
{!this.state.isLoading &&
2019-04-19 10:23:02 +00:00
<tbody>
{this.state.historyList.map((item, index) => {
return (
<HistoryItem
key={index}
item={item}
index={index}
2019-04-24 13:27:12 +00:00
canDownload={canDownload}
canCompare={canCompare}
2019-04-19 10:23:02 +00:00
onItemRestore={this.onItemRestore}
/>
);
})}
</tbody>
2019-04-26 07:33:11 +00:00
}
</table>
{(this.state.isReloadingData || this.state.isLoading) && <Loading />}
2019-04-24 06:49:39 +00:00
{this.state.nextCommit && !this.state.isLoading && !this.state.isReloadingData &&
<Button className="get-more-btn" onClick={this.reloadMore}>{gettext('More')}</Button>
}
2019-04-26 07:33:11 +00:00
</Fragment>
2019-04-19 10:23:02 +00:00
</div>
</div>
</Fragment>
);
}
}
ReactDOM.render (
<FileHistory />,
document.getElementById('wrapper')
);