2023-01-01 21:50:48 +00:00
|
|
|
import { useRouteQuery as useRouteQueryBase } from "@vueuse/router";
|
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
/* eslint no-redeclare: 0 */
|
|
|
|
import { WritableComputedRef } from "vue";
|
|
|
|
|
|
|
|
export function useRouteQuery(q: string, def: string[]): WritableComputedRef<string[]>;
|
|
|
|
export function useRouteQuery(q: string, def: string): WritableComputedRef<string>;
|
|
|
|
export function useRouteQuery(q: string, def: boolean): WritableComputedRef<boolean>;
|
2023-01-01 21:50:48 +00:00
|
|
|
export function useRouteQuery(q: string, def: number): WritableComputedRef<number>;
|
2023-02-18 06:41:01 +00:00
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
export function useRouteQuery(q: string, def: any): WritableComputedRef<any> {
|
|
|
|
const route = useRoute();
|
|
|
|
const router = useRouter();
|
|
|
|
|
2023-01-01 21:50:48 +00:00
|
|
|
const v = useRouteQueryBase(q, def);
|
|
|
|
|
|
|
|
const first = computed<string>(() => {
|
|
|
|
const qv = route.query[q];
|
|
|
|
if (Array.isArray(qv)) {
|
|
|
|
return qv[0]?.toString() || def;
|
|
|
|
}
|
|
|
|
return qv?.toString() || def;
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (route.query[q] === undefined) {
|
|
|
|
v.value = def;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
switch (typeof def) {
|
|
|
|
case "string":
|
|
|
|
return computed({
|
|
|
|
get: () => {
|
|
|
|
const qv = route.query[q];
|
|
|
|
if (Array.isArray(qv)) {
|
|
|
|
return qv[0];
|
|
|
|
}
|
|
|
|
return qv;
|
|
|
|
},
|
|
|
|
set: v => {
|
|
|
|
const query = { ...route.query, [q]: v };
|
|
|
|
router.push({ query });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
case "object": // array
|
|
|
|
return computed({
|
|
|
|
get: () => {
|
|
|
|
const qv = route.query[q];
|
|
|
|
if (Array.isArray(qv)) {
|
|
|
|
return qv;
|
|
|
|
}
|
|
|
|
return [qv];
|
|
|
|
},
|
|
|
|
set: v => {
|
|
|
|
const query = { ...route.query, [q]: v };
|
|
|
|
router.push({ query });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
case "boolean":
|
|
|
|
return computed({
|
|
|
|
get: () => {
|
2023-01-01 21:50:48 +00:00
|
|
|
return first.value === "true";
|
2022-11-02 05:58:46 +00:00
|
|
|
},
|
|
|
|
set: v => {
|
|
|
|
const query = { ...route.query, [q]: `${v}` };
|
|
|
|
router.push({ query });
|
|
|
|
},
|
|
|
|
});
|
2023-01-01 21:50:48 +00:00
|
|
|
case "number":
|
|
|
|
return computed({
|
|
|
|
get: () => parseInt(first.value, 10),
|
|
|
|
set: nv => {
|
|
|
|
v.value = nv.toString();
|
|
|
|
},
|
|
|
|
});
|
2022-11-02 05:58:46 +00:00
|
|
|
}
|
2023-01-01 21:50:48 +00:00
|
|
|
|
|
|
|
throw new Error("Invalid type");
|
2022-11-02 05:58:46 +00:00
|
|
|
}
|