Implements directory browser dialog, part 4: back/forward/up navigation

This commit is contained in:
BinaryAlley committed 2024-09-21 13:44:34 +03:00
1 parent c29d829c91
commit beb66ac65e
7 files changed
+167 -30

No files matched your search

@@ -48,6 +48,7 @@ internal class DirectoryProviderService : IDirectoryProviderService
return Errors.Permission.UnauthorizedAccess; return Errors.Permission.UnauthorizedAccess;
return ErrorOrFactory.From(_fileSystem.Directory.GetDirectories(path.Path) return ErrorOrFactory.From(_fileSystem.Directory.GetDirectories(path.Path)
.OrderBy(path => path) .OrderBy(path => path)
.Select(path => path.EndsWith(_fileSystem.Path.DirectorySeparatorChar) ? path : path + _fileSystem.Path.DirectorySeparatorChar)
.Select(path => FileSystemPathId.Create(path)) .Select(path => FileSystemPathId.Create(path))
.Where(errorOrPathId => !errorOrPathId.IsError) .Where(errorOrPathId => !errorOrPathId.IsError)
.Select(errorOrPathId => errorOrPathId.Value) .Select(errorOrPathId => errorOrPathId.Value)
@@ -72,7 +73,8 @@ internal class DirectoryProviderService : IDirectoryProviderService
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a file name or an error.</returns> /// <returns>An <see cref="ErrorOr{TValue}"/> containing either a file name or an error.</returns>
public ErrorOr<string> GetFileName(FileSystemPathId path) public ErrorOr<string> GetFileName(FileSystemPathId path)
{ {
return _fileSystem.Path.GetFileName(path.Path); string inputPath = path.Path.EndsWith(_fileSystem.Path.DirectorySeparatorChar) ? path.Path[..^1] : path.Path;
return _fileSystem.Path.GetFileName(inputPath);
} }
/// <summary> /// <summary>
@@ -82,7 +82,7 @@ public class UnixPathStrategy : IUnixPathStrategy
// if the name begins with a directory separator, remove it // if the name begins with a directory separator, remove it
name = name.TrimStart(PathSeparator); name = name.TrimStart(PathSeparator);
// combine the two parts with the Unix directory separator character // combine the two parts with the Unix directory separator character
return FileSystemPathId.Create(subpath + PathSeparator + name); return FileSystemPathId.Create(subpath + PathSeparator + name + PathSeparator);
} }
/// <summary> /// <summary>
@@ -121,7 +121,7 @@ public class UnixPathStrategy : IUnixPathStrategy
return Errors.FileManagement.InvalidPath; return Errors.FileManagement.InvalidPath;
// trim trailing slash for consistent processing // trim trailing slash for consistent processing
string tempPath = path.Path; string tempPath = path.Path;
if (tempPath.EndsWith("/")) if (tempPath.EndsWith('/'))
tempPath = tempPath.TrimEnd(PathSeparator); tempPath = tempPath.TrimEnd(PathSeparator);
// find the last occurrence of a slash // find the last occurrence of a slash
var lastIndex = tempPath.LastIndexOf(PathSeparator); var lastIndex = tempPath.LastIndexOf(PathSeparator);
@@ -81,7 +81,7 @@ public class WindowsPathStrategy : IWindowsPathStrategy
// if the name begins with a directory separator, remove it // if the name begins with a directory separator, remove it
name = name.TrimStart(PathSeparator); name = name.TrimStart(PathSeparator);
// combine the two parts with the Windows directory separator character // combine the two parts with the Windows directory separator character
return FileSystemPathId.Create(subpath + PathSeparator + name); return FileSystemPathId.Create(subpath + PathSeparator + name + PathSeparator);
} }
/// <summary> /// <summary>
@@ -33,9 +33,9 @@
</div> </div>
<div id="navigator-navigation-controls-container"> <div id="navigator-navigation-controls-container">
<div id="navigator-history"> <div id="navigator-history">
<img src="images/ui/back.svg" class="navigation-icon" alt="Back" title="Back" /> <img src="images/ui/back.svg" class="navigation-icon" alt="Back" title="Back" @onclick="NavigateBackAsync" />
<img src="images/ui/forward.svg" class="navigation-icon" alt="Forward" title="Forward" /> <img src="images/ui/forward.svg" class="navigation-icon" alt="Forward" title="Forward" @onclick="NavigateForwardAsync" />
<img src="images/ui/up.svg" class="navigation-icon" alt="Up One Level" title="Up One Level" /> <img src="images/ui/up.svg" class="navigation-icon" alt="Up One Level" title="Up One Level" @onclick="NavigateUpAsync" />
</div> </div>
<img src="images/ui/toggle-tree-view.svg" id="navigator-toggle-tree-view-button" alt="Toggle Tree View" title="Toggle Tree View" class="navigator-icon" /> <img src="images/ui/toggle-tree-view.svg" id="navigator-toggle-tree-view-button" alt="Toggle Tree View" title="Toggle Tree View" class="navigator-icon" />
<img src="images/ui/toggle-thumbnails.svg" id="navigator-toggle-thumbnails-button" alt="Toggle Thumbnails" title="Toggle Thumbanils" class="navigator-icon" /> <img src="images/ui/toggle-thumbnails.svg" id="navigator-toggle-thumbnails-button" alt="Toggle Thumbnails" title="Toggle Thumbanils" class="navigator-icon" />
@@ -98,7 +98,7 @@
<div class="treeview"> <div class="treeview">
@foreach (FileSystemTreeNodeModel node in treeViewRootNodes) @foreach (FileSystemTreeNodeModel node in treeViewRootNodes)
{ {
<FileSystemTreeViewNode Node="node" OnExpandToggle="ToggleTreeViewNode" OnNodeSelected="NavigateToPathAsync" selectedNodePath="@selectedNodePath" /> <FileSystemTreeViewNode Node="node" OnExpandToggle="ToggleTreeViewNode" OnNodeSelected="HandleFileSystemTreeViewNodeSelectedAsync" SelectedNodePath="@selectedNodePath" />
} }
</div> </div>
</div> </div>
@@ -165,7 +165,7 @@
<div class="navigator-dropdown opened" id="navigatorDropdown" data-path-segment-id="path-segment-@expandedPathSegmentIndex"> <div class="navigator-dropdown opened" id="navigatorDropdown" data-path-segment-id="path-segment-@expandedPathSegmentIndex">
@foreach (var directory in pathSegmentsSubdirectoriesCache[expandedPathSegmentIndex.Value]) @foreach (var directory in pathSegmentsSubdirectoriesCache[expandedPathSegmentIndex.Value])
{ {
<div class="navigator-option" data-path="@directory.Path" @onclick="@(() => NavigateToPathAsync(directory.Path))">@directory.Name</div> <div class="navigator-option" data-path="@directory.Path" @onclick="@(() => NavigateToPathAsync(directory.Path, isForwardNavigation: false, isBackwardNavigation: false))">@directory.Name</div>
} }
</div> </div>
} }
@@ -190,6 +190,8 @@
private FileSystemIconSize iconSize; private FileSystemIconSize iconSize;
private Dictionary<int, List<FileSystemTreeNodeModel>> pathSegmentsSubdirectoriesCache = []; private Dictionary<int, List<FileSystemTreeNodeModel>> pathSegmentsSubdirectoriesCache = [];
private DotNetObjectReference<FileSystemBrowser>? reference; private DotNetObjectReference<FileSystemBrowser>? reference;
private readonly Stack<string> undoStack = new();
private readonly Stack<string> redoStack = new();
/// <summary> /// <summary>
/// Initializes the component. /// Initializes the component.
@@ -198,7 +200,11 @@
{ {
await GetFileSystemPropertiesAsync().ConfigureAwait(false); await GetFileSystemPropertiesAsync().ConfigureAwait(false);
if (Path is not null) if (Path is not null)
await NavigateToPathAsync(Path).ConfigureAwait(false); {
if (!Path.EndsWith(pathSeparator))
Path += pathSeparator;
await NavigateToPathAsync(Path, isForwardNavigation: false, isBackwardNavigation: false).ConfigureAwait(false);
}
await ChangeViewModeAsync(viewMode).ConfigureAwait(false); await ChangeViewModeAsync(viewMode).ConfigureAwait(false);
// make the navigator path segments horizontally scrollable // make the navigator path segments horizontally scrollable
await JSRuntime.InvokeVoidAsync("addHorizontalScrolling", "navigator-address-bar").ConfigureAwait(false); await JSRuntime.InvokeVoidAsync("addHorizontalScrolling", "navigator-address-bar").ConfigureAwait(false);
@@ -311,13 +317,14 @@
for (int i = 1; i <= toggledIndex; i++) for (int i = 1; i <= toggledIndex; i++)
concatenatedPath = $"{(concatenatedPath != pathSeparator ? concatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}"; concatenatedPath = $"{(concatenatedPath != pathSeparator ? concatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}";
string parentConcatenatedPath = concatenatedPath; string parentConcatenatedPath = concatenatedPath;
// fetch directories for the expanded path segment
try try
{ {
if (concatenatedPath != pathSeparator) // on UNIX, dont get the parent, if we are at root path if (concatenatedPath != pathSeparator) // on UNIX, dont get the parent, if we are at root path
{ {
List<PathSegmentModel> parentLocation = await _apiHttpClient.GetAsync<List<PathSegmentModel>>($"path/get-path-parent?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false); PathSegmentModel[] parentLocationResponse = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/get-path-parent?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false);
parentConcatenatedPath = parentLocation[0].Path; parentConcatenatedPath = parentLocationResponse[0].Path;
for (int i = 1; i < parentLocation.Count; i++) for (int i = 1; i < parentLocationResponse.Length; i++)
parentConcatenatedPath = $"{(parentConcatenatedPath != pathSeparator ? parentConcatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}"; parentConcatenatedPath = $"{(parentConcatenatedPath != pathSeparator ? parentConcatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}";
} }
} }
@@ -327,11 +334,8 @@
} }
if (!concatenatedPath.EndsWith(pathSeparator)) if (!concatenatedPath.EndsWith(pathSeparator))
concatenatedPath += pathSeparator; concatenatedPath += pathSeparator;
// fetch directories for the expanded path segment // add current and parent directories too
List<FileSystemTreeNodeModel> directories = [ List<FileSystemTreeNodeModel> directories = [ new() { Path = concatenatedPath, Name = ".", }, new() { Path = parentConcatenatedPath, Name = ".." } ];
new() { Path = concatenatedPath, Name = ".", }, // current directory
new() { Path = parentConcatenatedPath, Name = ".." } // parent directory
];
// add the rest of the subdirectories of the expanded path node // add the rest of the subdirectories of the expanded path node
await foreach (FileSystemTreeNodeModel? directory in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"directories/get-tree-directories?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false)) await foreach (FileSystemTreeNodeModel? directory in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"directories/get-tree-directories?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false))
if (directory is not null) if (directory is not null)
@@ -356,19 +360,122 @@
/// Handles the selection of a node in the tree view. /// Handles the selection of a node in the tree view.
/// </summary> /// </summary>
/// <param name="path">The path of the selected node.</param> /// <param name="path">The path of the selected node.</param>
private async Task NavigateToPathAsync(string path) /// <param name="isForwardNavigation">Indicates whether the navigation is towards a location in the redo stack or not.</param>
/// <param name="isBackwardNavigation">Indicates whether the navigation is towards a location in the undo stack or not.</param>
private async Task NavigateToPathAsync(string path, bool isForwardNavigation, bool isBackwardNavigation)
{ {
selectedNodePath = path;
if (await CheckPathAsync(path).ConfigureAwait(false)) if (await CheckPathAsync(path).ConfigureAwait(false))
{ {
_path = path; // check if backward navigation is possible
if (isBackwardNavigation)
{
if (undoStack.Count > 0)
{
// get the first path in the backward navigation list
string backwardPath = undoStack.Pop();
// put the current path in the forward navigationlist, before navigating to the backward path
if (Path is not null)
{
if (!Path.EndsWith(pathSeparator))
Path += pathSeparator;
redoStack.Push(Path);
}
path = backwardPath;
}
else
return;
}
else if (isForwardNavigation) // check if forward navigation is possible
{
if (redoStack.Count > 0)
{
// get the first path in the forward navigation list
string forwardPath = redoStack.Pop();
// put the current path in the backward navigation list, before navigating to the forward path
if (Path is not null)
{
if (!Path.EndsWith(pathSeparator))
Path += pathSeparator;
if (undoStack.Count == 0 || (undoStack.Count > 0 && undoStack.Peek() != Path))
undoStack.Push(Path);
}
path = forwardPath;
}
else
return;
}
else // navigation to a brand new location (up, directory browse, manual path edit, etc)
{
redoStack.Clear();
if (Path is not null && Path != path) // when navigating up, Path will be set after navigation
{
if (undoStack.Count == 0 || (undoStack.Count > 0 && undoStack.Peek() != Path))
undoStack.Push(Path);
}
else if (_path is not null && _path != path) // when editing the address, Path already comes set, check against control value
{
if (undoStack.Count == 0 || (undoStack.Count > 0 && undoStack.Peek() != _path))
undoStack.Push(_path);
}
}
_path = path; // we are about to navigate to the new path, store its value in the control variable
await GetNavigatorPathAsync(path).ConfigureAwait(false); await GetNavigatorPathAsync(path).ConfigureAwait(false);
await GetFileSystemTreeAsync(path).ConfigureAwait(false); await GetFileSystemTreeAsync(path).ConfigureAwait(false);
await GetFileSystemItemsAsync(path).ConfigureAwait(false); await GetFileSystemItemsAsync(path).ConfigureAwait(false);
} }
else selectedNodePath = _path;
Path = _path; Path = _path; // also update the navigation input address
if (Path is not null && !Path.EndsWith(pathSeparator))
Path += pathSeparator;
await OnNodeSelected.InvokeAsync(path).ConfigureAwait(false); await OnNodeSelected.InvokeAsync(path).ConfigureAwait(false);
StateHasChanged();
}
/// <summary>
/// Provides backwards navigation.
/// </summary>
private async Task NavigateBackAsync()
{
if (Path is not null)
await NavigateToPathAsync(Path, isForwardNavigation: false, isBackwardNavigation: true).ConfigureAwait(false);
}
/// <summary>
/// Provides forwards navigation.
/// </summary>
private async Task NavigateForwardAsync()
{
if (Path is not null)
await NavigateToPathAsync(Path, isForwardNavigation: true, isBackwardNavigation: false).ConfigureAwait(false);
}
/// <summary>
/// Provides upwards navigation.
/// </summary>
private async Task NavigateUpAsync()
{
try
{
if (Path is not null)
{
// ask the API for the parent path
PathSegmentModel[] parentLocationResponse = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/get-path-parent?path={Uri.EscapeDataString(Path)}").ConfigureAwait(false);
// if the answer was valid, and we can navigate up, reconstruct the provided path from its segments, and navigate to it
if (parentLocationResponse is not null)
{
string parentConcatenatedPath = parentLocationResponse[0].Path;
for (int i = 1; i < parentLocationResponse.Length; i++)
parentConcatenatedPath = $"{(parentConcatenatedPath != pathSeparator ? parentConcatenatedPath : string.Empty)}{pathSeparator}{parentLocationResponse[i].Path}";
if (!parentConcatenatedPath.EndsWith(pathSeparator))
parentConcatenatedPath += pathSeparator;
await NavigateToPathAsync(parentConcatenatedPath, isForwardNavigation: false, isBackwardNavigation: false).ConfigureAwait(false);
}
}
}
catch (ApiException ex)
{
HandleApiException(ex);
}
} }
/// <summary> /// <summary>
@@ -396,6 +503,8 @@
{ {
_notificationService.Show("Specified path is not valid!", NotificationType.Error); // TODO: should ask error message from server when translation is implemented _notificationService.Show("Specified path is not valid!", NotificationType.Error); // TODO: should ask error message from server when translation is implemented
Path = _path; Path = _path;
if (Path is not null && !Path.EndsWith(pathSeparator))
Path += pathSeparator;
} }
PathExistsModel pathExistsResponse = await _apiHttpClient.GetAsync<PathExistsModel>($"path/check-path-exists?path={Uri.EscapeDataString(path)}").ConfigureAwait(false); PathExistsModel pathExistsResponse = await _apiHttpClient.GetAsync<PathExistsModel>($"path/check-path-exists?path={Uri.EscapeDataString(path)}").ConfigureAwait(false);
if (!pathExistsResponse.Exists) if (!pathExistsResponse.Exists)
@@ -447,7 +556,6 @@
}); });
} }
// if there was an initial path saved from a previous session, load the full tree up to that initial path // if there was an initial path saved from a previous session, load the full tree up to that initial path
// ask the whole tree for the saved path
List<FileSystemTreeNodeModel> initialTree = await _apiHttpClient.GetAsync<List<FileSystemTreeNodeModel>>($"directories/get-directory-tree?path={Uri.EscapeDataString(path!)}&includeFiles={IncludeFiles}").ConfigureAwait(false); List<FileSystemTreeNodeModel> initialTree = await _apiHttpClient.GetAsync<List<FileSystemTreeNodeModel>>($"directories/get-directory-tree?path={Uri.EscapeDataString(path!)}&includeFiles={IncludeFiles}").ConfigureAwait(false);
if (initialTree is not null && initialTree.Count > 0) if (initialTree is not null && initialTree.Count > 0)
{ {
@@ -550,7 +658,27 @@
isNavigationPathEditMode = false; isNavigationPathEditMode = false;
StateHasChanged(); StateHasChanged();
if (Path is not null) if (Path is not null)
await NavigateToPathAsync(Path).ConfigureAwait(false); {
if (!Path.EndsWith(pathSeparator))
Path += pathSeparator;
await NavigateToPathAsync(Path, isForwardNavigation: false, isBackwardNavigation: false).ConfigureAwait(false);
}
}
/// <summary>
/// Handles the selection of a node in the file system tree view.
/// </summary>
/// <param name="path">The path of the newly selected node.</param>
private async Task HandleFileSystemTreeViewNodeSelectedAsync(string path)
{
StateHasChanged();
if (selectedNodePath is not null)
{
Path = path;
if (!Path.EndsWith(pathSeparator))
Path += pathSeparator;
await NavigateToPathAsync(Path, isForwardNavigation: false, isBackwardNavigation: false).ConfigureAwait(false);
}
} }
/// <summary> /// <summary>
@@ -1,4 +1,5 @@
@using Lumina.Presentation.Web.Common.Models.FileManagement @using Lumina.Presentation.Web.Common.Models.FileManagement
<div id="navigator-view-mode-container"> <div id="navigator-view-mode-container">
<img src="images/ui/list.png" id="navigator-list-view" alt="List View" title="List View" /> <img src="images/ui/list.png" id="navigator-list-view" alt="List View" title="List View" />
<img src="images/ui/details.png" id="navigator-details-view" alt="Details View" title="Details View" /> <img src="images/ui/details.png" id="navigator-details-view" alt="Details View" title="Details View" />
@@ -48,13 +49,8 @@
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
if (Path is not null && PathSeparator is not null && FileSystemType is not null) if (Path is not null && PathSeparator is not null && FileSystemType is not null)
{
if (!Path.EndsWith(PathSeparator)) if (!Path.EndsWith(PathSeparator))
Path += PathSeparator; Path += PathSeparator;
Console.WriteLine("param path:" + Path);
Console.WriteLine("param PathSeparator:" + PathSeparator);
Console.WriteLine("param FileSystemType:" + FileSystemType?.PlatformType);
}
await base.OnParametersSetAsync().ConfigureAwait(false); await base.OnParametersSetAsync().ConfigureAwait(false);
} }
} }
@@ -46,7 +46,9 @@
/// </summary> /// </summary>
private async Task HandleNodeClick() private async Task HandleNodeClick()
{ {
Console.WriteLine("===node selected: " + Node.Path + "; selected node path: " + SelectedNodePath + "; is selected: " + IsSelected);
await OnNodeSelected.InvokeAsync(Node.Path).ConfigureAwait(false); await OnNodeSelected.InvokeAsync(Node.Path).ConfigureAwait(false);
StateHasChanged();
} }
/// <summary> /// <summary>
@@ -132,6 +132,10 @@ window.addEventListener('resize', function () {
} }
}); });
/**
* Attaches a click event listener to the window, invoking a .NET method when the window is clicked.
* @param {any} dotnetHelper - .NET object to invoke methods asynchronously.
*/
window.windowClickHandler = (dotnetHelper) => { window.windowClickHandler = (dotnetHelper) => {
window.addEventListener('click', (event) => { window.addEventListener('click', (event) => {
dotnetHelper.invokeMethodAsync('OnWindowClick', { dotnetHelper.invokeMethodAsync('OnWindowClick', {
@@ -142,6 +146,11 @@ window.windowClickHandler = (dotnetHelper) => {
}); });
}; };
/**
* Attaches a keydown event listener to the given element, invoking a .NET method when 'Escape' is pressed.
* @param {HTMLElement} element - The HTML element to attach the keydown listener to.
* @param {any} dotnetHelper - .NET object to invoke methods asynchronously.
*/
window.windowKeyDownHandler = (element, dotnetHelper) => { window.windowKeyDownHandler = (element, dotnetHelper) => {
element.addEventListener('keydown', (e) => { element.addEventListener('keydown', (e) => {
if (e.key === 'Escape') { if (e.key === 'Escape') {