@@ -22,7 +22,7 @@ import {
2222} from '@jupyterlab/filebrowser' ;
2323
2424import {
25- GitHubDrive
25+ GitHubDrive , parsePath
2626} from './contents' ;
2727
2828
@@ -55,7 +55,7 @@ class GitHubFileBrowser extends Widget {
5555 this . _drive = drive ;
5656
5757 // 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>' ) ;
5959 this . userName . addClass ( 'jp-GitHubEditableUserName' ) ;
6060 this . userName . node . title = 'Click to edit user/organization' ;
6161 this . _browser . toolbar . addItem ( 'user' , this . userName ) ;
@@ -66,18 +66,16 @@ class GitHubFileBrowser extends Widget {
6666 this . _openGitHubButton = new ToolbarButton ( {
6767 onClick : ( ) => {
6868 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 ) {
7171 window . open ( url ) ;
7272 return ;
7373 }
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 ) ;
8179 }
8280 window . open ( url ) ;
8381 } ,
@@ -93,9 +91,9 @@ class GitHubFileBrowser extends Widget {
9391 if ( ! this . _binderActive ) {
9492 return ;
9593 }
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' ) ;
9997 window . open ( url + '?urlpath=lab' ) ;
10098 } ,
10199 tooltip : 'Launch this repository on mybinder.org' ,
@@ -109,7 +107,6 @@ class GitHubFileBrowser extends Widget {
109107 this . _onPathChanged ( ) ;
110108
111109 this . _drive . rateLimitedState . changed . connect ( this . _updateErrorPanel , this ) ;
112- this . _drive . validUserState . changed . connect ( this . _updateErrorPanel , this ) ;
113110 }
114111
115112 /**
@@ -121,11 +118,13 @@ class GitHubFileBrowser extends Widget {
121118 * React to a change in user.
122119 */
123120 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 ( ) ;
129128 // Once we have the new listing, maybe give the file listing
130129 // focus. Once the input element is removed, the active element
131130 // appears to revert to document.body. If the user has subsequently
@@ -141,16 +140,24 @@ class GitHubFileBrowser extends Widget {
141140 * React to the path changing for the browser.
142141 */
143142 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+
145153 // Check for a valid user.
146- if ( ! this . _drive . validUserState . get ( ) ) {
154+ if ( ! this . _drive . validUser ) {
147155 this . _launchBinderButton . addClass ( MY_BINDER_DISABLED ) ;
148156 this . _binderActive = false ;
149157 return ;
150158 }
151159 // Check for a valid repo.
152- const repo = path . split ( '/' ) [ 0 ] . split ( ':' ) [ 1 ] ;
153- if ( ! repo ) {
160+ if ( ! resource . repository ) {
154161 this . _launchBinderButton . addClass ( MY_BINDER_DISABLED ) ;
155162 this . _binderActive = false ;
156163 return ;
@@ -175,18 +182,20 @@ class GitHubFileBrowser extends Widget {
175182 * React to a change in the validity of the drive.
176183 */
177184 private _updateErrorPanel ( ) : void {
185+ const resource = parsePath ( this . _browser . model . path . split ( ':' ) [ 1 ] ) ;
178186 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 ) {
182191 const listing = ( this . _browser . layout as PanelLayout ) . widgets [ 2 ] ;
183192 listing . node . removeChild ( this . _errorPanel . node ) ;
184193 this . _errorPanel . dispose ( ) ;
185194 this . _errorPanel = null ;
186195 }
187196
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 ) {
190199 this . _errorPanel = new GitHubErrorPanel (
191200 'You have been rate limited by GitHub! ' +
192201 'You will need to wait about an hour before ' +
@@ -195,10 +204,10 @@ class GitHubFileBrowser extends Widget {
195204 listing . node . appendChild ( this . _errorPanel . node ) ;
196205 }
197206
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!` :
202211 'Please enter a GitHub user name' ;
203212 this . _errorPanel = new GitHubErrorPanel ( message ) ;
204213 const listing = ( this . _browser . layout as PanelLayout ) . widgets [ 2 ] ;
@@ -212,6 +221,7 @@ class GitHubFileBrowser extends Widget {
212221 private _openGitHubButton : ToolbarButton ;
213222 private _launchBinderButton : ToolbarButton ;
214223 private _binderActive = false ;
224+ private _changeGuard = false ;
215225}
216226
217227/**
0 commit comments