@@ -22,7 +22,7 @@ import {
22
22
} from '@jupyterlab/filebrowser' ;
23
23
24
24
import {
25
- GitHubDrive
25
+ GitHubDrive , parsePath
26
26
} from './contents' ;
27
27
28
28
@@ -55,7 +55,7 @@ class GitHubFileBrowser extends Widget {
55
55
this . _drive = drive ;
56
56
57
57
// Create an editable name for the user/org name.
58
- this . userName = new GitHubEditableName ( drive . user , '<Edit User>' ) ;
58
+ this . userName = new GitHubEditableName ( '' , '<Edit User>' ) ;
59
59
this . userName . addClass ( 'jp-GitHubEditableUserName' ) ;
60
60
this . userName . node . title = 'Click to edit user/organization' ;
61
61
this . _browser . toolbar . addItem ( 'user' , this . userName ) ;
@@ -66,18 +66,16 @@ class GitHubFileBrowser extends Widget {
66
66
this . _openGitHubButton = new ToolbarButton ( {
67
67
onClick : ( ) => {
68
68
let url = GITHUB_BASE_URL ;
69
- // If there is no valid user, do nothing .
70
- if ( ! this . _drive . validUserState . get ( ) ) {
69
+ // If there is no valid user, open the GitHub homepage .
70
+ if ( ! this . _drive . validUser ) {
71
71
window . open ( url ) ;
72
72
return ;
73
73
}
74
- const user = this . _drive . user ;
75
- const path = this . _browser . model . path ;
76
- const repo = path . split ( '/' ) [ 0 ] . split ( ':' ) [ 1 ] ;
77
- url = URLExt . join ( url , user ) ;
78
- if ( repo ) {
79
- const dirPath = URLExt . join ( repo , ...path . split ( '/' ) . slice ( 1 ) ) ;
80
- url = URLExt . join ( url , repo , 'tree' , 'master' , dirPath ) ;
74
+ const resource = parsePath ( this . _browser . model . path . split ( ':' ) [ 1 ] ) ;
75
+ url = URLExt . join ( url , resource . user ) ;
76
+ if ( resource . repository ) {
77
+ url = URLExt . join ( url , resource . repository ,
78
+ 'tree' , 'master' , resource . path ) ;
81
79
}
82
80
window . open ( url ) ;
83
81
} ,
@@ -93,9 +91,9 @@ class GitHubFileBrowser extends Widget {
93
91
if ( ! this . _binderActive ) {
94
92
return ;
95
93
}
96
- const user = this . _drive . user ;
97
- const repo = this . _browser . model . path . split ( '/' ) [ 0 ] . split ( ':' ) [ 1 ] ;
98
- const url = URLExt . join ( MY_BINDER_BASE_URL , user , repo , 'master' ) ;
94
+ const resource = parsePath ( this . _browser . model . path . split ( ':' ) [ 1 ] ) ;
95
+ const url = URLExt . join ( MY_BINDER_BASE_URL , resource . user ,
96
+ resource . repository , 'master' ) ;
99
97
window . open ( url + '?urlpath=lab' ) ;
100
98
} ,
101
99
tooltip : 'Launch this repository on mybinder.org' ,
@@ -109,7 +107,6 @@ class GitHubFileBrowser extends Widget {
109
107
this . _onPathChanged ( ) ;
110
108
111
109
this . _drive . rateLimitedState . changed . connect ( this . _updateErrorPanel , this ) ;
112
- this . _drive . validUserState . changed . connect ( this . _updateErrorPanel , this ) ;
113
110
}
114
111
115
112
/**
@@ -121,11 +118,13 @@ class GitHubFileBrowser extends Widget {
121
118
* React to a change in user.
122
119
*/
123
120
private _onUserChanged ( sender : ObservableValue , args : ObservableValue . IChangedArgs ) {
124
- this . _drive . user = args . newValue as string ;
125
- // After the user has been changed, cd to their GitHub
126
- // root directory, since any previous directory is no
127
- // longer valid.
128
- this . _browser . model . cd ( '/' ) . then ( ( ) => {
121
+ if ( this . _changeGuard ) {
122
+ return ;
123
+ }
124
+ this . _changeGuard = true ;
125
+ this . _browser . model . cd ( `/${ args . newValue as string } ` ) . then ( ( ) => {
126
+ this . _changeGuard = false ;
127
+ this . _updateErrorPanel ( ) ;
129
128
// Once we have the new listing, maybe give the file listing
130
129
// focus. Once the input element is removed, the active element
131
130
// appears to revert to document.body. If the user has subsequently
@@ -141,16 +140,24 @@ class GitHubFileBrowser extends Widget {
141
140
* React to the path changing for the browser.
142
141
*/
143
142
private _onPathChanged ( ) : void {
144
- const path = this . _browser . model . path ;
143
+ const resource = parsePath ( this . _browser . model . path . split ( ':' ) [ 1 ] ) ;
144
+
145
+ // If we have navigated to the root, reset the user name.
146
+ if ( ! resource . user && ! this . _changeGuard ) {
147
+ this . _changeGuard = true ;
148
+ this . userName . name . set ( '' ) ;
149
+ this . _changeGuard = false ;
150
+ this . _updateErrorPanel ( ) ;
151
+ }
152
+
145
153
// Check for a valid user.
146
- if ( ! this . _drive . validUserState . get ( ) ) {
154
+ if ( ! this . _drive . validUser ) {
147
155
this . _launchBinderButton . addClass ( MY_BINDER_DISABLED ) ;
148
156
this . _binderActive = false ;
149
157
return ;
150
158
}
151
159
// Check for a valid repo.
152
- const repo = path . split ( '/' ) [ 0 ] . split ( ':' ) [ 1 ] ;
153
- if ( ! repo ) {
160
+ if ( ! resource . repository ) {
154
161
this . _launchBinderButton . addClass ( MY_BINDER_DISABLED ) ;
155
162
this . _binderActive = false ;
156
163
return ;
@@ -175,18 +182,20 @@ class GitHubFileBrowser extends Widget {
175
182
* React to a change in the validity of the drive.
176
183
*/
177
184
private _updateErrorPanel ( ) : void {
185
+ const resource = parsePath ( this . _browser . model . path . split ( ':' ) [ 1 ] ) ;
178
186
const rateLimited = this . _drive . rateLimitedState . get ( ) ;
179
- const validUser = this . _drive . validUserState . get ( ) ;
180
- // If everythings is valid, and an error panel is showing, remove it.
181
- if ( ! rateLimited && validUser && this . _errorPanel ) {
187
+ const validUser = this . _drive . validUser ;
188
+
189
+ // If we currently have an error panel, remove it.
190
+ if ( this . _errorPanel ) {
182
191
const listing = ( this . _browser . layout as PanelLayout ) . widgets [ 2 ] ;
183
192
listing . node . removeChild ( this . _errorPanel . node ) ;
184
193
this . _errorPanel . dispose ( ) ;
185
194
this . _errorPanel = null ;
186
195
}
187
196
188
- // If we are being rate limited and there is not error panel, make one .
189
- if ( rateLimited && ! this . _errorPanel ) {
197
+ // If we are being rate limited, make an error panel.
198
+ if ( rateLimited ) {
190
199
this . _errorPanel = new GitHubErrorPanel (
191
200
'You have been rate limited by GitHub! ' +
192
201
'You will need to wait about an hour before ' +
@@ -195,10 +204,10 @@ class GitHubFileBrowser extends Widget {
195
204
listing . node . appendChild ( this . _errorPanel . node ) ;
196
205
}
197
206
198
- // If we have an invalid user there is not error panel, make one .
199
- if ( ! validUser && ! this . _errorPanel ) {
200
- const message = this . _drive . user ?
201
- `"${ this . _drive . user } " appears to be an invalid user name!` :
207
+ // If we have an invalid user, make an error panel.
208
+ if ( ! validUser ) {
209
+ const message = resource . user ?
210
+ `"${ resource . user } " appears to be an invalid user name!` :
202
211
'Please enter a GitHub user name' ;
203
212
this . _errorPanel = new GitHubErrorPanel ( message ) ;
204
213
const listing = ( this . _browser . layout as PanelLayout ) . widgets [ 2 ] ;
@@ -212,6 +221,7 @@ class GitHubFileBrowser extends Widget {
212
221
private _openGitHubButton : ToolbarButton ;
213
222
private _launchBinderButton : ToolbarButton ;
214
223
private _binderActive = false ;
224
+ private _changeGuard = false ;
215
225
}
216
226
217
227
/**
0 commit comments