Use pinia

This commit is contained in:
2026-07-06 19:49:28 +02:00
parent d345ea9ec3
commit 9324a08cf9
12 changed files with 160 additions and 151 deletions
+3 -2
View File
@@ -1,13 +1,14 @@
<template> <template>
<div id="main"> <div id="main">
<NuxtRouteAnnouncer /> <NuxtRouteAnnouncer />
<NuxtPage /> <NuxtPage page-key="main" />
</div> </div>
</template> </template>
<style lang="css"> <style lang="css">
body, body,
#main { #main,
main {
height: 100vh; height: 100vh;
margin: 0; margin: 0;
} }
+5 -10
View File
@@ -1,10 +1,6 @@
<template> <template>
<ClientOnly> <ClientOnly>
<mgl-geo-json-source <mgl-geo-json-source source-id="clusters" :data="clusters">
v-if="clusters"
source-id="clusters"
:data="clusters"
>
<mgl-circle-layer <mgl-circle-layer
layer-id="clusters_dots" layer-id="clusters_dots"
:paint="cluster_paint" :paint="cluster_paint"
@@ -31,12 +27,14 @@ const cluster_paint = {
const cluster_label = { const cluster_label = {
"text-field": ["get", "id"], "text-field": ["get", "id"],
}; };
const map = useMap(); const map = useMap("main");
const { clusters } = defineProps<{ const { clusters } = defineProps<{
clusters: FeatureCollection<Point>; clusters: FeatureCollection<Point>;
}>(); }>();
console.log(clusters);
async function clusterClick(e: FeatureCollection) { async function clusterClick(e: FeatureCollection) {
const feature = e.features[0]; const feature = e.features[0];
if (!feature || !feature.properties) { if (!feature || !feature.properties) {
@@ -50,15 +48,12 @@ async function clusterClick(e: FeatureCollection) {
const data = await $fetch<FeatureCollection<Point>>( const data = await $fetch<FeatureCollection<Point>>(
`https://data.mobilites-m.fr/api/clusters/${code}/stops`, `https://data.mobilites-m.fr/api/clusters/${code}/stops`,
); );
if (!map.map) {
return;
}
new Popup() new Popup()
.setLngLat(coordinates) .setLngLat(coordinates)
.setHTML( .setHTML(
`Cluster code : ${feature.properties.code}<br/>This cluster contains ${data.features.length} stops`, `Cluster code : ${feature.properties.code}<br/>This cluster contains ${data.features.length} stops`,
) )
.addTo(map.map); .addTo(map.map!);
} }
function clusterEnter() { function clusterEnter() {
+57 -79
View File
@@ -1,16 +1,13 @@
<template> <template>
<ClientOnly> <ClientOnly>
<MglMap :map-style="style" :center="center" :zoom="zoom" height="100%"> <MglMap :map-style="style" :center="center" :zoom="zoom" map-key="main">
<MglNavigationControl /> <MglNavigationControl />
<MapLines <MapLines
v-for="r of routeWithLines" v-for="r of shownLines"
:key="r.route.id" :key="r.route.id"
:route-with-line="r" :route-with-line="r"
/> />
<MapClusters <MapClusters v-if="shownClusters" :clusters="shownClusters" />
v-if="clustersData.features"
:clusters="clustersData"
/>
</MglMap> </MglMap>
</ClientOnly> </ClientOnly>
</template> </template>
@@ -28,66 +25,47 @@ const style = "https://tiles.versatiles.org/assets/styles/colorful/style.json";
const center = new LngLat(5.735, 45.185); const center = new LngLat(5.735, 45.185);
const zoom = 12; const zoom = 12;
const map = useMap(); const store = useMresoStore();
await store.fetchData();
const shownClusters = ref<FeatureCollection<Point> | null>(null);
const selectedRoutes = ref<string[]>([]); const { selectedRoutesId } = defineProps<{
const selectedStops = ref<string[]>([]); selectedRoutesId: string[];
}>();
const clustersRequest = const map = useMap("main");
await useFetch<FeatureCollection<Point>>("/clusters.json"); watchEffect(async () => {
const clusters = computed(() => clustersRequest.data.value?.features ?? []); const allClusters = [];
const clustersData = computed<FeatureCollection<Point>>(() => { for (const routeId of selectedRoutesId) {
return { const apiClusters = await $fetch<Cluster[]>(
type: "FeatureCollection", `https://data.mobilites-m.fr/api/routers/default/index/routes/${routeId}/clusters`,
features: clusters.value.filter((f) =>
selectedStops.value.includes(f.properties?.id),
),
};
});
const linesRequest =
await useFetch<FeatureCollection<MultiLineString>>("/lignes.json");
const transport_lines = computed(() => linesRequest.data.value?.features ?? []);
const routesRequest = await useFetch<Route[]>("/routes.json");
async function selectRoute(routeId: string) {
if (!map.map) {
console.error("Map is not yet initialized ! aborting");
return;
}
selectedRoutes.value = [routeId];
const stops: string[] = [];
for (const route of selectedRoutes.value) {
const data = await $fetch<Cluster[]>(
`https://data.mobilites-m.fr/api/routers/default/index/routes/${route.replace("_", ":")}/clusters`,
); );
allClusters.push(...apiClusters);
for (const stop of data) {
stops.push(stop.id);
}
} }
shownClusters.value = {
selectedStops.value = stops; type: "FeatureCollection",
features: allClusters
const points = clusters.value.filter((c) => .map((c) => store.clustersById[c.id])
stops.includes(c.properties?.id), .filter((c) => c !== undefined),
); };
const firstPoint = points[0]; const firstPoint = shownClusters.value.features[0];
console.log(firstPoint);
if (!firstPoint) { if (!firstPoint) {
console.error("Data does not contains any points. Aborting");
return; return;
} }
const bounds = clusters.value const coordinates = [
.filter((c) => stops.includes(c.properties?.id)) ...shownClusters.value.features.map(
(point) => point.geometry.coordinates,
),
...shownLines.value
.map((line) => line.line.geometry.coordinates)
.flat(2),
];
const bounds = coordinates
// .filter((c) => stops.includes(c.properties?.id))
.reduce( .reduce(
(bounds, point) => { (bounds, coords) => {
return bounds.extend( return bounds.extend(new LngLat(coords[0], coords[1]));
new LngLat(
point.geometry.coordinates[0]!,
point.geometry.coordinates[1]!,
),
);
}, },
new LngLatBounds([ new LngLatBounds([
firstPoint.geometry.coordinates[0]!, firstPoint.geometry.coordinates[0]!,
@@ -96,27 +74,27 @@ async function selectRoute(routeId: string) {
firstPoint.geometry.coordinates[1]!, firstPoint.geometry.coordinates[1]!,
]), ]),
); );
map.map!.fitBounds(bounds, {
map.map.fitBounds(bounds, {
padding: 20, padding: 20,
}); });
} });
const shownLines = computed(() => {
const l: { route: Route; line: Feature<MultiLineString> }[] = [];
for (const routeId of selectedRoutesId) {
const route = store.routesById[routeId];
if (!route) {
continue;
}
const line = store.linesById[routeId.replace(":", "_")];
if (!line) {
continue;
}
l.push({
route,
line,
});
}
return l;
});
</script> </script>
<style scoped lang="css">
#lineSelector {
position: fixed;
left: 0;
top: 0;
height: 100vh;
overflow: auto;
background: white;
}
.lineLabel {
cursor: pointer;
}
.lineLabel.selected {
color: red !important;
}
</style>
-19
View File
@@ -1,19 +0,0 @@
<template>
<div v-if="routesRequest.data.value" id="lineSelector">
<div
v-for="route of routesRequest.data.value"
:key="route.id"
:class="{
selected: selectedRoutes.includes(route.id),
lineLabel: true,
}"
:style="{
background: '#' + route.color,
color: '#' + route.textColor,
}"
@click="selectRoute(route.id)"
>
{{ route.type }} : {{ route.shortName }}
</div>
</div>
</template>
+49
View File
@@ -0,0 +1,49 @@
<template>
<div id="lineSelector">
<div
v-for="route of store.routes"
:key="route.id"
:class="{
selected: selectedRoutesId.includes(route.id),
lineLabel: true,
}"
:style="{
background: '#' + route.color,
color: '#' + route.textColor,
}"
@click="selectRoute(route.id)"
>
{{ route.type }} : {{ route.shortName }}
</div>
</div>
</template>
<script setup lang="ts">
const store = useMresoStore();
const { selectedRoutesId } = defineProps<{
selectedRoutesId: string[];
}>();
function selectRoute(routeId: string) {
navigateTo(`/lines/${routeId}`);
}
</script>
<style scoped lang="css">
#lineSelector {
position: fixed;
left: 0;
top: 0;
height: 100vh;
overflow: auto;
background: white;
}
.lineLabel {
cursor: pointer;
}
.lineLabel.selected {
color: red !important;
}
</style>
+15
View File
@@ -0,0 +1,15 @@
<template>
<main>
<Transition>
<KeepAlive>
<Map map-key="main" :selectedRoutesId="[router.params.id]" />
</KeepAlive>
</Transition>
<SidebarContainer :selectedRoutesId="[router.params.id]" />
</main>
</template>
<script setup lang="ts">
const router = useRoute();
</script>
-6
View File
@@ -1,6 +0,0 @@
<template>
<main>
<SidebarContainer />
<Map />
</main>
</template>
+27 -29
View File
@@ -1,4 +1,9 @@
import type { Feature, FeatureCollection, MultiLineString, Point } from "geojson"; import type {
Feature,
FeatureCollection,
MultiLineString,
Point,
} from "geojson";
export const useMresoStore = defineStore("mresoStore", { export const useMresoStore = defineStore("mresoStore", {
state: () => ({ state: () => ({
@@ -6,41 +11,34 @@ export const useMresoStore = defineStore("mresoStore", {
lines: {} as FeatureCollection<MultiLineString>, lines: {} as FeatureCollection<MultiLineString>,
clusters: {} as FeatureCollection<Point>, clusters: {} as FeatureCollection<Point>,
stops: {} as FeatureCollection<Point>, stops: {} as FeatureCollection<Point>,
selectedRoutesId: [] as string[]
}), }),
actions: { actions: {
async fetch() { async fetchData(options: { signal?: AbortSignal } = {}) {
// const infos = await $fetch("https://api.nuxt.com/modules/pinia"); [this.routes, this.lines, this.clusters, this.stops] = await Promise.all([
$fetch<Route[]>("/routes.json", options),
$fetch<FeatureCollection<MultiLineString>>("/lines.json", options),
$fetch<FeatureCollection<Point>>("/clusters.json", options),
$fetch<FeatureCollection<Point>>("/stops.json", options),
]);
// this.name = infos.name; this.lines = await $fetch("/lines.json", options);
// this.description = infos.description; this.clusters = await $fetch("/clusters.json", options);
this.stops = await $fetch("/stops.json", options);
}, },
}, },
getters: { getters: {
routesById(state) { routesById(state): { [id: string]: Route } {
return Object.fromEntries(state.routes.map((r)=>[r.id, r])) return Object.fromEntries(state.routes.map((r) => [r.id, r]));
}, },
linesById(state) { linesById(state): { [id: string]: Feature<MultiLineString> } {
return Object.fromEntries(state.lines.features.map((l)=>[l.properties!.id, l])) return Object.fromEntries(
state.lines.features.map((l) => [l.properties!.id, l]),
);
}, },
shownLines() { clustersById(state): { [id: string]: Feature<Point> } {
const shownLines: { route: Route; line: Feature<MultiLineString> }[] = []; return Object.fromEntries(
for (const routeId of this.selectedRoutesId) { state.clusters.features.map((l) => [l.properties!.id, l]),
const route = this.routesById[routeId]; );
if (!route) {
continue;
}
const line = this.linesById[routeId.replace(":","_")]
if (!line) {
continue;
}
shownLines.push({
route,
line,
});
}
return shownLines;
}, },
} },
}
}); });
+1 -1
View File
@@ -7,7 +7,7 @@ interface Route {
textColor: string; textColor: string;
mode: string; mode: string;
type: string; type: string;
timeSheet: string; timeSheet: boolean;
pdfTimeSheet: boolean; pdfTimeSheet: boolean;
pdfMap: boolean; pdfMap: boolean;
} }
+2 -2
View File
@@ -1,7 +1,7 @@
// https://nuxt.com/docs/api/configuration/nuxt-config // https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: "2025-07-15", compatibilityDate: "2025-07-15",
devtools: { enabled: true }, devtools: { enabled: false },
modules: ["@nuxt/eslint", "nuxt-maplibre", "@pinia/nuxt"], modules: ["@nuxt/eslint", "nuxt-maplibre", "@pinia/nuxt"],
vite: { vite: {
optimizeDeps: { optimizeDeps: {
@@ -9,4 +9,4 @@ export default defineNuxtConfig({
// include: ["maplibre-gl"], // include: ["maplibre-gl"],
}, },
}, },
}); });
BIN
View File
Binary file not shown.
File diff suppressed because one or more lines are too long