FileView.razor 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 && ShowUploadPrompt)
  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 bool ShowUploadPrompt { get; set; } = false;
  236. [Parameter] public RenderFragment<FileEntry>? ContextMenuTemplate { get; set; }
  237. [Parameter] public bool EnableContextMenu { get; set; } = false;
  238. private bool ShowContextMenu = false;
  239. private FileEntry ContextMenuItem;
  240. private string ContextMenuId = "fileManagerContextMenu";
  241. private ContextMenu? CurrentContextMenu;
  242. [Parameter] public BaseFileAccess FileAccess { get; set; }
  243. [Parameter] public Func<FileEntry, bool>? Filter { get; set; }
  244. [Parameter] public Func<FileEntry, Task>? OnEntryClicked { get; set; }
  245. [Parameter] public Func<FileEntry[], Task>? OnSelectionChanged { get; set; }
  246. [Parameter] public Func<Task>? OnNavigateUpClicked { get; set; }
  247. private bool IsLoading = false;
  248. private string LoadingText = "";
  249. private FileEntry[] Entries = Array.Empty<FileEntry>();
  250. private string Path = "/";
  251. private Dictionary<FileEntry, bool> SelectionCache = new();
  252. public FileEntry[] Selection => SelectionCache.Where(x => x.Value).Select(x => x.Key).ToArray();
  253. private bool IsAllSelected => Entries.Length != 0 && SelectionCache.Count(x => x.Value) == Entries.Length;
  254. protected override async Task OnAfterRenderAsync(bool firstRender)
  255. {
  256. if (firstRender)
  257. await Refresh();
  258. }
  259. public async Task Refresh()
  260. {
  261. IsLoading = true;
  262. LoadingText = "Loading";
  263. await InvokeAsync(StateHasChanged);
  264. // Load current directory
  265. Path = await FileAccess.GetCurrentDirectory();
  266. // Load entries
  267. LoadingText = "Loading files and folders";
  268. await InvokeAsync(StateHasChanged);
  269. Entries = await FileAccess.List();
  270. // Sort entries
  271. LoadingText = "Sorting files and folders";
  272. await InvokeAsync(StateHasChanged);
  273. if (Filter != null)
  274. {
  275. Entries = Entries
  276. .Where(x => Filter.Invoke(x))
  277. .ToArray();
  278. }
  279. Entries = Entries
  280. .GroupBy(x => x.IsFile)
  281. .OrderBy(x => x.Key)
  282. .SelectMany(x => x.OrderBy(y => y.Name))
  283. .ToArray();
  284. // Build selection cache
  285. SelectionCache.Clear();
  286. foreach (var entry in Entries)
  287. SelectionCache.Add(entry, false);
  288. if (OnSelectionChanged != null)
  289. await OnSelectionChanged.Invoke(Array.Empty<FileEntry>());
  290. IsLoading = false;
  291. await InvokeAsync(StateHasChanged);
  292. }
  293. private async Task HandleEntryClick(FileEntry entry)
  294. {
  295. if (OnEntryClicked == null)
  296. return;
  297. await OnEntryClicked.Invoke(entry);
  298. }
  299. private async Task NavigateUp()
  300. {
  301. if (OnNavigateUpClicked == null)
  302. return;
  303. await OnNavigateUpClicked.Invoke();
  304. }
  305. #region Selection
  306. private async Task ChangeSelection(FileEntry entry, bool selectionState)
  307. {
  308. SelectionCache[entry] = selectionState;
  309. await InvokeAsync(StateHasChanged);
  310. if (OnSelectionChanged != null)
  311. {
  312. await OnSelectionChanged.Invoke(SelectionCache
  313. .Where(x => x.Value)
  314. .Select(x => x.Key)
  315. .ToArray()
  316. );
  317. }
  318. }
  319. private async Task ChangeAllSelection(bool toggle)
  320. {
  321. foreach (var key in SelectionCache.Keys)
  322. SelectionCache[key] = toggle;
  323. await InvokeAsync(StateHasChanged);
  324. if (OnSelectionChanged != null)
  325. {
  326. await OnSelectionChanged.Invoke(SelectionCache
  327. .Where(x => x.Value)
  328. .Select(x => x.Key)
  329. .ToArray()
  330. );
  331. }
  332. }
  333. #endregion
  334. #region Context Menu
  335. private async Task OnContextMenuAppear(MenuAppearingEventArgs data)
  336. {
  337. ContextMenuItem = (data.Data as FileEntry)!;
  338. ShowContextMenu = true;
  339. await InvokeAsync(StateHasChanged);
  340. }
  341. private async Task OnContextMenuHide()
  342. {
  343. ShowContextMenu = false;
  344. await InvokeAsync(StateHasChanged);
  345. }
  346. public async Task HideContextMenu()
  347. {
  348. ShowContextMenu = false;
  349. await InvokeAsync(StateHasChanged);
  350. }
  351. #endregion
  352. }