Updated Search & progressbar to composition api & typescript
This commit is contained in:
parent
9cc0514464
commit
70707fbd0e
@ -45,6 +45,7 @@ https://raw.githubusercontent.com/dzwillia/vue-simple-progress/master/src/compon
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
// We're leaving this untouched as you can read in the beginning
|
||||||
var isNumber = function (n) {
|
var isNumber = function (n) {
|
||||||
return !isNaN(parseFloat(n)) && isFinite(n);
|
return !isNaN(parseFloat(n)) && isFinite(n);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -64,143 +64,153 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script setup lang="ts">
|
||||||
import { mapActions, mapState, mapWritableState } from "pinia";
|
|
||||||
import { useFileStore } from "@/stores/file";
|
import { useFileStore } from "@/stores/file";
|
||||||
import { useLayoutStore } from "@/stores/layout";
|
import { useLayoutStore } from "@/stores/layout";
|
||||||
|
|
||||||
import url from "@/utils/url";
|
import url from "@/utils/url";
|
||||||
import { search } from "@/api";
|
import { search } from "@/api";
|
||||||
|
import { computed, inject, onMounted, ref, watch } from "vue";
|
||||||
|
import { useI18n } from "vue-i18n";
|
||||||
|
import { useRoute } from "vue-router";
|
||||||
|
|
||||||
var boxes = {
|
const boxes = {
|
||||||
image: { label: "images", icon: "insert_photo" },
|
image: { label: "images", icon: "insert_photo" },
|
||||||
audio: { label: "music", icon: "volume_up" },
|
audio: { label: "music", icon: "volume_up" },
|
||||||
video: { label: "video", icon: "movie" },
|
video: { label: "video", icon: "movie" },
|
||||||
pdf: { label: "pdf", icon: "picture_as_pdf" },
|
pdf: { label: "pdf", icon: "picture_as_pdf" },
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
const layoutStore = useLayoutStore();
|
||||||
name: "search",
|
const fileStore = useFileStore();
|
||||||
data: function () {
|
|
||||||
return {
|
|
||||||
value: "",
|
|
||||||
active: false,
|
|
||||||
ongoing: false,
|
|
||||||
results: [],
|
|
||||||
reload: false,
|
|
||||||
resultsCount: 50,
|
|
||||||
scrollable: null,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
inject: ["$showError"],
|
|
||||||
watch: {
|
|
||||||
show(val, old) {
|
|
||||||
this.active = val === "search";
|
|
||||||
|
|
||||||
if (old === "search" && !this.active) {
|
const value = ref<string>("");
|
||||||
if (this.reload) {
|
const active = ref<boolean>(false);
|
||||||
this.sReload = true;
|
const ongoing = ref<boolean>(false);
|
||||||
}
|
const results = ref<any[]>([]);
|
||||||
|
const reload = ref<boolean>(false);
|
||||||
|
const resultsCount = ref<number>(50);
|
||||||
|
|
||||||
document.body.style.overflow = "auto";
|
const $showError = inject<IToastError>("$showError")!;
|
||||||
this.reset();
|
|
||||||
this.value = "";
|
|
||||||
this.active = false;
|
|
||||||
this.$refs.input.blur();
|
|
||||||
} else if (this.active) {
|
|
||||||
this.reload = false;
|
|
||||||
this.$refs.input.focus();
|
|
||||||
document.body.style.overflow = "hidden";
|
|
||||||
}
|
|
||||||
},
|
|
||||||
value() {
|
|
||||||
if (this.results.length) {
|
|
||||||
this.reset();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
computed: {
|
|
||||||
...mapState(useFileStore, ["isListing"]),
|
|
||||||
...mapState(useLayoutStore, ["show"]),
|
|
||||||
...mapWritableState(useFileStore, { sReload: "reload" }),
|
|
||||||
boxes() {
|
|
||||||
return boxes;
|
|
||||||
},
|
|
||||||
isEmpty() {
|
|
||||||
return this.results.length === 0;
|
|
||||||
},
|
|
||||||
text() {
|
|
||||||
if (this.ongoing) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.value === ""
|
const input = ref<HTMLInputElement | null>(null);
|
||||||
? this.$t("search.typeToSearch")
|
const result = ref<HTMLElement | null>(null);
|
||||||
: this.$t("search.pressToSearch");
|
|
||||||
},
|
|
||||||
filteredResults() {
|
|
||||||
return this.results.slice(0, this.resultsCount);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
mounted() {
|
|
||||||
this.$refs.result.addEventListener("scroll", (event) => {
|
|
||||||
if (
|
|
||||||
event.target.offsetHeight + event.target.scrollTop >=
|
|
||||||
event.target.scrollHeight - 100
|
|
||||||
) {
|
|
||||||
this.resultsCount += 50;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
...mapActions(useLayoutStore, ["showHover", "closeHovers"]),
|
|
||||||
open() {
|
|
||||||
this.showHover("search");
|
|
||||||
},
|
|
||||||
close(event) {
|
|
||||||
event.stopPropagation();
|
|
||||||
event.preventDefault();
|
|
||||||
this.closeHovers();
|
|
||||||
},
|
|
||||||
keyup(event) {
|
|
||||||
if (event.key === "Escape") {
|
|
||||||
this.close(event);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.results.length = 0;
|
const { t } = useI18n();
|
||||||
},
|
|
||||||
init(string) {
|
|
||||||
this.value = `${string} `;
|
|
||||||
this.$refs.input.focus();
|
|
||||||
},
|
|
||||||
reset() {
|
|
||||||
this.ongoing = false;
|
|
||||||
this.resultsCount = 50;
|
|
||||||
this.results = [];
|
|
||||||
},
|
|
||||||
async submit(event) {
|
|
||||||
event.preventDefault();
|
|
||||||
|
|
||||||
if (this.value === "") {
|
const route = useRoute();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let path = this.$route.path;
|
// @ts-ignore
|
||||||
if (!this.isListing) {
|
watch(layoutStore.show, (newVal: string, oldVal: string) => {
|
||||||
path = url.removeLastDir(path) + "/";
|
active.value = newVal === "search";
|
||||||
}
|
|
||||||
|
|
||||||
this.ongoing = true;
|
if (oldVal === "search" && !active.value) {
|
||||||
|
if (reload.value) {
|
||||||
|
fileStore.reload = true;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
document.body.style.overflow = "auto";
|
||||||
this.results = await search(path, this.value);
|
reset();
|
||||||
} catch (error) {
|
value.value = "";
|
||||||
this.$showError(error);
|
active.value = false;
|
||||||
}
|
input.value?.blur();
|
||||||
|
} else if (active.value) {
|
||||||
|
reload.value = false;
|
||||||
|
input.value?.focus();
|
||||||
|
document.body.style.overflow = "hidden";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
this.ongoing = false;
|
watch(value, () => {
|
||||||
},
|
if (results.value.length) {
|
||||||
},
|
reset();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ...mapState(useFileStore, ["isListing"]),
|
||||||
|
// ...mapState(useLayoutStore, ["show"]),
|
||||||
|
// ...mapWritableState(useFileStore, { sReload: "reload" }),
|
||||||
|
|
||||||
|
const isEmpty = computed(() => {
|
||||||
|
return results.value.length === 0;
|
||||||
|
});
|
||||||
|
const text = computed(() => {
|
||||||
|
if (ongoing.value) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return value.value === ""
|
||||||
|
? t("search.typeToSearch")
|
||||||
|
: t("search.pressToSearch");
|
||||||
|
});
|
||||||
|
const filteredResults = computed(() => {
|
||||||
|
return results.value.slice(0, resultsCount.value);
|
||||||
|
});
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (result.value === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result.value.addEventListener("scroll", (event: Event) => {
|
||||||
|
if (
|
||||||
|
(event.target as HTMLElement).offsetHeight +
|
||||||
|
(event.target as HTMLElement).scrollTop >=
|
||||||
|
(event.target as HTMLElement).scrollHeight - 100
|
||||||
|
) {
|
||||||
|
resultsCount.value += 50;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const open = () => {
|
||||||
|
layoutStore.showHover("search");
|
||||||
|
};
|
||||||
|
|
||||||
|
const close = (event: Event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
event.preventDefault();
|
||||||
|
layoutStore.closeHovers();
|
||||||
|
};
|
||||||
|
|
||||||
|
const keyup = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === "Escape") {
|
||||||
|
close(event);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
results.value.length = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
const init = (string: string) => {
|
||||||
|
value.value = `${string} `;
|
||||||
|
input.value !== null ? input.value.focus() : "";
|
||||||
|
};
|
||||||
|
|
||||||
|
const reset = () => {
|
||||||
|
ongoing.value = false;
|
||||||
|
resultsCount.value = 50;
|
||||||
|
results.value = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
const submit = async (event: Event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
if (value.value === "") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let path = route.path;
|
||||||
|
if (!fileStore.isListing) {
|
||||||
|
path = url.removeLastDir(path) + "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
ongoing.value = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
results.value = await search(path, value.value);
|
||||||
|
} catch (error: any) {
|
||||||
|
$showError(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
ongoing.value = false;
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user