-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
1557 lines (1337 loc) · 58.7 KB
/
Copy pathapp.js
File metadata and controls
1557 lines (1337 loc) · 58.7 KB
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 currentPath = "";
let filesList = [];
let sortBy = 'name_asc';
let selectedItem = null;
let adminKey = new URLSearchParams(window.location.search).get('show_hidden') || "";
let activeFilter = 'all';
let isSelecting = false;
let selectedItems = new Set();
let currentEditorFile = null;
let lastSelectedIndex = -1;
let inRecycleBin = false;
let monacoEditor = null;
function setLoader(show) {
document.getElementById('globalLoader').style.display = show ? 'flex' : 'none';
}
// Monaco Loader
require.config({ paths: { vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.45.0/min/vs' } });
function toggleTheme() {
document.body.classList.toggle('light-mode');
const isLight = document.body.classList.contains('light-mode');
localStorage.setItem('theme', isLight ? 'light' : 'dark');
const icon = document.getElementById('themeIcon');
if (icon) {
icon.setAttribute('data-lucide', isLight ? 'moon' : 'sun');
lucide.createIcons();
}
}
if (localStorage.getItem('theme') === 'light') {
document.body.classList.add('light-mode');
}
// Initial icon render
window.addEventListener('DOMContentLoaded', () => {
if (document.body.classList.contains('light-mode')) {
const icon = document.getElementById('themeIcon');
if (icon) icon.setAttribute('data-lucide', 'moon');
}
lucide.createIcons();
initResizer();
});
function initResizer() {
const resizer = document.getElementById('panelResizer');
const panel = document.getElementById('detailsPanel');
if (!resizer || !panel) return;
let isResizing = false;
resizer.addEventListener('mousedown', (e) => {
isResizing = true;
resizer.classList.add('active');
document.body.style.cursor = 'ew-resize';
document.body.style.userSelect = 'none'; // Disable text selection while resizing
});
document.addEventListener('mousemove', (e) => {
if (!isResizing) return;
const width = window.innerWidth - e.clientX;
if (width >= 200 && width <= 800) {
panel.style.width = `${width}px`;
if (monacoEditor) monacoEditor.layout();
}
});
document.addEventListener('mouseup', () => {
if (isResizing) {
isResizing = false;
resizer.classList.remove('active');
document.body.style.cursor = 'default';
document.body.style.userSelect = 'auto';
}
});
}
function toggleSelectMode() {
isSelecting = !isSelecting;
const btn = document.getElementById('selectModeBtn');
// Toggle UI state
document.querySelectorAll('.file-card').forEach(c => {
c.classList.toggle('selecting', isSelecting);
if (!isSelecting) {
c.classList.remove('selected');
const cb = c.querySelector('.select-checkbox');
if (cb) cb.checked = false;
}
});
if (isSelecting) {
btn.innerHTML = `<i data-lucide="x"></i> Cancel`;
} else {
btn.innerHTML = `<i data-lucide="check-square"></i> Select`;
selectedItems.clear();
updateBatchToolbar();
}
lucide.createIcons();
}
function toggleItemSelection(item, card, index, isShift) {
if (isShift && lastSelectedIndex !== -1) {
const start = Math.min(lastSelectedIndex, index);
const end = Math.max(lastSelectedIndex, index);
const cards = document.querySelectorAll('.file-card');
const items = activeFilter === 'all' ? filesList : filesList.filter(it => it.is_dir || getFileType(it.name) === activeFilter);
for (let i = start; i <= end; i++) {
const it = items[i];
const c = cards[i];
const key = JSON.stringify({ path: it.path || currentPath, name: it.name });
if (!selectedItems.has(key)) {
selectedItems.add(key);
c.classList.add('selected');
c.querySelector('.select-checkbox').checked = true;
}
}
} else {
const key = JSON.stringify({ path: item.path || currentPath, name: item.name });
if (selectedItems.has(key)) {
selectedItems.delete(key);
card.classList.remove('selected');
card.querySelector('.select-checkbox').checked = false;
} else {
selectedItems.add(key);
card.classList.add('selected');
card.querySelector('.select-checkbox').checked = true;
}
}
lastSelectedIndex = index;
updateBatchToolbar();
}
function updateBatchToolbar() {
const bar = document.getElementById('batchToolbar');
const count = document.getElementById('batchCount');
count.textContent = `${selectedItems.size} items selected`;
bar.classList.toggle('active', selectedItems.size > 0);
}
async function fetchSysInfo() {
try {
const res = await fetch('/api/sysinfo');
const data = await res.json();
document.getElementById('sysInfo').innerHTML = `
<span><i data-lucide="monitor" style="width:14px; height:14px;"></i> ${data.os}</span>
<span><i data-lucide="cpu" style="width:14px; height:14px;"></i> CPU: ${data.cpu}%</span>
<span><i data-lucide="memory-stick" style="width:14px; height:14px;"></i> RAM: ${data.ram}%</span>
<span><i data-lucide="hard-drive" style="width:14px; height:14px;"></i> Disk: ${data.disk.percent}%</span>
`;
lucide.createIcons();
} catch (e) { }
}
setInterval(fetchSysInfo, 10000);
fetchSysInfo();
function setFilter(button) {
document.querySelectorAll(".filter-btn").forEach(b => b.classList.remove("active"));
button.classList.add("active");
activeFilter = button.dataset.filter;
renderFiles();
}
async function openSendToModal() {
// selectedItem is the item you right-clicked on
if (!selectedItem) return;
document.getElementById('contextMenu').style.display = 'none';
const modal = document.getElementById('sendToModal');
const list = document.getElementById('folderList');
list.innerHTML = "Loading folders...";
modal.style.display = "flex";
const res = await fetch('/api/all_folders');
const folders = await res.json();
list.innerHTML = "";
// Create a button for the Root "Home" folder
createFolderBtn(list, `<i data-lucide="home" style="width:14px; height:14px; margin-right:4px;"></i> Home (Root)`, "");
folders.forEach(path => {
if (path === "") return; // Skip root as we added it manually
// Indent subfolders for a "tree" look
const depth = path.split('/').length - 1;
const name = " ".repeat(depth) + `<i data-lucide="folder" style="width:14px; height:14px; margin-right:4px;"></i> ` + path.split('/').pop();
createFolderBtn(list, name, path);
});
lucide.createIcons();
}
function createFolderBtn(container, displayName, targetPath) {
const btn = document.createElement('button');
btn.className = "btn";
btn.style.textAlign = "left";
btn.style.padding = "10px";
btn.innerHTML = displayName;
btn.onclick = () => moveFile(targetPath);
container.appendChild(btn);
}
async function moveFile(destFolderPath) {
if (selectedItems.size > 0) {
const items = Array.from(selectedItems).map(s => JSON.parse(s));
for (const item of items) {
const finalDestination = destFolderPath ? `${destFolderPath}/${item.name}` : item.name;
await fetch('/api/rename', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
path: item.path,
old_name: item.name,
new_name: finalDestination
})
});
}
showToast(`Moved ${selectedItems.size} items to ${destFolderPath || 'Home'}`);
selectedItems.clear();
updateBatchToolbar();
closeSendToModal();
fetchFiles(currentPath);
return;
}
if (!selectedItem) return;
const fileName = selectedItem.name;
// This creates a path like "Folder/Subfolder/filename.ext"
// If destFolderPath is empty (Home), it becomes just "filename.ext"
const finalDestination = destFolderPath ? `${destFolderPath}/${fileName}` : fileName;
const res = await fetch('/api/rename', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
path: currentPath, // Where the file is now
old_name: fileName, // The file name
new_name: finalDestination // Where the file is going
})
});
if (res.ok) {
showToast(`Moved to ${destFolderPath || 'Home'}`);
closeSendToModal();
fetchFiles(currentPath);
} else {
showToast("Failed to move file. Check if a file with that name already exists in the destination.", "error");
}
}
function closeSendToModal() {
document.getElementById('sendToModal').style.display = 'none';
}
const FILE_TYPES = {
image: ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico'],
video: ['mp4', 'webm', 'mkv', 'avi', 'mov', 'flv', 'mpeg'],
audio: ['mp3', 'wav', 'ogg', 'aac', 'flac'],
pdf: ['pdf'],
html: ['html', 'htm'],
text: [
'txt', 'md', 'log', 'ini', 'cfg', 'csv', 'json', 'xml', 'yml', 'yaml',
],
code: [
'c', 'h', 'cpp', 'hpp', 'cc',
'py', 'js', 'ts', 'java', 'cs', 'go', 'rs',
'php', 'rb', 'sh', 'bat', 'ps1',
'swift', 'kt', 'dart', 'lua', 'r'
],
archive: ['zip', 'rar', '7z', 'tar', 'gz'],
others: ['xls', 'xlsx', 'xlsm', 'xlsb', 'xltx', 'docx'],
};
function getFileType(name) {
const ext = name.split('.').pop().toLowerCase();
for (const [type, list] of Object.entries(FILE_TYPES)) {
if (list.includes(ext)) return type;
}
return 'other';
}
function updateStats() {
const counts = {
total: 0,
image: 0,
video: 0,
audio: 0,
pdf: 0,
text: 0,
code: 0,
html: 0,
other: 0
};
filesList.forEach(f => {
if (f.is_dir) return;
counts.total++;
const t = getFileType(f.name);
counts[t] = (counts[t] || 0) + 1;
});
const stats = [];
// Total (always show)
stats.push(`
<button class="count-btn" title="Total Files">
<i data-lucide="file"></i>
<span>${counts.total}</span>
</button>
`);
if (counts.image > 0) stats.push(statBtn('Images', 'image', counts.image));
if (counts.video > 0) stats.push(statBtn('Videos', 'video', counts.video));
if (counts.audio > 0) stats.push(statBtn('Audios', 'music', counts.audio));
if (counts.pdf > 0) stats.push(statBtn('PDFs', 'file-text', counts.pdf));
const textCode = counts.text + counts.code;
if (textCode > 0) stats.push(statBtn('Text & Code', 'code', textCode));
if (counts.html > 0) stats.push(statBtn('HTML Files', 'file-code', counts.html));
if (counts.other > 0) stats.push(statBtn('Other Files', 'file-digit', counts.other));
document.getElementById('statsBar').innerHTML = stats.join('');
lucide.createIcons();
}
/* Helper */
function statBtn(title, icon, value) {
return `
<button class="count-btn" title="${title}">
<i data-lucide="${icon}"></i>
<span>${value}</span>
</button>
`;
}
async function fetchFiles(path = "", fromHistory = false) {
setLoader(true);
const isGlobal = document.getElementById('globalSearch').checked;
const q = document.getElementById('searchInput').value;
let url;
if (inRecycleBin) {
url = `/api/recycle_bin`;
} else if (isGlobal && q) {
url = `/api/search?q=${encodeURIComponent(q)}`;
} else {
url = `/api/list?path=${encodeURIComponent(path)}`;
}
if (adminKey) url += (url.includes('?') ? '&' : '?') + `show_hidden=${adminKey}`;
const res = await fetch(url);
const data = await res.json();
setLoader(false);
if (Array.isArray(data)) {
// Global search results
filesList = data;
currentPath = path; // Keep path same or handle as search view
} else {
currentPath = data.path;
filesList = data.items;
}
// Add to browser history for mobile back button support
if (!fromHistory && !inRecycleBin && !isGlobal) {
const state = { path: currentPath };
const url = currentPath ? `?path=${encodeURIComponent(currentPath)}` : '?';
history.pushState(state, '', url);
}
updateBreadcrumbs();
applySortAndRender();
updateStats();
}
function updateBreadcrumbs() {
const container = document.getElementById('breadcrumb');
container.innerHTML = `<span onclick="fetchFiles('')"><i data-lucide="home" style="width:14px; height:14px; vertical-align:text-bottom;"></i></span>`;
let accum = "";
currentPath.split('/').filter(p => p).forEach(part => {
accum += (accum ? '/' : '') + part;
const thisPath = accum;
container.innerHTML += ` <span style="opacity:0.3">/</span> <span onclick="fetchFiles('${thisPath}')">${part}</span>`;
});
}
// Lazy Load Globals
let renderedCount = 0;
const BATCH_SIZE = 50;
let currentFilteredList = [];
let observerTarget = null;
function renderFiles(reset = true) {
const container = document.getElementById('explorer');
const isGrid = container.classList.contains('grid');
// Store filtered list globally for pagination
currentFilteredList = filesList.filter(item => {
const fileType = item.is_dir ? 'dir' : getFileType(item.name);
return activeFilter === 'all' || fileType === activeFilter || item.is_dir;
});
if (currentFilteredList.length === 0) {
container.innerHTML = `
<div class="empty-state">
<i data-lucide="folder-open"></i>
<h3>No items found</h3>
<p>Try changing the filter or uploading a new file.</p>
</div>
`;
lucide.createIcons();
return;
}
if (reset) {
container.innerHTML = "";
renderedCount = 0;
// Create Sentinel for Infinite Scroll
observerTarget = document.createElement('div');
observerTarget.id = 'scroll-sentinel';
observerTarget.style.height = '10px';
container.appendChild(observerTarget);
// Setup Observer
const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
renderNextBatch();
}
}, { root: container, rootMargin: '200px' });
observer.observe(observerTarget);
}
// Initial Render
renderNextBatch();
}
function renderNextBatch() {
const container = document.getElementById('explorer');
const isGrid = container.classList.contains('grid');
const sentinel = document.getElementById('scroll-sentinel');
const nextBatch = currentFilteredList.slice(renderedCount, renderedCount + BATCH_SIZE);
if (nextBatch.length === 0) return;
nextBatch.forEach((item, index) => {
const globalIndex = renderedCount + index; // Track actual index
const card = document.createElement('div');
card.className = 'file-card';
card.dataset.name = item.name.toLowerCase();
const encodedUrl = getFullUrl(item.name);
const isImg = /\.(jpg|jpeg|png|gif|webp|svg)$/i.test(item.name);
const isSelected = selectedItems.has(JSON.stringify({ path: item.path || currentPath, name: item.name }));
card.innerHTML = `
<input type="checkbox" class="select-checkbox" ${isSelected ? 'checked' : ''} onclick="event.stopPropagation()">
${isGrid ? `
<div class="preview-area">${isImg ? `<img src="${encodedUrl}" loading="lazy">` : `<i data-lucide="${item.is_dir ? 'folder' : 'file-text'}"></i>`}</div>
<div class="file-info"><div class="file-name">${item.name}</div></div>
` : `
<div class="preview-area">${isImg ? `<img src="${encodedUrl}" loading="lazy">` : `<i data-lucide="${item.is_dir ? 'folder' : 'file-text'}"></i>`}</div>
<div class="file-info">
<div class="file-name">${item.name}</div>
<div class="list-meta">${item.is_dir ? 'Folder' : formatSize(item.size)}</div>
<div class="list-meta">${item.is_dir ? '--' : item.name.split('.').pop().toUpperCase()}</div>
</div>
`}
`;
if (isSelecting) card.classList.add('selecting');
if (isSelected) card.classList.add('selected');
card.oncontextmenu = (e) => { e.preventDefault(); showMenu(e, item); };
card.onclick = (e) => {
if (e.target.classList.contains('select-checkbox')) {
toggleItemSelection(item, card, globalIndex, e.shiftKey);
return;
}
if (isSelecting) {
toggleItemSelection(item, card, globalIndex, e.shiftKey);
return;
}
// Single click for preview/drill-down
if (item.is_dir) {
const targetPath = item.path !== undefined ? item.path : (currentPath ? `${currentPath}/${item.name}` : item.name);
fetchFiles(targetPath);
} else {
const itemRelativePath = item.path !== undefined ? (item.path ? `${item.path}/${item.name}` : item.name) : (currentPath ? `${currentPath}/${item.name}` : item.name);
const type = getFileType(item.name);
if (type === 'image' || type === 'video') {
openMediaFullscreen(getFullUrl(item.name), type);
} else {
openFilePreview({ ...item, relativePath: itemRelativePath });
}
}
};
card.title = item.name;
// Insert before sentinel
if (sentinel) {
container.insertBefore(card, sentinel);
} else {
container.appendChild(card);
}
});
renderedCount += nextBatch.length;
lucide.createIcons();
}
function copyToClipboard(text) {
// Attempt the modern way first
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(() => {
showToast("Copied!");
}).catch(err => {
fallbackCopyTextToClipboard(text);
});
} else {
// Use the old-school way if modern API is blocked
fallbackCopyTextToClipboard(text);
}
}
function fallbackCopyTextToClipboard(text) {
const textArea = document.createElement("textarea");
textArea.value = text;
// Ensure the textarea is off-screen
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
const successful = document.execCommand('copy');
if (successful) showToast("Copied (Fallback)!");
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function renderLinkDashboard(links, fullPath) {
const isEditMode = document.body.dataset.dashEdit === 'true';
let html = `
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:24px;">
<h2 style="margin:0; font-family:'Outfit';">Design Space</h2>
<div style="display:flex; gap:10px;">
<button class="btn ${isEditMode ? 'btn-primary' : ''}" onclick="toggleDashEdit('${fullPath}')"><i data-lucide="edit-2"></i> ${isEditMode ? 'Finish' : 'Edit Interface'}</button>
${isEditMode ? `<button class="btn" onclick="promptAddLink('${fullPath}')"><i data-lucide="plus"></i> Add Item</button>` : ''}
</div>
</div>
<div class="dash-grid">`;
links.forEach((link, index) => {
const icon = link.type === 'url' ? 'external-link' : 'file-text';
const displayDesc = link.description || (link.type === 'text' ? link.value : 'No description provided.');
html += `
<div class="dash-card">
${isEditMode ? `<span style="position:absolute; top:12px; right:12px; cursor:pointer; color:#fb7185;" onclick="deleteLink('${fullPath}', ${index})"><i data-lucide="x-circle"></i></span>` : ''}
<i data-lucide="${icon}"></i>
<div>
<h3>${link.title}</h3>
<p style="word-break:break-all;">${displayDesc}</p>
</div>
<div style="display:flex; gap:8px; margin-top:auto;">
${link.type === 'url' ?
`<a href="${link.value}" target="_blank" class="btn btn-primary" style="flex:1; justify-content:center; text-decoration:none;">Open Resource</a>` :
`<div style="display:flex; gap:4px; flex:1;">
<button onclick="copyToClipboard('${link.value}')" class="btn btn-primary" style="flex:1; justify-content:center;">Copy Value</button>
${link.value.includes('\n') ? `<button class="btn" onclick="viewValue('${link.value.replace(/'/g, "\\'")}')" title="Expand"><i data-lucide="maximize-2" style="width:14px;"></i></button>` : ''}
</div>`
}
</div>
</div>`;
});
html += `</div>`;
setTimeout(() => lucide.createIcons(), 10);
return html;
}
function toggleDashEdit(path) {
document.body.dataset.dashEdit = document.body.dataset.dashEdit === 'true' ? 'false' : 'true';
openFilePreview({ name: path.split('/').pop(), relativePath: path });
}
async function promptAddLink(filename) {
const title = prompt("Enter Title (e.g., My Portfolio):");
if (!title) return;
const description = prompt("Enter short description:");
const value = prompt("Enter URL or Text data:");
if (!value) return;
const type = value.startsWith('http') ? 'url' : 'text';
const res = await fetch(getFullUrl(filename));
const data = await res.json();
data.links.push({ title, description, value, type });
saveJsonToServer(filename, data);
}
async function deleteLink(filename, index) {
if (!confirm("Delete this link?")) return;
const res = await fetch(getFullUrl(filename));
const data = await res.json();
data.links.splice(index, 1);
saveJsonToServer(filename, data);
}
async function saveJsonToServer(fullPath, content) {
const res = await fetch('/api/save_json', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
filename: fullPath,
content: content
})
});
if (res.ok) {
showToast("Updated successfully");
// Don't reopen preview if we are in search mode or similar
if (!document.getElementById('saveFileBtn').style.display) {
openFilePreview({ name: fullPath.split('/').pop(), relativePath: fullPath });
}
} else {
showToast("Error saving file. Check server logs.", "error");
}
}
async function saveEditorContent() {
if (!currentEditorFile) return;
const content = monacoEditor ? monacoEditor.getValue() : document.getElementById('editorArea').value;
const res = await fetch('/api/save_json', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
filename: currentEditorFile.relativePath,
content: content,
raw: true // Backend should handle raw text if needed, or we just use save_json for now
})
});
// Wait, handle_save_json expects content to be JSON object.
// I should either update server.py or send as a string in a JSON object.
// Let's send it as a string in a JSON object for simplicity.
if (res.ok) {
showToast("File saved!");
document.getElementById('saveFileBtn').style.display = 'none';
toggleDetails(false);
} else {
showToast("Error saving file.", "error");
}
}
async function openFilePreview(item) {
const panel = document.getElementById('detailsPanel');
const content = document.getElementById('detailContent');
const path = item.relativePath || item.name;
const url = `/${path}`;
const type = getFileType(item.name);
setLoader(true);
let preview = '';
if (item.name.endsWith('.json')) {
try {
const res = await fetch(url);
const data = await res.json();
if (data && Array.isArray(data.links)) {
content.innerHTML = renderLinkDashboard(data.links, path);
panel.classList.add('active');
setLoader(false);
return;
}
} catch (e) { }
}
if (type === 'image') {
preview = `
<div style="display:flex; flex-direction:column; gap:16px;">
<div style="display:flex; justify-content:center; position:relative;">
<img src="${url}" style="max-width:100%; max-height:70vh; border-radius:12px; box-shadow:var(--shadow-lg);">
<button class="btn" style="position:absolute; bottom:12px; right:12px;" onclick="openMediaFullscreen('${url}', 'image')"><i data-lucide="maximize"></i> Fullscreen</button>
</div>
</div>`;
}
else if (type === 'video') {
preview = `
<div style="display:flex; flex-direction:column; gap:16px;">
<div style="position:relative;">
<video src="${url}" controls style="width:100%; border-radius:12px;"></video>
<button class="btn" style="position:absolute; top:12px; right:12px;" onclick="openMediaFullscreen('${url}', 'video')"><i data-lucide="maximize"></i> Fullscreen</button>
</div>
</div>`;
}
else if (type === 'audio') preview = `<audio src="${url}" controls style="width:100%"></audio>`;
else if (type === 'pdf') preview = `<iframe src="${url}" style="width:100%; height:80vh; border:none; border-radius:12px;"></iframe>`;
else if (item.name.endsWith('.md')) {
const text = await fetch(url).then(r => r.text());
preview = `<div class="split-preview">
<div id="monaco-container" class="editor-container" style="position:relative;">
<button class="btn" style="position:absolute; top:8px; right:8px; z-index:10; padding:4px;" onclick="toggleFullscreen()" title="Fullscreen"><i data-lucide="maximize"></i></button>
</div>
<div class="markdown-body" id="md-render" style="background:var(--bg-card); padding:24px; border-radius:12px; overflow-y:auto;">${marked.parse(text)}</div>
</div>`;
setTimeout(() => createMonaco(text, 'markdown', true), 100);
}
else if (type === 'text' || type === 'code' || type === 'html' || item.name.endsWith('.json')) {
const text = await fetch(url).then(r => r.text());
currentEditorFile = item;
document.getElementById('saveFileBtn').style.display = 'block';
preview = `
<div id="monaco-container" class="editor-container" style="position:relative;">
<button class="btn" style="position:absolute; top:8px; right:8px; z-index:10; padding:4px;" onclick="toggleFullscreen()" title="Fullscreen"><i data-lucide="maximize"></i></button>
</div>
<div style="display:flex; gap:12px; margin-top:16px;">
<button class="btn btn-primary" onclick="saveEditorContent()"><i data-lucide="save"></i> Save Changes</button>
<button class="btn" onclick="copyToClipboard(monacoEditor.getValue())"><i data-lucide="copy"></i> Copy Source</button>
<button class="btn" onclick="openRaw(currentEditorFile)"><i data-lucide="external-link"></i> New Tab</button>
${type === 'html' ? `<button class="btn" onclick="openPage('${path}')"><i data-lucide="external-link"></i> Live Preview</button>` : ''}
</div>`;
const lang = item.name.endsWith('.c') ? 'c' : (item.name.endsWith('.java') ? 'java' : (item.name.endsWith('.py') ? 'python' : type));
setTimeout(() => createMonaco(text, lang), 100);
}
else {
preview = `<div style="text-align:center; padding:40px;">
<i data-lucide="file" style="width:64px; height:64px; opacity:0.2;"></i>
<h3>No preview available</h3>
<a href="${url}" download class="btn btn-primary" style="margin-top:16px;">Download to View</a>
</div>`;
}
content.innerHTML = `
<div style="margin-bottom:24px;">${preview}</div>
<div class="glass-info" style="padding:20px; border-radius:16px; background:var(--glass);">
<div style="display:flex; justify-content:space-between; align-items:flex-start;">
<div>
<h2 style="margin:0; font-family:'Outfit';">${item.name}</h2>
<p style="color:var(--text-muted); margin:4px 0;">${item.is_dir ? 'Directory' : formatSize(item.size)} • /Home/${path}</p>
</div>
</div>
</div>
<!-- Comments Section -->
<div class="comments-section" style="margin-top:20px; border-top:1px solid var(--border); padding-top:15px;">
<h4 style="margin:0 0 10px 0;">Comments</h4>
<div id="commentsList" style="max-height:200px; overflow-y:auto; margin-bottom:10px;"></div>
<div style="display:flex; gap:8px;">
<input type="text" id="commentInput" class="search-box" placeholder="Add a comment..." onkeydown="if(event.key==='Enter') postComment('${path}')">
<button class="btn" onclick="postComment('${path}')"><i data-lucide="send"></i></button>
</div>
</div>
</div>
`;
panel.classList.add('active');
lucide.createIcons();
setLoader(false);
loadComments(path);
}
function createMonaco(content, lang, isMarkdown = false) {
if (monacoEditor) { monacoEditor.dispose(); }
require(['vs/editor/editor.main'], function () {
monacoEditor = monaco.editor.create(document.getElementById('monaco-container'), {
value: content,
language: lang === 'code' ? 'javascript' : lang,
theme: document.body.classList.contains('light-mode') ? 'vs' : 'vs-dark',
automaticLayout: true,
fontSize: 14,
fontFamily: 'Fira Code, monospace',
minimap: { enabled: false },
roundedSelection: true,
scrollBeyondLastLine: false,
padding: { top: 16 }
});
if (isMarkdown) {
monacoEditor.onDidChangeModelContent(() => {
document.getElementById('md-render').innerHTML = marked.parse(monacoEditor.getValue());
});
}
});
}
async function deleteBatch() {
if (selectedItems.size === 0) return;
if (!confirm(`Delete ${selectedItems.size} items?`)) return;
const items = Array.from(selectedItems).map(s => JSON.parse(s));
const res = await fetch('/api/batch_delete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items })
});
if (res.ok) {
showToast(`Deleted ${selectedItems.size} items`);
selectedItems.clear();
updateBatchToolbar();
fetchFiles(currentPath);
}
}
async function downloadBatch() {
if (selectedItems.size === 0) return;
const items = Array.from(selectedItems).map(s => JSON.parse(s));
const res = await fetch('/api/zip', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items, filename: `selected_items_${Date.now()}.zip` })
});
if (res.ok) {
const blob = await res.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `selected_items_${Date.now()}.zip`;
document.body.appendChild(a);
a.click();
a.remove();
} else {
showToast("Failed to create ZIP.", "error");
}
}
function openBatchMove() {
if (selectedItems.size === 0) return;
openSendToModal();
}
function escapeHtml(str) {
return str.replace(/[&<>"']/g, m => ({
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}[m]));
}
function showMenu(e, item) {
selectedItem = item;
const menu = document.getElementById('contextMenu');
// Hide all items first
menu.querySelectorAll('.dropdown-item').forEach(el => el.style.display = 'none');
if (inRecycleBin) {
menu.querySelector('[onclick="handleMenuAction(\'restore\')"]').style.display = 'flex';
menu.querySelector('[onclick="handleMenuAction(\'purge\')"]').style.display = 'flex';
menu.querySelector('[onclick="handleMenuAction(\'details\')"]').style.display = 'flex';
} else {
menu.querySelector('[onclick="handleMenuAction(\'details\')"]').style.display = 'flex';
menu.querySelector('[onclick="handleMenuAction(\'rename\')"]').style.display = 'flex';
menu.querySelector('[onclick="openSendToModal()"]').style.display = 'flex';
menu.querySelector('[onclick="handleMenuAction(\'delete\')"]').style.display = 'flex';
if (!item.is_dir) {
menu.querySelector('[onclick="handleMenuAction(\'download\')"]').style.display = 'flex';
menu.querySelector('[onclick="handleMenuAction(\'share\')"]').style.display = 'flex';
}
}
menu.style.display = 'block';
const x = Math.min(e.clientX, window.innerWidth - 190);
const y = Math.min(e.clientY, window.innerHeight - (inRecycleBin ? 120 : 200));
menu.style.left = `${x}px`;
menu.style.top = `${y}px`;
}
function toggleDetails(show, item = null) {
const panel = document.getElementById('detailsPanel');
const content = document.getElementById('detailContent');
if (!panel || !content) {
console.error('Details panel elements not found');
return;
}
if (!show) {
panel.classList.remove('active');
return;
}
if (!item) {
console.error('No item provided to toggleDetails');
return;
}
const encodedUrl = getFullUrl(item.name);
const isImg = !item.is_dir && /\.(jpg|jpeg|png|gif|webp|svg)$/i.test(item.name);
content.innerHTML = `
<div style="width:100%; height:200px; background:#000; display:flex; align-items:center; justify-content:center; border-radius:12px; margin-bottom:1.5rem; overflow:hidden;">
${isImg ? `<img src="${encodedUrl}" style="max-width:100%; max-height:100%; object-fit:contain;">` : `<i data-lucide="${item.is_dir ? 'folder' : 'file-text'}" style="width:80px; height:80px;"></i>`}
</div>
<div style="display:grid; gap:15px;">
<div><small style="color:var(--text-muted)">NAME</small><p style="word-break:break-all;">${item.name}</p></div>
<div><small style="color:var(--text-muted)">SIZE</small><p>${item.is_dir ? 'Folder' : formatSize(item.size)}</p></div>
<div><small style="color:var(--text-muted)">LOCATION</small><p style="color:var(--primary)">/Home/${currentPath}</p></div>
</div>
`;
panel.classList.add('active');
lucide.createIcons();
}
function uploadFiles(files) {
const drawer = document.getElementById('uploadDrawer');
const list = document.getElementById('uploadList');
drawer.style.display = 'flex';
// Clear previous finished uploads if drawer was closed
// list.innerHTML = "";
Array.from(files).forEach(file => {
const item = document.createElement('div');
item.className = 'upload-item';
const fileId = 'up-' + Math.random().toString(36).substr(2, 9);
item.id = fileId;
item.innerHTML = `
<div class="upload-item-header">
<span style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap; max-width:180px;">${file.name}</span>
<span class="up-percent">0%</span>
</div>
<div class="upload-item-bar"><div class="upload-item-progress"></div></div>
`;
list.prepend(item);
const xhr = new XMLHttpRequest();
const fd = new FormData();
fd.append('file', file);
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) {
const p = Math.round((e.loaded / e.total) * 100);
item.querySelector('.up-percent').textContent = `${p}%`;
item.querySelector('.upload-item-progress').style.width = `${p}%`;
}
};
xhr.onload = () => {
item.style.borderColor = 'var(--primary)';
setTimeout(() => {
// Keep it for a bit then maybe fade out or stay in list
item.querySelector('.up-percent').innerHTML = '<i data-lucide="check" style="width:12px; height:12px; color:#10b981"></i>';
lucide.createIcons();
}, 1000);
fetchFiles(currentPath);
};
xhr.open('POST', `/api/upload?path=${encodeURIComponent(currentPath)}`);
xhr.send(fd);
});
}
function closeUploadDrawer() {
document.getElementById('uploadDrawer').style.display = 'none';
document.getElementById('uploadList').innerHTML = "";
}
// --- Standard Logic & Listeners ---
function handleMenuAction(action) {
const encodedUrl = getFullUrl(selectedItem.name);
if (action === 'details') {
toggleDetails(true, selectedItem);
}
if (action === 'download') { const a = document.createElement('a'); a.href = encodedUrl; a.download = selectedItem.name; a.click(); }
if (action === 'rename') renameItem(selectedItem.name);
if (action === 'delete') deleteItem(selectedItem.name);
if (action === 'restore') restoreItem(selectedItem.name);