summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-filemanager/htdocs/luci-static/resources/view/system/filemanager.js
blob: d49a759847ef84690d9f74f9f42b026e76b409e4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
'use strict';
'require view';
'require fs';
'require ui';
'require dom';
'require rpc';
'require view.system.filemanager.md as md';
'require view.system.filemanager.md_help as md_help';
'require view.system.filemanager.HexEditor as HE';

const callFileList = rpc.declare({
	object: 'file',
	method: 'list',
	params: [ 'path' ]
});

const fileTypes = {
	'block' : _('Block device'),
	'char' : _('Character device'),
	'directory' : _('Directory'),
	'fifo' : _('FIFO/Pipe'),
	'file' : _('File'),
	'socket' : _('Socket'),
	'symlink' : _('Symlink'),
}

function pop(a, message, severity) {
	ui.addNotification(a, message, severity)
}

function popTimeout(a, message, timeout, severity) {
	ui.addTimeLimitedNotification(a, message, timeout, severity)
}

// Initialize global variables
let currentPath = '/'; // Current path in the filesystem
const selectedItems = new Set(); // Set of selected files/directories
let sortField = 'name'; // Field to sort files by
let sortAscending = true; // Sort direction (ascending/descending)
let configFilePath = '/etc/config/filemanager'; // Path to the configuration file

// Initialize drag counter
let dragCounter = 0;

// Configuration object to store interface settings
let config = {
	// Column widths in the file table
	columnWidths: {
		'name': 150,
		'type': 100,
		'size': 100,
		'mtime': 150,
		'permissions': 70,
		'actions': 100,
	},

	// Minimum column widths
	columnMinWidths: {
		'name': 100,
		'type': 80,
		'size': 80,
		'mtime': 120,
		'permissions': 70,
		'actions': 80,
	},

	// Maximum column widths
	columnMaxWidths: {
		'name': 300,
		'type': 200,
		'size': 200,
		'mtime': 300,
		'permissions': 70,
		'actions': 200,
	},

	// Padding and window sizes
	padding: 10,
	paddingMin: 5,
	paddingMax: 20,
	currentDirectory: '/', // Current directory

	windowHeight: 800,
	windowWidth: 400,

	texteditorHeight: 550,
	texteditorWidth: 850,
	hexeditorHeight: 550,
	hexeditorWidth: 850,

	// otherSettings: {} // Additional settings
};

// Function to upload a file to the server
function uploadFile(filename, filedata, onProgress) {
	return new Promise(function(resolve, reject) {
		let formData = new FormData();
		formData.append('sessionid', rpc.getSessionID()); // Add session ID
		formData.append('filename', filename); // File name including path
		formData.append('filedata', filedata); // File data

		let xhr = new XMLHttpRequest();
		xhr.open('POST', L.env.cgi_base + '/cgi-upload', true); // Configure the request

		// Monitor upload progress
		xhr.upload.onprogress = function(event) {
			if (event.lengthComputable && onProgress) {
				let percent = (event.loaded / event.total) * 100;
				onProgress(percent); // Call the progress callback with percentage
			}
		};

		// Handle request completion
		xhr.onload = () => {
			if (xhr.status === 200) {
				resolve(xhr.responseText); // Upload successful
			} else {
				reject(new Error(xhr.statusText)); // Upload error
			}
		};

		// Handle network errors
		xhr.onerror = () => {
			reject(new Error('Network error'));
		};

		xhr.send(formData); // Send the request
	});
}


// Function to load settings from the configuration file

function parseKeyValuePairs(input, delimiter, callback) {
	const pairs = input.split(',');
	pairs.forEach((pair) => {
		const [key, value] = pair.split(delimiter);
		if (key && value) callback(key.trim(), value.trim());
	});
}

async function loadConfig() {
	try {
		const content = await fs.read(configFilePath);
		const lines = content.trim().split('\n');

		lines.forEach((line) => {
			if (!line.includes('option')) return;

			const splitLines = line.split('option').filter(Boolean);

			splitLines.forEach((subline) => {
				const formattedLine = "option " + subline.trim();
				const match = formattedLine.match(/^option\s+(\S+)\s+'([^']+)'$/);

				if (!match) return;

				const [, key, value] = match;

				switch (key) {
					case 'columnWidths':
					case 'columnMinWidths':
					case 'columnMaxWidths':
						parseKeyValuePairs(value, ':', (k, v) => {
							config[key] = config[key] || {};
							config[key][k] = parseInt(v, 10);
						});
						break;
					default:
						config[key] = isNaN(value) ? value : parseInt(value, 10);
				}
			});
		});
	} catch (err) {
		console.error('Failed to load config: ' + err.message);
	}
}

// Function to save settings to the configuration file
function saveConfig() {

	let configLines = ['config filemanager',
		'\toption columnWidths \'' + Object.keys(config.columnWidths).map((field) => {
			return field + ':' + config.columnWidths[field];
		}).join(',') + '\'',
		'\toption columnMinWidths \'' + Object.keys(config.columnMinWidths).map((field) => {
			return field + ':' + config.columnMinWidths[field];
		}).join(',') + '\'',
		'\toption columnMaxWidths \'' + Object.keys(config.columnMaxWidths).map((field) => {
			return field + ':' + config.columnMaxWidths[field];
		}).join(',') + '\'',
		'\toption padding \'' + config.padding + '\'',
		'\toption paddingMin \'' + config.paddingMin + '\'',
		'\toption paddingMax \'' + config.paddingMax + '\'',
		'\toption currentDirectory \'' + config.currentDirectory + '\'',
		'\toption windowHeight \'' + config.windowHeight + '\'',
		'\toption windowWidth \'' + config.windowWidth + '\'',
		'\toption texteditorWidth \'' + config.texteditorWidth + '\'',
		'\toption texteditorHeight \'' + config.texteditorHeight + '\'',
		'\toption hexeditorWidth \'' + config.hexeditorWidth + '\'',
		'\toption hexeditorHeight \'' + config.hexeditorHeight + '\'',
	];

	const configContent = configLines.join('\n') + '\n';

	// Write settings to file
	return fs.write(configFilePath, configContent).then(() => {
		return Promise.resolve();
	}).catch((err) => {
		return Promise.reject(new Error('Failed to save configuration: ' + err.message));
	});
}

// Function to correctly join paths
function joinPath(path, name) {
	return path.endsWith('/') ? path + name : path + '/' + name;
}

function modeToRwx(mode) {
	const perms = mode & 0o777; // extract permission bits

	const toRwx = n => 
		((n & 4) ? 'r' : '-') +
		((n & 2) ? 'w' : '-') +
		((n & 1) ? 'x' : '-');

	const owner = toRwx((perms >> 6) & 0b111);
	const group = toRwx((perms >> 3) & 0b111);
	const world = toRwx(perms & 0b111);

	return `${owner}${group}${world}`;
}


function modeToOctal(mode) {
	const perms = mode & 0o777;
	return perms.toString(8);
}

// Function to get a list of files in a directory
function getFileList(path) {
	return callFileList(path).then((res) => {
		const files = [];
		res?.entries?.forEach((file) => {
			files.push({
				...file,
				permissions: modeToRwx(file.mode),
				numericPermissions: modeToOctal(file.mode),
			});
		});

		return files;
	});
}

// Function to insert CSS styles into the document
function insertCss(cssContent) {
	const styleElement = document.createElement('style');
	styleElement.type = 'text/css';
	styleElement.appendChild(document.createTextNode(cssContent));
	document.head.appendChild(styleElement);
}

