Remove old v2 structure
This commit is contained in:
@@ -1,427 +0,0 @@
|
||||
@using Moonlight.Features.FileManager.Models.Abstractions.FileAccess
|
||||
@using MoonCoreUI.Services
|
||||
@using MoonCore.Helpers
|
||||
@using Moonlight.Features.FileManager.Services
|
||||
|
||||
@inject ToastService ToastService
|
||||
@inject AlertService AlertService
|
||||
@inject SharedFileAccessService SharedFileAccessService
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
@implements IDisposable
|
||||
|
||||
<LazyLoader @ref="LazyLoader" Load="Load">
|
||||
<table class="w-100 table table-responsive table-row-bordered">
|
||||
<tbody>
|
||||
|
||||
@if (ShowHeader)
|
||||
{
|
||||
<tr>
|
||||
@if (ShowSelect)
|
||||
{
|
||||
<td class="w-10px align-middle">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" @oninput="args => ToggleAll(args)">
|
||||
</div>
|
||||
</td>
|
||||
}
|
||||
@if (ShowIcons)
|
||||
{
|
||||
<td></td>
|
||||
}
|
||||
<td class="align-middle fs-6 text-muted">
|
||||
Name
|
||||
</td>
|
||||
@if (ShowSize)
|
||||
{
|
||||
<td class="align-middle fs-6 text-muted d-none d-sm-table-cell text-end">
|
||||
Size
|
||||
</td>
|
||||
}
|
||||
@if (ShowLastModified)
|
||||
{
|
||||
<td class="align-middle fs-6 text-muted d-none d-sm-table-cell text-end">
|
||||
Last modified at
|
||||
</td>
|
||||
}
|
||||
@if (SelectedEntries.Count == 0)
|
||||
{
|
||||
<td></td>
|
||||
}
|
||||
else
|
||||
{
|
||||
<td class="w-50 fs-6 text-end">
|
||||
<span class="text-primary">@SelectedEntries.Count</span> element(s) selected
|
||||
<div class="ms-2 btn-group">
|
||||
<WButton OnClick="() => Delete(SelectedEntries.ToArray())" CssClasses="btn btn-icon btn-danger">
|
||||
<i class="text-white bx bx-sm bx-trash"></i>
|
||||
</WButton>
|
||||
</div>
|
||||
</td>
|
||||
}
|
||||
</tr>
|
||||
}
|
||||
|
||||
@if (ShowGoUp && Path != "/" && !DisableNavigation)
|
||||
{
|
||||
<tr>
|
||||
@if (ShowSelect)
|
||||
{
|
||||
<td class="w-10px align-middle">
|
||||
</td>
|
||||
}
|
||||
@if (ShowIcons)
|
||||
{
|
||||
<td class="w-10px align-middle">
|
||||
</td>
|
||||
}
|
||||
<td class="align-middle fs-6">
|
||||
@{
|
||||
var upPath = "..";
|
||||
}
|
||||
|
||||
<a href="#"
|
||||
@onclick:preventDefault
|
||||
@onclick="() => Navigate(upPath)">
|
||||
Go up
|
||||
</a>
|
||||
</td>
|
||||
@if (ShowSize)
|
||||
{
|
||||
<td class="align-middle fs-6 d-none d-sm-table-cell text-end">
|
||||
<span>-</span>
|
||||
</td>
|
||||
}
|
||||
@if (ShowLastModified)
|
||||
{
|
||||
<td class="align-middle fs-6 d-none d-sm-table-cell text-end">
|
||||
-
|
||||
</td>
|
||||
}
|
||||
@if (ShowActions)
|
||||
{
|
||||
<td class="w-50 text-end">
|
||||
</td>
|
||||
}
|
||||
</tr>
|
||||
}
|
||||
|
||||
@foreach (var entry in Entries)
|
||||
{
|
||||
<tr>
|
||||
@if (ShowSelect)
|
||||
{
|
||||
<td class="w-10px align-middle">
|
||||
<div class="form-check">
|
||||
@if (SelectedEntries.Contains(entry))
|
||||
{
|
||||
<input class="form-check-input" type="checkbox" value="1" checked="checked" @oninput="args => HandleSelected(entry, args)">
|
||||
}
|
||||
else
|
||||
{
|
||||
<input class="form-check-input" type="checkbox" value="0" @oninput="args => HandleSelected(entry, args)">
|
||||
}
|
||||
</div>
|
||||
</td>
|
||||
}
|
||||
@if (ShowIcons)
|
||||
{
|
||||
<td class="w-10px align-middle">
|
||||
@if (entry.IsFile)
|
||||
{
|
||||
<i class="bx bx-md bx-file"></i>
|
||||
}
|
||||
else
|
||||
{
|
||||
<i class="bx bx-md bx-folder"></i>
|
||||
}
|
||||
</td>
|
||||
}
|
||||
<td class="align-middle fs-6">
|
||||
@if (DisableNavigation)
|
||||
{
|
||||
<span>@(entry.Name)</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<a href="#"
|
||||
@onclick:preventDefault
|
||||
@onclick="() => HandleClick(entry)">
|
||||
@(entry.Name)
|
||||
</a>
|
||||
}
|
||||
</td>
|
||||
@if (ShowSize)
|
||||
{
|
||||
<td class="align-middle fs-6 d-none d-sm-table-cell text-end">
|
||||
@if (entry.IsFile)
|
||||
{
|
||||
@(Formatter.FormatSize(entry.Size))
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>-</span>
|
||||
}
|
||||
</td>
|
||||
}
|
||||
@if (ShowLastModified)
|
||||
{
|
||||
<td class="align-middle fs-6 d-none d-sm-table-cell text-end">
|
||||
@(Formatter.FormatDate(entry.LastModifiedAt))
|
||||
</td>
|
||||
}
|
||||
@if (ShowActions)
|
||||
{
|
||||
<td class="w-50 text-end">
|
||||
<div class="btn-group">
|
||||
<WButton OnClick="() => Delete(entry)" CssClasses="btn btn-icon btn-danger">
|
||||
<i class="text-white bx bx-sm bx-trash"></i>
|
||||
</WButton>
|
||||
|
||||
<div class="dropdown">
|
||||
<button class="btn btn-icon btn-secondary rounded-start-0" type="button" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
<i class="text-white bx bx-sm bx-dots-horizontal-rounded"></i>
|
||||
</button>
|
||||
<ul class="dropdown-menu">
|
||||
<li>
|
||||
<a href="#" @onclick:preventDefault @onclick="() => Rename(entry)" class="dropdown-item">Rename</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="#" @onclick:preventDefault @onclick="() => Download(entry)" class="dropdown-item">Download</a>
|
||||
</li>
|
||||
@if (OnMoveRequested != null)
|
||||
{
|
||||
<li>
|
||||
<a href="#" @onclick:preventDefault @onclick="() => RequestMove(entry)" class="dropdown-item">Move</a>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
}
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</LazyLoader>
|
||||
|
||||
|
||||
@code
|
||||
{
|
||||
[Parameter] public IFileAccess FileAccess { get; set; }
|
||||
|
||||
[Parameter] public Func<FileEntry, bool>? Filter { get; set; }
|
||||
[Parameter] public bool ShowSize { get; set; } = true;
|
||||
[Parameter] public bool ShowLastModified { get; set; } = true;
|
||||
[Parameter] public bool ShowIcons { get; set; } = true;
|
||||
[Parameter] public bool ShowActions { get; set; } = true;
|
||||
[Parameter] public bool ShowSelect { get; set; } = true;
|
||||
[Parameter] public bool ShowGoUp { get; set; } = true;
|
||||
[Parameter] public bool ShowHeader { get; set; } = true;
|
||||
[Parameter] public bool DisableNavigation { get; set; } = false;
|
||||
[Parameter] public Func<FileEntry, Task>? OnFileClicked { get; set; }
|
||||
[Parameter] public Func<Task>? OnSelectionChanged { get; set; }
|
||||
[Parameter] public Func<string, Task>? OnPathChanged { get; set; }
|
||||
[Parameter] public Func<FileEntry, Task>? OnMoveRequested { get; set; }
|
||||
|
||||
public readonly List<FileEntry> SelectedEntries = new();
|
||||
|
||||
private LazyLoader LazyLoader;
|
||||
private FileEntry[] Entries;
|
||||
private string Path = "/";
|
||||
|
||||
private async Task Load(LazyLoader lazyLoader)
|
||||
{
|
||||
await lazyLoader.SetText("Loading files and folders");
|
||||
|
||||
// Load all entries
|
||||
Entries = await FileAccess.List();
|
||||
|
||||
await lazyLoader.SetText("Sorting files and folders");
|
||||
|
||||
// Perform sorting and filtering
|
||||
if (Filter != null)
|
||||
{
|
||||
Entries = Entries
|
||||
.Where(x => Filter.Invoke(x))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
Entries = Entries
|
||||
.GroupBy(x => x.IsFile)
|
||||
.OrderBy(x => x.Key)
|
||||
.SelectMany(x => x.OrderBy(y => y.Name))
|
||||
.ToArray();
|
||||
|
||||
SelectedEntries.Clear();
|
||||
|
||||
Path = await FileAccess.GetCurrentDirectory();
|
||||
|
||||
if (OnPathChanged != null)
|
||||
await OnPathChanged.Invoke(Path);
|
||||
}
|
||||
|
||||
private async Task HandleClick(FileEntry fileEntry)
|
||||
{
|
||||
if (fileEntry.IsDirectory && !DisableNavigation)
|
||||
{
|
||||
await Navigate(fileEntry.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (OnFileClicked != null)
|
||||
await OnFileClicked.Invoke(fileEntry);
|
||||
}
|
||||
}
|
||||
|
||||
#region Actions
|
||||
|
||||
private async Task Delete(params FileEntry[] entries)
|
||||
{
|
||||
if (entries.Length == 0)
|
||||
return;
|
||||
|
||||
var toastId = "fileDelete" + GetHashCode();
|
||||
await ToastService.CreateProgress(toastId, $"[0/{entries.Length}] Deleting items");
|
||||
|
||||
int i = 0;
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
await ToastService.ModifyProgress(toastId, $"[{i + 1}/{entries.Length}] Deleting '{entry.Name}'");
|
||||
|
||||
await FileAccess.Delete(entry.Name);
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
await ToastService.RemoveProgress(toastId);
|
||||
await ToastService.Success($"Successfully deleted {i} item(s)");
|
||||
|
||||
await LazyLoader.Reload();
|
||||
}
|
||||
|
||||
private async Task Rename(FileEntry fileEntry)
|
||||
{
|
||||
var name = await AlertService.Text($"Rename '{fileEntry.Name}'", "", fileEntry.Name);
|
||||
|
||||
if (string.IsNullOrEmpty(name))
|
||||
return;
|
||||
|
||||
await FileAccess.Move(fileEntry.Name, name);
|
||||
|
||||
await LazyLoader.Reload();
|
||||
}
|
||||
|
||||
private async Task RequestMove(FileEntry fileEntry)
|
||||
{
|
||||
if (OnMoveRequested == null)
|
||||
return;
|
||||
|
||||
await OnMoveRequested.Invoke(fileEntry);
|
||||
}
|
||||
|
||||
private async Task Download(FileEntry fileEntry)
|
||||
{
|
||||
try
|
||||
{
|
||||
await SharedFileAccessService.Register(FileAccess);
|
||||
var token = await SharedFileAccessService.GenerateToken(FileAccess);
|
||||
var url = $"/api/download?token={token}&name={fileEntry.Name}";
|
||||
|
||||
await ToastService.Info("Starting download...");
|
||||
Navigation.NavigateTo(url, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Warn("Unable to start download");
|
||||
Logger.Warn(e);
|
||||
|
||||
await ToastService.Danger("Failed to start download");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Selection
|
||||
|
||||
private async Task HandleSelected(FileEntry fileEntry, ChangeEventArgs args)
|
||||
{
|
||||
if (args.Value == null) // This should never be called. Still i want to handle it
|
||||
return;
|
||||
|
||||
if (args.Value.ToString() == "True")
|
||||
{
|
||||
if (!SelectedEntries.Contains(fileEntry))
|
||||
SelectedEntries.Add(fileEntry);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SelectedEntries.Contains(fileEntry))
|
||||
SelectedEntries.Remove(fileEntry);
|
||||
}
|
||||
|
||||
if (OnSelectionChanged != null)
|
||||
await OnSelectionChanged.Invoke();
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private async Task ToggleAll(ChangeEventArgs args)
|
||||
{
|
||||
if (args.Value == null)
|
||||
return;
|
||||
|
||||
if (args.Value.ToString() == "True")
|
||||
{
|
||||
foreach (var entry in Entries)
|
||||
{
|
||||
if (!SelectedEntries.Contains(entry))
|
||||
SelectedEntries.Add(entry);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedEntries.Clear();
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Navigation
|
||||
|
||||
public async Task Navigate(string name)
|
||||
{
|
||||
await LazyLoader.Reload(async loader =>
|
||||
{
|
||||
await loader.SetText("Switching directory on target");
|
||||
await FileAccess.ChangeDirectory(name);
|
||||
|
||||
if (OnPathChanged != null)
|
||||
await OnPathChanged.Invoke(await FileAccess.GetCurrentDirectory());
|
||||
});
|
||||
}
|
||||
|
||||
public async Task NavigateToPath(string path)
|
||||
{
|
||||
await LazyLoader.Reload(async loader =>
|
||||
{
|
||||
await loader.SetText("Switching directory on target");
|
||||
await FileAccess.SetDirectory(path);
|
||||
|
||||
if (OnPathChanged != null)
|
||||
await OnPathChanged.Invoke(await FileAccess.GetCurrentDirectory());
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public async Task Refresh() => await LazyLoader.Reload();
|
||||
|
||||
public async void Dispose()
|
||||
{
|
||||
await SharedFileAccessService.Unregister(FileAccess);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user