Implement back button functionality for directory navigation

The back button functionality was added to the web application. Now users can navigate directories back and forth, improving user experience and interaction. This new feature is achieved through a 'handleBack' function in the 'back.js' file which listens to the click event on the 'Back' button. When clicked, it adjusts the current directory and navigates the application to the parent directory. This is especially crucial for a software that has a file system or explorer-like structure. The 'Back' button was also modified in 'index.html' to support this functionality and the 'handleBack' function was called in the 'Main' function in 'index.js' to initialise it at the start.
This commit is contained in:
Ian Wijma 2023-11-27 23:50:18 +11:00
parent d2f6d1640a
commit 6378dadea9
3 changed files with 18 additions and 3 deletions

View File

@ -10,9 +10,8 @@
<body class="h-screen max-h-screen flex flex-col overflow-hidden">
<header class="bg-amber-500 w-screen h-12 flex items-center justify-between">
<span class="flex">
<button>Back</button>
<button>Forward</button>
<span class="flex gap-2">
<button data-ui="up-one-dir">Back</button>
</span>
<span class="flex">

View File

@ -1,12 +1,14 @@
import {renderFavourite} from "./parts/favourites.js";
import {renderBreadcrumbs} from "./parts/breadcrumbs.js";
import {renderFiles} from "./parts/files.js";
import {handleBack} from "./parts/back.js";
async function Main() {
renderBreadcrumbs();
renderFavourite();
await renderFiles();
handleBack();
}
Main();

14
src/parts/back.js Normal file
View File

@ -0,0 +1,14 @@
import {getOneElementOrThrow} from "../libs/element.js";
import {getCurrentDir, navigate} from "../libs/navigation.js";
import {sep} from "../libs/path.js";
export const handleBack = () => {
const backEl = getOneElementOrThrow(document, '[data-ui=up-one-dir]');
backEl.addEventListener('click', () => {
const currentDir = getCurrentDir();
const directorySplit = currentDir.split(sep);
const newDir = directorySplit.splice(0, directorySplit.length - 2);
navigate(sep + newDir.join(sep));
});
}