Skip to content

Commit d2938b8

Browse files
committed
Only conditionally activate binder button.
1 parent 6cd10aa commit d2938b8

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@jupyterlab/docregistry": "^0.12.0",
3939
"@jupyterlab/filebrowser": "^0.12.0",
4040
"@jupyterlab/services": "^0.51.0",
41+
"@phosphor/algorithm": "^1.1.2",
4142
"@phosphor/widgets": "^1.5.0"
4243
},
4344
"devDependencies": {

src/browser.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4+
import {
5+
find
6+
} from '@phosphor/algorithm';
7+
48
import {
59
PanelLayout, Widget
610
} from '@phosphor/widgets';
@@ -21,9 +25,22 @@ import {
2125
GitHubDrive
2226
} from './contents';
2327

28+
29+
/**
30+
* The base url for a mybinder deployment.
31+
*/
2432
const MY_BINDER_BASE_URL = 'https://mybinder.org/v2/gh';
33+
34+
/**
35+
* The GitHub base url.
36+
*/
2537
const GITHUB_BASE_URL = 'https://github.com';
2638

39+
/**
40+
* The className for disabling the mybinder button.
41+
*/
42+
const MY_BINDER_DISABLED = 'jp-MyBinderButton-disabled';
43+
2744
/**
2845
* Widget for hosting the GitHub filebrowser.
2946
*/
@@ -37,18 +54,27 @@ class GitHubFileBrowser extends Widget {
3754
this._browser = browser;
3855
this._drive = drive;
3956

57+
// Create an editable name for the user/org name.
4058
this.userName = new GitHubEditableName(drive.user, '<Edit User>');
4159
this.userName.addClass('jp-GitHubEditableUserName');
4260
this.userName.node.title = 'Click to edit user/organization';
4361
this._browser.toolbar.addItem('user', this.userName);
4462
this.userName.name.changed.connect(this._onUserChanged, this);
4563

46-
const openGitHubButton = new ToolbarButton({
64+
// Create a button that opens GitHub at the appropriate
65+
// repo+directory.
66+
this._openGitHubButton = new ToolbarButton({
4767
onClick: () => {
68+
let url = GITHUB_BASE_URL;
69+
// If there is no valid user, do nothing.
70+
if (!this._drive.validUserState.get()) {
71+
window.open(url);
72+
return;
73+
}
4874
const user = this._drive.user;
4975
const path = this._browser.model.path;
5076
const repo = path.split('/')[0].split(':')[1];
51-
let url = URLExt.join(GITHUB_BASE_URL, user);
77+
url = URLExt.join(url, user);
5278
if (repo) {
5379
const dirPath = URLExt.join(repo, ...path.split('/').slice(1));
5480
url = URLExt.join(url, repo, 'tree', 'master', dirPath);
@@ -58,10 +84,15 @@ class GitHubFileBrowser extends Widget {
5884
className: 'jp-GitHubIcon',
5985
tooltip: 'Open this repository on GitHub'
6086
});
61-
this._browser.toolbar.addItem('GitHub', openGitHubButton);
87+
this._browser.toolbar.addItem('GitHub', this._openGitHubButton);
6288

63-
const launchBinderButton = new ToolbarButton({
89+
// Create a button the opens MyBinder to the appropriate repo.
90+
this._launchBinderButton = new ToolbarButton({
6491
onClick: () => {
92+
// If binder is not active for this directory, do nothing.
93+
if (!this._binderActive) {
94+
return;
95+
}
6596
const user = this._drive.user;
6697
const repo = this._browser.model.path.split('/')[0].split(':')[1];
6798
const url = URLExt.join(MY_BINDER_BASE_URL, user, repo, 'master');
@@ -70,7 +101,12 @@ class GitHubFileBrowser extends Widget {
70101
tooltip: 'Launch this repository on mybinder.org',
71102
className: 'jp-MyBinderButton'
72103
});
73-
this._browser.toolbar.addItem('binder', launchBinderButton);
104+
this._browser.toolbar.addItem('binder', this._launchBinderButton);
105+
106+
// Set up a listener to check if we can launch mybinder.
107+
this._browser.model.pathChanged.connect(this._onPathChanged, this);
108+
// Trigger an initial pathChanged to check for binder state.
109+
this._onPathChanged();
74110

75111
this._drive.rateLimitedState.changed.connect(this._updateErrorPanel, this);
76112
this._drive.validUserState.changed.connect(this._updateErrorPanel, this);
@@ -101,6 +137,40 @@ class GitHubFileBrowser extends Widget {
101137
});
102138
}
103139

140+
/**
141+
* React to the path changing for the browser.
142+
*/
143+
private _onPathChanged(): void {
144+
const path = this._browser.model.path;
145+
// Check for a valid user.
146+
if(!this._drive.validUserState.get()) {
147+
this._launchBinderButton.addClass(MY_BINDER_DISABLED);
148+
this._binderActive = false;
149+
return;
150+
}
151+
// Check for a valid repo.
152+
const repo = path.split('/')[0].split(':')[1];
153+
if (!repo) {
154+
this._launchBinderButton.addClass(MY_BINDER_DISABLED);
155+
this._binderActive = false;
156+
return;
157+
}
158+
// Check for one of the special values indicating we can
159+
// launch the repository.
160+
const item = find(this._browser.model.items(), i => {
161+
return i.name === 'requirements.txt' || i.name === 'environment.yml' ||
162+
i.name === 'apt.txt' || i.name === 'REQUIRE' ||
163+
i.name === 'Dockerfile';
164+
});
165+
if (item) {
166+
this._launchBinderButton.removeClass(MY_BINDER_DISABLED);
167+
this._binderActive = true;
168+
return;
169+
}
170+
this._launchBinderButton.addClass(MY_BINDER_DISABLED);
171+
this._binderActive = false;
172+
}
173+
104174
/**
105175
* React to a change in the validity of the drive.
106176
*/
@@ -139,6 +209,9 @@ class GitHubFileBrowser extends Widget {
139209
private _browser: FileBrowser;
140210
private _drive: GitHubDrive;
141211
private _errorPanel: GitHubErrorPanel | null;
212+
private _openGitHubButton: ToolbarButton;
213+
private _launchBinderButton: ToolbarButton;
214+
private _binderActive = false;
142215
}
143216

144217
/**

style/index.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
background-image: url(binder.svg);
9494
}
9595

96+
.jp-MyBinderButton-disabled {
97+
opacity: 0.3;
98+
}
99+
100+
96101
.jp-GitHubBrowser .jp-GitHubIcon {
97102
flex: 1 1;
98103
background-image: url(octocat.png);

0 commit comments

Comments
 (0)