forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch-menu-view.js
More file actions
141 lines (125 loc) · 4.55 KB
/
Copy pathbranch-menu-view.js
File metadata and controls
141 lines (125 loc) · 4.55 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
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import Commands, {Command} from '../atom/commands';
import {BranchPropType, BranchSetPropType} from '../prop-types';
import {GitError} from '../git-shell-out-strategy';
export default class BranchMenuView extends React.Component {
static propTypes = {
// Atom environment
workspace: PropTypes.object.isRequired,
commands: PropTypes.object.isRequired,
notificationManager: PropTypes.object.isRequired,
// Model
repository: PropTypes.object,
branches: BranchSetPropType.isRequired,
currentBranch: BranchPropType.isRequired,
checkout: PropTypes.func,
}
state = {
createNew: false,
checkedOutBranch: null,
}
render() {
const branchNames = this.props.branches.getNames().filter(Boolean);
let currentBranchName = this.props.currentBranch.isDetached() ? 'detached' : this.props.currentBranch.getName();
if (this.state.checkedOutBranch) {
currentBranchName = this.state.checkedOutBranch;
if (branchNames.indexOf(this.state.checkedOutBranch) === -1) {
branchNames.push(this.state.checkedOutBranch);
}
}
const disableControls = !!this.state.checkedOutBranch;
const branchEditorClasses = cx('github-BranchMenuView-item', 'github-BranchMenuView-editor', {
hidden: !this.state.createNew,
});
const branchSelectListClasses = cx('github-BranchMenuView-item', 'github-BranchMenuView-select', 'input-select', {
hidden: !!this.state.createNew,
});
const iconClasses = cx('github-BranchMenuView-item', 'icon', {
'icon-git-branch': !disableControls,
'icon-sync': disableControls,
});
const newBranchEditor = (
<div className={branchEditorClasses}>
<atom-text-editor
ref={e => { this.editorElement = e; }}
mini={true}
readonly={disableControls ? true : undefined}
/>
</div>
);
const selectBranchView = (
/* eslint-disable jsx-a11y/no-onchange */
<select
className={branchSelectListClasses}
onChange={this.didSelectItem}
disabled={disableControls}
value={currentBranchName}>
{this.props.currentBranch.isDetached() &&
<option key="detached" value="detached" disabled>{this.props.currentBranch.getName()}</option>
}
{branchNames.map(branchName => {
return <option key={`branch-${branchName}`} value={branchName}>{branchName}</option>;
})}
</select>
);
return (
<div className="github-BranchMenuView">
<Commands registry={this.props.commands} target=".github-BranchMenuView-editor atom-text-editor[mini]">
<Command command="tool-panel:unfocus" callback={this.cancelCreateNewBranch} />
<Command command="core:cancel" callback={this.cancelCreateNewBranch} />
<Command command="core:confirm" callback={this.createBranch} />
</Commands>
<div className="github-BranchMenuView-selector">
<span className={iconClasses} />
{newBranchEditor}
{selectBranchView}
<button className="github-BranchMenuView-item github-BranchMenuView-button btn"
onClick={this.createBranch} disabled={disableControls}> New Branch </button>
</div>
</div>
);
}
didSelectItem = async event => {
const branchName = event.target.value;
await this.checkout(branchName);
}
createBranch = async () => {
if (this.state.createNew) {
const branchName = this.editorElement.getModel().getText().trim();
await this.checkout(branchName, {createNew: true});
} else {
await new Promise(resolve => {
this.setState({createNew: true}, () => {
this.editorElement.focus();
resolve();
});
});
}
}
checkout = async (branchName, options) => {
this.editorElement.classList.remove('is-focused');
await new Promise(resolve => {
this.setState({checkedOutBranch: branchName}, resolve);
});
try {
await this.props.checkout(branchName, options);
await new Promise(resolve => {
this.setState({checkedOutBranch: null, createNew: false}, resolve);
});
this.editorElement.getModel().setText('');
} catch (error) {
this.editorElement.classList.add('is-focused');
await new Promise(resolve => {
this.setState({checkedOutBranch: null}, resolve);
});
if (!(error instanceof GitError)) {
throw error;
}
}
}
cancelCreateNewBranch = () => {
this.setState({createNew: false});
}
}