FileView.razor 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. @using MoonCore.Helpers
  2. @using Moonlight.Features.FileManager.Models.Abstractions.FileAccess
  3. @using BlazorContextMenu
  4. @inject IJSRuntime JsRuntime
  5. <div class="@(IsLoading ? "table-loading" : "")">
  6. @if (IsLoading)
  7. {
  8. <div class="table-loading-message table-loading-message fs-3 fw-bold text-white">
  9. Loading...
  10. </div>
  11. }
  12. <table class="w-100 table table-row-bordered @(IsLoading ? "blur" : "table-hover") fs-6">
  13. <tbody>
  14. <tr class="text-muted">
  15. @if (ShowSelect)
  16. {
  17. <td class="w-10px align-middle">
  18. <div class="form-check">
  19. @if (IsAllSelected)
  20. {
  21. <input class="form-check-input" type="checkbox" value="1" checked="checked" @oninput="() => ChangeAllSelection(false)">
  22. }
  23. else
  24. {
  25. <input class="form-check-input" type="checkbox" value="0" @oninput="() => ChangeAllSelection(true)">
  26. }
  27. </div>
  28. </td>
  29. }
  30. <td class="w-10px"></td>
  31. <td>Name</td>
  32. @if (ShowSize)
  33. {
  34. <td class="d-none d-md-table-cell">Size</td>
  35. }
  36. @if (ShowDate)
  37. {
  38. <td class="d-none d-md-table-cell">Last modified</td>
  39. }
  40. @if (EnableContextMenu)
  41. {
  42. <td></td>
  43. }
  44. @if (AdditionTemplate != null)
  45. {
  46. <td></td>
  47. }
  48. </tr>
  49. @if (Path != "/" && ShowNavigateUp)
  50. {
  51. <tr class="fw-semibold">
  52. @if (ShowSelect)
  53. {
  54. <td class="align-middle w-10px"></td>
  55. }
  56. <td class="w-10px">
  57. <i class="bx bx-sm bx-chevrons-left"></i>
  58. </td>
  59. <td>
  60. <a href="#" @onclick:preventDefault @onclick="NavigateUp">
  61. Back to parent folder
  62. </a>
  63. </td>
  64. @if (ShowSize)
  65. {
  66. <td></td>
  67. }
  68. @if (ShowDate)
  69. {
  70. <td></td>
  71. }
  72. @if (EnableContextMenu)
  73. {
  74. <td></td>
  75. }
  76. @if (AdditionTemplate != null)
  77. {
  78. <td></td>
  79. }
  80. </tr>
  81. }
  82. @foreach (var entry in Entries)
  83. {
  84. if (EnableContextMenu)
  85. {
  86. <ContextMenuTrigger MenuId="@ContextMenuId" WrapperTag="tr" Data="entry">
  87. @if (ShowSelect)
  88. {
  89. <td class="w-10px align-middle">
  90. <div class="form-check">
  91. @if (SelectionCache.ContainsKey(entry) && SelectionCache[entry])
  92. {
  93. <input class="form-check-input" type="checkbox" value="1" checked="checked" @oninput="() => ChangeSelection(entry, false)">
  94. }
  95. else
  96. {
  97. <input class="form-check-input" type="checkbox" value="0" @oninput="() => ChangeSelection(entry, true)">
  98. }
  99. </div>
  100. </td>
  101. }
  102. <td class="align-middle w-10px">
  103. @if (entry.IsFile)
  104. {
  105. <i class="bx bx-md bxs-file-blank text-white"></i>
  106. }
  107. else
  108. {
  109. <i class="bx bx-md bxs-folder text-primary"></i>
  110. }
  111. </td>
  112. <td class="align-middle">
  113. <a href="#" @onclick:preventDefault @onclick="() => HandleEntryClick(entry)">
  114. @entry.Name
  115. </a>
  116. </td>
  117. @if (ShowSize)
  118. {
  119. <td class="align-middle d-none d-md-table-cell">
  120. @if (entry.IsFile)
  121. {
  122. @Formatter.FormatSize(entry.Size)
  123. }
  124. </td>
  125. }
  126. @if (ShowDate)
  127. {
  128. <td class="align-middle d-none d-md-table-cell">
  129. @Formatter.FormatDate(entry.LastModifiedAt)
  130. </td>
  131. }
  132. <td class="d-table-cell d-md-none">
  133. <div class="dropstart">
  134. <button class="btn btn-icon btn-secondary" data-bs-toggle="dropdown" aria-expanded="false">
  135. <i class="bx bx-sm bx-dots-horizontal"></i>
  136. </button>
  137. <div class="dropdown-menu fs-6">
  138. @if (ContextMenuTemplate != null)
  139. {
  140. @ContextMenuTemplate.Invoke(entry)
  141. }
  142. </div>
  143. </div>
  144. </td>
  145. @if (AdditionTemplate != null)
  146. {
  147. @AdditionTemplate.Invoke(entry)
  148. }
  149. </ContextMenuTrigger>
  150. }
  151. else
  152. {
  153. <tr>
  154. @if (ShowSelect)
  155. {
  156. <td class="w-10px align-middle">
  157. <div class="form-check">
  158. @if (SelectionCache.ContainsKey(entry) && SelectionCache[entry])
  159. {
  160. <input class="form-check-input" type="checkbox" value="1" checked="checked" @oninput="() => ChangeSelection(entry, false)">
  161. }
  162. else
  163. {
  164. <input class="form-check-input" type="checkbox" value="0" @oninput="() => ChangeSelection(entry, true)">
  165. }
  166. </div>
  167. </td>
  168. }
  169. <td class="align-middle w-10px">
  170. @if (entry.IsFile)
  171. {
  172. <i class="bx bx-md bxs-file-blank text-white"></i>
  173. }
  174. else
  175. {
  176. <i class="bx bx-md bxs-folder text-primary"></i>
  177. }
  178. </td>
  179. <td class="align-middle">
  180. <a href="#" @onclick:preventDefault @onclick="() => HandleEntryClick(entry)">
  181. @entry.Name
  182. </a>
  183. </td>
  184. @if (ShowSize)
  185. {
  186. <td class="align-middle d-none d-md-table-cell">
  187. @if (entry.IsFile)
  188. {
  189. @Formatter.FormatSize(entry.Size)
  190. }
  191. </td>
  192. }
  193. @if (ShowDate)
  194. {
  195. <td class="align-middle d-none d-md-table-cell">
  196. @Formatter.FormatDate(entry.LastModifiedAt)
  197. </td>
  198. }
  199. @if (AdditionTemplate != null)
  200. {
  201. @AdditionTemplate.Invoke(entry)
  202. }
  203. </tr>
  204. }
  205. }
  206. </tbody>
  207. </table>
  208. @if (Entries.Length == 0)
  209. {
  210. <div class="py-4">
  211. <IconAlert Color="primary" Title="No files and folders found" Icon="bx-cloud-upload">
  212. Drag and drop files and folders here to start uploading them or click on the upload button on the top
  213. </IconAlert>
  214. </div>
  215. }
  216. </div>
  217. @if (EnableContextMenu && ContextMenuTemplate != null)
  218. {
  219. <ContextMenu @ref="CurrentContextMenu" Id="@ContextMenuId" OnAppearing="OnContextMenuAppear" OnHiding="OnContextMenuHide">
  220. @if (ShowContextMenu)
  221. {
  222. <div class="dropdown-menu show fs-6">
  223. @ContextMenuTemplate.Invoke(ContextMenuItem)
  224. </div>
  225. }
  226. </ContextMenu>
  227. }
  228. @code
  229. {
  230. [Parameter] public RenderFragment<FileEntry>? AdditionTemplate { get; set; }
  231. [Parameter] public bool ShowSize { get; set; } = true;
  232. [Parameter] public bool ShowDate { get; set; } = true;
  233. [Parameter] public bool ShowSelect { get; set; } = true;
  234. [Parameter] public bool ShowNavigateUp { get; set; } = true;
  235. [Parameter] public RenderFragment<FileEntry>? ContextMenuTemplate { get; set; }
  236. [Parameter] public bool EnableContextMenu { get; set; } = false;
  237. private bool ShowContextMenu = false;
  238. private FileEntry ContextMenuItem;
  239. private string ContextMenuId = "fileManagerContextMenu";
  240. private ContextMenu? CurrentContextMenu;
  241. [Parameter] public BaseFileAccess FileAccess { get; set; }
  242. [Parameter] public Func<FileEntry, bool>? Filter { get; set; }
  243. [Parameter] public Func<FileEntry, Task>? OnEntryClicked { get; set; }
  244. [Parameter] public Func<FileEntry[], Task>? OnSelectionChanged { get; set; }
  245. [Parameter] public Func<Task>? OnNavigateUpClicked { get; set; }
  246. private bool IsLoading = false;
  247. private string LoadingText = "";
  248. private FileEntry[] Entries = Array.Empty<FileEntry>();
  249. private string Path = "/";
  250. private Dictionary<FileEntry, bool> SelectionCache = new();
  251. public FileEntry[] Selection => SelectionCache.Where(x => x.Value).Select(x => x.Key).ToArray();
  252. private bool IsAllSelected => Entries.Length != 0 && SelectionCache.Count(x => x.Value) == Entries.Length;
  253. protected override async Task OnAfterRenderAsync(bool firstRender)
  254. {
  255. if (firstRender)
  256. await Refresh();
  257. }
  258. public async Task Refresh()
  259. {
  260. IsLoading = true;
  261. LoadingText = "Loading";
  262. await InvokeAsync(StateHasChanged);
  263. // Load current directory
  264. Path = await FileAccess.GetCurrentDirectory();
  265. // Load entries
  266. LoadingText = "Loading files and folders";
  267. await InvokeAsync(StateHasChanged);
  268. Entries = await FileAccess.List();
  269. // Sort entries
  270. LoadingText = "Sorting files and folders";
  271. await InvokeAsync(StateHasChanged);
  272. if (Filter != null)
  273. {
  274. Entries = Entries
  275. .Where(x => Filter.Invoke(x))
  276. .ToArray();
  277. }
  278. Entries = Entries
  279. .GroupBy(x => x.IsFile)
  280. .OrderBy(x => x.Key)
  281. .SelectMany(x => x.OrderBy(y => y.Name))
  282. .ToArray();
  283. // Build selection cache
  284. SelectionCache.Clear();
  285. foreach (var entry in Entries)
  286. SelectionCache.Add(entry, false);
  287. if (OnSelectionChanged != null)
  288. await OnSelectionChanged.Invoke(Array.Empty<FileEntry>());
  289. IsLoading = false;
  290. await InvokeAsync(StateHasChanged);
  291. }
  292. private async Task HandleEntryClick(FileEntry entry)
  293. {
  294. if (OnEntryClicked == null)
  295. return;
  296. await OnEntryClicked.Invoke(entry);
  297. }
  298. private async Task NavigateUp()
  299. {
  300. if (OnNavigateUpClicked == null)
  301. return;
  302. await OnNavigateUpClicked.Invoke();
  303. }
  304. #region Selection
  305. private async Task ChangeSelection(FileEntry entry, bool selectionState)
  306. {
  307. SelectionCache[entry] = selectionState;
  308. await InvokeAsync(StateHasChanged);
  309. if (OnSelectionChanged != null)
  310. {
  311. await OnSelectionChanged.Invoke(SelectionCache
  312. .Where(x => x.Value)
  313. .Select(x => x.Key)
  314. .ToArray()
  315. );
  316. }
  317. }
  318. private async Task ChangeAllSelection(bool toggle)
  319. {
  320. foreach (var key in SelectionCache.Keys)
  321. SelectionCache[key] = toggle;
  322. await InvokeAsync(StateHasChanged);
  323. if (OnSelectionChanged != null)
  324. {
  325. await OnSelectionChanged.Invoke(SelectionCache
  326. .Where(x => x.Value)
  327. .Select(x => x.Key)
  328. .ToArray()
  329. );
  330. }
  331. }
  332. #endregion
  333. #region Context Menu
  334. private async Task OnContextMenuAppear(MenuAppearingEventArgs data)
  335. {
  336. ContextMenuItem = (data.Data as FileEntry)!;
  337. ShowContextMenu = true;
  338. await InvokeAsync(StateHasChanged);
  339. }
  340. private async Task OnContextMenuHide()
  341. {
  342. ShowContextMenu = false;
  343. await InvokeAsync(StateHasChanged);
  344. }
  345. public async Task HideContextMenu()
  346. {
  347. ShowContextMenu = false;
  348. await InvokeAsync(StateHasChanged);
  349. }
  350. #endregion
  351. }