120 lines
3.3 KiB
Vue
120 lines
3.3 KiB
Vue
<template>
|
|
<li class="folder-node">
|
|
<div
|
|
class="folder-row"
|
|
:class="{ selected }"
|
|
:style="{ paddingInlineStart: `${depth * 14}px` }"
|
|
>
|
|
<button
|
|
class="folder-expand"
|
|
:aria-expanded="expanded"
|
|
:aria-label="name"
|
|
@click="expanded = !expanded"
|
|
>
|
|
<AppIcon :name="expanded ? 'expand_more' : 'chevron_right'" />
|
|
</button>
|
|
<router-link
|
|
:to="url"
|
|
:title="name"
|
|
:aria-current="selected ? 'page' : undefined"
|
|
@click="layout.closeHovers"
|
|
>
|
|
<AppIcon :name="expanded ? 'folder_open' : 'folder'" /><span>{{
|
|
name
|
|
}}</span>
|
|
</router-link>
|
|
</div>
|
|
<ul v-if="expanded">
|
|
<li v-if="loading" class="tree-status">{{ t("files.loading") }}</li>
|
|
<li v-else-if="error" class="tree-status">
|
|
<button @click="load">{{ t("explorer.retry") }}</button>
|
|
</li>
|
|
<FolderTreeNode
|
|
v-for="folder in folders"
|
|
:key="folder.url"
|
|
:url="folder.url"
|
|
:name="folder.name"
|
|
:depth="depth + 1"
|
|
/>
|
|
</ul>
|
|
</li>
|
|
</template>
|
|
<script setup lang="ts">
|
|
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
|
import { useRoute } from "vue-router";
|
|
import { useI18n } from "vue-i18n";
|
|
import { useFileStore } from "@/stores/file";
|
|
import { useAuthStore } from "@/stores/auth";
|
|
import { useLayoutStore } from "@/stores/layout";
|
|
import { files as api } from "@/api";
|
|
const props = defineProps<{ url: string; name: string; depth: number }>();
|
|
const route = useRoute(),
|
|
file = useFileStore(),
|
|
auth = useAuthStore(),
|
|
layout = useLayoutStore();
|
|
const { t } = useI18n();
|
|
const normalized = (url: string) => url.replace(/\/+$/, "") + "/";
|
|
const expanded = ref(props.depth === 0),
|
|
loading = ref(false),
|
|
error = ref(false);
|
|
const children = ref<ResourceItem[]>([]);
|
|
const selected = computed(
|
|
() => !!file.req?.isDir && normalized(file.req.url) === normalized(props.url)
|
|
);
|
|
const folders = computed(() =>
|
|
children.value
|
|
.filter(
|
|
(item) =>
|
|
item.isDir && (!auth.user?.hideDotfiles || !item.name.startsWith("."))
|
|
)
|
|
.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }))
|
|
);
|
|
let controller: AbortController | undefined;
|
|
function useCurrentListing() {
|
|
if (!selected.value || !file.req) return false;
|
|
controller?.abort();
|
|
children.value = file.req.items;
|
|
loading.value = false;
|
|
error.value = false;
|
|
return true;
|
|
}
|
|
async function load() {
|
|
if (useCurrentListing()) return;
|
|
controller?.abort();
|
|
const request = (controller = new AbortController());
|
|
loading.value = true;
|
|
error.value = false;
|
|
try {
|
|
const result = await api.fetch(props.url, request.signal);
|
|
if (request.signal.aborted) return;
|
|
children.value = result.items || [];
|
|
} catch {
|
|
if (!request.signal.aborted) error.value = true;
|
|
} finally {
|
|
if (controller === request) loading.value = false;
|
|
}
|
|
}
|
|
watch(
|
|
() => file.req,
|
|
() => {
|
|
if (expanded.value) useCurrentListing();
|
|
}
|
|
);
|
|
watch(
|
|
() => route.path,
|
|
(path) => {
|
|
if (normalized(path).startsWith(normalized(props.url)))
|
|
expanded.value = true;
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
watch(
|
|
expanded,
|
|
(value) => {
|
|
if (value) load();
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
onBeforeUnmount(() => controller?.abort());
|
|
</script>
|