forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-pull-view.js
More file actions
228 lines (211 loc) · 6.38 KB
/
push-pull-view.js
File metadata and controls
228 lines (211 loc) · 6.38 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import {RemotePropType, BranchPropType} from '../prop-types';
import Tooltip from '../atom/tooltip';
import RefHolder from '../models/ref-holder';
function getIconClass(icon, animation) {
return cx(
'github-PushPull-icon',
'icon',
`icon-${icon}`,
{[`animate-${animation}`]: !!animation},
);
}
export default class PushPullView extends React.Component {
static propTypes = {
currentBranch: BranchPropType.isRequired,
currentRemote: RemotePropType.isRequired,
isSyncing: PropTypes.bool,
isFetching: PropTypes.bool,
isPulling: PropTypes.bool,
isPushing: PropTypes.bool,
behindCount: PropTypes.number,
aheadCount: PropTypes.number,
push: PropTypes.func.isRequired,
pull: PropTypes.func.isRequired,
fetch: PropTypes.func.isRequired,
originExists: PropTypes.bool,
tooltipManager: PropTypes.object.isRequired,
}
static defaultProps = {
isSyncing: false,
isFetching: false,
isPulling: false,
isPushing: false,
behindCount: 0,
aheadCount: 0,
}
constructor(props) {
super(props);
this.refTileNode = new RefHolder();
}
onClickPush = clickEvent => {
if (this.props.isSyncing) {
return;
}
this.props.push({
force: clickEvent.metaKey || clickEvent.ctrlKey,
setUpstream: !this.props.currentRemote.isPresent(),
});
}
onClickPull = clickEvent => {
if (this.props.isSyncing) {
return;
}
this.props.pull();
}
onClickPushPull = clickEvent => {
if (this.props.isSyncing) {
return;
}
if (clickEvent.metaKey || clickEvent.ctrlKey) {
this.props.push({
force: true,
});
} else {
this.props.pull();
}
}
onClickPublish = clickEvent => {
if (this.props.isSyncing) {
return;
}
this.props.push({
setUpstream: !this.props.currentRemote.isPresent(),
});
}
onClickFetch = clickEvent => {
if (this.props.isSyncing) {
return;
}
this.props.fetch();
}
getTileStates() {
const modKey = process.platform === 'darwin' ? 'Cmd' : 'Ctrl';
return {
fetching: {
tooltip: 'Fetching from remote',
icon: 'sync',
text: 'Fetching',
iconAnimation: 'rotate',
},
pulling: {
tooltip: 'Pulling from remote',
icon: 'arrow-down',
text: 'Pulling',
iconAnimation: 'down',
},
pushing: {
tooltip: 'Pushing to remote',
icon: 'arrow-up',
text: 'Pushing',
iconAnimation: 'up',
},
ahead: {
onClick: this.onClickPush,
tooltip: `Click to push<br />${modKey}-click to force push<br />Right-click for more`,
icon: 'arrow-up',
text: `Push ${this.props.aheadCount}`,
},
behind: {
onClick: this.onClickPull,
tooltip: 'Click to pull<br />Right-click for more',
icon: 'arrow-down',
text: `Pull ${this.props.behindCount}`,
},
aheadBehind: {
onClick: this.onClickPushPull,
tooltip: `Click to pull<br />${modKey}-click to force push<br />Right-click for more`,
icon: 'arrow-down',
text: `Pull ${this.props.behindCount}`,
secondaryIcon: 'arrow-up',
secondaryText: `${this.props.aheadCount} `,
},
published: {
onClick: this.onClickFetch,
tooltip: 'Click to fetch<br />Right-click for more',
icon: 'sync',
text: 'Fetch',
},
unpublished: {
onClick: this.onClickPublish,
tooltip: 'Click to set up a remote tracking branch<br />Right-click for more',
icon: 'cloud-upload',
text: 'Publish',
},
noRemote: {
tooltip: 'There is no remote named "origin"',
icon: 'stop',
text: 'No remote',
},
detached: {
tooltip: 'Create a branch if you wish to push your work anywhere',
icon: 'stop',
text: 'Not on branch',
},
};
}
render() {
const isAhead = this.props.aheadCount > 0;
const isBehind = this.props.behindCount > 0;
const isUnpublished = !this.props.currentRemote.isPresent();
const isDetached = this.props.currentBranch.isDetached();
const isFetching = this.props.isFetching;
const isPulling = this.props.isPulling;
const isPushing = this.props.isPushing;
const hasOrigin = !!this.props.originExists;
const tileStates = this.getTileStates();
let tileState;
if (isFetching) {
tileState = tileStates.fetching;
} else if (isPulling) {
tileState = tileStates.pulling;
} else if (isPushing) {
tileState = tileStates.pushing;
} else if (isAhead && !isBehind && !isUnpublished) {
tileState = tileStates.ahead;
} else if (isBehind && !isAhead && !isUnpublished) {
tileState = tileStates.behind;
} else if (isBehind && isAhead && !isUnpublished) {
tileState = tileStates.aheadBehind;
} else if (!isBehind && !isAhead && !isUnpublished && !isDetached) {
tileState = tileStates.published;
} else if (isUnpublished && !isDetached && hasOrigin) {
tileState = tileStates.unpublished;
} else if (isUnpublished && !isDetached && !hasOrigin) {
tileState = tileStates.noRemote;
} else if (isDetached) {
tileState = tileStates.detached;
}
return (
<div
onClick={tileState.onClick}
ref={this.refTileNode.setter}
className={cx('github-PushPull', 'inline-block', {'github-branch-detached': isDetached})}>
{tileState && (
<Fragment>
<span>
{tileState.secondaryText && (
<span className="secondary">
<span className={getIconClass(tileState.secondaryIcon)} />
{tileState.secondaryText}
</span>
)}
<span className={getIconClass(tileState.icon, tileState.iconAnimation)} />
{tileState.text}
</span>
<Tooltip
key="tooltip"
manager={this.props.tooltipManager}
target={this.refTileNode}
title={`<div style="text-align: left; line-height: 1.2em;">${tileState.tooltip}</div>`}
showDelay={atom.tooltips.hoverDefaults.delay.show}
hideDelay={atom.tooltips.hoverDefaults.delay.hide}
/>
</Fragment>
)}
</div>
);
}
}