filebrowser/frontend/src/components/prompts/FileList.vue

155 lines
3.7 KiB
Vue
Raw Normal View History

2018-02-01 12:17:04 +00:00
<template>
<div>
<ul class="file-list">
2021-03-21 11:51:58 +00:00
<li
@click="itemClick"
2018-02-01 12:17:04 +00:00
@touchstart="touchstart"
@dblclick="next"
role="button"
tabindex="0"
:aria-label="item.name"
:aria-selected="selected == item.url"
2021-03-21 11:51:58 +00:00
:key="item.name"
v-for="item in items"
:data-url="item.url"
>
{{ item.name }}
</li>
2018-02-01 12:17:04 +00:00
</ul>
2021-03-21 11:51:58 +00:00
<p>
{{ $t("prompts.currentlyNavigating") }} <code>{{ nav }}</code
>.
</p>
2018-02-01 12:17:04 +00:00
</div>
</template>
<script>
import { mapState } from "pinia";
import { useAuthStore } from "@/stores/auth";
import { useFileStore } from "@/stores/file";
2021-03-21 11:51:58 +00:00
import url from "@/utils/url";
import { files } from "@/api";
2018-02-01 12:17:04 +00:00
export default {
2021-03-21 11:51:58 +00:00
name: "file-list",
2018-02-01 12:17:04 +00:00
data: function () {
return {
items: [],
touches: {
2021-03-21 11:51:58 +00:00
id: "",
count: 0,
2018-02-01 12:17:04 +00:00
},
selected: null,
2021-03-21 11:51:58 +00:00
current: window.location.pathname,
};
2018-02-01 12:17:04 +00:00
},
inject: ["$showError"],
2018-02-01 12:17:04 +00:00
computed: {
...mapState(useAuthStore, ["user"]),
...mapState(useFileStore, ["req"]),
2021-03-21 11:51:58 +00:00
nav() {
return decodeURIComponent(this.current);
},
2018-02-01 12:17:04 +00:00
},
2021-03-21 11:51:58 +00:00
mounted() {
this.fillOptions(this.req);
2018-02-01 12:17:04 +00:00
},
methods: {
2021-03-21 11:51:58 +00:00
fillOptions(req) {
2018-02-01 12:17:04 +00:00
// Sets the current path and resets
// the current items.
2021-03-21 11:51:58 +00:00
this.current = req.url;
this.items = [];
2018-02-01 12:17:04 +00:00
2021-03-21 11:51:58 +00:00
this.$emit("update:selected", this.current);
2018-02-01 12:17:04 +00:00
// If the path isn't the root path,
// show a button to navigate to the previous
// directory.
2021-03-21 11:51:58 +00:00
if (req.url !== "/files/") {
2018-02-01 12:17:04 +00:00
this.items.push({
2021-03-21 11:51:58 +00:00
name: "..",
url: url.removeLastDir(req.url) + "/",
});
2018-02-01 12:17:04 +00:00
}
// If this folder is empty, finish here.
2021-03-21 11:51:58 +00:00
if (req.items === null) return;
2018-02-01 12:17:04 +00:00
// Otherwise we add every directory to the
// move options.
for (let item of req.items) {
2021-03-21 11:51:58 +00:00
if (!item.isDir) continue;
2018-02-01 12:17:04 +00:00
this.items.push({
name: item.name,
2021-03-21 11:51:58 +00:00
url: item.url,
});
2018-02-01 12:17:04 +00:00
}
},
next: function (event) {
// Retrieves the URL of the directory the user
// just clicked in and fill the options with its
// content.
2021-03-21 11:51:58 +00:00
let uri = event.currentTarget.dataset.url;
2018-02-01 12:17:04 +00:00
2021-03-21 11:51:58 +00:00
files.fetch(uri).then(this.fillOptions).catch(this.$showError);
2018-02-01 12:17:04 +00:00
},
2021-03-21 11:51:58 +00:00
touchstart(event) {
let url = event.currentTarget.dataset.url;
2018-02-01 12:17:04 +00:00
// In 300 milliseconds, we shall reset the count.
setTimeout(() => {
2021-03-21 11:51:58 +00:00
this.touches.count = 0;
}, 300);
2018-02-01 12:17:04 +00:00
// If the element the user is touching
// is different from the last one he touched,
// reset the count.
if (this.touches.id !== url) {
2021-03-21 11:51:58 +00:00
this.touches.id = url;
this.touches.count = 1;
return;
2018-02-01 12:17:04 +00:00
}
2021-03-21 11:51:58 +00:00
this.touches.count++;
2018-02-01 12:17:04 +00:00
// If there is more than one touch already,
// open the next screen.
if (this.touches.count > 1) {
2021-03-21 11:51:58 +00:00
this.next(event);
2018-02-01 12:17:04 +00:00
}
},
itemClick: function (event) {
2021-03-21 11:51:58 +00:00
if (this.user.singleClick) this.next(event);
else this.select(event);
},
2018-02-01 12:17:04 +00:00
select: function (event) {
// If the element is already selected, unselect it.
if (this.selected === event.currentTarget.dataset.url) {
2021-03-21 11:51:58 +00:00
this.selected = null;
this.$emit("update:selected", this.current);
return;
2018-02-01 12:17:04 +00:00
}
// Otherwise select the element.
2021-03-21 11:51:58 +00:00
this.selected = event.currentTarget.dataset.url;
this.$emit("update:selected", this.selected);
},
createDir: async function () {
this.$store.commit("showHover", {
prompt: "newDir",
action: null,
confirm: null,
props: {
redirect: false,
base: this.current === this.$route.path ? null : this.current,
},
});
},
2021-03-21 11:51:58 +00:00
},
};
2018-02-01 12:17:04 +00:00
</script>