174 lines
4.7 KiB
Svelte
174 lines
4.7 KiB
Svelte
<script lang="ts">
|
|
import { formatDataVolume } from "util/Formatting";
|
|
import { fs_check_response, fs_get_trash, fs_path_url, fs_rename, fs_trash, type FSNode } from "lib/FilesystemAPI.svelte";
|
|
import { loading_run } from "lib/Loading";
|
|
import { onMount } from "svelte";
|
|
import { toolbar_store } from "wrap/HeaderStore";
|
|
import { FSNavigator } from "filesystem/FSNavigator";
|
|
import Button from "layout/Button.svelte";
|
|
import ListView from "./ListView.svelte";
|
|
import { FileAction, type FileActionHandler } from "./FileManagerLib";
|
|
|
|
let {
|
|
nav,
|
|
root_path = "/me",
|
|
}: {
|
|
nav: FSNavigator
|
|
root_path?: string
|
|
} = $props();
|
|
|
|
// The trash entries are not the children of the .Trash directory, so they get a
|
|
// navigator of their own for the ListView to list and sort
|
|
const trash = new FSNavigator(false)
|
|
|
|
let size = $derived($trash.children.reduce(
|
|
(acc, node) => acc + (node.type === "dir" ? node.recursive_size : node.file_size), 0
|
|
))
|
|
|
|
const load = () => loading_run(async () => {
|
|
try {
|
|
trash.children = await fs_get_trash(root_path)
|
|
} catch (err: any) {
|
|
alert("Failed to load the trash bin: " + (err.message ?? err))
|
|
}
|
|
trash.selecting = false
|
|
trash.selection.clear()
|
|
trash.sort_children("")
|
|
})
|
|
|
|
const file_event: FileActionHandler = (action, index, e) => {
|
|
if (action === FileAction.Drag) {
|
|
return
|
|
} else if (action === FileAction.Context) {
|
|
// On a touch screen a long press starts selecting, like in the file
|
|
// manager. A right click is left to the browser's own menu
|
|
if (!navigator.maxTouchPoints) {
|
|
return
|
|
}
|
|
trash.selecting = true
|
|
}
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
|
|
if (trash.selecting) {
|
|
trash.select(trash.children[index], (e as MouseEvent).shiftKey)
|
|
} else {
|
|
nav.navigate(trash.children[index].path, true)
|
|
}
|
|
}
|
|
|
|
const selected = () => [...trash.selection.values()]
|
|
|
|
// Runs an action on all the selected entries and reloads the listing
|
|
const run_selected = async (action: (node: FSNode) => Promise<any>) => {
|
|
const results = await loading_run(() => Promise.allSettled(selected().map(action)))
|
|
const failed = results.filter(r => r.status === "rejected") as PromiseRejectedResult[]
|
|
if (failed.length !== 0) {
|
|
alert(
|
|
failed.length + " of " + results.length + " failed: " +
|
|
(failed[0].reason.message ?? failed[0].reason)
|
|
)
|
|
}
|
|
await load()
|
|
}
|
|
|
|
const restore = () => run_selected(async node => {
|
|
if (!node.original_path) {
|
|
throw new Error("the original location of " + node.name + " is not known")
|
|
}
|
|
return fs_rename(node.path, root_path + node.original_path)
|
|
})
|
|
|
|
const delete_selected = () => {
|
|
if (confirm(`Permanently delete ${selected().length} of the items in the trash?`)) {
|
|
run_selected(node => fs_trash(node.path))
|
|
}
|
|
}
|
|
|
|
const empty_trash = async () => {
|
|
if (!confirm("Permanently delete all items in the trash?")) {
|
|
return
|
|
}
|
|
try {
|
|
await loading_run(async () => fs_check_response(
|
|
await fetch(fs_path_url(root_path) + "?empty_trash", { method: "DELETE" })
|
|
))
|
|
} catch (err: any) {
|
|
alert(err.message ?? err)
|
|
}
|
|
await load()
|
|
}
|
|
|
|
onMount(() => {
|
|
toolbar_store.set(toolbar)
|
|
load()
|
|
return () => toolbar_store.set(null)
|
|
})
|
|
</script>
|
|
|
|
{#snippet toolbar()}
|
|
{#if $trash.selecting}
|
|
<div class="toolbar toolbar_edit">
|
|
<Button icon="close" click={() => trash.set_selecting(false)}/>
|
|
<div class="toolbar_spacer">Selecting files</div>
|
|
<Button click={restore} icon="restore_from_trash" label="Restore"/>
|
|
<button onclick={delete_selected} class="button_red">
|
|
<i class="icon">delete_forever</i>
|
|
Delete
|
|
</button>
|
|
</div>
|
|
{:else}
|
|
<div class="toolbar">
|
|
<div>
|
|
<button onclick={() => history.back()} title="Back">
|
|
<i class="icon">arrow_back</i>
|
|
</button>
|
|
<button onclick={() => nav.navigate_up()} title="Up">
|
|
<i class="icon">north</i>
|
|
</button>
|
|
<button onclick={load} title="Refresh trash bin">
|
|
<i class="icon">refresh</i>
|
|
</button>
|
|
</div>
|
|
<div>Trash bin ({formatDataVolume(size, 3)})</div>
|
|
<div>
|
|
{#if $trash.children.length !== 0}
|
|
<button onclick={() => trash.set_selecting(true)} title="Select files to restore or delete">
|
|
<i class="icon">select_all</i>
|
|
</button>
|
|
<button onclick={empty_trash} class="button_red">
|
|
<i class="icon">delete_forever</i>
|
|
Delete all
|
|
</button>
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
{/if}
|
|
{/snippet}
|
|
|
|
<ListView nav={trash} file_event={file_event} show_hidden hide_edit hide_branding trash/>
|
|
|
|
<style>
|
|
/* Same as the toolbar of the file manager */
|
|
.toolbar {
|
|
display: flex;
|
|
flex-direction: row;
|
|
flex-wrap: wrap;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
border-top: 1px solid var(--separator);
|
|
}
|
|
.toolbar > * {
|
|
flex: 0 0 content;
|
|
display: flex;
|
|
flex-direction: row;
|
|
}
|
|
.toolbar_spacer {
|
|
flex: 1 1 auto;
|
|
text-align: center;
|
|
}
|
|
.toolbar_edit {
|
|
background-color: var(--green_tint);
|
|
}
|
|
</style>
|