import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import MD5 from 'MD5';
import { UncontrolledTooltip } from 'reactstrap';
import { gettext, siteRoot, mediaUrl } from '../../utils/constants';
import { Utils } from '../../utils/utils';
import { seafileAPI } from '../../utils/seafile-api';
import URLDecorator from '../../utils/url-decorator';
import ItemDropDownMenu from '../dropdown-menu/item-dropdown-menu';
import Rename from '../rename';
import ModalPortal from '../modal-portal';
import MoveDirentDialog from '../dialog/move-dirent-dialog';
import CopyDirentDialog from '../dialog/copy-dirent-dialog';
import ShareDialog from '../dialog/share-dialog';
import ZipDownloadDialog from '../dialog/zip-download-dialog';
import EditFileTagDialog from '../dialog/edit-filetag-dialog';
import '../../css/dirent-list-item.css';
const propTypes = {
path: PropTypes.string.isRequired,
repoID: PropTypes.string.isRequired,
isItemFreezed: PropTypes.bool.isRequired,
showShareBtn: PropTypes.bool.isRequired,
dirent: PropTypes.object.isRequired,
onItemClick: PropTypes.func.isRequired,
onFreezedItem: PropTypes.func.isRequired,
onUnfreezedItem: PropTypes.func.isRequired,
onItemRenameToggle: PropTypes.func.isRequired,
onItemSelected: PropTypes.func.isRequired,
onItemDelete: PropTypes.func.isRequired,
onItemRename: PropTypes.func.isRequired,
onItemMove: PropTypes.func.isRequired,
onItemCopy: PropTypes.func.isRequired,
onDirentClick: PropTypes.func.isRequired,
updateDirent: PropTypes.func.isRequired,
showImagePopup: PropTypes.func.isRequired,
currentRepoInfo: PropTypes.object,
isRepoOwner: PropTypes.bool,
isAdmin: PropTypes.bool.isRequired,
repoEncrypted: PropTypes.bool.isRequired,
isGroupOwnedRepo: PropTypes.bool.isRequired,
onItemMouseDown: PropTypes.func.isRequired,
onItemContextMenu: PropTypes.func.isRequired,
selectedDirentList: PropTypes.array.isRequired,
activeDirent: PropTypes.object,
getDirentItemMenuList: PropTypes.func.isRequired,
};
class DirentListItem extends React.Component {
constructor(props) {
super(props);
this.state = {
isOperationShow: false,
highlight: false,
isZipDialogOpen: false,
isMoveDialogShow: false,
isCopyDialogShow: false,
isShareDialogShow: false,
isMutipleOperation: false,
isShowTagTooltip: false,
isDragTipShow: false,
isDropTipshow: false,
isEditFileTagShow: false,
};
}
componentWillReceiveProps(nextProps) {
if (!nextProps.isItemFreezed) {
this.setState({
highlight: false,
isOperationShow: false,
}, () => {
if (nextProps.activeDirent && nextProps.activeDirent.name === nextProps.dirent.name) {
this.setState({isOperationShow: true});
}
});
}
}
//UI Interactive
onMouseEnter = () => {
if (!this.props.isItemFreezed) {
this.setState({
highlight: true,
isOperationShow: true,
});
}
this.setState({isDragTipShow: true});
}
onMouseOver = () => {
if (!this.props.isItemFreezed) {
this.setState({
highlight: true,
isOperationShow: true,
});
}
this.setState({isDragTipShow: true});
}
onMouseLeave = () => {
if (!this.props.isItemFreezed) {
this.setState({
highlight: false,
isOperationShow: false,
});
}
this.setState({isDragTipShow: false});
}
onUnfreezedItem = () => {
this.setState({
highlight: false,
isOperationShow: false,
});
this.props.onUnfreezedItem();
}
//buiness handler
onItemSelected = () => {
this.props.onItemSelected(this.props.dirent);
}
onItemStarred = () => {
let dirent = this.props.dirent;
let repoID = this.props.repoID;
let filePath = this.getDirentPath(dirent);
if (dirent.starred) {
seafileAPI.unstarItem(repoID, filePath).then(() => {
this.props.updateDirent(this.props.dirent, 'starred', false);
});
} else {
seafileAPI.starItem(repoID, filePath).then(() => {
this.props.updateDirent(this.props.dirent, 'starred', true);
});
}
}
// on '
'
onDirentClick = (e) => {
// '' is clicked
e.stopPropagation();
if (e.target.tagName == 'TD') {
this.props.onDirentClick(this.props.dirent);
}
}
onItemClick = (e) => {
e.preventDefault();
const dirent = this.props.dirent;
if (Utils.imageCheck(dirent.name)) {
this.props.showImagePopup(dirent);
} else {
this.props.onItemClick(dirent);
}
}
onItemDelete = (e) => {
e.nativeEvent.stopImmediatePropagation(); //for document event
this.props.onItemDelete(this.props.dirent);
}
onItemShare = (e) => {
e.nativeEvent.stopImmediatePropagation(); //for document event
this.setState({isShareDialogShow: !this.state.isShareDialogShow});
}
closeSharedDialog = () => {
this.setState({isShareDialogShow: !this.state.isShareDialogShow});
}
onMenuItemClick = (operation, opDirent, event) => {
switch(operation) {
case 'Download':
this.onItemDownload(event);
break;
case 'Share':
this.onItemShare(event);
break;
case 'Delete':
this.onItemDelete(event);
break;
case 'Rename':
this.onItemRenameToggle();
break;
case 'Move':
this.onItemMoveToggle();
break;
case 'Copy':
this.onItemCopyToggle();
break;
case 'Tags':
this.onEditFileTagToggle();
break;
case 'Permission':
this.onPermissionItem();
break;
case 'Unlock':
this.onUnlockItem();
break;
case 'Lock':
this.onLockItem();
break;
case 'Comment':
this.onComnentItem();
break;
case 'History':
this.onHistory();
break;
case 'Access Log':
this.onAccessLog();
break;
case 'Open via Client':
this.onOpenViaClient();
break;
default:
break;
}
}
onEditFileTagToggle = () => {
this.setState({
isEditFileTagShow: !this.state.isEditFileTagShow
});
}
onFileTagChanged = () => {
let direntPath = this.getDirentPath(this.props.dirent);
this.props.onFileTagChanged(this.props.dirent, direntPath);
}
onItemRenameToggle = () => {
this.props.onItemRenameToggle(this.props.dirent);
this.setState({
isOperationShow: false,
isRenameing: true,
});
}
onRenameConfirm = (newName) => {
this.props.onItemRename(this.props.dirent, newName);
this.onRenameCancel();
}
onRenameCancel = () => {
this.setState({isRenameing: false});
this.onUnfreezedItem();
}
onItemMoveToggle = () => {
this.setState({isMoveDialogShow: !this.state.isMoveDialogShow});
}
onItemCopyToggle = () => {
this.setState({isCopyDialogShow: !this.state.isCopyDialogShow});
}
onPermissionItem = () => {
}
onLockItem = () => {
let repoID = this.props.repoID;
let filePath = this.getDirentPath(this.props.dirent);
seafileAPI.lockfile(repoID, filePath).then(() => {
this.props.updateDirent(this.props.dirent, 'is_locked', true);
this.props.updateDirent(this.props.dirent, 'locked_by_me', true);
});
}
onUnlockItem = () => {
let repoID = this.props.repoID;
let filePath = this.getDirentPath(this.props.dirent);
seafileAPI.unlockfile(repoID, filePath).then(() => {
this.props.updateDirent(this.props.dirent, 'is_locked', false);
this.props.updateDirent(this.props.dirent, 'locked_by_me', false);
});
}
onComnentItem = () => {
}
onHistory = () => {
let repoID = this.props.repoID;
let filePath = this.getDirentPath(this.props.dirent);
let url = URLDecorator.getUrl({type: 'file_revisions', repoID: repoID, filePath: filePath});
location.href = url;
}
onAccessLog = () => {
}
onOpenViaClient = () => {
let repoID = this.props.repoID;
let filePath = this.getDirentPath(this.props.dirent);
let url = URLDecorator.getUrl({type: 'open_via_client', repoID: repoID, filePath: filePath});
location.href = url;
}
onItemDownload = (e) => {
e.nativeEvent.stopImmediatePropagation();
let dirent = this.props.dirent;
let repoID = this.props.repoID;
let direntPath = this.getDirentPath(dirent);
if (dirent.type === 'dir') {
this.setState({
isZipDialogOpen: true
});
} else {
let url = URLDecorator.getUrl({type: 'download_file_url', repoID: repoID, filePath: direntPath});
location.href = url;
}
}
closeZipDialog = () => {
this.setState({
isZipDialogOpen: false
});
}
getDirentPath = (dirent) => {
let path = this.props.path;
return path === '/' ? path + dirent.name : path + '/' + dirent.name;
}
onTagTooltipToggle = (e) => {
e.stopPropagation();
this.setState({isShowTagTooltip: !this.state.isShowTagTooltip});
}
onItemMove = (destRepo, dirent, selectedPath, currentPath) => {
this.props.onItemMove(destRepo, dirent, selectedPath, currentPath);
}
onItemDragStart = (e) => {
let nodeRootPath = '';
nodeRootPath = this.props.path === '/' ? `${this.props.path}${this.props.dirent.name}` : this.props.path;
let dragStartItemData = {nodeDirent: this.props.dirent, nodeParentPath: this.props.path, nodeRootPath: nodeRootPath};
dragStartItemData = JSON.stringify(dragStartItemData);
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setDragImage(this.refs.drag_icon, 15, 15);
e.dataTransfer.setData('applicaiton/drag-item-info', dragStartItemData);
}
onItemDragEnter = () => {
if (this.props.dirent.type === 'dir') {
this.setState({isDropTipshow: true});
}
}
onItemDragOver = (e) => {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
}
onItemDragLeave = () => {
this.setState({isDropTipshow: false});
}
onItemDragDrop = (e) => {
this.setState({isDropTipshow: false});
if (e.dataTransfer.files.length) { // uploaded files
return;
}
let dragStartItemData = e.dataTransfer.getData('applicaiton/drag-item-info');
dragStartItemData = JSON.parse(dragStartItemData);
let {nodeDirent, nodeParentPath, nodeRootPath} = dragStartItemData;
let dropItemData = this.props.dirent;
if (nodeDirent.name === dropItemData.name) {
return;
}
if (dropItemData.type !== 'dir') {
return;
}
// copy the dirent to it's child. eg: A/B -> A/B/C
if (dropItemData.type === 'dir' && nodeDirent.type === 'dir') {
if (nodeParentPath !== this.props.path) {
if (this.props.path.indexOf(nodeRootPath) !== -1) {
return;
}
}
}
let selectedPath = Utils.joinPath(this.props.path, this.props.dirent.name);
this.onItemMove(this.props.currentRepoInfo, nodeDirent, selectedPath, nodeParentPath);
}
onItemMouseDown = (event) => {
this.props.onItemMouseDown(event);
}
onItemContextMenu = (event) => {
let dirent = this.props.dirent;
this.props.onItemContextMenu(event, dirent);
}
renderItemOperation = () => {
let { dirent, selectedDirentList, currentRepoInfo } = this.props;
if (currentRepoInfo.permission === 'cloud-edit' || currentRepoInfo.permission === 'preview') {
return '';
}
return (
{selectedDirentList.length > 1 ?
{this.state.isOperationShow && !dirent.isSelected &&
-
{this.props.showShareBtn &&
-
}
-
-
}
:
{this.state.isOperationShow &&
-
{this.props.showShareBtn &&
-
}
-
-
}
}
)
}
render() {
let { path, dirent, activeDirent } = this.props;
let direntPath = Utils.joinPath(path, dirent.name);
let dirHref = '';
if (this.props.currentRepoInfo) {
dirHref = siteRoot + 'library/' + this.props.repoID + '/' + this.props.currentRepoInfo.repo_name + Utils.encodePath(direntPath);
}
let fileHref = siteRoot + 'lib/' + this.props.repoID + '/file' + Utils.encodePath(direntPath);
let toolTipID = MD5(dirent.name).slice(0, 7);
let tagTitle = '';
if (dirent.file_tags && dirent.file_tags.length > 0) {
dirent.file_tags.forEach(item => {
tagTitle += item.name + ' ';
});
}
let iconUrl = Utils.getDirentIcon(dirent);
let trClass = this.state.highlight ? 'tr-highlight ' : '';
trClass += this.state.isDropTipshow ? 'tr-drop-effect' : '';
trClass += (activeDirent && activeDirent.name === dirent.name) ? 'tr-active' : '';
trClass += dirent.isSelected? 'tr-active' : '';
return (
|
{dirent.starred !== undefined && !dirent.starred && }
{dirent.starred !== undefined && dirent.starred && }
|
{dirent.encoded_thumbnail_src ?
 :

}
{dirent.is_locked &&  }
|
{this.state.isRenameing ?
:
{dirent.name}
}
|
{(dirent.type !== 'dir' && dirent.file_tags) && (
{dirent.file_tags.map((fileTag, index) => {
let length = dirent.file_tags.length;
return (
);
})}
{tagTitle}
)}
|
{this.renderItemOperation()} |
{dirent.size && dirent.size} |
{dirent.mtime_relative} |
{this.state.isMoveDialogShow &&
}
{this.state.isCopyDialogShow &&
}
{this.state.isEditFileTagShow &&
}
{this.state.isZipDialogOpen &&
}
{this.state.isShareDialogShow &&
}
);
}
}
DirentListItem.propTypes = propTypes;
export default DirentListItem;
|