// CSS styles for the file manager interface
const cssContent = `
.cbi-button-apply, .cbi-button-reset, .cbi-button-save:not(.custom-save-button) {
	display: none !important;
}
.cbi-page-actions {
	background: none !important;
	border: none !important;
	padding: ${config.padding}px 0 !important;
	margin: 0 !important;
	display: flex;
	justify-content: flex-start;
	margin-top: 10px;
}
.cbi-tabmenu {
	background: none !important;
	border: none !important;
	margin: 0 !important;
	padding: 0 !important;
}
.cbi-tabmenu li {
	display: inline-block;
	margin-right: 10px;
}
#file-list-container {
	margin-top: 30px !important;
	overflow: auto;
	border: 1px solid #ccc;
	padding: 0;
	min-width: 600px;
	position: relative;
	resize: both;
}
#file-list-container.drag-over {
	border: 2px dashed #00BFFF;
	background-color: rgba(0, 191, 255, 0.1);
}
/* Add extra space to the left of the Name and Type columns */
.table th:nth-child(1), .table td:nth-child(1),  /* Name column */
.table th:nth-child(2), .table td:nth-child(2) { /* Type column */
	padding-left: 5px; /* Adjust this value for the desired spacing */
}
/* Add extra space to the right of the Size column */
.table th:nth-child(3), .table td:nth-child(3) { /* Size column */
	padding-right: 5px; /* Adjust this value for the desired spacing */
}
/* Add extra space to the left of the Size column header */
.table th:nth-child(3) { /* Size column header */
	padding-left: 15px; /* Adjust this value for the desired spacing */
}

#drag-overlay {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 191, 255, 0.2);
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 24px;
	color: #00BFFF;
	z-index: 10;
	pointer-events: none;
}
#content-editor {
	margin-top: 30px !important;
}
.editor-container {
	display: flex;
	flex-direction: column;
	resize: both;
	overflow: hidden;
}
.editor-content {
	flex: 1;
	display: flex;
	overflow: hidden;
}
.line-numbers {
	width: 50px;
	background-color: #f0f0f0;
	text-align: right;
	padding-right: 5px;
	user-select: none;
	border-right: 1px solid #ccc;
	overflow: hidden;
	flex-shrink: 0;
	-ms-overflow-style: none; /* Hide scrollbar in IE и Edge */
	scrollbar-width: none; /* Hide scrollbar in Firefox */
}
.line-numbers::-webkit-scrollbar {
	display: none; /* Hide scrollbar in Chrome, Safari и Opera */
}
.line-numbers div {
	font-family: monospace;
	font-size: 14px;
	line-height: 1.2em;
	height: 1.2em;
}
#editor-message {
	font-size: 18px;
	font-weight: bold;
}
#editor-textarea {
	flex: 1;
	resize: none;
	border: none;
	font-family: monospace;
	font-size: 14px;
	line-height: 1.2em;
	padding: 0;
	margin: 0;
	overflow: auto;
	box-sizing: border-box;
}
#editor-textarea, .line-numbers {
	overflow-y: scroll;
}
th {
	text-align: left !important;
	position: sticky;
	top: 0;
	border-right: 1px solid #ddd;
	box-sizing: border-box;
	padding-right: 30px;
	white-space: nowrap;
	min-width: 100px;
	background-color: #fff;
	z-index: 2;
}
td {
	text-align: left !important;
	border-right: 1px solid #ddd;
	box-sizing: border-box;
	white-space: nowrap;
	min-width: 100px;
	overflow: hidden;
	text-overflow: ellipsis;
}
tr:hover {
	background-color: #f0f0f0 !important;
}
.download-button {
	color: green;
	cursor: pointer;
	margin-left: 5px;
}
.delete-button {
	color: red;
	cursor: pointer;
	margin-left: 5px;
}
.edit-button {
	color: blue;
	cursor: pointer;
	margin-left: 5px;
}
.duplicate-button {
	color: orange;
	cursor: pointer;
	margin-left: 5px;
}
.symlink {
	color: green;
}
.status-link {
	color: blue;
	text-decoration: underline;
	cursor: pointer;
}
.action-button {
	margin-right: 10px;
	cursor: pointer;
}
.size-cell {
	font-family: monospace;
	box-sizing: border-box;
	white-space: nowrap;
	align-items: center;
}
.size-number {
	display: inline-block;
	width: 8ch;
	text-align: right;
}
.size-unit {
	display: inline-block;
	width: 4ch;
	text-align: right;
	margin-left: 0.5ch;
}
.table {
	table-layout: fixed;
	border-collapse: collapse;
	white-space: nowrap;
	width: 100%;
}
.table th:nth-child(3), .table td:nth-child(3) {
	width: 100px;
	min-width: 100px;
	max-width: 500px;
}
.table th:nth-child(3) + th, .table td:nth-child(3) + td {
	padding-left: 10px;
}
.resizer {
	position: absolute;
	right: 0;
	top: 0;
	width: 5px;
	height: 100%;
	cursor: col-resize;
	user-select: none;
	z-index: 3;
}
.resizer::after {
	content: "";
	position: absolute;
	right: 2px;
	top: 0;
	width: 1px;
	height: 100%;
	background: #aaa;
}
#file-list-container.resizeable {
	resize: both;
	overflow: auto;
}
.sort-button {
	position: absolute;
	right: 10px;
	top: 50%;
	transform: translateY(-50%);
	background: none;
	border: 1px solid #ccc; /* Add a visible border */
	color: #fff; /* White text color for better contrast on dark backgrounds */
	cursor: pointer;
	padding: 2px 5px; /* Add padding for better clickability */
	font-size: 12px; /* Set font size */
	border-radius: 4px; /* Rounded corners for a better appearance */
	background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent black background */
	transition: background-color 0.3s, color 0.3s; /* Smooth transition effects for hover */
}

.sort-button:hover {
	background-color: #fff; /* Change background to white on hover */
	color: #000; /* Change text color to black on hover */
	border-color: #fff; /* White border on hover */
}
.sort-button:focus {
	outline: none;
}
#status-bar {
	margin-top: 10px;
	padding: 10px;
	background-color: #f9f9f9;
	border: 1px solid #ccc;
	min-height: 40px;
	display: flex;
	align-items: center;
	justify-content: space-between;
}
#status-info {
	font-weight: bold;
	display: flex;
	align-items: center;
}
#status-progress {
	width: 50%;
}
.cbi-progressbar {
	width: 100%;
	background-color: #e0e0e0;
	border-radius: 5px;
	overflow: hidden;
	height: 10px;
}
.cbi-progressbar div {
	height: 100%;
	background-color: #76c7c0;
	width: 0%;
	transition: width 0.2s;
}
.file-manager-header {
	display: flex;
	align-items: center;
}
.file-manager-header h2 {
	margin: 0;
}
.file-manager-header input {
	margin-left: 10px;
	width: 100%;
	max-width: 700px;
	font-size: 18px;
}
.file-manager-header button {
	margin-left: 10px;
	font-size: 18px;
}
.directory-link {
	/* Choose a color with good contrast or let the theme decide */
	color: #00BFFF; /* DeepSkyBlue */
	font-weight: bold;
}

.file-link {
	color: inherit; /* Use the default text color */
}
`;


