1
1
// Pure function that generates an nbgitpuller URL
2
- function generateRegularUrl ( hubUrl , urlPath , repoUrl , branch ) {
2
+ function generateRegularUrl ( hubUrl , urlPath , repoUrl , branch , compressed , source ) {
3
3
4
4
// assume hubUrl is a valid URL
5
5
var url = new URL ( hubUrl ) ;
6
6
7
7
url . searchParams . set ( 'repo' , repoUrl ) ;
8
-
8
+
9
+ if ( compressed ) {
10
+ url . searchParams . set ( 'provider' , source ) ;
11
+ }
12
+
9
13
if ( urlPath ) {
10
14
url . searchParams . set ( 'urlpath' , urlPath ) ;
11
15
}
12
16
13
17
if ( branch ) {
14
18
url . searchParams . set ( 'branch' , branch ) ;
19
+ } else if ( source == "git" ) {
20
+ url . searchParams . set ( 'branch' , "main" ) ;
15
21
}
16
-
22
+
17
23
if ( ! url . pathname . endsWith ( '/' ) ) {
18
24
url . pathname += '/'
19
25
}
@@ -22,20 +28,26 @@ function generateRegularUrl(hubUrl, urlPath, repoUrl, branch) {
22
28
return url . toString ( ) ;
23
29
}
24
30
25
- function generateCanvasUrl ( hubUrl , urlPath , repoUrl , branch ) {
31
+ function generateCanvasUrl ( hubUrl , urlPath , repoUrl , branch , compressed , source ) {
26
32
// assume hubUrl is a valid URL
27
33
var url = new URL ( hubUrl ) ;
28
34
29
35
var nextUrlParams = new URLSearchParams ( ) ;
30
36
31
37
nextUrlParams . append ( 'repo' , repoUrl ) ;
32
-
38
+
39
+ if ( compressed ) {
40
+ nextUrlParams . append ( 'provider' , source ) ;
41
+ }
42
+
33
43
if ( urlPath ) {
34
44
nextUrlParams . append ( 'urlpath' , urlPath ) ;
35
45
}
36
46
37
47
if ( branch ) {
38
48
nextUrlParams . append ( 'branch' , branch ) ;
49
+ } else if ( source == "git" ) {
50
+ nextUrlParams . append ( 'branch' , "main" ) ;
39
51
}
40
52
41
53
var nextUrl = '/hub/user-redirect/git-pull?' + nextUrlParams . toString ( ) ;
@@ -50,20 +62,26 @@ function generateCanvasUrl(hubUrl, urlPath, repoUrl, branch) {
50
62
}
51
63
52
64
function generateBinderUrl ( hubUrl , userName , repoName , branch , urlPath ,
53
- contentRepoUrl , contentRepoBranch ) {
65
+ contentRepoUrl , contentRepoBranch , compressed , source ) {
54
66
55
67
var url = new URL ( hubUrl ) ;
56
68
57
69
var nextUrlParams = new URLSearchParams ( ) ;
58
70
59
71
nextUrlParams . append ( 'repo' , contentRepoUrl ) ;
60
72
73
+ if ( compressed ) {
74
+ nextUrlParams . append ( 'provider' , source ) ;
75
+ }
76
+
61
77
if ( urlPath ) {
62
78
nextUrlParams . append ( 'urlpath' , urlPath ) ;
63
79
}
64
80
65
81
if ( contentRepoBranch ) {
66
82
nextUrlParams . append ( 'branch' , contentRepoBranch ) ;
83
+ } else if ( source == "git" ) {
84
+ nextUrlParams . append ( 'branch' , "main" ) ;
67
85
}
68
86
69
87
var nextUrl = 'git-pull?' + nextUrlParams . toString ( ) ;
@@ -100,37 +118,41 @@ var apps = {
100
118
}
101
119
}
102
120
121
+ function clearLinks ( ) {
122
+ document . getElementById ( 'default-link' ) . value = "" ;
123
+ document . getElementById ( 'binder-link' ) . value = "" ;
124
+ document . getElementById ( 'canvas-link' ) . value = "" ;
125
+ }
126
+
127
+
103
128
function changeTab ( div ) {
104
129
var hub = document . getElementById ( "hub" ) ;
105
130
var hub_help_text = document . getElementById ( "hub-help-text" ) ;
106
- var env_repo = document . getElementById ( "repo" ) ;
107
- var env_repo_branch = document . getElementById ( "branch" ) ;
108
- var env_repo_help_text = document . getElementById ( "env-repo-help-text" ) ;
109
- var content_repo = document . getElementById ( "content-repo-group" ) ;
110
- var content_branch = document . getElementById ( "content-branch-group" ) ;
131
+ var env_repo_group = document . getElementById ( "env-repo-group" ) ;
132
+ var env_repo = document . getElementById ( "env-repo" ) ;
111
133
var id = div . id ;
112
-
134
+ var form = document . getElementById ( 'linkgenerator' ) ;
135
+
136
+ clearLinks ( ) ;
113
137
if ( id . includes ( "binder" ) ) {
114
138
hub . placeholder = "https://mybinder.org" ;
115
139
hub . value = "https://mybinder.org" ;
116
140
hub_help_text . hidden = true ;
117
141
hub . labels [ 0 ] . innerHTML = "BinderHub URL" ;
118
- env_repo . labels [ 0 ] . innerHTML = "Git Environment Repository URL" ;
119
- env_repo_help_text . hidden = false ;
120
- env_repo_branch . required = true ;
121
- env_repo_branch . pattern = ".+" ;
122
- content_repo . hidden = false ;
123
- content_branch . hidden = false ;
142
+
143
+ env_repo_group . style . display = '' ;
144
+ env_repo . disabled = false ;
145
+
124
146
} else {
125
147
hub . placeholder = "https://hub.example.com" ;
148
+ hub . value = "" ;
126
149
hub_help_text . hidden = false ;
127
150
hub . labels [ 0 ] . innerHTML = "JupyterHub URL" ;
128
- env_repo . labels [ 0 ] . innerHTML = "Git Repository URL" ;
129
- env_repo_help_text . hidden = true ;
130
- env_repo_branch . required = false ;
131
- content_repo . hidden = true ;
132
- content_branch . hidden = true ;
151
+
152
+ env_repo_group . style . display = 'none' ;
153
+ env_repo . disabled = true ;
133
154
}
155
+ displaySource ( ) ;
134
156
}
135
157
136
158
/**
@@ -141,28 +163,60 @@ function changeTab(div) {
141
163
* See https://github.com/git/git/blob/1c52ecf4ba0f4f7af72775695fee653f50737c71/builtin/clone.c#L276
142
164
*/
143
165
function generateCloneDirectoryName ( gitCloneUrl ) {
166
+ if ( gitCloneUrl . slice ( - 1 ) == "/" )
167
+ gitCloneUrl = gitCloneUrl . slice ( 0 , - 1 ) ;
144
168
var lastPart = gitCloneUrl . split ( '/' ) . slice ( - 1 ) [ 0 ] ;
145
169
return lastPart . split ( ':' ) . slice ( - 1 ) [ 0 ] . replace ( / ( \. g i t | \. b u n d l e ) ? / , '' ) ;
146
170
}
147
171
172
+ function handleSource ( args ) {
173
+ source = args [ "source" ] ;
174
+ branch = "" ;
175
+ compressed = true ;
176
+ sourceUrl = "" ;
177
+ if ( source == "git" ) {
178
+ sourceUrl = args [ "contentRepoUrl" ] ;
179
+ branch = args [ "contentRepoBranch" ] ;
180
+ compressed = false ;
181
+ } else if ( source == "googledrive" ) {
182
+ sourceUrl = args [ "driveUrl" ] ;
183
+ } else if ( source == "dropbox" ) {
184
+ sourceUrl = args [ "dropUrl" ] ;
185
+ } else if ( source == "standard" ) {
186
+ sourceUrl = args [ "webUrl" ] ;
187
+ }
188
+ return {
189
+ "branch" : branch ,
190
+ "sourceUrl" : sourceUrl ,
191
+ "compressed" : compressed
192
+ }
193
+ }
194
+
148
195
function displayLink ( ) {
149
196
var form = document . getElementById ( 'linkgenerator' ) ;
150
-
197
+
151
198
form . classList . add ( 'was-validated' ) ;
152
199
if ( form . checkValidity ( ) ) {
153
200
var hubUrl = document . getElementById ( 'hub' ) . value ;
154
- var repoUrl = document . getElementById ( 'repo' ) . value ;
155
- var branch = document . getElementById ( 'branch' ) . value ;
201
+ var driveUrl = document . getElementById ( 'drive-url' ) . value ;
202
+ var dropUrl = document . getElementById ( 'drop-url' ) . value ;
203
+ var webUrl = document . getElementById ( 'standard-url' ) . value ;
204
+ var envRepoUrl = document . getElementById ( 'env-repo' ) . value ;
205
+ var envGitBranch = document . getElementById ( 'env-branch' ) . value ;
156
206
var contentRepoUrl = document . getElementById ( 'content-repo' ) . value ;
157
207
var contentRepoBranch = document . getElementById ( 'content-branch' ) . value ;
158
208
var filePath = document . getElementById ( 'filepath' ) . value ;
159
209
var appName = form . querySelector ( 'input[name="app"]:checked' ) . value ;
160
210
var activeTab = document . querySelector ( ".nav-link.active" ) . id ;
161
-
211
+ var source = form . querySelector ( 'input[name="source"]:checked' ) . value ;
212
+
162
213
if ( appName === 'custom' ) {
163
214
var urlPath = document . getElementById ( 'urlpath' ) . value ;
164
215
} else {
165
- var repoName = generateCloneDirectoryName ( repoUrl ) ;
216
+ var repoName = generateCloneDirectoryName ( contentRepoUrl ) ;
217
+ if ( source !== "git" ) {
218
+ repoName = ""
219
+ }
166
220
var urlPath ;
167
221
if ( activeTab === "tab-auth-binder" ) {
168
222
var contentRepoName = new URL ( contentRepoUrl ) . pathname . split ( '/' ) . pop ( ) . replace ( / \. g i t $ / , '' ) ;
@@ -171,26 +225,37 @@ function displayLink() {
171
225
urlPath = apps [ appName ] . generateUrlPath ( repoName + '/' + filePath ) ;
172
226
}
173
227
}
174
-
228
+ args = {
229
+ "source" : source ,
230
+ "contentRepoUrl" : contentRepoUrl ,
231
+ "contentRepoBranch" : contentRepoBranch ,
232
+ "driveUrl" : driveUrl ,
233
+ "dropUrl" : dropUrl ,
234
+ "webUrl" : webUrl
235
+ }
236
+ config = handleSource ( args )
175
237
if ( activeTab === "tab-auth-default" ) {
176
238
document . getElementById ( 'default-link' ) . value = generateRegularUrl (
177
- hubUrl , urlPath , repoUrl , branch
239
+ hubUrl , urlPath , config [ "sourceUrl" ] , config [ " branch" ] , config [ "compressed" ] , source
178
240
) ;
179
241
} else if ( activeTab === "tab-auth-canvas" ) {
180
242
document . getElementById ( 'canvas-link' ) . value = generateCanvasUrl (
181
- hubUrl , urlPath , repoUrl , branch
243
+ hubUrl , urlPath , config [ "sourceUrl" ] , config [ " branch" ] , config [ "compressed" ] , source
182
244
) ;
183
245
} else if ( activeTab === "tab-auth-binder" ) {
184
246
// FIXME: userName parsing using new URL(...) assumes a
185
247
// HTTP based repoUrl. Does it make sense to create a
186
248
// BinderHub link for SSH URLs? Then let's fix this parsing.
187
- var userName = new URL ( repoUrl ) . pathname . split ( '/' ) [ 1 ] ;
249
+ var userName = new URL ( envRepoUrl ) . pathname . split ( '/' ) [ 1 ] ;
188
250
document . getElementById ( 'binder-link' ) . value = generateBinderUrl (
189
- hubUrl , userName , repoName , branch , urlPath , contentRepoUrl , contentRepoBranch
251
+ hubUrl , userName , repoName , envGitBranch , urlPath , config [ "sourceUrl" ] , config [ "branch" ] , config [ "compressed" ] , source
190
252
) ;
191
253
}
254
+ } else {
255
+ clearLinks ( ) ;
192
256
}
193
257
}
258
+
194
259
function populateFromQueryString ( ) {
195
260
// preseed values if specified in the url
196
261
var params = new URLSearchParams ( window . location . search ) ;
@@ -213,6 +278,33 @@ function populateFromQueryString() {
213
278
}
214
279
}
215
280
281
+ function hideShowByClassName ( cls , hideShow ) {
282
+ [ ] . forEach . call ( document . querySelectorAll ( cls ) , function ( el ) {
283
+ el . style . display = hideShow ;
284
+ setDisabled = ( hideShow == 'none' )
285
+ $ ( el ) . find ( "input" ) . each ( function ( ) {
286
+ $ ( this ) . prop ( "disabled" , setDisabled ) ;
287
+ } ) ;
288
+ } ) ;
289
+ }
290
+
291
+
292
+ function displaySource ( ) {
293
+ var form = document . getElementById ( 'linkgenerator' ) ;
294
+ var source = form . querySelector ( 'input[name="source"]:checked' ) . value ;
295
+ hideShowByClassName ( ".source" , 'none' ) ;
296
+
297
+ if ( source == 'git' ) {
298
+ hideShowByClassName ( ".source-git" , '' ) ;
299
+ } else if ( source == 'googledrive' ) {
300
+ hideShowByClassName ( ".source-googledrive" , '' ) ;
301
+ } else if ( source == 'dropbox' ) {
302
+ hideShowByClassName ( ".source-dropbox" , '' ) ;
303
+ } else if ( source == "standard" ) {
304
+ hideShowByClassName ( ".source-standard" , '' ) ;
305
+ }
306
+ }
307
+
216
308
/**
217
309
* Main loop of the program.
218
310
*
@@ -225,6 +317,7 @@ function render() {
225
317
var form = document . getElementById ( 'linkgenerator' ) ;
226
318
var appName = form . querySelector ( 'input[name="app"]:checked' ) . value ;
227
319
320
+
228
321
if ( appName == 'custom' ) {
229
322
document . getElementById ( 'urlpath' ) . disabled = false ;
230
323
document . getElementById ( 'filepath' ) . disabled = true ;
@@ -238,6 +331,7 @@ function render() {
238
331
document . getElementById ( 'filepath' ) . disabled = false ;
239
332
}
240
333
}
334
+
241
335
displayLink ( ) ;
242
336
}
243
337
@@ -246,11 +340,21 @@ function render() {
246
340
*/
247
341
function main ( ) {
248
342
// Hook up any changes in form elements to call render()
249
- document . querySelectorAll ( '#linkgenerator input[type="radio "]' ) . forEach (
343
+ document . querySelectorAll ( '#linkgenerator input[name="app "]' ) . forEach (
250
344
function ( element ) {
251
345
element . addEventListener ( 'change' , render ) ;
252
346
}
253
347
)
348
+ document . querySelectorAll ( '#linkgenerator input[name="source"]' ) . forEach (
349
+ function ( element ) {
350
+ element . addEventListener ( 'change' , function ( ) {
351
+ displaySource ( ) ;
352
+ render ( ) ;
353
+ }
354
+ ) ;
355
+ }
356
+ )
357
+
254
358
document . querySelectorAll ( '#linkgenerator input[type="text"], #linkgenerator input[type="url"]' ) . forEach (
255
359
function ( element ) {
256
360
element . addEventListener ( 'input' , render ) ;
@@ -269,7 +373,9 @@ function main() {
269
373
}
270
374
}
271
375
376
+
272
377
// Do an initial render, to make sure our disabled / enabled properties are correctly set
378
+ displaySource ( ) ;
273
379
render ( ) ;
274
380
}
275
381
0 commit comments