import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { gettext, repoID } from '../../utils/constants'; import URLDecorator from '../../utils/url-decorator'; import editorUtilities from '../../utils/editor-utilties'; import Loading from '../loading'; import DirentListItem from './dirent-list-item'; import ZipDownloadDialog from '../dialog/zip-download-dialog'; import MoveDirentDialog from '../dialog/move-dirent-dialog'; import CopyDirentDialog from '../dialog/copy-dirent-dialog'; const propTypes = { path: PropTypes.string.isRequired, direntList: PropTypes.array.isRequired, onItemDelete: PropTypes.func.isRequired, onItemRename: PropTypes.func.isRequired, onItemClick: PropTypes.func.isRequired, onItemMove: PropTypes.func.isRequired, onItemCopy: PropTypes.func.isRequired, onItemDetails: PropTypes.func.isRequired, updateDirent: PropTypes.func.isRequired, isDirentListLoading: PropTypes.bool.isRequired, isRepoOwner: PropTypes.bool, currentRepo: PropTypes.object, }; class DirentListView extends React.Component { constructor(props) { super(props); this.state = { progress: 0, isItemFreezed: false, isProgressDialogShow: false, isMoveDialogShow: false, isCopyDialogShow: false, currentDirent: false, direntPath: '', }; } onFreezedItem = () => { this.setState({isItemFreezed: true}); } onUnfreezedItem = () => { this.setState({isItemFreezed: false}); } onRenameMenuItemClick = () => { this.onFreezedItem(); } onDirentItemMove = (dirent, direntPath) => { this.setState({ isMoveDialogShow: true, currentDirent: dirent, direntPath: direntPath }); } onDirentItemCopy = (dirent, direntPath) => { this.setState({ isCopyDialogShow: true, currentDirent: dirent, direntPath: direntPath }); } onItemMove = (repo, direntPath, moveToDirentPath) => { this.props.onItemMove(repo, direntPath, moveToDirentPath); } onCancelMove = () => { this.setState({isMoveDialogShow: false}); } onItemCopy = (repo, direntPath, copyToDirentPath) => { this.props.onItemCopy(repo, direntPath, copyToDirentPath); } onCancelCopy = () => { this.setState({isCopyDialogShow: false}); } onItemDetails = (dirent, direntPath) => { this.props.onItemDetails(dirent, direntPath); } onItemDownload = (dirent, direntPath) => { if (dirent.type === 'dir') { this.setState({isProgressDialogShow: true, progress: 0}); editorUtilities.zipDownload(this.props.path, dirent.name).then(res => { this.zip_token = res.data['zip_token']; this.addDownloadAnimation(); this.interval = setInterval(this.addDownloadAnimation, 1000); }); } else { let url = URLDecorator.getUrl({type: 'download_file_url', repoID: repoID, filePath: direntPath}); location.href = url; } } addDownloadAnimation = () => { let _this = this; let token = this.zip_token; editorUtilities.queryZipProgress(token).then(res => { let data = res.data; let progress = data.total === 0 ? 100 : (data.zipped / data.total * 100).toFixed(0); this.setState({progress: parseInt(progress)}); if (data['total'] === data['zipped']) { this.setState({ progress: 100 }); clearInterval(this.interval); location.href = URLDecorator.getUrl({type: 'download_dir_zip_url', token: token}); setTimeout(function() { _this.setState({isProgressDialogShow: false}); }, 500); } }); } onCancelDownload = () => { let zip_token = this.zip_token; editorUtilities.cancelZipTask(zip_token).then(res => { this.setState({ isProgressDialogShow: false, }); }); } render() { const { direntList } = this.props; if (this.props.isDirentListLoading) { return (); } return ( { direntList.length !== 0 && direntList.map((dirent, index) => { return ( ); }) }
{gettext('Name')} {gettext('Size')} {gettext('Last Update')}
{this.state.isProgressDialogShow && } {this.state.isMoveDialogShow && } {this.state.isCopyDialogShow && }
); } } DirentListView.propTypes = propTypes; export default DirentListView;