// Main exported view module
return view.extend({
	editorMode: 'text',
	hexEditorInstance: null,
	// Method called when the view is loaded
	load() {
		const self = this;
		return loadConfig().then(() => {
			currentPath = config.currentDirectory || '/';
			return getFileList(currentPath); // Load the file list for the current directory
		});
	},

	// Method to render the interface
	render(data) {
		const self = this;
		insertCss(cssContent); // Insert CSS styles
		const viewContainer = E('div', {
			'id': 'file-manager-container'
		}, [
			// File Manager Header
			E('div', {
				'class': 'file-manager-header'
			}, [
				E('h2', {}, _('File Manager: ')),
				E('input', {
					'type': 'text',
					'id': 'path-input',
					'value': currentPath,
					'style': 'margin-left: 10px;',
					'keydown'(event) {
						if (event.key === 'Enter') {
							self.handleGoButtonClick(); // Trigger directory navigation on Enter
						}
					}
				}),
				E('button', {
					'id': 'go-button',
					'click': this.handleGoButtonClick.bind(this),
					'style': 'margin-left: 10px;'
				}, _('Go'))
			]),

			// Tab Panels
			E('div', {
				'class': 'cbi-tabcontainer',
				'id': 'tab-group'
			}, [
				E('ul', {
					'class': 'cbi-tabmenu'
				}, [
					E('li', {
						'class': 'cbi-tab cbi-tab-active',
						'id': 'tab-filemanager'
					}, [
						E('a', {
							'href': '#',
							'click': this.switchToTab.bind(this, 'filemanager')
						}, _('File Manager'))
					]),
					E('li', {
						'class': 'cbi-tab',
						'id': 'tab-editor'
					}, [
						E('a', {
							'href': '#',
							'click': this.switchToTab.bind(this, 'editor')
						}, _('Editor'))
					]),
					E('li', {
						'class': 'cbi-tab',
						'id': 'tab-settings'
					}, [
						E('a', {
							'href': '#',
							'click': this.switchToTab.bind(this, 'settings')
						}, _('Settings'))
					]),
					// Help Tab
					E('li', {
						'class': 'cbi-tab',
						'id': 'tab-help'
					}, [
						E('a', {
							'href': '#',
							'click': this.switchToTab.bind(this, 'help')
						}, _('Help'))
					])
				])
			]),

			// Tab Contents
			E('div', {
				'class': 'cbi-tabcontainer-content'
			}, [
				// File Manager Content
				E('div', {
					'id': 'content-filemanager',
					'class': 'cbi-tab',
					'style': 'display:block;'
				}, [
					// File List Container with Drag-and-Drop
					(() => {
						// Create the container for the file list and drag-and-drop functionality
						const fileListContainer = E('div', {
							'id': 'file-list-container',
							'class': 'resizeable',
							'style': 'width: ' + config.windowWidth + 'px; height: ' + config.windowHeight + 'px;'
						}, [
							E('table', {
								'class': 'table',
								'id': 'file-table'
							}, [
								E('thead', {}, [
									E('tr', {}, [
										E('th', {
											'data-field': 'name'
										}, [
											_('Name'),
											E('button', {
												'class': 'sort-button',
												'data-field': 'name',
												'title': _('Sort by Name')
											}, '↕'),
											E('div', {
												'class': 'resizer'
											})
										]),
										E('th', {
											'data-field': 'permissions'
										}, [
											_('Permissions'),
											E('button', {
												'class': 'sort-button',
												'data-field': 'permissions',
												'title': _('Sort by Permissions')
											}, '↕'),
											E('div', {
												'class': 'resizer'
											})
										]),
										E('th', {
											'data-field': 'type'
										}, [
											_('Type'),
											E('button', {
												'class': 'sort-button',
												'data-field': 'type',
												'title': _('Sort by Type')
											}, '↕'),
											E('div', {
												'class': 'resizer'
											})
										]),
										E('th', {
											'data-field': 'size'
										}, [
											_('Size'),
											E('button', {
												'class': 'sort-button',
												'data-field': 'size',
												'title': _('Sort by Size')
											}, '↕'),
											E('div', {
												'class': 'resizer'
											})
										]),
										E('th', {
											'data-field': 'mtime'
										}, [
											_('Last Modified'),
											E('button', {
												'class': 'sort-button',
												'data-field': 'mtime',
												'title': _('Sort by Last Modified')
											}, '↕'),
											E('div', {
												'class': 'resizer'
											})
										]),
										E('th', {'data-field': 'actions'}, [
											E('input', {
												'type': 'checkbox',
												'id': 'select-all-checkbox',
												'style': 'margin-right: 5px;',
												'change': this.handleSelectAllChange.bind(this),
												'click': this.handleSelectAllClick.bind(this)
											}),
											_('Actions')
										])
									])
								]),
								E('tbody', {
									'id': 'file-list'
								})
							]),
							E('div', {
								'id': 'drag-overlay',
								'style': 'display:none;'
							}, _('Drop files here to upload'))
						]);

						// Attach drag-and-drop event listeners
						fileListContainer.addEventListener('dragenter', this.handleDragEnter.bind(this));
						fileListContainer.addEventListener('dragover', this.handleDragOver.bind(this));
						fileListContainer.addEventListener('dragleave', this.handleDragLeave.bind(this));
						fileListContainer.addEventListener('drop', this.handleDrop.bind(this));

						return fileListContainer;
					}).call(this), // Ensure 'this' context is preserved

					// Status Bar
					E('div', {
						'id': 'status-bar'
					}, [
						E('div', {
							'id': 'status-info'
						}, _('No file selected.')),
						E('div', {
							'id': 'status-progress'
						})
					]),

					// Page Actions
					E('div', {
						'class': 'cbi-page-actions'
					}, [
						E('button', {
							'class': 'btn action-button',
							'click': this.handleUploadClick.bind(this)
						}, _('Upload File')),
						E('button', {
							'class': 'btn action-button',
							'click': this.handleMakeDirectoryClick.bind(this)
						}, _('Create Folder')),
						E('button', {
							'class': 'btn action-button',
							'click': this.handleCreateFileClick.bind(this)
						}, _('Create File')),
						E('button', {
							'id': 'delete-selected-button',
							'class': 'btn action-button',
							'style': 'display: none;',
							'click': this.handleDeleteSelected.bind(this)
						}, _('Delete Selected'))
					])
				]),

				// Editor Content
				E('div', {
					'id': 'content-editor',
					'class': 'cbi-tab',
					'style': 'display:none;'
				}, [
					E('p', {
						'id': 'editor-message'
					}, _('Select a file from the list to edit it here.')),
					E('div', {
						'id': 'editor-container'
					})
				]),
				// Help Content
				E('div', {
					'id': 'content-help',
					'class': 'cbi-tab',
					'style': 'display:none; padding: 10px; overflow:auto; width: 650px; height: 600px; resize: both; border: 1px solid #ccc; box-sizing: border-box;'
				}, [
					// The content will be dynamically inserted by renderHelp()
				]),

				// Settings Content
				E('div', {
					'id': 'content-settings',
					'class': 'cbi-tab',
					'style': 'display:none;'
				}, [
					E('div', {
						'style': 'margin-top: 20px;'
					}, [
						E('h3', {}, _('Interface Settings')),
						E('div', {
							'id': 'settings-container'
						}, [
							E('form', {
								'id': 'settings-form'
							}, [
								E('div', {}, [
									E('label', {}, _('Window Width:')),
									E('input', {
										'type': 'number',
										'id': 'windowWidth-input',
										'value': config.windowWidth,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Window Height:')),
									E('input', {
										'type': 'number',
										'id': 'windowHeight-input',
										'value': config.windowHeight,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Text Editor Width:')),
									E('input', {
										'type': 'number',
										'id': 'texteditorWidth-input',
										'value': config.texteditorWidth,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Text Editor Height:')),
									E('input', {
										'type': 'number',
										'id': 'texteditorHeight-input',
										'value': config.texteditorHeight,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Hex Editor Width:')),
									E('input', {
										'type': 'number',
										'id': 'hexeditorWidth-input',
										'value': config.hexeditorWidth,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Hex Editor Height:')),
									E('input', {
										'type': 'number',
										'id': 'hexeditorHeight-input',
										'value': config.hexeditorHeight,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Column Widths (format: name:width,type:width,...):')),
									E('input', {
										'type': 'text',
										'id': 'columnWidths-input',
										'value': Object.values(config.columnWidths).join(''),
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Column Min Widths (format: name:minWidth,type:minWidth,...):')),
									E('input', {
										'type': 'text',
										'id': 'columnMinWidths-input',
										'value': Object.values(config.columnMinWidths).join(''),
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Column Max Widths (format: name:maxWidth,type:maxWidth,...):')),
									E('input', {
										'type': 'text',
										'id': 'columnMaxWidths-input',
										'value': Object.values(config.columnMaxWidths).join(''),
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Padding:')),
									E('input', {
										'type': 'number',
										'id': 'padding-input',
										'value': config.padding,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Padding Min:')),
									E('input', {
										'type': 'number',
										'id': 'paddingMin-input',
										'value': config.paddingMin,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Padding Max:')),
									E('input', {
										'type': 'number',
										'id': 'paddingMax-input',
										'value': config.paddingMax,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {}, [
									E('label', {}, _('Current Directory:')),
									E('input', {
										'type': 'text',
										'id': 'currentDirectory-input',
										'value': config.currentDirectory,
										'style': 'width:100%; margin-bottom:10px;'
									})
								]),
								E('div', {
									'class': 'cbi-page-actions'
								}, [
									E('button', {
										'class': 'btn cbi-button-save custom-save-button',
										'click': this.handleSaveSettings.bind(this)
									}, _('Save'))
								])
							])
						])
					])
				])
			])
		]);
		// Add event listeners
		const sortButtons = viewContainer.querySelectorAll('.sort-button[data-field]');
		sortButtons.forEach((button) => {
			button.addEventListener('click', (event) => {
				event.preventDefault();
				const field = button.getAttribute('data-field');
				if (field) {
					self.sortBy(field); // Sort the file list by the selected field
				}
			});
		});
		// Load the file list and initialize resizeable columns
		this.loadFileList(currentPath).then(() => {
			self.initResizeableColumns();
			const fileListContainer = document.getElementById('file-list-container');
			if (fileListContainer && typeof ResizeObserver !== 'undefined') {
				// Initialize ResizeObserver only once
				if (!self.fileListResizeObserver) {
					self.fileListResizeObserver = new ResizeObserver((entries) => {
						for (let entry of entries) {
							const newWidth = entry.contentRect.width;
							const newHeight = entry.contentRect.height;

							// Update config only if newWidth and newHeight are greater than 0
							if (newWidth > 0 && newHeight > 0) {
								config.windowWidth = newWidth;
								config.windowHeight = newHeight;
							}
						}
					});
					self.fileListResizeObserver.observe(fileListContainer);
				}
			}
		});
		return viewContainer;
	},

	// Handler for the "Select All" checkbox click
	handleSelectAllClick(ev) {
		if (ev.altKey) {
			ev.preventDefault(); // Prevent the default checkbox behavior
			this.handleInvertSelection();
		} else {
			// Proceed with normal click handling; the 'change' event will be triggered
		}
	},

	// Function to invert selection
	handleInvertSelection() {
		const allCheckboxes = document.querySelectorAll('.select-checkbox');
		allCheckboxes.forEach((checkbox) => {
			checkbox.checked = !checkbox.checked;
			const filePath = checkbox.getAttribute('data-file-path');
			if (checkbox.checked) {
				selectedItems.add(filePath);
			} else {
				selectedItems.delete(filePath);
			}
		});
		// Update the "Select All" checkbox state
		this.updateSelectAllCheckbox();
		// Update the "Delete Selected" button visibility
		this.updateDeleteSelectedButton();
	},

	/**
	 * Switches the active tab in the interface and performs necessary actions based on the selected tab.
	 *
	 * @param {string} tab - The identifier of the tab to switch to ('filemanager', 'editor', 'settings', or 'help').
	 */
	switchToTab(tab) {
		// Retrieve the content containers for each tab
		const fileManagerContent = document.getElementById('content-filemanager');
		const editorContent = document.getElementById('content-editor');
		const settingsContent = document.getElementById('content-settings');
		const helpContent = document.getElementById('content-help');

		// Retrieve the tab elements
		const tabFileManager = document.getElementById('tab-filemanager');
		const tabEditor = document.getElementById('tab-editor');
		const tabSettings = document.getElementById('tab-settings');
		const tabHelp = document.getElementById('tab-help');

		// Ensure all necessary elements are present
		if (fileManagerContent && editorContent && settingsContent && helpContent && tabFileManager && tabEditor && tabSettings && tabHelp) {
			// Display the selected tab's content and hide the others
			fileManagerContent.style.display = (tab === 'filemanager') ? 'block' : 'none';
			editorContent.style.display = (tab === 'editor') ? 'block' : 'none';
			settingsContent.style.display = (tab === 'settings') ? 'block' : 'none';
			helpContent.style.display = (tab === 'help') ? 'block' : 'none';

			// Update the active tab's styling
			tabFileManager.className = (tab === 'filemanager') ? 'cbi-tab cbi-tab-active' : 'cbi-tab';
			tabEditor.className = (tab === 'editor') ? 'cbi-tab cbi-tab-active' : 'cbi-tab';
			tabSettings.className = (tab === 'settings') ? 'cbi-tab cbi-tab-active' : 'cbi-tab';
			tabHelp.className = (tab === 'help') ? 'cbi-tab cbi-tab-active' : 'cbi-tab';

			// Perform actions based on the selected tab
			if (tab === 'filemanager') {
				// Reload and display the updated file list when the File Manager tab is activated
				this.loadFileList(currentPath)
					.then(() => {
						// Initialize resizeable columns after successfully loading the file list
						this.initResizeableColumns();
					})
					.catch((err) => {
						// Display an error notification if loading the file list fails
						pop(null, E('p', _('Failed to update file list: %s').format(err.message)), 'error');
					});
			} else if (tab === 'settings') {
				// Load and display settings when the Settings tab is activated
				this.loadSettings();
			} else if (tab === 'help') {
				// Render the Help content when the Help tab is activated
				this.renderHelp();
			}
			// No additional actions are required for the Editor tab in this context
		}
	},

	/**
	 * Renders the Help content by converting Markdown to HTML and inserting it into the Help container.
	 */
	renderHelp() {
		const self = this;

		// Convert Markdown to HTML

		const helpContentHTML = md.parseMarkdown(md_help.helpContentMarkdown);


		// Get the Help content container
		const helpContent = document.getElementById('content-help');

		if (helpContent) {
			// Insert the converted HTML into the Help container
			helpContent.innerHTML = helpContentHTML;

			// Initialize resizeable functionality for the Help window
			self.initResizeableHelp();
		} else {
			console.error('Help content container not found.');
			pop(null, E('p', _('Failed to render Help content: Container not found.')), 'error');
		}
	},

	/**
	 * Initializes the resizeable functionality for the Help window.
	 */
	initResizeableHelp() {
		const helpContent = document.getElementById('content-help');

		if (helpContent) {
			// Set initial dimensions
			helpContent.style.width = '700px';
			helpContent.style.height = '600px';
			helpContent.style.resize = 'both';
			helpContent.style.overflow = 'auto';
			helpContent.style.border = '1px solid #ccc';
			helpContent.style.padding = '10px';
			helpContent.style.boxSizing = 'border-box';

			// Optional: Add a drag handle for better user experience
			/*
			var dragHandle = E('div', {
				'class': 'resize-handle',
				'style': 'width: 10px; height: 10px; background: #ccc; position: absolute; bottom: 0; right: 0; cursor: se-resize;'
			});
			helpContent.appendChild(dragHandle);
			*/
		} else {
			console.error('Help content container not found for resizing.');
		}
	},

	// Handler for the "Go" button click to navigate to a directory
	handleGoButtonClick() {
		// Logic to navigate to the specified directory and update the file list
		const self = this;
		const pathInput = document.getElementById('path-input');
		if (pathInput) {
			const newPath = pathInput.value.trim() || '/';
			fs.stat(newPath).then((stat) => {
				if (stat.type === 'directory') {
					currentPath = newPath;
					pathInput.value = currentPath;
					self.loadFileList(currentPath).then(() => {
						self.initResizeableColumns();
					});
				} else {
					pop(null, E('p', _('The specified path does not appear to be a directory.')), 'error');
				}
			}).catch((err) => {
				pop(null, E('p', _('Failed to access the specified path: %s').format(err.message)), 'error');
			});
		}
	},

	// Handler for dragging files over the drop zone
	handleDragEnter(event) {
		event.preventDefault();
		event.stopPropagation();
		dragCounter++;
		const fileListContainer = document.getElementById('file-list-container');
		const dragOverlay = document.getElementById('drag-overlay');
		if (fileListContainer && dragOverlay) {
			fileListContainer.classList.add('drag-over');
			dragOverlay.style.display = 'flex';
		}
	},

	// Handler for when files are over the drop zone
	handleDragOver(event) {
		event.preventDefault();
		event.stopPropagation();
		event.dataTransfer.dropEffect = 'copy'; // Indicate copy action
	},

	// Handler for leaving the drop zone
	handleDragLeave(event) {
		event.preventDefault();
		event.stopPropagation();
		dragCounter--;
		if (dragCounter === 0) {
			const fileListContainer = document.getElementById('file-list-container');
			const dragOverlay = document.getElementById('drag-overlay');
			if (fileListContainer && dragOverlay) {
				fileListContainer.classList.remove('drag-over');
				dragOverlay.style.display = 'none';
			}
		}
	},

	// Handler for dropping files into the drop zone
	handleDrop(event) {
		event.preventDefault();
		event.stopPropagation();
		dragCounter = 0; // Reset counter
		const self = this;
		const files = event.dataTransfer.files;
		const fileListContainer = document.getElementById('file-list-container');
		const dragOverlay = document.getElementById('drag-overlay');
		if (fileListContainer && dragOverlay) {
			fileListContainer.classList.remove('drag-over');
			dragOverlay.style.display = 'none';
		}
		if (files.length > 0) {
			self.uploadFiles(files);
		}
	},

	// Handler for uploading a file
	handleUploadClick(ev) {
		const self = this;
		const fileInput = document.createElement('input');
		fileInput.type = 'file';
		fileInput.multiple = true; // Allow selecting multiple files
		fileInput.style.display = 'none';
		document.body.appendChild(fileInput);
		fileInput.onchange = (event) => {
			const files = event.target.files;
			if (!files || files.length === 0) {
				pop(null, E('p', _('No file selected.')), 'error');
				return;
			}
			self.uploadFiles(files); // Use the shared upload function
		};
		fileInput.click();
	},

	uploadFiles(files) {
		const self = this;
		const directoryPath = currentPath;
		const statusInfo = document.getElementById('status-info');
		const statusProgress = document.getElementById('status-progress');
		const totalFiles = files.length;
		let uploadedFiles = 0;

		function uploadNextFile(index) {
			if (index >= totalFiles) {
				self.loadFileList(currentPath).then(() => {
					self.initResizeableColumns();
				});
				return;
			}

			const file = files[index];
			const fullFilePath = joinPath(directoryPath, file.name);
			if (statusInfo) {
				statusInfo.textContent = _('Uploading: "%s"...').format(file.name);
			}
			if (statusProgress) {
				statusProgress.innerHTML = '';
				const progressBarContainer = E('div', {
					'class': 'cbi-progressbar',
					'title': '0%'
				}, [E('div', {
					'style': 'width:0%'
				})]);
				statusProgress.appendChild(progressBarContainer);
			}

			uploadFile(fullFilePath, file, (percent) => {
				if (statusProgress) {
					const progressBar = statusProgress.querySelector('.cbi-progressbar div');
					if (progressBar) {
						progressBar.style.width = percent.toFixed(2) + '%';
						statusProgress.querySelector('.cbi-progressbar').setAttribute('title', percent.toFixed(2) + '%');
					}
				}
			}).then(() => {
				if (statusProgress) {
					statusProgress.innerHTML = '';
				}
				if (statusInfo) {
					statusInfo.textContent = _('File "%s" uploaded successfully.').format(file.name);
				}
				popTimeout(null, E('p', _('File "%s" uploaded successfully.').format(file.name)), 5000, 'info');
				uploadedFiles++;
				uploadNextFile(index + 1);
			}).catch((err) => {
				if (statusProgress) {
					statusProgress.innerHTML = '';
				}
				if (statusInfo) {
					statusInfo.textContent = _('Upload failed for file "%s": %s').format(file.name, err.message);
				}
				pop(null, E('p', _('Upload failed for file "%s": %s').format(file.name, err.message)), 'error');
				uploadNextFile(index + 1);
			});
		}
		uploadNextFile(0);
	},

	// Handler for creating a directory
	handleMakeDirectoryClick(ev) {
		// Logic to create a new directory
		const self = this;
		const statusInfo = document.getElementById('status-info');
		const statusProgress = document.getElementById('status-progress');
		if (statusInfo && statusProgress) {
			statusInfo.innerHTML = '';
			statusProgress.innerHTML = '';
			const dirNameInput = E('input', {
				'type': 'text',
				'placeholder': _('Directory Name'),
				'style': 'margin-right: 10px;'
			});
			const saveButton = E('button', {
				'class': 'btn',
				'disabled': true,
				'click'() {
					self.createDirectory(dirNameInput.value);
				}
			}, _('Save'));
			dirNameInput.addEventListener('input', () => {
				if (dirNameInput.value.trim()) {
					saveButton.disabled = false;
				} else {
					saveButton.disabled = true;
				}
			});
			statusInfo.appendChild(E('span', {}, _('Create Directory: ')));
			statusInfo.appendChild(dirNameInput);
			statusProgress.appendChild(saveButton);
		}
	},

	// Function to create a directory
	createDirectory(dirName) {
		// Execute the 'mkdir' command and update the interface
		const self = this;
		const trimmedDirName = dirName.trim();
		const dirPath = joinPath(currentPath, trimmedDirName);
		fs.exec('mkdir', [dirPath]).then((res) => {
			if (res.code !== 0) {
				return Promise.reject(new Error(res.stderr.trim()));
			}
			popTimeout(null, E('p', _('Directory "%s" created successfully.').format(trimmedDirName)), 5000, 'info');
			self.loadFileList(currentPath).then(() => {
				self.initResizeableColumns();
			});
			const statusInfo = document.getElementById('status-info');
			const statusProgress = document.getElementById('status-progress');
			if (statusInfo) statusInfo.textContent = _('No directory selected.');
			if (statusProgress) statusProgress.innerHTML = '';
		}).catch((err) => {
			pop(null, E('p', _('Failed to create directory "%s": %s').format(trimmedDirName, err.message)), 'error');
		});
	},

	// Handler for creating a file
	handleCreateFileClick(ev) {
		// Logic to create a new file
		const self = this;
		const statusInfo = document.getElementById('status-info');
		const statusProgress = document.getElementById('status-progress');
		if (statusInfo && statusProgress) {
			statusInfo.innerHTML = '';
			statusProgress.innerHTML = '';
			const fileNameInput = E('input', {
				'type': 'text',
				'placeholder': _('File Name'),
				'style': 'margin-right: 10px;'
			});
			const createButton = E('button', {
				'class': 'btn',
				'disabled': true,
				'click'() {
					self.createFile(fileNameInput.value);
				}
			}, _('Create'));
			fileNameInput.addEventListener('input', () => {
				if (fileNameInput.value.trim()) {
					createButton.disabled = false;
				} else {
					createButton.disabled = true;
				}
			});
			statusInfo.appendChild(E('span', {}, _('Create File: ')));
			statusInfo.appendChild(fileNameInput);
			statusProgress.appendChild(createButton);
		}
	},

	// Function to create a file
	createFile(fileName) {
		// Execute the 'touch' command and update the interface
		const self = this;
		const trimmedFileName = fileName.trim();
		const filePath = joinPath(currentPath, trimmedFileName);
		fs.exec('touch', [filePath]).then((res) => {
			if (res.code !== 0) {
				return Promise.reject(new Error(res.stderr.trim()));
			}
			popTimeout(null, E('p', _('File "%s" created successfully.').format(trimmedFileName)), 5000, 'info');
			self.loadFileList(currentPath).then(() => {
				self.initResizeableColumns();
			});
			const statusInfo = document.getElementById('status-info');
			const statusProgress = document.getElementById('status-progress');
			if (statusInfo) statusInfo.textContent = _('No file selected.');
			if (statusProgress) statusProgress.innerHTML = '';
		}).catch((err) => {
			pop(null, E('p', _('Failed to create file "%s": %s').format(trimmedFileName, err.message)), 'error');
		});
	},

	// Handler for checkbox state change on a file
	handleCheckboxChange(ev) {
		const cb = ev.target;
		const filePath = cb.dataset.filePath;

		cb.checked
			? selectedItems.add(filePath)
			: selectedItems.delete(filePath);

		this.updateDeleteSelectedButton();
		this.updateSelectAllCheckbox();
	},

	// Update the "Delete Selected" button
	updateDeleteSelectedButton() {
		const btn = document.getElementById('delete-selected-button');
		if (!btn) return;

		btn.style.display = selectedItems.size > 0 ? '' : 'none';
	},

	// Update the "Select All" checkbox state
	updateSelectAllCheckbox() {
		const selectAll = document.getElementById('select-all-checkbox');
		if (!selectAll) return;

		const checkboxes = [...document.querySelectorAll('.select-checkbox')];
		if (checkboxes.length === 0) {
			selectAll.checked = false;
			selectAll.indeterminate = false;
			return;
		}

		const total = checkboxes.length;
		const checked = checkboxes.filter(cb => cb.checked).length;

		selectAll.checked = checked === total;
		selectAll.indeterminate = checked > 0 && checked < total;
	},

	// Handler for the "Select All" checkbox change
	handleSelectAllChange(ev) {
		const checked = ev.target.checked;
		const checkboxes = [...document.querySelectorAll('.select-checkbox')];

		selectedItems.clear();

		checkboxes.forEach(cb => {
			cb.checked = checked;
			if (checked) selectedItems.add(cb.dataset.filePath);
		});

		this.updateDeleteSelectedButton();
		this.updateSelectAllCheckbox();
	},

	// Handler for deleting selected items
	handleDeleteSelected() {
		// Delete selected files and directories
		const self = this;
		if (selectedItems.size === 0) {
			return;
		}
		if (!confirm(_('Are you sure you want to delete the selected files and directories?'))) {
			return;
		}
		const promises = [];
		selectedItems.forEach((filePath) => {
			promises.push(fs.remove(filePath).catch((err) => {
				pop(null, E('p', _('Failed to delete %s: %s').format(filePath, err.message)), 'error');
			}));
		});
		Promise.all(promises).then(() => {
			popTimeout(null, E('p', _('Selected files and directories deleted successfully.')), 5000, 'info');
			selectedItems.clear();
			self.updateDeleteSelectedButton();
			self.loadFileList(currentPath).then(() => {
				self.initResizeableColumns();
			});
		}).catch((err) => {
			pop(null, E('p', _('Failed to delete selected files and directories: %s').format(err.message)), 'error');
		});
	},

	// Function to load the file list
	loadFileList(path) {
		const self = this;
		selectedItems.clear();

		return getFileList(path).then(files => {
			// 1. Get column order dynamically from table header
			const columns = Array.from(
				document.querySelectorAll('#file-table thead th[data-field]')
			).map(th => th.getAttribute('data-field'));


			const fileList = document.getElementById('file-list');
			if (!fileList) {
				pop(null, E('p', _('Failed to display the file list.')), 'error');
				return;
			}

			fileList.innerHTML = '';
			files.sort(self.compareFiles.bind(self));

			//
			// Add ".." parent row
			//
			if (path !== '/') {
				const parentPath = path.substring(0, path.lastIndexOf('/')) || '/';

				const tr = E('tr', {
					'data-file-path': parentPath,
					'data-file-type': 'directory'
				});

				// Create cells for *every* column
				for (const col of columns) {
					if (col === 'name') {
						tr.appendChild(
							E('td', { colspan: columns.length }, [
								E('a', {
									href: '#',
									click: () => self.handleDirectoryClick(parentPath)
								}, '.. (Parent Directory)')
							])
						);
						break;
					} else {
						tr.appendChild(E('td')); // empty cell
					}
				}

				fileList.appendChild(tr);
			}

			//
			// 2. For each file, create row dynamically
			//
			for (const file of files) {
				const fullPath = joinPath(path, file.name);
				const tr = E('tr', {
					'data-file-path': fullPath,
					'data-file-type': file.type,
					'data-permissions': file.permissions,
					'data-numeric-permissions': file.numericPermissions,
					'data-owner': file?.user || file.uid,
					'data-group': file?.group || file.gid,
					'data-size': file.size
				});

				//
				// Prebuild common reusable items
				//
				const nameLink = E('a', {
					href: '#',
					title: file.permissions,
					class: `${file.type}-link`,
					click(event) {
						if (file.type === 'directory' || file?.target?.type === 'directory') {
							self.handleDirectoryClick(fullPath);
						} else {
							event.preventDefault();
							self.handleFileClick(fullPath, event.altKey ? 'hex' : 'text');
						}
					}
				}, file?.target ? `${file.name} → ${file.target?.name}` : file.name);

				const actions = [];
				const checkbox = E('input', {
					type: 'checkbox',
					class: 'select-checkbox',
					'data-file-path': fullPath,
					change: ev => self.handleCheckboxChange(ev)
				});
				actions.push(checkbox);

				actions.push(E('span', {
					class: 'edit-button',
					title: _('Edit properties'),
					click: () => self.handleEditFile(fullPath, file)
				}, '✏️'));

				actions.push(E('span', {
					class: 'duplicate-button',
					title: _('Duplicate'),
					click: () => self.handleDuplicateFile(fullPath, file)
				}, '📑'));

				actions.push(E('span', {
					class: 'delete-button',
					title: _('Delete'),
					click: () => self.handleDeleteFile(fullPath, file)
				}, '🗑️'));

				if (file.type === 'file') {
					actions.push(E('span', {
						class: 'download-button',
						title: _('Download'),
						click: () => self.handleDownloadFile(fullPath)
					}, '⬇️'));
				}

				//
				// 3. Build `<td>` dynamically based on column definitions
				//
				for (const col of columns) {
					let td;

					switch (col) {
						case 'name':
							td = E('td', {}, [nameLink]);
							break;

						case 'type':
							td = E('td', {}, fileTypes[file.type] || file.type);
							break;

						case 'size':
							if (file.type === 'directory' || (file.type === 'symlink' && file.size === -1)) {
								td = E('td', { class: 'size-cell' }, [
									E('span', { class: 'size-number' }, '-'),
									E('span', { class: 'size-unit' }, ''),
								]);
							} else {
								const formatted = self.getFormattedSize(file.size);
								td = E('td', { class: 'size-cell' }, [
									E('span', { class: 'size-number' }, formatted.number),
									E('span', { class: 'size-unit' }, formatted.unit)
								]);
							}
							break;

						case 'mtime':
							td = E('td', {}, new Date(file.mtime * 1000).toLocaleString());
							break;

						case 'actions':
							td = E('td', {}, actions);
							break;

						case 'permissions':
							td = E('td', {}, file.permissions);
							break;

						default:
							// Support future dynamically-added columns
							td = E('td', {}, file[col] ?? '');
							break;
					}

					tr.appendChild(td);
				}

				fileList.appendChild(tr);
			}

			//
			// housekeeping
			//
			const statusInfo = document.getElementById('status-info');
			const statusProgress = document.getElementById('status-progress');

			if (statusInfo) statusInfo.textContent = _('No file selected.');
			if (statusProgress) statusProgress.innerHTML = '';

			self.setInitialColumnWidths();
			self.updateSelectAllCheckbox();
			self.updateDeleteSelectedButton();
			return Promise.resolve();
		}).catch((err) => {
			pop(null, E('p', _('Failed to load file list: %s').format(err.message)), 'error');
			return Promise.reject(err);
		});
	},

	// Function to format file size
	getFormattedSize(size) {
		/* 64 bit systems i.e. rpcd have max size of 128 TB */
		const units = [' ', 'K', 'M', 'G', 'T'];
		let index = 0;
		let value = size;

		if (size > 0) {
			// Keep dividing until below 1024 or no more units
			while (value >= 1024 && index < units.length - 1) {
				value /= 1024;
				index++;
			}
		}

		// Format to 2 decimals, always 6 chars wide
		const num = value.toFixed(2).padStart(6, ' ');

		return {
			number: num,
			unit: ' ' + units[index] + 'B'
		};
	},

	// Function to sort files
	sortBy(field) {
		// Change the sort field and direction, and reload the file list
		if (sortField === field) {
			sortAscending = !sortAscending;
		} else {
			sortField = field;
			sortAscending = true;
		}
		this.loadFileList(currentPath);
	},

	// Function to compare files for sorting
	compareFiles(a, b) {
		// Compare files based on the selected field and direction
		const order = sortAscending ? 1 : -1;
		let aValue = a[sortField];
		let bValue = b[sortField];
		if (sortField === 'size') {
			aValue = (a.type === 'directory' || (a.type === 'symlink' && a.size === -1)) ? -1 : a.size;
			bValue = (b.type === 'directory' || (b.type === 'symlink' && b.size === -1)) ? -1 : b.size;
		}
		if (aValue < bValue) return -1 * order;
		if (aValue > bValue) return 1 * order;
		return 0;
	},

	// Set initial column widths in the table
	setInitialColumnWidths() {
		// Apply column width settings to the file table
		const table = document.getElementById('file-table');
		if (!table) {
			return;
		}
		const headers = table.querySelectorAll('th');
		headers.forEach((header, index) => {
			const field = header.getAttribute('data-field');
			if (field && config.columnWidths[field]) {
				const width = config.columnWidths[field];
				const minWidth = config.columnMinWidths[field] || 50;
				const maxWidth = config.columnMaxWidths[field] || 500;
				header.style.width = width + 'px';
				header.style.minWidth = minWidth + 'px';
				header.style.maxWidth = maxWidth + 'px';
				const rows = table.querySelectorAll('tr');
				rows.forEach((row, rowIndex) => {
					const cell = row.children[index];
					if (cell) {
						cell.style.width = width + 'px';
						cell.style.minWidth = minWidth + 'px';
						cell.style.maxWidth = maxWidth + 'px';
					}
				});
			}
		});
	},

	// Handler for clicking on a directory
	handleDirectoryClick(newPath) {
		// Navigate to the selected directory and update the file list
		const self = this;
		currentPath = newPath || '/';
		const pathInput = document.getElementById('path-input');
		if (pathInput) {
			pathInput.value = currentPath;
		}
		this.loadFileList(currentPath).then(() => {
			self.initResizeableColumns();
		});
	},

	/**
	 * Determines whether a given Uint8Array represents UTF-8 text data.
	 *
	 * @param {Uint8Array} uint8Array - The binary data to check.
	 * @returns {boolean} - Returns true if the data is UTF-8 text, false otherwise.
	 */
	isText(uint8Array) {

		const len = uint8Array.length;
		let i = 0;

		while (i < len) {
			const byte = uint8Array[i];

			if (byte === 0) return false; // Null byte indicates binary

			if (byte <= 0x7F) {
				// ASCII character, no action needed
				i++;
				continue;
			} else if ((byte & 0xE0) === 0xC0) {
				// 2-byte sequence
				if (i + 1 >= len || (uint8Array[i + 1] & 0xC0) !== 0x80) return false;
				i += 2;
			} else if ((byte & 0xF0) === 0xE0) {
				// 3-byte sequence
				if (
					i + 2 >= len ||
					(uint8Array[i + 1] & 0xC0) !== 0x80 ||
					(uint8Array[i + 2] & 0xC0) !== 0x80
				) {
					return false;
				}
				i += 3;
			} else if ((byte & 0xF8) === 0xF0) {
				// 4-byte sequence
				if (
					i + 3 >= len ||
					(uint8Array[i + 1] & 0xC0) !== 0x80 ||
					(uint8Array[i + 2] & 0xC0) !== 0x80 ||
					(uint8Array[i + 3] & 0xC0) !== 0x80
				) {
					return false;
				}
				i += 4;
			} else {
				// Invalid UTF-8 byte
				return false;
			}
		}

		return true;
	},

	// Function to handle clicking on a file to open it in the editor
	handleFileClick(filePath, mode) {
		const self = this;
		const fileRow = document.querySelector(`tr[data-file-path='${filePath}']`);
		const editorMessage = document.getElementById('editor-message');

		// Set original file permissions
		self.originalFilePermissions = fileRow ? fileRow.getAttribute('data-numeric-permissions') : '644';
		self.editorMode = mode;

		// Display loading message
		if (editorMessage) editorMessage.textContent = _('Loading file...');

		// Read the file as binary data
		fs.read_direct(filePath, 'blob')
			.then(blob => blob.arrayBuffer())
			.then(arrayBuffer => {
				const uint8Array = new Uint8Array(arrayBuffer);
				self.fileData = uint8Array;
				self.fileContent = ''; // Can be used for display or left empty
				self.editorMode = 'hex';
				self.textType = self.isText(uint8Array) ? 'text' : 'hex';
				if (mode === 'text') {
					// Determine if the file is text
					if (self.textType === 'text') {
						// If text, decode the content
						self.fileContent = new TextDecoder().decode(uint8Array);
						self.editorMode = 'text';
					} else {
						// If not text, show a warning and set mode to hex
						if (editorMessage) {
							editorMessage.textContent = _('The file does not contain valid text data. Opening in hex mode...');
						}
						pop(null, E('p', _('Opening file in hex mode since it is not a text file.')), 'warning');
					}
				}
			})
			.then(() => {
				// Render the editor and switch to the editor tab
				self.renderEditor(filePath);
				self.switchToTab('editor');
			})
			.catch(err => {
				// Handle errors during file reading
				pop(null, E('p', _('Failed to open file: %s').format(err.message)), 'error');
			});
	},
	// Adjust padding for line numbers in the editor
	adjustLineNumbersPadding() {
		// Update padding based on scrollbar size
		const lineNumbersDiv = document.getElementById('line-numbers');
		const editorTextarea = document.getElementById('editor-textarea');
		if (!lineNumbersDiv || !editorTextarea) {
			return;
		}
		const scrollbarHeight = editorTextarea.offsetHeight - editorTextarea.clientHeight;
		lineNumbersDiv.style.paddingBottom = scrollbarHeight + 'px';
	},

	// Handler for downloading a file
	handleDownloadFile(filePath) {
		// Download the file to the user's local machine
		const self = this;
		const fileName = filePath.split('/').pop();
		// Use the read_direct method to download the file
		fs.read_direct(filePath, 'blob')
			.then((blob) => {
				if (!(blob instanceof Blob)) {
					throw new Error(_('Response is not a Blob'));
				}
				const url = window.URL.createObjectURL(blob);
				const a = document.createElement('a');
				a.href = url;
				a.download = fileName;
				document.body.appendChild(a);
				a.click();
				a.remove();
				window.URL.revokeObjectURL(url);
			}).catch((err) => {
				pop(null, E('p', _('Failed to download file "%s": %s').format(fileName, err.message)), 'error');
			});
	},

	// Handler for deleting a file
	handleDeleteFile(filePath, fileInfo) {
		// Delete the selected file or directory
		const self = this;
		const itemName = filePath.split('/').pop();
		const itemTypeLabel = fileTypes[fileInfo?.type];

		if (confirm(_('Are you sure you want to delete this %s: "%s"?').format(itemTypeLabel, itemName))) {
			fs.remove(filePath).then(() => {
				popTimeout(null, E('p', _('Successfully deleted %s: "%s".').format(itemTypeLabel, itemName)), 5000, 'info');
				self.loadFileList(currentPath).then(() => {
					self.initResizeableColumns();
				});
				const statusInfo = document.getElementById('status-info');
				if (statusInfo) {
					statusInfo.textContent = _('Deleted %s: "%s".').format(itemTypeLabel, itemName);
				}
			}).catch((err) => {
				pop(null, E('p', _('Failed to delete %s "%s": %s').format(itemTypeLabel, itemName, err.message)), 'error');
			});
		}
	},

	// Update line numbers in the text editor
	updateLineNumbers() {
		// Update the line numbers display when the text changes
		const lineNumbersDiv = document.getElementById('line-numbers');
		const editorTextarea = document.getElementById('editor-textarea');
		if (!lineNumbersDiv || !editorTextarea) return;

		// Count lines
		const lineCount = editorTextarea.value.split('\n').length;

		// Build HTML using join — much faster than concatenation
		lineNumbersDiv.innerHTML = Array.from({ length: lineCount }, (_, i) => `<div>${i + 1}</div>`).join('');
	},

	// Synchronize scrolling between line numbers and text
	syncScroll() {
		// Sync scrolling of line numbers with the text area
		const lineNumbersDiv = document.getElementById('line-numbers');
		const editorTextarea = document.getElementById('editor-textarea');
		if (!lineNumbersDiv || !editorTextarea) {
			return;
		}
		lineNumbersDiv.scrollTop = editorTextarea.scrollTop;
	},

	// Toggle line numbers display in the editor
	toggleLineNumbers() {
		// Ensure the editor is in Text Mode before toggling line numbers
		if (this.editorMode !== 'text') {
			console.warn('Toggle Line Numbers is only available in Text Mode.');
			return;
		}

		// Get the line numbers div and the textarea
		const lineNumbersDiv = document.getElementById('line-numbers');
		const editorTextarea = document.getElementById('editor-textarea');
		if (!lineNumbersDiv || !editorTextarea) {
			console.error('Line numbers div or editor textarea not found.');
			return;
		}

		// Toggle the display of line numbers
		if (lineNumbersDiv.style.display === 'none' || !lineNumbersDiv.style.display) {
			lineNumbersDiv.style.display = 'block';
			this.updateLineNumbers();
			this.adjustLineNumbersPadding();
			this.syncScroll();
		} else {
			lineNumbersDiv.style.display = 'none';
			lineNumbersDiv.innerHTML = '';
		}
	},

	// Generate a name for a copy of a file
	getCopyName(originalName, existingNames) {
		// Split filename into base name + extension
		const dotIndex = originalName.lastIndexOf('.');
		const hasExt = dotIndex > 0 && dotIndex < originalName.length - 1;

		const base = hasExt ? originalName.slice(0, dotIndex) : originalName;
		const ext  = hasExt ? originalName.slice(dotIndex) : '';

		// First attempt: "name (copy).ext"
		let candidate = `${base} (copy)${ext}`;

		// If taken, try: "name (copy 2).ext", "name (copy 3).ext", ...
		let counter = 2;
		while (existingNames.includes(candidate)) {
			candidate = `${base} (copy ${counter++})${ext}`;
		}

		return candidate;
	},

	// Handler for duplicating a file
	handleDuplicateFile(filePath, fileInfo) {
		// Copy the file or directory with a new name
		const self = this;
		getFileList(currentPath).then((files) => {
			const existingNames = files.map((f) => {
				return f.name;
			});
			const newName = self.getCopyName(fileInfo.name, existingNames);
			const newPath = joinPath(currentPath, newName);
			let command;
			let args;
			if (fileInfo.type === 'directory') {
				command = 'cp';
				args = ['-rp', filePath, newPath];
			} else if (fileInfo.type === 'symlink') {
				command = 'cp';
				args = ['-Pp', filePath, newPath];
			} else {
				command = 'cp';
				args = ['-p', filePath, newPath];
			}
			fs.exec(command, args).then((res) => {
				if (res.code !== 0) {
					return Promise.reject(new Error(res.stderr.trim()));
				}
				popTimeout(null, E('p', _('Successfully duplicated %s "%s" as "%s".').format(_('item'), fileInfo.name, newName)), 5000, 'info');
				self.loadFileList(currentPath).then(() => {
					self.initResizeableColumns();
				});
			}).catch((err) => {
				pop(null, E('p', _('Failed to duplicate %s "%s": %s').format(_('item'), fileInfo.name, err.message)), 'error');
			});
		}).catch((err) => {
			pop(null, E('p', _('Failed to get file list: %s').format(err.message)), 'error');
		});
	},

	// Handler for saving a file after editing
	handleSaveFile(filePath) {
		const self = this;
		let contentBlob;

		if (self.editorMode === 'text') {
			const textarea = document.querySelector('#editor-container textarea');
			if (!textarea) {
				pop(null, E('p', _('Editor textarea not found.')), 'error');
				return;
			}
			const content = textarea.value;
			self.fileContent = content;

			// Convert content to Uint8Array in chunks not exceeding 8KB
			const CHUNK_SIZE = 8 * 1024; // 8KB
			const totalLength = content.length;
			let chunks = [];
			for (let i = 0; i < totalLength; i += CHUNK_SIZE) {
				const chunkStr = content.slice(i, i + CHUNK_SIZE);
				const chunkBytes = new TextEncoder().encode(chunkStr);
				chunks.push(chunkBytes);
			}
			// Concatenate chunks into a single Uint8Array
			const totalBytes = chunks.reduce((prev, curr) => {
				return prev + curr.length;
			}, 0);
			let dataArray = new Uint8Array(totalBytes);
			let offset = 0;
			chunks.forEach((chunk) => {
				dataArray.set(chunk, offset);
				offset += chunk.length;
			});
			self.fileData = dataArray; // Update binary data

			contentBlob = new Blob([self.fileData], {
				type: 'application/octet-stream'
			});
		} else if (self.editorMode === 'hex') {
			// Get data from hex editor
			self.fileData = self.hexEditorInstance.getData(); // Assuming getData method is implemented in HexEditor
			contentBlob = new Blob([self.fileData], {
				type: 'application/octet-stream'
			});
		}

		const statusInfo = document.getElementById('status-info');
		const statusProgress = document.getElementById('status-progress');
		const fileName = filePath.split('/').pop();
		if (statusInfo) {
			statusInfo.textContent = _('Saving file: "%s"...').format(fileName);
		}
		if (statusProgress) {
			statusProgress.innerHTML = '';
			const progressBarContainer = E('div', {
				'class': 'cbi-progressbar',
				'title': '0%'
			}, [E('div', {
				'style': 'width:0%'
			})]);
			statusProgress.appendChild(progressBarContainer);
		}

		uploadFile(filePath, contentBlob, (percent) => {
			if (statusProgress) {
				const progressBar = statusProgress.querySelector('.cbi-progressbar div');
				if (progressBar) {
					progressBar.style.width = percent.toFixed(2) + '%';
					statusProgress.querySelector('.cbi-progressbar').setAttribute('title', percent.toFixed(2) + '%');
				}
			}
		}).then(() => {
			const permissions = self.originalFilePermissions;
			if (permissions !== undefined) {
				return fs.exec('chmod', [permissions, filePath]).then((res) => {
					if (res.code !== 0) {
						throw new Error(res.stderr.trim());
					}
				}).then(() => {
					if (statusInfo) {
						statusInfo.textContent = _('File "%s" uploaded successfully.').format(fileName);
					}
					popTimeout(null, E('p', _('File "%s" uploaded successfully.').format(fileName)), 5000, 'info');
					return self.loadFileList(currentPath).then(() => {
						self.initResizeableColumns();
					});
				}).catch((err) => {
					pop(null, E('p', _('Failed to apply permissions to file "%s": %s').format(fileName, err.message)), 'error');
				});
			} else {
				if (statusInfo) {
					statusInfo.textContent = _('File "%s" uploaded successfully.').format(fileName);
				}
				popTimeout(null, E('p', _('File "%s" uploaded successfully.').format(fileName)), 5000, 'info');
				return self.loadFileList(currentPath).then(() => {
					self.initResizeableColumns();
				});
			}
		}).catch((err) => {
			if (statusProgress) {
				statusProgress.innerHTML = '';
			}
			if (statusInfo) {
				statusInfo.textContent = _('Failed to save file "%s": %s').format(fileName, err.message);
			}
			pop(null, E('p', _('Failed to save file "%s": %s').format(fileName, err.message)), 'error');
		});
	},

	// Handler for clicking on a symbolic link
	handleSymlinkClick(linkPath, targetPath, mode) {
		// Navigate to the target of the symbolic link
		const self = this;
		if (!targetPath.startsWith('/')) {
			targetPath = joinPath(currentPath, targetPath);
		}
		fs.stat(targetPath).then((stat) => {
			if (stat.type === 'directory') {
				self.handleDirectoryClick(targetPath);
			} else if (stat.type === 'file') {
				self.handleFileClick(targetPath, mode);
			} else {
				pop(null, E('p', _('The symlink points to an unsupported type.')), 'error');
			}
		}).catch((err) => {
			pop(null, E('p', _('Failed to access symlink target: %s').format(err.message)), 'error');
		});
		const statusInfo = document.getElementById('status-info');
		if (statusInfo) {
			statusInfo.textContent = _('Symlink: ') + linkPath + ' -> ' + targetPath;
		}
	},

	// Initialize resizeable columns in the table
	initResizeableColumns() {
		// Add handlers to adjust column widths
		const self = this;
		const table = document.getElementById('file-table');
		if (!table) {
			return;
		}
		const headers = table.querySelectorAll('th');
		headers.forEach((header, index) => {
			const resizer = header.querySelector('.resizer');
			if (resizer) {
				resizer.removeEventListener('mousedown', header.resizeHandler);
				header.resizeHandler = (e) => {
					e.preventDefault();
					const startX = e.pageX;
					const startWidth = header.offsetWidth;
					const field = header.getAttribute('data-field');
					const minWidth = config.columnMinWidths[field] || 50;
					const maxWidth = config.columnMaxWidths[field] || 500;

					function doDrag(e) {
						const currentX = e.pageX;
						const newWidth = startWidth + (currentX - startX);
						if (newWidth >= minWidth && newWidth <= maxWidth) {
							header.style.width = newWidth + 'px';
							if (field) {
								config.columnWidths[field] = newWidth;
							}
							const rows = table.querySelectorAll('tr');
							rows.forEach((row, rowIndex) => {
								const cell = row.children[index];
								if (cell) {
									cell.style.width = newWidth + 'px';
								}
							});
						}
					}

					function stopDrag() {
						document.removeEventListener('mousemove', doDrag, false);
						document.removeEventListener('mouseup', stopDrag, false);
						saveConfig();
					}
					document.addEventListener('mousemove', doDrag, false);
					document.addEventListener('mouseup', stopDrag, false);
				};
				resizer.addEventListener('mousedown', header.resizeHandler, false);
			}
		});
	},

	// Handler for editing a file's properties (name, permissions, etc.)
	handleEditFile(filePath, fileInfo) {
		// Display a form to edit the file's properties
		const self = this;
		const statusInfo = document.getElementById('status-info');
		const statusProgress = document.getElementById('status-progress');
		if (statusInfo && statusProgress) {
			statusInfo.innerHTML = '';
			statusProgress.innerHTML = '';
			const nameInput = E('input', {
				'type': 'text',
				'value': fileInfo.name,
				'placeholder': fileInfo.name,
				'style': 'margin-right: 10px;'
			});
			const permsInput = E('input', {
				'type': 'text',
				'placeholder': fileInfo.numericPermissions,
				'style': 'margin-right: 10px; width: 80px;'
			});
			const ownerInput = E('input', {
				'type': 'text',
				'placeholder': fileInfo?.user || fileInfo.uid,
				'style': 'margin-right: 10px; width: 100px;'
			});
			const groupInput = E('input', {
				'type': 'text',
				'placeholder': fileInfo?.group || fileInfo.gid,
				'style': 'margin-right: 10px; width: 100px;'
			});
			const saveButton = E('button', {
				'class': 'btn',
				'disabled': true,
				'click'() {
					self.saveFileChanges(filePath, fileInfo, nameInput.value, permsInput.value, ownerInput.value, groupInput.value);
				}
			}, _('Save'));
			[nameInput, permsInput, ownerInput, groupInput].forEach((input) => {
				input.addEventListener('input', () => {
					if (nameInput.value !== fileInfo.name || permsInput.value || ownerInput.value || groupInput.value) {
						saveButton.disabled = false;
					} else {
						saveButton.disabled = true;
					}
				});
			});
			statusInfo.appendChild(E('span', {}, _('Editing %s: "%s"').format(_('item'), fileInfo.name)));
			statusInfo.appendChild(nameInput);
			statusInfo.appendChild(permsInput);
			statusInfo.appendChild(ownerInput);
			statusInfo.appendChild(groupInput);
			statusProgress.appendChild(saveButton);
		}
	},

	// Save changes to a file's properties
	saveFileChanges(filePath, fileInfo, newName, newPerms, newOwner, newGroup) {
		// Apply changes and update the interface
		const self = this;
		const commands = [];
		const originalPath = filePath;
		const originalName = fileInfo.name;
		const newItemName = newName || originalName;

		if (newName && newName !== fileInfo.name) {
			const newPath = joinPath(currentPath, newName);
			commands.push(['mv', [filePath, newPath]]);
			filePath = newPath;
		}
		if (newPerms) {
			commands.push(['chmod', [newPerms, filePath]]);
		}

		if (newOwner || newGroup) {
			const owner = newOwner ?? (fileInfo?.user || fileInfo.uid);
			const group = newGroup ?? (fileInfo?.group || fileInfo.gid);

			commands.push(['chown', [`${owner}:${group}`, filePath]]);
		}

		let promise = Promise.resolve();
		commands.forEach((cmd) => {
			promise = promise.then(() => {
				return fs.exec(cmd[0], cmd[1]).then((res) => {
					if (res.code !== 0) {
						return Promise.reject(new Error(res.stderr.trim()));
					}
				});
			});
		});
		promise.then(() => {
			popTimeout(null, E('p', _('Changes to %s "%s" uploaded successfully.').format(_('item'), newItemName)), 5000, 'info');
			self.loadFileList(currentPath).then(() => {
				self.initResizeableColumns();
			});
			const statusInfo = document.getElementById('status-info');
			const statusProgress = document.getElementById('status-progress');
			if (statusInfo) statusInfo.textContent = _('No item selected.');
			if (statusProgress) statusProgress.innerHTML = '';
		}).catch((err) => {
			pop(null, E('p', _('Failed to save changes to %s "%s": %s').format(_('item'), newItemName, err.message)), 'error');
		});
	},

	handleSaveSettings(ev) {
		ev.preventDefault();
		var self = this;

		const parseAndSetConfig = (configPath, value) => {
			const input = document.getElementById(`${configPath}-input`);
			if (!input) return;
			let v = input.value.trim();

			if (typeof value == 'object') {
				parseKeyValuePairs(v, ':', (k, v) => {
					config[configPath][k] = parseInt(v, 10);
				});

			} else 
				config[configPath] = v;
		};

		Object.entries(config).forEach(([configPath, value]) => {
			parseAndSetConfig(configPath, value);
		});

			saveConfig().then(() => {
				popTimeout(null, E('p', _('Settings uploaded successfully.')), 5000, 'info');
				self.setInitialColumnWidths();
				const styleElement = document.querySelector('style');
				if (styleElement) {
					styleElement.textContent = styleElement.textContent.replace(/padding: \d+px/g, 'padding: ' + config.padding + 'px');
				}
				const fileListContainer = document.getElementById('file-list-container');
				if (fileListContainer) {
					fileListContainer.style.width = config.windowWidth + 'px';
					fileListContainer.style.height = config.windowHeight + 'px';
				}
				currentPath = config.currentDirectory || '/';
				const pathInput = document.getElementById('path-input');
				if (pathInput) {
					pathInput.value = currentPath;
				}
				self.loadFileList(currentPath).then(() => {
					self.initResizeableColumns();
				});
				const editorContainer = document.getElementById('editor-container');
				if (editorContainer) {
					const editorMode = self.editorMode;
					const editorSizes = {
						width: config[`${editorMode}editorWidth`] || 850,
						height: config[`${editorMode}editorHeight`] || 550
					};
					editorContainer.style.width = editorSizes.width + 'px';
					editorContainer.style.height = editorSizes.height + 'px';
				}
			}).catch((err) => {
				pop(null, E('p', _('Failed to save settings: %s').format(err.message)), 'error');
			});
	},

	// Load settings into the settings form
	loadSettings() {
		const setInputValue = (inputId, value) => {
			const input = document.getElementById(`${inputId}-input`);
			if (!input) return;
			let v;
			if (typeof value == 'object')
				input.value = Object.entries(value).map(([id, value]) => {
					return `${id}:${value}`
				}).join(',');
			else
				input.value = value;
		};

		Object.entries(config).forEach(([inputId, value]) => {
			setInputValue(inputId, value);
		});
	},

	updateUI() {
		const styleElement = document.querySelector('style');
		if (styleElement) {
			styleElement.textContent = styleElement.textContent.replace(/padding: \d+px/g, `padding: ${config.padding}px`);
		}

		const fileListContainer = document.getElementById('file-list-container');
		if (fileListContainer) {
			fileListContainer.style.width = `${config.windowWidth}px`;
			fileListContainer.style.height = `${config.windowHeight}px`;
		}

		const editorContainer = document.getElementById('editor-container');
		if (editorContainer) {
			const editorMode = this.editorMode;
			const editorHeight = config[`${editorMode}editorHeight`] || 550;
			const editorWidth = config[`${editorMode}editorWidth`] || 850;
			editorContainer.style.width = `${editorWidth}px`;
			editorContainer.style.height = `${editorHeight}px`;
		}
	},

	renderEditor(filePath) {
		const self = this;

		const editorContainer = document.getElementById('editor-container');

		// Clear the editor container
		editorContainer.innerHTML = '';

		// Get the sizes from the config
		const mode = self.editorMode; // 'text' or 'hex'
		const editorHeight = config[`${mode}editorHeight`] || 550;
		const editorWidth = config[`${mode}editorWidth`] || 850;

		// Create the editor content container
		const editorContentContainer = E('div', {
			'class': 'editor-content',
			'style': 'flex: 1; display: flex; overflow: hidden;'
		}, []);

		// Action buttons array
		let actionButtons = [];

		if (mode === 'text') {
			// Create line numbers div (initially hidden)
			const lineNumbersDiv = E('div', {
				'id': 'line-numbers',
				'class': 'line-numbers',
				'style': 'display: none;' // Initially hidden
			}, []);

			// Create textarea for text editing
			const editorTextarea = E('textarea', {
				'wrap': 'off',
				'id': 'editor-textarea',
				'style': 'flex: 1; resize: none; border: none; padding: 0; margin: 0; overflow: auto;'
			}, [self.fileContent || '']);

			// Append line numbers and textarea to the editor content container
			editorContentContainer.appendChild(lineNumbersDiv);
			editorContentContainer.appendChild(editorTextarea);

			// Add event listeners for updating line numbers and synchronizing scroll
			editorTextarea.addEventListener('input', self.updateLineNumbers.bind(self));
			editorTextarea.addEventListener('scroll', self.syncScroll.bind(self));
			lineNumbersDiv.addEventListener('scroll', () => {
				editorTextarea.scrollTop = lineNumbersDiv.scrollTop;
			});

			// Define action buttons specific to Text Mode
			actionButtons = [
				E('button', {
					'class': 'btn cbi-button-save custom-save-button',
					'click'() {
						self.handleSaveFile(filePath);
					}
				}, _('Save')),
				E('button', {
					'class': 'btn',
					'id': 'toggle-hex-mode',
					'style': 'margin-left: 10px;',
					'click'() {
						self.toggleHexMode(filePath);
					}
				}, _('Toggle to Hex Mode')),
				E('button', {
					'class': 'btn',
					'id': 'toggle-line-numbers',
					'style': 'margin-left: 10px;',
					'click'() {
						self.toggleLineNumbers();
					}
				}, _('Toggle Line Numbers'))
			];
		} else if (mode === 'hex') {
			// Create hex editor container
			const hexeditContainer = E('div', {
				'id': 'hexedit-container',
				'style': 'flex: 1; overflow: hidden; display: flex; flex-direction: column;'
			});

			// Append hex editor to the editor content container
			editorContentContainer.appendChild(hexeditContainer);

			// Initialize the HexEditor instance

			self.hexEditorInstance = HE.initialize(hexeditContainer);

			// Load data into the HexEditor
			self.hexEditorInstance.setData(self.fileData); // self.fileData is a Uint8Array

			// Define action buttons specific to Hex Mode
			actionButtons = [
				E('button', {
					'class': 'btn cbi-button-save custom-save-button',
					'click'() {
						self.handleSaveFile(filePath);
					}
				}, _('Save')),
				...(self.textType !== 'hex' ? [
					E('button', {
						'class': 'btn',
						'id': 'toggle-text-mode',
						'style': 'margin-left: 10px;',
						'click'() {
							self.toggleHexMode(filePath);
						}
					}, _('Toggle to ASCII Mode'))
				] : [])
			];
		}

		// Create the editor container with resizing and scrolling
		const editor = E('div', {
			'class': 'editor-container',
			'style': 'display: flex; flex-direction: column; width: ' + editorWidth + 'px; height: ' + editorHeight + 'px; resize: both; overflow: hidden;'
		}, [
			editorContentContainer,
			E('div', {
				'class': 'cbi-page-actions'
			}, actionButtons)
		]);

		// Append the editor to the editorContainer
		editorContainer.appendChild(editor);

		// Update status bar and message
		const statusInfo = document.getElementById('status-info');
		if (statusInfo) {
			statusInfo.textContent = _('Editing: ') + filePath;
		}
		const editorMessage = document.getElementById('editor-message');
		if (editorMessage) {
			editorMessage.textContent = _('Editing: ') + filePath;
		}

		// Clear any progress messages
		const statusProgress = document.getElementById('status-progress');
		if (statusProgress) {
			statusProgress.innerHTML = '';
		}

		// **Add ResizeObserver to editor-container to update config.editorContainerSizes**
		if (typeof ResizeObserver !== 'undefined') {
			// Disconnect existing observer if it exists to prevent multiple observers
			if (self.editorResizeObserver) {
				self.editorResizeObserver.disconnect();
				self.editorResizeObserver = null;
			}

			// Initialize a new ResizeObserver instance
			self.editorResizeObserver = new ResizeObserver((entries) => {
				for (let entry of entries) {
					let newWidth = Math.round(entry.contentRect.width);
					let newHeight = Math.round(entry.contentRect.height);

					// Update config only if newWidth and newHeight are greater than 0
					if (newWidth > 0 && newHeight > 0) {
						config.editorWidth = newWidth;
						config.editorHeight = newHeight;
					}
				}
			});

			// Observe the editor container
			self.editorResizeObserver.observe(editor);
		}
	},

	/**
	 * Toggles the editor mode between text and hex.
	 *
	 * @param {string} filePath - The path of the file to be edited.
	 */
	toggleHexMode(filePath) {
		const self = this;

		if (self.editorMode === 'text') {
			// Before switching to hex mode, update self.fileData from the textarea
			const textarea = document.querySelector('#editor-container textarea');
			if (textarea) {
				const content = textarea.value;
				self.fileContent = content;

				// Convert content to Uint8Array
				const encoder = new TextEncoder();
				self.fileData = encoder.encode(content);
			}
			self.editorMode = 'hex';
		} else {
			// Before switching to text mode, check if the file is textual
			if (self.textType !== 'text') {
				pop(null, E('p', _('This file is not a text file and cannot be edited in text mode.')), 'error');
				return; // Abort the toggle
			}

			// Before switching to text mode, update self.fileData from HexEditor
			if (self.hexEditorInstance) {
				const hexData = self.hexEditorInstance.getData();
				if (hexData instanceof Uint8Array) {
					self.fileData = hexData;
				} else {
					pop(null, E('p', _('Failed to retrieve data from Hex Editor.')), 'error');
					return; // Abort the toggle if data retrieval fails
				}
			}

			// Convert self.fileData to string
			const decoder = new TextDecoder();
			try {
				self.fileContent = decoder.decode(self.fileData);
			} catch (error) {
				pop(null, E('p', _('Failed to decode file data to text: %s').format(error.message)), 'error');
				return; // Abort the toggle if decoding fails
			}
			self.editorMode = 'text';
		}

		// Re-render the editor with the updated mode and content
		self.renderEditor(filePath);
	}

});