-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfilebrowser.js
3695 lines (2238 loc) · 87.3 KB
/
filebrowser.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let hoveringSidebarToggle = false;
// show bookmark on hover
sidebarToggle.addEventListener('mouseover', () => {
hoveringSidebarToggle = true;
if (!body.classList.contains('expanded')) {
sidebarToggle.classList.add('visible');
}
})
// hide bookmark on mouse out
sidebarToggle.addEventListener('mouseout', () => {
hoveringSidebarToggle = false;
if (!body.classList.contains('expanded')) {
window.setTimeout(() => {
if (!hoveringSidebarToggle &&
!body.classList.contains('expanded')) {
sidebarToggle.classList.remove('visible');
}
}, 1500);
}
})
// toggle sidebar on click of bookmark
sidebarToggle.addEventListener('click', () => {
toggleSidebar(!body.classList.contains('expanded'));
saveSidebarStateLS();
})
// render sidebar
// call this function when signed in to git
// to render sidebar
async function renderSidebarHTML(pageNum = 1) {
// if not already loading, start loading
if (loader.style.opacity != '1') {
startLoading();
}
// map tree location
const [user, repo, contents] = treeLoc;
const [repoName, branch] = repo.split(':');
// if not signed into git
// and navigated to Repositories page
if (gitToken == '' && repo == '') {
// stop loading
stopLoading();
// show sign-in screen
sidebar.classList.add('intro');
return;
}
let resp;
// if navigating in repository
if (repo != '') {
// get repo obj from local storage
const repoObj = modifiedRepos[user + '/' + repoName];
// if repo is empty
if (repoObj && repoObj.empty) {
// stop loading
stopLoading();
// show intro screen
fileWrapper.innerHTML = fileIntroScreen;
// if repo is owned by logged user
if (user === loggedUser) {
// show repo name
sidebarLogo.innerText = repoName;
} else {
// show username and repo name
sidebarLogo.innerText = user + '/' + repoName;
}
// scroll to start of repo name
sidebarLogo.scrollTo(0, 0);
scrolledSidebarTitle();
// change header options
header.classList.remove('out-of-repo');
// hide search button
searchButton.classList.add('hidden');
return;
}
const currentTime = new Date().getTime();
// if repo obj dosen't exist
if (!repoObj || !repoObj.defaultBranch
|| repoObj.repoDataExpiration === undefined || repoObj.branchExpiration === undefined
|| repoObj.repoDataExpiration < currentTime) {
// get repo obj from git
// and save to modified repos
fetchRepoAndSaveToModRepos(treeLoc);
}
// render branch menu
renderBranchMenuHTML();
}
// if sidebar title is empty
if (sidebarLogo.innerText === '') {
if (contents != '') {
// if repo is owned by logged user
if (user === loggedUser) {
// show repo name and path
sidebarLogo.innerText = repoName + contents;
} else {
// show username, repo name and path
sidebarLogo.innerText = user + '/' + repoName + contents;
}
sidebarLogo.classList.add('notransition');
// scroll to end of title
sidebarLogo.scrollTo({
left: sidebarLogo.scrollWidth - sidebarLogo.offsetLeft
});
scrolledSidebarTitle();
onNextFrame(() => {
sidebarLogo.classList.remove('notransition');
});
} else if (repo != '') {
// if repo is owned by logged user
if (user === loggedUser) {
// show repo name
sidebarLogo.innerText = repoName;
} else {
// show username and repo name
sidebarLogo.innerText = user + '/' + repoName;
}
sidebarLogo.classList.add('notransition');
// scroll to start of title
sidebarLogo.scrollTo(0, 0);
scrolledSidebarTitle();
onNextFrame(() => {
sidebarLogo.classList.remove('notransition');
});
} else {
// show title
sidebarLogo.innerText = 'Repositories';
// hide branch button
sidebarBranch.classList.remove('visible');
sidebarLogo.classList.add('notransition');
// scroll to start of title
sidebarLogo.scrollTo(0, 0);
scrolledSidebarTitle();
onNextFrame(() => {
sidebarLogo.classList.remove('notransition');
});
}
}
// get items in current tree from git
resp = await git.getItems(treeLoc, pageNum);
// if switched directory while loading, return
// through cde.run links the branch can change (from no branch to the default branch),
// so don't take it into account
if (user !== treeLoc[0] ||
repoName !== treeLoc[1].split(':')[0] ||
contents !== treeLoc[2]) {
return;
}
if (resp.message && resp.message == 'Not Found') {
// if couldn't find repository, show not found screen
// stop loading
stopLoading();
// get repo obj from local storage
const repoObj = modifiedRepos[user + '/' + repoName];
// if repo obj exists
if (repoObj) {
// delete repo obj from modified repos
deleteModRepo(user + '/' + repoName);
}
// if not signed in
if (gitToken == '') {
const dialogResp = await showDialog(async () => {
await openGitHubSignIn();
// hide dialog
hideDialog();
}, 'Hmm... the repo you\'re\nlooking for can\'t be found.\nTry signing in.', 'Sign in', true);
// if chosen to sign in, return
if (dialogResp == true) return;
} else { // if signed in
await showDialog(hideDialog, 'Hmm... the repo you\'re\nlooking for can\'t be found.', 'OK', true);
}
// change location
treeLoc[1] = '';
treeLoc[2] = '';
saveTreeLocLS(treeLoc);
// change sidebar title
sidebarLogo.innerText = 'Repositories';
// hide branch button
sidebarBranch.classList.remove('visible');
// scroll to start of repo name
sidebarLogo.scrollTo(0, 0);
scrolledSidebarTitle();
renderSidebarHTML();
return;
}
if (resp.message && resp.message === 'This repository is empty.') {
// if repository is empty, show file intro screen
// stop loading
stopLoading();
// get repo obj from local storage
const repoObj = modifiedRepos[user + '/' + repoName];
// if repo obj exists
if (repoObj) {
// update repo empty status in local storage
updateModRepoEmptyStatus(repoObj.fullName, true);
}
// show intro screen
fileWrapper.innerHTML = fileIntroScreen;
// if repo is owned by logged user
if (user === loggedUser) {
// show repo name
sidebarLogo.innerText = repoName;
} else {
// show username and repo name
sidebarLogo.innerText = user + '/' + repoName;
}
// scroll to start of repo name
sidebarLogo.scrollTo(0, 0);
scrolledSidebarTitle();
// change header options
header.classList.remove('out-of-repo');
// hide search button
searchButton.classList.add('hidden');
return;
}
if (resp.message && resp.message.startsWith('No commit found for the ref')) {
// if couldn't find branch, show not found screen
// get repo obj from local storage
const repoObj = modifiedRepos[user + '/' + repoName];
let defaultBranch;
if (repoObj && repoObj.defaultBranch) {
defaultBranch = repoObj.defaultBranch;
} else {
defaultBranch = (await git.getRepo(treeLoc)).default_branch;
}
// stop loading
stopLoading();
showMessage('Hmm... that branch can\'t be found.', 5000);
// add branch to tree
treeLoc[1] = repo.split(':')[0] + ':' + defaultBranch;
saveTreeLocLS(treeLoc);
// update selected branch in local storage
updateModRepoSelectedBranch((user + '/' + repoName), defaultBranch);
// update branches in local storage
updateModRepoBranchExpiration((user + '/' + repoName), 0);
renderSidebarHTML();
return;
}
if (resp.message && resp.message == 'Bad credentials') {
// if failed to get items,
// show sign-in screen
// stop loading
stopLoading();
showMessage('Your sign-in token expired.', 4000);
sidebar.classList.add('intro');
return;
}
// render modified files
let eclipsedFiles;
// if navigating in repository
if (repo != '') {
// get all eclipsed files in directory
eclipsedFiles = Object.values(modifiedFiles).filter(modFile => modFile.dir == treeLoc.join());
}
// save rendered HTML
let out = '';
// if response
if (resp) {
// show title
if (contents != '') {
// if repo is owned by logged user
if (user === loggedUser) {
// show repo name and path
sidebarLogo.innerText = repoName + contents;
} else {
// show username, repo name and path
sidebarLogo.innerText = user + '/' + repoName + contents;
}
// scroll to end of title
sidebarLogo.scrollTo({
left: sidebarLogo.scrollWidth - sidebarLogo.offsetLeft
});
sidebarLogo.classList.add('notransition');
onNextFrame(() => {
sidebarLogo.classList.remove('notransition');
});
scrolledSidebarTitle();
} else if (repo != '') {
// if repo is owned by logged user
if (user === loggedUser) {
// show repo name
sidebarLogo.innerText = repoName;
} else {
// show username and repo name
sidebarLogo.innerText = user + '/' + repoName;
}
// scroll to start of repo name
sidebarLogo.scrollTo(0, 0);
sidebarLogo.classList.add('notransition');
onNextFrame(() => {
sidebarLogo.classList.remove('notransition');
});
scrolledSidebarTitle();
} else {
// show title
sidebarLogo.innerText = 'Repositories';
// hide branch button
sidebarBranch.classList.remove('visible');
// scroll to start of repo name
sidebarLogo.scrollTo(0, 0);
sidebarLogo.classList.add('notransition');
onNextFrame(() => {
sidebarLogo.classList.remove('notransition');
});
scrolledSidebarTitle();
}
// if navigating in repository
if (repo != '') {
// change header options
header.classList.remove('out-of-repo');
// if files exist
if (resp.length > 0 || eclipsedFiles.length > 0) {
// show search button
searchButton.classList.remove('hidden');
// render files
resp.forEach(item => {
// if item is a file
if (item.type == 'file') {
// get the file's latest version
let file = getLatestVersion(item);
// search for matching eclipsed files
for (let i = 0; i < eclipsedFiles.length; i++) {
let modFile = eclipsedFiles[i];
// if eclipsed file has matching SHA or name
if (modFile.sha === file.sha || modFile.name === file.name) {
// remove eclipsed file from array
eclipsedFiles.splice(i, 1);
// reset index
i--;
}
}
protectModFileInSidebar(file.sha, file.name);
// add modified flag to file
let modified = '';
if (modifiedFiles[file.sha] &&
!modifiedFiles[file.sha].eclipsed) modified = ' modified';
// add icon to file
const fileType = getFileType(file.name);
let fileIconHTML = fileIcon;
if (fileType === 'image') fileIconHTML = imageIcon;
if (fileType === 'video') fileIconHTML = videoIcon;
if (fileType === 'audio') fileIconHTML = audioIcon;
if (file.name.includes('README')) fileIconHTML = readmeIcon;
out += `
<div class="item file`+ modified +`" sha="`+ file.sha +`">
<div class="label">
`+ fileIconHTML +`
<a class="name">`+ file.name +`</a>
</div>
<div class="push-wrapper">
`+ pushIcon +`
</div>
</div>
`;
} else { // if item is a folder
out += `
<div class="item folder">
<div class="label">
`+ folderIcon +`
<a class="name">`+ item.name +`</a>
</div>
`+ arrowIcon +`
</div>
`;
}
});
// render eclipsed files from array
let eclipsedFileNames = {};
eclipsedFiles.forEach(file => {
// if file isn't already in HTML
if (!eclipsedFileNames[file.name]) {
// add file to HTML
eclipsedFileNames[file.name] = true;
// get the file's latest version
file = getLatestVersion(file);
// add modified flag to file
let modified = '';
if (!file.eclipsed) modified = ' modified';
// add icon to file
const fileType = getFileType(file.name);
let fileIconHTML = fileIcon;
if (fileType === 'image') fileIconHTML = imageIcon;
if (fileType === 'video') fileIconHTML = videoIcon;
if (fileType === 'audio') fileIconHTML = audioIcon;
if (file.name.includes('README')) fileIconHTML = readmeIcon;
out = `
<div class="item file`+ modified +`" sha="`+ file.sha +`">
<div class="label">
`+ fileIconHTML +`
<a class="name">`+ file.name +`</a>
</div>
<div class="push-wrapper">
`+ pushIcon +`
</div>
</div>
` + out;
}
});
} else {
// if no files exist,
// show intro screen in HTML
out = fileIntroScreen;
// hide search button
searchButton.classList.add('hidden');
}
} else { // else, show all repositories
// change header options
header.classList.add('out-of-repo');
// get rendered repos
let renderedRepos = {};
// get all user-owned modified repos
const userModRepos = Object.keys(modifiedRepos).filter(repo => repo.split('/')[0] === loggedUser);
// if repositories exist
if (resp.length > 0 || userModRepos.length > 0) {
// show search button
searchButton.classList.remove('hidden');
// render repositories
resp.forEach(item => {
// if repo is in modified repos
if (modifiedRepos[item.full_name]) {
// add repo to rendered repos
renderedRepos[item.full_name] = true;
}
// if eclipsed repo already exists in HTML
// when rendering more pages, return
const eclipsedRepoEl = fileWrapper
.querySelector(
'.repo[fullname="'+ item.full_name +'"]'
);
if (eclipsedRepoEl) {
return;
}
let fullName;
// if repo is owned by logged user
if (item.full_name.split('/')[0] === loggedUser) {
// show repo name
fullName = item.name;
} else {
// show username and repo name
fullName = item.full_name;
}
let repoObj;
// if repo obj dosen't already exist
if (!modifiedRepos[item.full_name]) {
// get repo data expiration time
// (two months from now)
let expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + (2 * 4 * 7));
const twoMonthsTime = expirationDate.getTime();
// create repo obj
repoObj = createRepoObj(item.full_name, item.default_branch, item.default_branch,
(item.permissions.push ?? false),
null, item.private, item.fork, false,
twoMonthsTime, 0);
} else {
repoObj = false;
}
out += `
<div class="item repo" ` + (repoObj ? ('repoObj="' + encodeURI(JSON.stringify(repoObj)) + '"') : ('fullName="' + item.full_name + '"')) + `>
<div class="label">
`+ repoIcon +`
<a class="name">`+ fullName +`</a>
</div>
`+ arrowIcon +`
</div>
`;
});
// if rendering first page
if (pageNum === 1) {
// render eclipsed repos
for (const modRepoName in modifiedRepos) {
const modRepo = modifiedRepos[modRepoName];
// if repo isn't rendered
// and user has push access in repo
if (!renderedRepos[modRepoName]
&& modRepo.pushAccess) {
// render repo
let fullName;
// if repo is owned by logged user
if (modRepoName.split('/')[0] === loggedUser) {
// show repo name
fullName = modRepoName.split('/')[1];
} else {
// show username and repo name
fullName = modRepoName;
}
out += `
<div class="item repo" ` + ('fullName="' + modRepoName + '"') + `>
<div class="label">
`+ repoIcon +`
<a class="name">`+ fullName +`</a>
</div>
`+ arrowIcon +`
</div>
`;
}
}
}
// if non-eclipsed repositories exist
// and resp length is equal to max length
if (resp.length > 0 && resp.length === 100) {
// render 'more' button
const nextPage = (pageNum + 1);
out += `
<div class="item more" nextPage="`+ nextPage +`">
<div class="label">
`+ moreIcon +`
<a class="name">more</a>
</div>
</div>
`;
}
} else if (pageNum === 1) { // if rendering first page
// if no repositories exist,
// show intro screen in HTML
out = repoIntroScreen;
// hide search button
searchButton.classList.add('hidden');
}
}
}
// add rendered HTML to DOM
// if rendering first page
if (pageNum === 1) {
fileWrapper.innerHTML = out;
sidebar.scrollTo(0, 0);
} else { // if rendering additional pages
// if there's a duplicate more button, remove it
const moreButton = fileWrapper.querySelector('.item.more');
if (moreButton) {
moreButton.remove();
}
// don't override existing HTML items
fileWrapper.innerHTML += out;
}
// stop loading
stopLoading();
// add item event listeners
addHTMLItemListeners();
// if selected file is in current directory
if (selectedFile.dir == treeLoc.join()) {
let selectedEl = fileWrapper.querySelector('.item[sha="'+ selectedFile.sha +'"]');
if (selectedEl) {
// select file
selectedEl.classList.add('selected');
selectedEl.scrollIntoViewIfNeeded();
}
}
// if selected file exists
if (selectedFile.sha !== '') {
// protect unsaved code
protectUnsavedCode();
}
// hide branch menu
branchMenu.classList.remove('visible');
sidebarBranch.classList.remove('active');
// if searching
if (header.classList.contains('searching')) {
// refresh search
searchInput.search();
}
}
// load more repos if scrolled to bottom of sidebar
sidebar.addEventListener('scroll', () => {
const moreButton = fileWrapper.querySelector('.item.more');
// if more button exists
// and is not disabled (loading more)
if (moreButton &&
!moreButton.classList.contains('disabled')) {
const maxScroll = sidebar.scrollHeight - sidebar.clientHeight;
// if scrolled to bottom of sidebar
if (sidebar.scrollTop >= maxScroll) {
// load more repos
clickedOnMoreButtonHTML(moreButton);
}
}
});
// adds item event listeners
function addHTMLItemListeners() {
let items = fileWrapper.querySelectorAll('.item');
// run on all items
items.forEach(item => {
// navigate on click
item.addEventListener('click', async (e) => {
// if item is a repository
if (item.classList.contains('repo')) {
// parse repo obj from HTML
const repoObj = getAttr(item, 'repoObj') ? JSON.parse(decodeURI(getAttr(item, 'repoObj'))) :
modifiedRepos[getAttr(item, 'fullName')];
// change location
const repoLoc = repoObj.fullName.split('/');
treeLoc[0] = repoLoc[0],
treeLoc[1] = repoLoc[1] + ':' + repoObj.selBranch;
saveTreeLocLS(treeLoc);
// if repo obj is in HTML
if (getAttr(item, 'repoObj')) {
// add repo obj to modified repos
addRepoToModRepos(repoObj);
}
// if repo isn't empty
if (!repoObj.empty) {
// render sidebar
await renderSidebarHTML();
// close search
searchInput.closeSearch();
} else {
// close search
searchInput.closeSearch();
// show intro screen
fileWrapper.innerHTML = fileIntroScreen;
// if repo is owned by logged user
if (repoLoc[0] === loggedUser) {
// show repo name
sidebarLogo.innerText = repoLoc[1];
} else {
// show username and repo name
sidebarLogo.innerText = repoLoc[0] + '/' + repoLoc[1];
}
// scroll to start of repo name
sidebarLogo.scrollTo(0, 0);
scrolledSidebarTitle();
// change header options
header.classList.remove('out-of-repo');
// hide search button