2018-08-30 12:13:37 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {Treebeard, decorators} from 'react-treebeard';
|
2018-10-08 05:43:51 +00:00
|
|
|
import {Form} from 'tabler-react';
|
2018-08-30 12:13:37 +00:00
|
|
|
|
2018-10-11 11:08:47 +00:00
|
|
|
const style = {
|
|
|
|
tree: {
|
|
|
|
base: {
|
|
|
|
height: '100%',
|
|
|
|
listStyle: 'none',
|
|
|
|
backgroundColor: '#1C1C1C',
|
|
|
|
margin: 0,
|
|
|
|
padding: 0,
|
|
|
|
color: '#9DA5AB',
|
|
|
|
fontFamily: 'lucida grande ,tahoma,verdana,arial,sans-serif',
|
|
|
|
fontSize: '14px'
|
|
|
|
},
|
|
|
|
node: {
|
|
|
|
base: {
|
|
|
|
position: 'relative'
|
|
|
|
},
|
|
|
|
link: {
|
|
|
|
cursor: 'pointer',
|
|
|
|
position: 'relative',
|
|
|
|
padding: '0px 5px',
|
|
|
|
display: 'block'
|
|
|
|
},
|
|
|
|
activeLink: {
|
|
|
|
background: '#31363F'
|
|
|
|
},
|
|
|
|
toggle: {
|
|
|
|
base: {
|
|
|
|
position: 'relative',
|
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
marginLeft: '-5px',
|
|
|
|
height: '24px',
|
|
|
|
width: '24px'
|
|
|
|
},
|
|
|
|
wrapper: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: '50%',
|
|
|
|
left: '50%',
|
|
|
|
margin: '-7px 0 0 -7px',
|
|
|
|
height: '14px'
|
|
|
|
},
|
|
|
|
height: 14,
|
|
|
|
width: 14,
|
|
|
|
arrow: {
|
|
|
|
fill: '#9DA5AB',
|
|
|
|
strokeWidth: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
header: {
|
|
|
|
base: {
|
|
|
|
display: 'inline-block',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
color: '#9DA5AB'
|
|
|
|
},
|
|
|
|
connector: {
|
|
|
|
width: '2px',
|
|
|
|
height: '12px',
|
|
|
|
borderLeft: 'solid 2px black',
|
|
|
|
borderBottom: 'solid 2px black',
|
|
|
|
position: 'absolute',
|
|
|
|
top: '0px',
|
|
|
|
left: '-21px'
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
lineHeight: '24px',
|
|
|
|
verticalAlign: 'middle'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
subtree: {
|
|
|
|
listStyle: 'none',
|
|
|
|
paddingLeft: '19px'
|
|
|
|
},
|
|
|
|
loading: {
|
|
|
|
color: '#E2C089'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-30 12:13:37 +00:00
|
|
|
const Header = ({style, node}) => {
|
|
|
|
const iconType = node.children ? 'folder' : 'file';
|
|
|
|
const iconClass = `fe fe-${iconType}`;
|
|
|
|
const iconStyle = {marginRight: '5px'};
|
|
|
|
|
|
|
|
return (
|
2018-10-09 06:01:48 +00:00
|
|
|
<div style={style.base}>
|
|
|
|
<div style={style.title}>
|
|
|
|
<i className={iconClass} style={iconStyle} />
|
|
|
|
{node.name}
|
2018-08-30 12:13:37 +00:00
|
|
|
</div>
|
2018-10-09 06:01:48 +00:00
|
|
|
</div>
|
2018-08-30 12:13:37 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Header.propTypes = {
|
|
|
|
style: PropTypes.object,
|
|
|
|
node: PropTypes.object
|
|
|
|
};
|
|
|
|
|
|
|
|
decorators.Header = Header;
|
|
|
|
|
|
|
|
class FileExplorer extends React.Component {
|
2018-10-09 06:01:48 +00:00
|
|
|
constructor(props) {
|
2018-08-30 12:13:37 +00:00
|
|
|
super(props);
|
2018-10-08 05:43:51 +00:00
|
|
|
this.state = {
|
2018-10-09 06:01:48 +00:00
|
|
|
showHidden: false
|
2018-10-08 05:43:51 +00:00
|
|
|
};
|
2018-08-30 12:13:37 +00:00
|
|
|
}
|
2018-08-30 12:13:37 +00:00
|
|
|
|
2018-10-09 06:01:48 +00:00
|
|
|
onToggle(node, toggled) {
|
2018-08-30 12:13:37 +00:00
|
|
|
node.active = true;
|
2018-10-09 06:01:48 +00:00
|
|
|
if (node.children) {
|
2018-08-30 12:13:37 +00:00
|
|
|
node.toggled = toggled;
|
2018-08-30 12:13:37 +00:00
|
|
|
} else {
|
|
|
|
this.props.fetchFile(node);
|
2018-08-30 12:13:37 +00:00
|
|
|
}
|
2018-10-09 06:01:48 +00:00
|
|
|
this.setState({cursor: node});
|
|
|
|
}
|
|
|
|
|
|
|
|
onHiddenToggle(e) {
|
|
|
|
this.setState({showHidden: e.target.checked});
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeEquals(a, b) {
|
|
|
|
return a && b && a.path && b.path && a.name && b.name &&
|
|
|
|
a.path === b.path &&
|
|
|
|
a.name === b.name;
|
2018-08-30 12:13:37 +00:00
|
|
|
}
|
2018-08-30 12:13:37 +00:00
|
|
|
|
2018-10-09 06:01:48 +00:00
|
|
|
isNodeInPath(a, b) {
|
|
|
|
return a && b && a.path && b.path &&
|
|
|
|
a.path !== b.path &&
|
|
|
|
b.path.indexOf(a.path) > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
filterHidden(nodes) {
|
|
|
|
let filtered = [];
|
|
|
|
if (!Array.isArray(nodes)) return filtered;
|
|
|
|
|
|
|
|
// we need a foreach to build an array instead of a
|
|
|
|
// filter to prevent mutating the original object (in props)
|
|
|
|
nodes.forEach(node => {
|
|
|
|
const {showHidden, cursor} = this.state;
|
|
|
|
if (!showHidden && node.isHidden) return;
|
|
|
|
let updatedNode = {...node};
|
|
|
|
|
|
|
|
// if it's a folder, filter the children
|
|
|
|
if (node.children) {
|
2018-10-09 21:13:29 +00:00
|
|
|
const children = this.filterHidden(node.children);
|
2018-10-09 06:01:48 +00:00
|
|
|
if (children.length) {
|
|
|
|
updatedNode.children = children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this is the selected node, set it as active
|
|
|
|
if (this.nodeEquals(node, cursor)) {
|
|
|
|
updatedNode.active = cursor.active;
|
2018-10-09 21:13:29 +00:00
|
|
|
// if this node is the selected node and is a folder, set
|
|
|
|
// it as toggled (expanded) according to the selected node
|
2018-10-09 06:01:48 +00:00
|
|
|
if (node.children) updatedNode.toggled = cursor.toggled;
|
|
|
|
}
|
2018-10-09 21:13:29 +00:00
|
|
|
// if this node is a folder, and it's a parent of the selected
|
|
|
|
// folder, force toggle it
|
2018-10-09 06:01:48 +00:00
|
|
|
if (node.children && this.isNodeInPath(node, cursor)) {
|
|
|
|
updatedNode.toggled = true;
|
|
|
|
}
|
|
|
|
filtered.push(updatedNode);
|
2018-10-08 05:43:51 +00:00
|
|
|
});
|
2018-10-09 06:01:48 +00:00
|
|
|
|
|
|
|
return filtered;
|
2018-10-08 05:43:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 06:01:48 +00:00
|
|
|
render() {
|
2018-08-30 12:13:37 +00:00
|
|
|
return (
|
2018-10-08 05:43:51 +00:00
|
|
|
<React.Fragment>
|
2018-10-09 06:01:48 +00:00
|
|
|
<Form.Switch type="checkbox" name="toggle" value={true} label="Show hidden files" onChange={this.onHiddenToggle.bind(this)} />
|
2018-10-08 05:43:51 +00:00
|
|
|
<Treebeard
|
2018-10-09 06:01:48 +00:00
|
|
|
data={this.filterHidden(this.props.files)}
|
2018-10-08 05:43:51 +00:00
|
|
|
decorators={decorators}
|
|
|
|
onToggle={this.onToggle.bind(this)}
|
2018-10-11 11:08:47 +00:00
|
|
|
style={style}
|
2018-10-08 05:43:51 +00:00
|
|
|
/>
|
|
|
|
</React.Fragment>
|
2018-08-30 12:13:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileExplorer.propTypes = {
|
2018-08-30 12:13:37 +00:00
|
|
|
files: PropTypes.array,
|
|
|
|
fetchFile: PropTypes.func
|
2018-08-30 12:13:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default FileExplorer;
|