import React from 'react'; import PropTypes from 'prop-types'; import { permission } from '../../utils/constants'; import TextTranslation from '../../utils/text-translation'; import ItemDropdownMenu from '../dropdown-menu/item-dropdown-menu'; import { Utils, isMobile } from '../../utils/utils'; const LEFT_INDENT = 20; const propTypes = { userPerm: PropTypes.string, node: PropTypes.object.isRequired, currentPath: PropTypes.string.isRequired, leftIndent: PropTypes.number.isRequired, isNodeMenuShow: PropTypes.bool.isRequired, isItemFreezed: PropTypes.bool.isRequired, onNodeClick: PropTypes.func.isRequired, onNodeExpanded: PropTypes.func.isRequired, onNodeCollapse: PropTypes.func.isRequired, onNodeDragStart: PropTypes.func.isRequired, freezeItem: PropTypes.func.isRequired, unfreezeItem: PropTypes.func.isRequired, onMenuItemClick: PropTypes.func, onNodeDragMove: PropTypes.func, onNodeDrop: PropTypes.func, handleContextClick: PropTypes.func.isRequired, onNodeDragEnter: PropTypes.func.isRequired, onNodeDragLeave: PropTypes.func.isRequired, isDisplayFiles: PropTypes.bool, }; class TreeNodeView extends React.Component { constructor(props) { super(props); this.state = { isHighlight: false, isShowOperationMenu: false, isNodeDropShow: false, }; const { userPerm } = props; this.canDrag = userPerm === 'rw'; const { isCustomPermission, customPermission } = Utils.getUserPermission(userPerm); if (isCustomPermission) { const { modify } = customPermission.permission; this.canDrag = modify; } } UNSAFE_componentWillReceiveProps(nextProps) { if (!nextProps.isItemFreezed) { this.setState({ isShowOperationMenu: false, isHighlight: false, }); } } onMouseEnter = () => { if (!this.props.isItemFreezed) { this.setState({ isShowOperationMenu: true, isHighlight: true, }); } }; onMouseOver = () => { if (!this.props.isItemFreezed) { this.setState({ isShowOperationMenu: true, isHighlight: true, }); } }; onMouseLeave = () => { if (!this.props.isItemFreezed) { this.setState({ isShowOperationMenu: false, isHighlight: false, }); } }; onNodeClick = () => { const { node } = this.props; const { object } = node; if (object.isDir()) { this.props.onNodeClick(this.props.node); return; } const { isCustomPermission, customPermission } = Utils.getUserPermission(object.permission); if (isCustomPermission) { const { preview: canPreview, modify: canModify } = customPermission.permission; if (!canPreview && !canModify) return; } this.props.onNodeClick(this.props.node); }; onLoadToggle = (e) => { e.stopPropagation(); let { node } = this.props; if (node.isExpanded) { this.props.onNodeCollapse(node); } else { this.props.onNodeExpanded(node); } }; onNodeDragStart = (e) => { if (Utils.isIEBrowser() || !this.canDrag) { return false; } this.props.onNodeDragStart(e, this.props.node); }; onNodeDragEnter = (e) => { if (Utils.isIEBrowser() || !this.canDrag) { return false; } if (this.props.node.object.type === 'dir') { this.setState({ isNodeDropShow: true }); } this.props.onNodeDragEnter(e, this.props.node); }; onNodeDragMove = (e) => { if (Utils.isIEBrowser() || !this.canDrag) { return false; } this.props.onNodeDragMove(e); }; onNodeDragLeave = (e) => { if (Utils.isIEBrowser() || !this.canDrag) { return false; } this.setState({ isNodeDropShow: false }); this.props.onNodeDragLeave(e, this.props.node); }; onNodeDrop = (e) => { if (Utils.isIEBrowser() || !this.canDrag) { return false; } e.stopPropagation(); this.setState({ isNodeDropShow: false }); this.props.onNodeDrop(e, this.props.node); }; unfreezeItem = () => { this.setState({ isShowOperationMenu: false }); this.props.unfreezeItem(); }; onMenuItemClick = (operation, event, node) => { this.props.onMenuItemClick(operation, node); }; onItemMouseDown = (event) => { event.stopPropagation(); if (event.button === 2) { return; } }; onItemContextMenu = (event) => { this.handleContextClick(event); }; handleContextClick = (event) => { this.props.handleContextClick(event, this.props.node); this.setState({ isShowOperationMenu: false }); }; getNodeTypeAndIcon = () => { let { node } = this.props; let icon = ''; let type = ''; if (node.object.type === 'dir') { icon = ; type = 'dir'; } else { let index = node.object.name.lastIndexOf('.'); if (index === -1) { icon = ; type = 'file'; } else { let suffix = node.object.name.slice(index).toLowerCase(); if (suffix === '.png' || suffix === '.jpg' || suffix === '.jpeg' || suffix === '.gif' || suffix === '.bmp') { icon = ; type = 'image'; } else if (suffix === '.md' || suffix === '.markdown') { icon = ; type = 'file'; } else { icon = ; type = 'file'; } } } return { icon, type }; }; calculateMenuList = (node) => { let { NEW_FOLDER, NEW_FILE, COPY, MOVE, RENAME, DELETE, OPEN_VIA_CLIENT } = TextTranslation; let menuList = [RENAME, DELETE, COPY, MOVE, OPEN_VIA_CLIENT]; if (node.object.type === 'dir') { menuList = [NEW_FOLDER, NEW_FILE, COPY, MOVE, RENAME, DELETE]; } const { userPerm } = this.props; const { isCustomPermission, customPermission } = Utils.getUserPermission(userPerm); if (!isCustomPermission) { return menuList; } menuList = []; const { create: canCreate, modify: canModify, delete: canDelete, copy: canCopy } = customPermission.permission; if (node.object.type === 'dir') { canCreate && menuList.push(NEW_FOLDER, NEW_FILE); } canCopy && menuList.push(COPY); canModify && menuList.push(MOVE, RENAME); canDelete && menuList.push(DELETE); if (node.object.type !== 'dir') { menuList.push(OPEN_VIA_CLIENT); } return menuList; }; renderChildren = () => { let { node } = this.props; if (!node.hasChildren()) { return ''; } return (