forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-patch-header-view.js
209 lines (184 loc) · 5.84 KB
/
file-patch-header-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import path from 'path';
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import Octicon from '../atom/octicon';
import RefHolder from '../models/ref-holder';
import IssueishDetailItem from '../items/issueish-detail-item';
import ChangedFileItem from '../items/changed-file-item';
import CommitDetailItem from '../items/commit-detail-item';
import {ItemTypePropType} from '../prop-types';
import {addEvent} from '../reporter-proxy';
export default class FilePatchHeaderView extends React.Component {
static propTypes = {
relPath: PropTypes.string.isRequired,
newPath: PropTypes.string,
stagingStatus: PropTypes.oneOf(['staged', 'unstaged']),
isPartiallyStaged: PropTypes.bool,
hasUndoHistory: PropTypes.bool,
hasMultipleFileSelections: PropTypes.bool.isRequired,
tooltips: PropTypes.object.isRequired,
undoLastDiscard: PropTypes.func.isRequired,
diveIntoMirrorPatch: PropTypes.func.isRequired,
openFile: PropTypes.func.isRequired,
// should probably change 'toggleFile' to 'toggleFileStagingStatus'
// because the addition of another toggling function makes the old name confusing.
toggleFile: PropTypes.func.isRequired,
itemType: ItemTypePropType.isRequired,
isCollapsed: PropTypes.bool.isRequired,
triggerExpand: PropTypes.func.isRequired,
triggerCollapse: PropTypes.func.isRequired,
};
constructor(props) {
super(props);
this.refMirrorButton = new RefHolder();
this.refOpenFileButton = new RefHolder();
}
render() {
return (
<header className="github-FilePatchView-header">
{this.renderCollapseButton()}
<span className="github-FilePatchView-title">
{this.renderTitle()}
</span>
{this.renderButtonGroup()}
</header>
);
}
togglePatchCollapse = () => {
if (this.props.isCollapsed) {
addEvent('expand-file-patch', {component: this.constructor.name, package: 'github'});
this.props.triggerExpand();
} else {
addEvent('collapse-file-patch', {component: this.constructor.name, package: 'github'});
this.props.triggerCollapse();
}
}
renderCollapseButton() {
if (this.props.itemType === ChangedFileItem) {
return null;
}
const icon = this.props.isCollapsed ? 'chevron-right' : 'chevron-down';
return (
<button
className="github-FilePatchView-collapseButton"
onClick={this.togglePatchCollapse}>
<Octicon className="github-FilePatchView-collapseButtonIcon" icon={icon} />
</button>
);
}
renderTitle() {
if (this.props.itemType === ChangedFileItem) {
const status = this.props.stagingStatus;
return (
<span>{status[0].toUpperCase()}{status.slice(1)} Changes for {this.renderDisplayPath()}</span>
);
} else {
return this.renderDisplayPath();
}
}
renderDisplayPath() {
if (this.props.newPath && this.props.newPath !== this.props.relPath) {
const oldPath = this.renderPath(this.props.relPath);
const newPath = this.renderPath(this.props.newPath);
return <span>{oldPath} <span>→</span> {newPath}</span>;
} else {
return this.renderPath(this.props.relPath);
}
}
renderPath(filePath) {
const dirname = path.dirname(filePath);
const basename = path.basename(filePath);
if (dirname === '.') {
return <span className="gitub-FilePatchHeaderView-basename">{basename}</span>;
} else {
return (
<span>
{dirname}{path.sep}<span className="gitub-FilePatchHeaderView-basename">{basename}</span>
</span>
);
}
}
renderButtonGroup() {
if (this.props.itemType === CommitDetailItem || this.props.itemType === IssueishDetailItem) {
return null;
} else {
return (
<span className="btn-group">
{this.renderUndoDiscardButton()}
{this.renderMirrorPatchButton()}
{this.renderOpenFileButton()}
{this.renderToggleFileButton()}
</span>
);
}
}
renderUndoDiscardButton() {
const unstagedChangedFileItem = this.props.itemType === ChangedFileItem && this.props.stagingStatus === 'unstaged';
if (unstagedChangedFileItem && this.props.hasUndoHistory) {
return (
<button className="btn icon icon-history" onClick={this.props.undoLastDiscard}>
Undo Discard
</button>
);
} else {
return null;
}
}
renderMirrorPatchButton() {
if (!this.props.isPartiallyStaged) {
return null;
}
const attrs = this.props.stagingStatus === 'unstaged'
? {
iconClass: 'icon-tasklist',
buttonText: 'View Staged',
}
: {
iconClass: 'icon-list-unordered',
buttonText: 'View Unstaged',
};
return (
<Fragment>
<button
ref={this.refMirrorButton.setter}
className={cx('btn', 'icon', attrs.iconClass)}
onClick={this.props.diveIntoMirrorPatch}>
{attrs.buttonText}
</button>
</Fragment>
);
}
renderOpenFileButton() {
let buttonText = 'Jump To File';
if (this.props.hasMultipleFileSelections) {
buttonText += 's';
}
return (
<Fragment>
<button
ref={this.refOpenFileButton.setter}
className="btn icon icon-code github-FilePatchHeaderView-jumpToFileButton"
onClick={this.props.openFile}>
{buttonText}
</button>
</Fragment>
);
}
renderToggleFileButton() {
const attrs = this.props.stagingStatus === 'unstaged'
? {
buttonClass: 'icon-move-down',
buttonText: 'Stage File',
}
: {
buttonClass: 'icon-move-up',
buttonText: 'Unstage File',
};
return (
<button className={cx('btn', 'icon', attrs.buttonClass)} onClick={this.props.toggleFile}>
{attrs.buttonText}
</button>
);
}
}