1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-18 09:12:55 +00:00
seahub/frontend/src/components/tree-view/tree-node-view.js

329 lines
9.8 KiB
JavaScript
Raw Normal View History

2018-08-06 10:29:12 +00:00
import React from 'react';
2018-10-16 10:19:51 +00:00
import PropTypes from 'prop-types';
import { permission } from '../../utils/constants';
2019-04-21 08:44:15 +00:00
import TextTranslation from '../../utils/text-translation';
2019-04-22 06:00:16 +00:00
import ItemDropdownMenu from '../dropdown-menu/item-dropdown-menu';
import { Utils } from '../../utils/utils';
2018-08-06 10:29:12 +00:00
2018-10-16 10:19:51 +00:00
const propTypes = {
userPerm: PropTypes.string,
2019-01-28 08:48:03 +00:00
node: PropTypes.object.isRequired,
2018-11-22 03:26:00 +00:00
currentPath: PropTypes.string.isRequired,
2018-10-16 10:19:51 +00:00
paddingLeft: PropTypes.number.isRequired,
2019-01-28 08:48:03 +00:00
isNodeMenuShow: PropTypes.bool.isRequired,
isItemFreezed: PropTypes.bool.isRequired,
onNodeClick: PropTypes.func.isRequired,
onNodeExpanded: PropTypes.func.isRequired,
onNodeCollapse: PropTypes.func.isRequired,
onNodeDragStart: PropTypes.func.isRequired,
2019-04-22 04:18:35 +00:00
freezeItem: PropTypes.func.isRequired,
unfreezeItem: PropTypes.func.isRequired,
onMenuItemClick: PropTypes.func,
2019-03-27 03:25:27 +00:00
onNodeDragMove: PropTypes.func,
onNodeDrop: PropTypes.func,
handleContextClick: PropTypes.func.isRequired,
onNodeDragEnter: PropTypes.func.isRequired,
onNodeDragLeave:PropTypes.func.isRequired,
2018-10-16 10:19:51 +00:00
};
2018-08-06 10:29:12 +00:00
class TreeNodeView extends React.Component {
constructor(props) {
super(props);
this.state = {
2019-01-28 08:48:03 +00:00
isHighlight: false,
2019-03-27 03:25:27 +00:00
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;
}
}
componentWillReceiveProps(nextProps) {
if (!nextProps.isItemFreezed) {
this.setState({
isShowOperationMenu: false,
isHighlight: false,
});
}
}
onMouseEnter = () => {
2019-01-28 08:48:03 +00:00
if (!this.props.isItemFreezed) {
this.setState({
2019-01-28 08:48:03 +00:00
isShowOperationMenu: true,
isHighlight: true,
});
}
}
2019-04-17 12:54:33 +00:00
onMouseOver = () => {
if (!this.props.isItemFreezed) {
this.setState({
isShowOperationMenu: true,
isHighlight: true,
});
}
}
onMouseLeave = () => {
2019-01-28 08:48:03 +00:00
if (!this.props.isItemFreezed) {
this.setState({
2019-01-28 08:48:03 +00:00
isShowOperationMenu: false,
isHighlight: false,
});
}
}
2019-01-28 08:48:03 +00:00
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;
}
2019-01-28 08:48:03 +00:00
this.props.onNodeClick(this.props.node);
}
2019-04-17 02:48:44 +00:00
onLoadToggle = (e) => {
e.stopPropagation();
2019-01-28 08:48:03 +00:00
let { node } = this.props;
if (node.isExpanded) {
this.props.onNodeCollapse(node);
} else {
this.props.onNodeExpanded(node);
2018-08-06 10:29:12 +00:00
}
}
2019-01-28 08:48:03 +00:00
onNodeDragStart = (e) => {
if (Utils.isIEBrower() || !this.canDrag) {
return false;
}
2019-01-28 08:48:03 +00:00
this.props.onNodeDragStart(e, this.props.node);
}
2018-08-06 10:29:12 +00:00
2019-03-27 03:25:27 +00:00
onNodeDragEnter = (e) => {
if (Utils.isIEBrower() || !this.canDrag) {
return false;
}
2019-03-27 03:25:27 +00:00
if (this.props.node.object.type === 'dir') {
this.setState({isNodeDropShow: true});
}
this.props.onNodeDragEnter(e, this.props.node);
}
onNodeDragMove = (e) => {
if (Utils.isIEBrower() || !this.canDrag) {
return false;
}
2019-03-27 03:25:27 +00:00
this.props.onNodeDragMove(e);
}
onNodeDragLeave = (e) => {
if (Utils.isIEBrower() || !this.canDrag) {
return false;
}
2019-03-27 03:25:27 +00:00
this.setState({isNodeDropShow: false});
this.props.onNodeDragLeave(e, this.props.node);
}
onNodeDrop = (e) => {
if (Utils.isIEBrower() || !this.canDrag) {
return false;
}
e.stopPropagation();
2019-03-27 03:25:27 +00:00
this.setState({isNodeDropShow: false});
this.props.onNodeDrop(e, this.props.node);
}
2019-04-22 04:18:35 +00:00
unfreezeItem = () => {
2019-01-28 08:48:03 +00:00
this.setState({isShowOperationMenu: false});
2019-04-22 04:18:35 +00:00
this.props.unfreezeItem();
2018-08-06 10:29:12 +00:00
}
2019-04-21 09:56:55 +00:00
onMenuItemClick = (operation, event, node) => {
2019-01-28 08:48:03 +00:00
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});
}
2019-01-28 08:48:03 +00:00
getNodeTypeAndIcon = () => {
let { node } = this.props;
let icon = '';
let type = '';
2019-01-28 08:48:03 +00:00
if (node.object.type === 'dir') {
icon = <i className="far fa-folder"></i>;
2018-08-06 10:29:12 +00:00
type = 'dir';
} else {
2019-01-28 08:48:03 +00:00
let index = node.object.name.lastIndexOf('.');
if (index === -1) {
icon = <i className="far fa-file"></i>;
2018-08-06 10:29:12 +00:00
type = 'file';
} else {
2019-01-28 08:48:03 +00:00
let suffix = node.object.name.slice(index).toLowerCase();
2019-02-26 02:34:09 +00:00
if (suffix === '.png' || suffix === '.jpg' || suffix === '.jpeg' || suffix === '.gif' || suffix === '.bmp') {
icon = <i className="far fa-image"></i>;
2018-08-06 10:29:12 +00:00
type = 'image';
}
2019-02-26 02:34:09 +00:00
else if (suffix === '.md' || suffix === '.markdown') {
icon = <i className="far fa-file-alt"></i>;
type = 'file';
}
else {
icon = <i className="far fa-file"></i>;
2018-08-06 10:29:12 +00:00
type = 'file';
}
}
}
2019-01-28 08:48:03 +00:00
return {icon, type};
}
2018-08-06 10:29:12 +00:00
calculateMenuList = (node) => {
2019-04-21 08:44:15 +00:00
let { NEW_FOLDER, NEW_FILE, COPY, MOVE, RENAME, DELETE, OPEN_VIA_CLIENT} = TextTranslation;
let menuList = [RENAME, DELETE, COPY, MOVE, OPEN_VIA_CLIENT];
2019-04-21 08:44:15 +00:00
if (node.object.type === 'dir') {
menuList = [NEW_FOLDER, NEW_FILE, COPY, MOVE, RENAME, DELETE];
2019-04-21 08:44:15 +00:00
}
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;
2019-04-21 08:44:15 +00:00
}
2019-01-28 08:48:03 +00:00
renderChildren = () => {
let { node, paddingLeft } = this.props;
if (!node.hasChildren()) {
return '';
}
return (
<div className="children" style={{paddingLeft: paddingLeft}}>
{node.children.map(item => {
return (
<TreeNodeView
2019-01-28 08:48:03 +00:00
key={item.path}
node={item}
paddingLeft={paddingLeft}
userPerm={this.props.userPerm}
2019-01-28 08:48:03 +00:00
currentPath={this.props.currentPath}
isNodeMenuShow={this.props.isNodeMenuShow}
isItemFreezed={this.props.isItemFreezed}
onNodeClick={this.props.onNodeClick}
onNodeCollapse={this.props.onNodeCollapse}
onNodeExpanded={this.props.onNodeExpanded}
2019-04-22 04:18:35 +00:00
freezeItem={this.props.freezeItem}
2019-04-21 09:56:55 +00:00
onMenuItemClick={this.props.onMenuItemClick}
2019-04-22 04:18:35 +00:00
unfreezeItem={this.unfreezeItem}
2019-03-27 03:25:27 +00:00
onNodeDragStart={this.props.onNodeDragStart}
onNodeDragMove={this.props.onNodeDragMove}
onNodeDrop={this.props.onNodeDrop}
onNodeDragEnter={this.props.onNodeDragEnter}
onNodeDragLeave={this.props.onNodeDragLeave}
handleContextClick={this.props.handleContextClick}
2019-01-28 08:48:03 +00:00
/>
);
})}
</div>
);
}
render() {
let { currentPath, node, isNodeMenuShow, userPerm } = this.props;
let { type, icon } = this.getNodeTypeAndIcon();
2019-01-28 08:48:03 +00:00
let hlClass = this.state.isHighlight ? 'tree-node-inner-hover ' : '';
if (node.path === currentPath) {
hlClass = 'tree-node-hight-light';
}
const { isCustomPermission } = Utils.getUserPermission(userPerm)
2018-08-06 10:29:12 +00:00
return (
2019-01-28 08:48:03 +00:00
<div className="tree-node">
<div
type={type}
className={`tree-node-inner text-nowrap ${hlClass} ${node.path === '/'? 'hide': ''} ${this.state.isNodeDropShow ? 'tree-node-drop' : ''}`}
title={node.object.name}
onMouseEnter={this.onMouseEnter}
2019-04-17 12:54:33 +00:00
onMouseOver={this.onMouseOver}
onMouseLeave={this.onMouseLeave}
onMouseDown={this.onItemMouseDown}
onContextMenu={this.onItemContextMenu}
onClick={this.onNodeClick}
>
<div className="tree-node-text" draggable={this.canDrag} onDragStart={this.onNodeDragStart} onDragEnter={this.onNodeDragEnter} onDragLeave={this.onNodeDragLeave} onDragOver={this.onNodeDragMove} onDrop={this.onNodeDrop}>{node.object.name}</div>
<div className="left-icon">
2019-01-28 08:48:03 +00:00
{type === 'dir' && (!node.isLoaded || (node.isLoaded && node.hasChildren())) && (
<i
2019-01-28 08:48:03 +00:00
className={`folder-toggle-icon fa ${node.isExpanded ? 'fa-caret-down' : 'fa-caret-right'}`}
onMouseDown={e => e.stopPropagation()}
onClick={this.onLoadToggle}
></i>
)}
<i className="tree-node-icon">{icon}</i>
</div>
2019-01-28 08:48:03 +00:00
{isNodeMenuShow && (
<div className="right-icon">
{((userPerm === 'rw' || permission || isCustomPermission) && this.state.isShowOperationMenu) && (
<ItemDropdownMenu
2019-04-22 06:00:16 +00:00
item={this.props.node}
2019-04-22 07:15:51 +00:00
toggleClass={'fas fa-ellipsis-v'}
getMenuList={this.calculateMenuList}
2019-01-28 08:48:03 +00:00
onMenuItemClick={this.onMenuItemClick}
2019-04-22 04:18:35 +00:00
freezeItem={this.props.freezeItem}
unfreezeItem={this.unfreezeItem}
2019-01-28 08:48:03 +00:00
/>
)}
</div>
)}
2018-08-06 10:29:12 +00:00
</div>
2019-01-28 08:48:03 +00:00
{node.isExpanded && this.renderChildren()}
2018-08-06 10:29:12 +00:00
</div>
);
}
}
2018-10-16 10:19:51 +00:00
TreeNodeView.propTypes = propTypes;
2018-08-06 10:29:12 +00:00
export default TreeNodeView;