forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhunk-header-view.js
95 lines (84 loc) · 3.03 KB
/
hunk-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
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import {autobind} from '../helpers';
import {RefHolderPropType, ItemTypePropType} from '../prop-types';
import RefHolder from '../models/ref-holder';
import Tooltip from '../atom/tooltip';
import Keystroke from '../atom/keystroke';
import CommitDetailItem from '../items/commit-detail-item';
import IssueishDetailItem from '../items/issueish-detail-item';
function theBuckStopsHere(event) {
event.stopPropagation();
}
export default class HunkHeaderView extends React.Component {
static propTypes = {
refTarget: RefHolderPropType.isRequired,
hunk: PropTypes.object.isRequired,
isSelected: PropTypes.bool.isRequired,
stagingStatus: PropTypes.oneOf(['unstaged', 'staged']),
selectionMode: PropTypes.oneOf(['hunk', 'line']).isRequired,
toggleSelectionLabel: PropTypes.string,
discardSelectionLabel: PropTypes.string,
tooltips: PropTypes.object.isRequired,
keymaps: PropTypes.object.isRequired,
toggleSelection: PropTypes.func,
discardSelection: PropTypes.func,
mouseDown: PropTypes.func.isRequired,
itemType: ItemTypePropType.isRequired,
};
constructor(props) {
super(props);
autobind(this, 'didMouseDown', 'renderButtons');
this.refDiscardButton = new RefHolder();
}
render() {
const conditional = {
'github-HunkHeaderView--isSelected': this.props.isSelected,
'github-HunkHeaderView--isHunkMode': this.props.selectionMode === 'hunk',
};
return (
<div className={cx('github-HunkHeaderView', conditional)} onMouseDown={this.didMouseDown}>
<span className="github-HunkHeaderView-title">
{this.props.hunk.getHeader().trim()} {this.props.hunk.getSectionHeading().trim()}
</span>
{this.renderButtons()}
</div>
);
}
renderButtons() {
if (this.props.itemType === CommitDetailItem || this.props.itemType === IssueishDetailItem) {
return null;
} else {
return (
<Fragment>
<button
className="github-HunkHeaderView-stageButton"
onClick={this.props.toggleSelection}
onMouseDown={theBuckStopsHere}>
<Keystroke keymaps={this.props.keymaps} command="core:confirm" refTarget={this.props.refTarget} />
{this.props.toggleSelectionLabel}
</button>
{this.props.stagingStatus === 'unstaged' && (
<Fragment>
<button
ref={this.refDiscardButton.setter}
className="icon-trashcan github-HunkHeaderView-discardButton"
onClick={this.props.discardSelection}
onMouseDown={theBuckStopsHere}
/>
<Tooltip
manager={this.props.tooltips}
target={this.refDiscardButton}
title={this.props.discardSelectionLabel}
/>
</Fragment>
)}
</Fragment>
);
}
}
didMouseDown(event) {
return this.props.mouseDown(event, this.props.hunk);
}
}