From 895c0d86ac30b1de50fb8ffecc1aabd4217bc8fc Mon Sep 17 00:00:00 2001 From: emizzle Date: Tue, 9 Oct 2018 17:01:48 +1100 Subject: [PATCH] Finish adding show/hide of hidden files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hidden files/folders (those beginning with “.”) are now hidden and shown depending on the state of the toggle. --- embark-ui/src/components/FileExplorer.js | 83 ++++++++++++++++++------ 1 file changed, 62 insertions(+), 21 deletions(-) diff --git a/embark-ui/src/components/FileExplorer.js b/embark-ui/src/components/FileExplorer.js index 269610f8..27b513cd 100644 --- a/embark-ui/src/components/FileExplorer.js +++ b/embark-ui/src/components/FileExplorer.js @@ -9,12 +9,12 @@ const Header = ({style, node}) => { const iconStyle = {marginRight: '5px'}; return ( -
-
- - {node.name} -
+
+
+ + {node.name}
+
); }; @@ -26,40 +26,81 @@ Header.propTypes = { decorators.Header = Header; class FileExplorer extends React.Component { - constructor(props){ + constructor(props) { super(props); this.state = { - files: this.props.files + showHidden: false }; } - onToggle(node, toggled){ - let oldNode = this.state.cursor; - if(oldNode) { - oldNode.active = false; - } + onToggle(node, toggled) { node.active = true; - if(node.children) { + if (node.children) { node.toggled = toggled; } else { this.props.fetchFile(node); } - this.setState({ cursor: node }); + this.setState({cursor: node}); } - onHiddenToggle(value){ - this.setState({files: this.props.files.filter((file) => { - return; - }) + 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; + } + + 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) { + let children = this.filterHidden(node.children); + 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; + // if this node has children, set it as toggled + if (node.children) updatedNode.toggled = cursor.toggled; + } + // if this is a directory, and it lies in the path + // of the selected node, ensure it's toggled + if (node.children && this.isNodeInPath(node, cursor)) { + updatedNode.toggled = true; + } + filtered.push(updatedNode); }); + + return filtered; } - render(){ + render() { return ( - { this.showHidden = input; }} onToggle={this.onHiddenToggle.bind(this)} /> +