import React, { Component, Fragment } from 'react'; import PropTypes from 'prop-types'; import { gettext, repoID, serviceUrl, slug, siteRoot } from '../../utils/constants'; import { seafileAPI } from '../../utils/seafile-api'; import Repo from '../../models/repo'; import Dirent from '../../models/dirent'; import CommonToolbar from '../../components/toolbar/common-toolbar'; import PathToolbar from '../../components/toolbar/path-toolbar'; import MarkdownViewer from '../../components/markdown-viewer'; import DirentListView from '../../components/dirent-list-view/dirent-list-view'; import DirentDetail from '../../components/dirent-detail/dirent-details'; import CreateFolder from '../../components/dialog/create-folder-dialog'; import CreateFile from '../../components/dialog/create-file-dialog'; const propTypes = { content: PropTypes.string, lastModified: PropTypes.string, latestContributor: PropTypes.string, permission: PropTypes.string, filePath: PropTypes.string.isRequired, isFileLoading: PropTypes.bool.isRequired, isViewFileState: PropTypes.bool.isRequired, changedNode: PropTypes.object, onMenuClick: PropTypes.func.isRequired, onSearchedClick: PropTypes.func.isRequired, onMainNavBarClick: PropTypes.func.isRequired, onLinkClick: PropTypes.func.isRequired, onMainItemClick: PropTypes.func.isRequired, onMainItemDelete: PropTypes.func.isRequired, onMainItemRename: PropTypes.func.isRequired, onMainItemMove: PropTypes.func.isRequired, onMainItemCopy: PropTypes.func.isRequired, onMainAddFile: PropTypes.func.isRequired, onMainAddFolder: PropTypes.func.isRequired, switchViewMode: PropTypes.func.isRequired, }; class MainPanel extends Component { constructor(props) { super(props); this.state = { isWikiMode: true, direntList: [], newMenuShow: false, uploadMenuShow: false, showFileDialog: false, showFolderDialog: false, createFileType: '', isDirentDetailShow: false, currentDirent: null, currentFilePath: '', isDirentListLoading: true, currentRepo: null, isRepoOwner: false, }; } componentDidMount() { seafileAPI.getRepoInfo(repoID).then(res => { let repo = new Repo(res.data); seafileAPI.getAccountInfo().then(res => { let user_email = res.data.email; let isRepoOwner = repo.owner_email === user_email; this.setState({ currentRepo: repo, isRepoOwner: isRepoOwner, }); }); }); document.addEventListener('click', this.hideOperationMenu); } componentWillReceiveProps(nextProps) { let node = nextProps.changedNode; if (node && node.isDir()) { let path = node.path; this.updateViewList(path); } } componentWillUnmount() { document.removeEventListener('click', this.hideOperationMenu); } updateViewList = (filePath) => { this.setState({isDirentListLoading: true}); seafileAPI.listDir(repoID, filePath).then(res => { let direntList = []; res.data.forEach(item => { let dirent = new Dirent(item); direntList.push(dirent); }); this.setState({ direntList: direntList, isDirentListLoading: false, }); }); } onMenuClick = () => { this.props.onMenuClick(); } onMainNavBarClick = (e) => { this.props.onMainNavBarClick(e.target.dataset.path); } switchViewMode = (e) => { e.preventDefault(); if (e.target.id === 'wiki') { return; } this.setState({isWikiMode: false}); this.props.switchViewMode(e.target.id); } onEditClick = (e) => { e.preventDefault(); window.location.href= serviceUrl + '/lib/' + repoID + '/file' + this.props.filePath + '?mode=edit'; } onUploadClick = (e) => { this.toggleOperationMenu(e); this.setState({ newMenuShow: false, uploadMenuShow: !this.state.uploadMenuShow, }); } onNewClick = (e) => { this.toggleOperationMenu(e); this.setState({ newMenuShow: !this.state.newMenuShow, uploadMenuShow: false, }); } onShareClick = () => { alert('share btn clicked'); } toggleOperationMenu = (e) => { e.nativeEvent.stopImmediatePropagation(); let targetRect = e.target.getClientRects()[0]; let left = targetRect.x; let top = targetRect.y + targetRect.height; let style = {position: 'fixed', display: 'block', left: left, top: top}; this.setState({operationMenuStyle: style}); } hideOperationMenu = () => { this.setState({ uploadMenuShow: false, newMenuShow: false, }); } addFolder = () => { this.setState({showFolderDialog: !this.showFolderDialog}); } addFile = () => { this.setState({ showFileDialog: !this.showFileDialog, createFileType: '', }); } addMarkdownFile = () => { this.setState({ showFileDialog: !this.showFileDialog, createFileType: '.md', }); } addFolderCancel = () => { this.setState({showFolderDialog: !this.state.showFolderDialog}); } addFileCancel = () => { this.setState({showFileDialog: !this.state.showFileDialog}); } onMainAddFile = (filePath, isDraft) => { this.setState({showFileDialog: !this.state.showFileDialog}); this.props.onMainAddFile(filePath, isDraft); } onMainAddFolder = (dirPath) => { this.setState({showFolderDialog: !this.state.showFolderDialog}); this.props.onMainAddFolder(dirPath); } onItemDetails = (dirent, direntPath) => { this.setState({ currentDirent: dirent, currentFilePath: direntPath, isDirentDetailShow: true, }); } onItemDetailsClose = () => { this.setState({isDirentDetailShow: false}); } render() { let filePathList = this.props.filePath.split('/'); let nodePath = ''; let pathElem = filePathList.map((item, index) => { if (item === '') { return; } if (index === (filePathList.length - 1)) { return ( /{item} ); } else { nodePath += '/' + item; return ( / {item} ); } }); return (
{ this.props.permission === 'rw' && } { !this.props.isViewFileState && }
{ this.state.uploadMenuShow &&
  • {gettext('Upload Files')}
  • {gettext('Upload Folder')}
} { this.state.newMenuShow &&
  • {gettext('New Folder')}
  • {gettext('New File')}
  • {gettext('New Markdown File')}
}
{gettext('Libraries')} / {this.props.filePath === '/' ? {slug} : {slug} } {pathElem}
{ this.props.isViewFileState ? : }
{ this.state.isDirentDetailShow &&
}
{this.state.showFileDialog && } {this.state.showFolderDialog && }
); } } MainPanel.propTypes = propTypes; export default MainPanel;