2018-08-06 10:29:12 +00:00
|
|
|
import React from 'react';
|
2018-08-22 08:39:42 +00:00
|
|
|
import MenuControl from '../menu-component/node-menu-control'
|
2018-08-06 10:29:12 +00:00
|
|
|
|
|
|
|
function sortByType(a, b) {
|
|
|
|
if (a.type == "dir" && b.type != "dir") {
|
|
|
|
return -1;
|
|
|
|
} else if (a.type != "dir" && b.type == "dir") {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TreeNodeView extends React.Component {
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isMenuIconShow: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick = (e) => {
|
|
|
|
// e.nativeEvent.stopImmediatePropagation();
|
|
|
|
let { node } = this.props;
|
|
|
|
this.props.treeView.onNodeClick(e, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseEnter = () => {
|
|
|
|
if (!this.props.isNodeItemFrezee) {
|
|
|
|
this.setState({
|
|
|
|
isMenuIconShow: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseLeave = () => {
|
|
|
|
if (!this.props.isNodeItemFrezee) {
|
|
|
|
this.setState({
|
|
|
|
isMenuIconShow: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCollapse = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
const { node } = this.props;
|
|
|
|
if (this.props.treeView.toggleCollapse) {
|
|
|
|
this.props.treeView.toggleCollapse(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onDragStart = (e) => {
|
|
|
|
const { node } = this.props;
|
|
|
|
this.props.treeView.onDragStart(e, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMenuControlClick = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.nativeEvent.stopImmediatePropagation();
|
|
|
|
const { node } = this.props;
|
|
|
|
this.props.treeView.onShowContextMenu(e, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
hideMenuIcon = () => {
|
|
|
|
this.setState({
|
|
|
|
isMenuIconShow: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
document.addEventListener('click', this.hideMenuIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('click', this.hideMenuIcon);
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:29:12 +00:00
|
|
|
renderCollapse = () => {
|
|
|
|
const { node } = this.props;
|
|
|
|
|
|
|
|
if (node.hasChildren()) {
|
|
|
|
const { isExpanded } = node;
|
|
|
|
return (
|
|
|
|
<i
|
|
|
|
className={isExpanded ? 'folder-toggle-icon fa fa-caret-down' : 'folder-toggle-icon fa fa-caret-right'}
|
|
|
|
onMouseDown={e => e.stopPropagation()}
|
|
|
|
onClick={this.handleCollapse}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderChildren = () => {
|
|
|
|
const { node } = this.props;
|
|
|
|
if (node.children && node.children.length) {
|
|
|
|
const childrenStyles = {
|
|
|
|
paddingLeft: this.props.paddingLeft
|
|
|
|
};
|
|
|
|
var l = node.children.sort(sortByType);
|
|
|
|
/*
|
|
|
|
the `key` property is needed. Otherwise there is a warning in the console
|
|
|
|
*/
|
|
|
|
return (
|
|
|
|
<div className="children" style={childrenStyles}>
|
|
|
|
{l.map(child => {
|
|
|
|
return (
|
|
|
|
<TreeNodeView
|
|
|
|
node={child}
|
|
|
|
key={child.path}
|
|
|
|
paddingLeft={this.props.paddingLeft}
|
|
|
|
treeView={this.props.treeView}
|
2018-08-22 08:39:42 +00:00
|
|
|
isNodeItemFrezee={this.props.isNodeItemFrezee}
|
|
|
|
permission={this.props.permission}
|
2018-09-01 05:48:20 +00:00
|
|
|
currentFilePath={this.props.currentFilePath}
|
2018-08-06 10:29:12 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
renderMenuController() {
|
|
|
|
if (this.props.permission === "rw") {
|
|
|
|
return (
|
|
|
|
<div className="right-icon">
|
|
|
|
<MenuControl
|
|
|
|
isShow={this.state.isMenuIconShow}
|
|
|
|
onClick={this.onMenuControlClick}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNodeTypeAndIcon() {
|
|
|
|
const node = this.props.node;
|
|
|
|
let icon = '';
|
|
|
|
let type = '';
|
|
|
|
if (node.type === 'dir') {
|
2018-08-06 10:29:12 +00:00
|
|
|
icon = <i className="far fa-folder"/>;
|
|
|
|
type = 'dir';
|
2018-08-22 08:39:42 +00:00
|
|
|
} else {
|
2018-08-06 10:29:12 +00:00
|
|
|
let index = node.name.lastIndexOf(".");
|
2018-08-22 08:39:42 +00:00
|
|
|
if (index === -1) {
|
2018-08-06 10:29:12 +00:00
|
|
|
icon = <i className="far fa-file"/>;
|
|
|
|
type = 'file';
|
|
|
|
} else {
|
|
|
|
type = node.name.substring(index).toLowerCase();
|
|
|
|
if (type === ".png" || type === ".jpg") {
|
|
|
|
icon = <i className="far fa-image"/>;
|
|
|
|
type = 'image';
|
|
|
|
} else {
|
|
|
|
icon = <i className="far fa-file"/>;
|
|
|
|
type = 'file';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
return { type, icon };
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const styles = {};
|
|
|
|
let node = this.props.node;
|
|
|
|
let { type, icon } = this.getNodeTypeAndIcon();
|
2018-09-01 03:34:27 +00:00
|
|
|
let hlClass = "";
|
2018-09-01 05:48:20 +00:00
|
|
|
if (node.path === this.props.currentFilePath) {
|
2018-09-01 03:34:27 +00:00
|
|
|
hlClass = "tree-node-hight-light";
|
|
|
|
}
|
|
|
|
let customClass = "tree-node " + hlClass;
|
2018-08-22 08:39:42 +00:00
|
|
|
|
2018-08-06 10:29:12 +00:00
|
|
|
return (
|
2018-09-01 03:34:27 +00:00
|
|
|
<div type={type} className={customClass} style={styles}>
|
2018-08-22 08:39:42 +00:00
|
|
|
<div
|
|
|
|
onMouseLeave={this.onMouseLeave}
|
|
|
|
onMouseEnter={this.onMouseEnter}
|
2018-08-06 10:29:12 +00:00
|
|
|
onClick={this.onClick}
|
2018-08-22 08:39:42 +00:00
|
|
|
type={type}
|
|
|
|
className={`tree-node-inner text-nowrap ${node.name === '/'? 'hide': ''}`}
|
|
|
|
>
|
|
|
|
<div className="tree-node-text" type={type} draggable="true" onDragStart={this.onDragStart}>{node.name}</div>
|
|
|
|
<div className="left-icon">
|
|
|
|
{this.renderCollapse()}
|
|
|
|
<i type={type} className="tree-node-icon">{icon}</i>
|
|
|
|
</div>
|
|
|
|
{this.renderMenuController()}
|
2018-08-06 10:29:12 +00:00
|
|
|
</div>
|
|
|
|
{node.isExpanded ? this.renderChildren() : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TreeNodeView;
|