|
| 1 | +/* @flow weak */ |
| 2 | +import React, { Component } from 'react' |
| 3 | +import { bindActionCreators } from 'redux' |
| 4 | +import { dispatchCommand } from '../../commands' |
| 5 | +import { connect } from 'react-redux' |
| 6 | +import cx from 'classnames' |
| 7 | +import _ from 'lodash' |
| 8 | + |
| 9 | +import * as GitActions from './actions' |
| 10 | + |
| 11 | +class GitFileTree extends Component { |
| 12 | + constructor (props) { |
| 13 | + super(props) |
| 14 | + this.state = { |
| 15 | + showTreeView: true, |
| 16 | + showShrinkPathView: true, |
| 17 | + displayOnly: false |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + render () { |
| 22 | + return ( |
| 23 | + <div className='git-filetree-container' tabIndex={1} > |
| 24 | + <GitFileTreeNode path='/' |
| 25 | + showTreeView={this.state.showTreeView} |
| 26 | + showShrinkPathView={this.state.showShrinkPathView} |
| 27 | + displayOnly={this.state.displayOnly} /> |
| 28 | + </div> |
| 29 | + ) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +class _GitFileTreeNode extends Component { |
| 35 | + constructor (props) { |
| 36 | + super(props) |
| 37 | + } |
| 38 | + |
| 39 | + render () { |
| 40 | + const { |
| 41 | + node, |
| 42 | + statusFiles, |
| 43 | + displayOnly, |
| 44 | + showTreeView, |
| 45 | + showShrinkPathView, |
| 46 | + visualParentPath, // for usage in shrinkPathView |
| 47 | + ...actionProps} = this.props |
| 48 | + const {toggleNodeFold, selectNode, toggleStaging} = actionProps |
| 49 | + |
| 50 | + const childrenStagingStatus = this.getChildrenStagingStatus() |
| 51 | + const FILETREE_INDENT = 14 |
| 52 | + let indentCompensation = this.getIndentCompensation() |
| 53 | + |
| 54 | + return ( |
| 55 | + <div className='filetree-node-container'> |
| 56 | + { node.isRoot ? |
| 57 | + (<div className='filetree-node' ref={r => this.nodeDOM = r} > |
| 58 | + { displayOnly ? null |
| 59 | + : <span className='filetree-node-checkbox' |
| 60 | + style={{marginRight: 0}} |
| 61 | + onClick={e => toggleStaging(node)} > |
| 62 | + <i className={cx('fa', { |
| 63 | + 'fa-check-square': (!node.isDir && node.isStaged) || childrenStagingStatus === 'ALL', |
| 64 | + 'fa-square-o': (!node.isDir && !node.isStaged) || childrenStagingStatus === 'NONE', |
| 65 | + 'fa-minus-square': childrenStagingStatus === 'SOME', |
| 66 | + })}></i> |
| 67 | + </span> |
| 68 | + } |
| 69 | + <span className='filetree-node-label'>File Status |
| 70 | + { displayOnly ? <span> ({node.leafNodes.length} changed) </span> |
| 71 | + : <span> ({this.getStagedLeafNodes().length} staged / {node.leafNodes.length} changed) </span> |
| 72 | + } |
| 73 | + </span> |
| 74 | + </div>) |
| 75 | + |
| 76 | + : !showTreeView && node.isDir ? null // flatView |
| 77 | + |
| 78 | + : showShrinkPathView && this.shouldHideAtShrinkPath() ? null |
| 79 | + |
| 80 | + : (<div className={cx('filetree-node', {'focus':node.isFocused})} |
| 81 | + ref={r => this.nodeDOM = r} |
| 82 | + onClick={e => selectNode(node)} > |
| 83 | + { displayOnly ? null |
| 84 | + : <span className='filetree-node-checkbox' |
| 85 | + onClick={e => toggleStaging(node)} > |
| 86 | + <i className={cx('fa', { |
| 87 | + 'fa-check-square': (!node.isDir && node.isStaged) || childrenStagingStatus === 'ALL', |
| 88 | + 'fa-square-o': (!node.isDir && !node.isStaged) || childrenStagingStatus === 'NONE', |
| 89 | + 'fa-minus-square': childrenStagingStatus === 'SOME', |
| 90 | + })}></i> |
| 91 | + </span> |
| 92 | + } |
| 93 | + <span className='filetree-node-arrow' |
| 94 | + onClick={e => toggleNodeFold(node, null, e.altKey)} |
| 95 | + style={{ |
| 96 | + 'marginLeft': !showTreeView ? '0' : `${(node.depth - 1 - indentCompensation)*FILETREE_INDENT}px` |
| 97 | + }} > |
| 98 | + <i className={cx({ |
| 99 | + 'fa fa-angle-right': node.isFolded, |
| 100 | + 'fa fa-angle-down': !node.isFolded, |
| 101 | + 'hidden': !node.isDir || node.childrenCount === 0 |
| 102 | + })}></i> |
| 103 | + </span> |
| 104 | + <span className='filetree-node-icon'> |
| 105 | + <i className={cx('fa file-status-indicator', node.status.toLowerCase(), { |
| 106 | + 'fa-folder-o': node.isDir, |
| 107 | + 'fa-pencil-square': node.status == 'MODIFIED', |
| 108 | + 'fa-plus-square': node.status == 'UNTRACKED', |
| 109 | + 'fa-minus-square': node.status == 'MISSING' |
| 110 | + })}></i> |
| 111 | + </span> |
| 112 | + <span className='filetree-node-label'> |
| 113 | + { showTreeView ? |
| 114 | + (node.isDir && showShrinkPathView ? |
| 115 | + node.path.replace(visualParentPath, '').replace(/^\//, '') |
| 116 | + : node.name) |
| 117 | + : node.path} |
| 118 | + </span> |
| 119 | + <div className='filetree-node-bg'></div> |
| 120 | + </div>) |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + { node.isDir ? |
| 125 | + <div className={cx('filetree-node-children', {isFolded: node.isFolded})}> |
| 126 | + {node.children.map(childNodePath => |
| 127 | + <GitFileTreeNode |
| 128 | + key={childNodePath} |
| 129 | + path={childNodePath} |
| 130 | + showTreeView={showTreeView} |
| 131 | + showShrinkPathView={showShrinkPathView} |
| 132 | + visualParentPath={showShrinkPathView && this.shouldHideAtShrinkPath() ? visualParentPath : node.path} |
| 133 | + indentCompensation={indentCompensation} |
| 134 | + displayOnly={displayOnly} /> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + : null } |
| 138 | + </div> |
| 139 | + ) |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + componentDidUpdate () { |
| 144 | + if (this.props.node.isFocused) { |
| 145 | + this.nodeDOM.scrollIntoViewIfNeeded && this.nodeDOM.scrollIntoViewIfNeeded() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + getStagedLeafNodes () { |
| 150 | + const {node, statusFiles} = this.props |
| 151 | + if (!node.isDir) return [] |
| 152 | + return node.leafNodes.filter(leafNodePath => |
| 153 | + statusFiles.get(leafNodePath).get('isStaged') |
| 154 | + ) |
| 155 | + } |
| 156 | + |
| 157 | + getChildrenStagingStatus () { |
| 158 | + const {node, statusFiles} = this.props |
| 159 | + if (!node.isDir) return false |
| 160 | + let stagedLeafNodes = this.getStagedLeafNodes() |
| 161 | + if (stagedLeafNodes.length == 0) { |
| 162 | + return 'NONE' |
| 163 | + } else if (stagedLeafNodes.length === node.leafNodes.length) { |
| 164 | + return 'ALL' |
| 165 | + } else { |
| 166 | + return 'SOME' |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + shouldHideAtShrinkPath () { |
| 171 | + const {node, statusFiles} = this.props |
| 172 | + if (!node.isDir) return false |
| 173 | + if (node.children.length !== 1) return false |
| 174 | + |
| 175 | + let childNode = statusFiles.get(node.children[0]) |
| 176 | + if (!childNode.isDir) return false |
| 177 | + return true |
| 178 | + } |
| 179 | + |
| 180 | + getIndentCompensation () { |
| 181 | + let {node, visualParentPath, indentCompensation, showShrinkPathView} = this.props |
| 182 | + // if not showShrinkPathView, indentCompensation is uneccssary, set to 0 |
| 183 | + if (!showShrinkPathView) return 0 |
| 184 | + |
| 185 | + // else, let's do some math: |
| 186 | + if (!indentCompensation) indentCompensation = 0 |
| 187 | + let indentOffset |
| 188 | + if (node.isRoot) { |
| 189 | + indentOffset = 1 |
| 190 | + } else { |
| 191 | + indentOffset = (node.path.split('/').length - visualParentPath.split('/').length) || 1 |
| 192 | + } |
| 193 | + return indentCompensation + indentOffset - 1 |
| 194 | + } |
| 195 | + |
| 196 | +} |
| 197 | +const GitFileTreeNode = connect( |
| 198 | + (state, ownProps) => ({ |
| 199 | + statusFiles: state.GitState.statusFiles, |
| 200 | + node: state.GitState.statusFiles.get(ownProps.path) |
| 201 | + }), |
| 202 | + dispatch => bindActionCreators(GitActions, dispatch) |
| 203 | +)(_GitFileTreeNode) |
| 204 | + |
| 205 | +export default GitFileTree |
0 commit comments