523 lines
15 KiB
Vue
523 lines
15 KiB
Vue
<template>
|
|
<div
|
|
ref="breederMap"
|
|
class="breeder-map-component position-relative"
|
|
>
|
|
<div class="container">
|
|
<div class="row">
|
|
<div class="col-12 col-lg-6">
|
|
<div>
|
|
<h2 class="h2">
|
|
{{ manageableText.title }}
|
|
</h2>
|
|
<p>
|
|
{{ manageableText.main_text }}
|
|
</p>
|
|
<p>
|
|
<img
|
|
src="@/images/breeding/picto-preparation-site.svg"
|
|
alt="Pictogramme représentant un site de production"
|
|
>
|
|
{{ manageableText.picto_text }}
|
|
</p>
|
|
</div>
|
|
<!-- Desktop swiper -->
|
|
<div class="breeder-swiper-container d-none d-lg-block">
|
|
<div
|
|
class="swiper swiper-breeders-map position-relative"
|
|
id="swiper-breeders-map"
|
|
>
|
|
<div class="swiper-wrapper w-fit h-fit">
|
|
<div
|
|
class="swiper-slide h-100"
|
|
v-for="(breederModel, id) in breederModels"
|
|
:key="id"
|
|
>
|
|
<div v-html="breederModel" />
|
|
</div>
|
|
</div>
|
|
<button class="swiper-btns swiper-btn-prev border-0 d-flex justify-content-center align-items-center position-absolute top-50 z-1 bg-white rounded-pill">
|
|
<img
|
|
src="@/images/common/arrow-brown.svg"
|
|
:title="__('Navigation du slider (gauche)')"
|
|
:alt="__('flèche vers la gauche')"
|
|
role="button"
|
|
>
|
|
</button>
|
|
<button class="swiper-btns swiper-btn-next border-0 d-flex justify-content-center align-items-center position-absolute top-50 translate-middle-y z-1 bg-white rounded-pill">
|
|
<img
|
|
src="@/images/common/arrow-brown.svg"
|
|
:title="__('Navigation du slider (droite)')"
|
|
:alt="__('flèche vers la droite')"
|
|
role="button"
|
|
>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-12 col-lg-6">
|
|
<div id="map" />
|
|
</div>
|
|
<!-- Mobile swiper -->
|
|
<div class="breeder-swiper-container d-lg-none">
|
|
<div
|
|
class="swiper swiper-breeders-map position-relative"
|
|
id="swiper-breeders-map"
|
|
>
|
|
<div class="swiper-wrapper w-fit h-fit">
|
|
<div
|
|
class="swiper-slide h-100"
|
|
v-for="(breederModel, id) in breederModels"
|
|
:key="id"
|
|
>
|
|
<div v-html="breederModel" />
|
|
</div>
|
|
</div>
|
|
<button class="swiper-btns swiper-btn-prev border-0 d-flex justify-content-center align-items-center position-absolute top-50 z-1 bg-white rounded-pill">
|
|
<img
|
|
src="@/images/common/arrow-brown.svg"
|
|
:title="__('Navigation du slider (gauche)')"
|
|
:alt="__('flèche vers la gauche')"
|
|
role="button"
|
|
>
|
|
</button>
|
|
<button class="swiper-btns swiper-btn-next border-0 d-flex justify-content-center align-items-center position-absolute top-50 translate-middle-y z-1 bg-white rounded-pill">
|
|
<img
|
|
src="@/images/common/arrow-brown.svg"
|
|
:title="__('Navigation du slider (droite)')"
|
|
:alt="__('flèche vers la droite')"
|
|
role="button"
|
|
>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import * as departmentGeoJson from "@/documents/departments.json";
|
|
import { __ } from "@modules/trans";
|
|
import type { GeoJsonObject } from "geojson";
|
|
import * as L from "leaflet";
|
|
import Swiper from "swiper";
|
|
import { Navigation } from "swiper/modules";
|
|
import { nextTick, onBeforeMount, onMounted, ref, useAttrs, watch } from "vue";
|
|
import axios from "~/axios";
|
|
import parseLaravelErrorMessages from "./../../modules/parseLaravelErrorMessages";
|
|
import route from "./../../modules/route";
|
|
|
|
defineOptions({
|
|
name: "SearchModelComponent"
|
|
});
|
|
|
|
// * ATTRIBUTES
|
|
const attrs = useAttrs();
|
|
|
|
// * REFS
|
|
const breederMap = ref<HTMLDivElement|null>(null);
|
|
|
|
// * DATA
|
|
const breederModels = ref<LaravelModelList>([]);
|
|
const selectedDepartment = ref<number>();
|
|
const countPerDepartment = ref<Record<string, string|number>[]>();
|
|
const manageableText = ref<{
|
|
title: string,
|
|
main_text: string,
|
|
picto_text: string,
|
|
}>({
|
|
title: "Découvrez les portraits des éleveurs",
|
|
main_text: "Amet minim mollit non deserunt ullamco est sit aliqua dolor do amet sint. Velit officia consequat duis enim velit mollit. Exercitation veniam consequat sunt nostrud amet.",
|
|
picto_text: "Tous les élevages sont situés à moins de 200km de nos sites de préparation",
|
|
});
|
|
|
|
const swiper = ref<Swiper[]>([]);
|
|
|
|
// * WATCHERS
|
|
watch(countPerDepartment, () => {
|
|
selectedDepartment.value = getRandomDepartment();
|
|
getDepartmentBreeders();
|
|
initBreederSwiper();
|
|
initMap();
|
|
});
|
|
|
|
// * LIFECYCLE
|
|
onBeforeMount(() => {
|
|
getBreederCountPerDepartment();
|
|
getManageableContent();
|
|
});
|
|
onMounted(() => {
|
|
const json = String(attrs.json ?? "{}"),
|
|
data = JSON.parse(json);
|
|
|
|
selectedDepartment.value = Number.isInteger(data.selectedDepartment) ? data.selectedDepartment : 72;
|
|
});
|
|
|
|
function getManageableContent() {
|
|
const getMangeableContentRoute = route("fo.breeders.get-manageable-text-component");
|
|
if (!getMangeableContentRoute) {
|
|
throw new Error("Undefined route " + getMangeableContentRoute);
|
|
}
|
|
window.axios
|
|
.get(getMangeableContentRoute)
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
.then((response: { data: any; }) => {
|
|
manageableText.value = response.data;
|
|
})
|
|
.catch(ajaxErrorHandler);
|
|
|
|
}
|
|
|
|
// * BREEDERS
|
|
function getRandomDepartment(defaultDepartment: number = 72) {
|
|
if (!countPerDepartment.value) {
|
|
return defaultDepartment;
|
|
}
|
|
const randomKey = Math.floor(Math.random() * countPerDepartment.value.length);
|
|
|
|
return Number(countPerDepartment.value[randomKey].department);
|
|
}
|
|
|
|
function getDepartmentBreeders() {
|
|
const getDepartmentBreedersViewsRoute = route("fo.breeders.get-department-breeders-views");
|
|
if (!getDepartmentBreedersViewsRoute) {
|
|
throw new Error("Undefined route " + getDepartmentBreedersViewsRoute);
|
|
}
|
|
window.axios
|
|
.post(getDepartmentBreedersViewsRoute, { DEPARTMENT: String(selectedDepartment.value) })
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
.then((response: { data: any; }) => {
|
|
breederModels.value = response.data;
|
|
})
|
|
.then(() => {
|
|
refreshSwiper();
|
|
})
|
|
.catch(ajaxErrorHandler);
|
|
return;
|
|
}
|
|
|
|
function getBreederCountPerDepartment() {
|
|
const getBreederCountRoute = route("fo.breeders.get-count-per-department");
|
|
if (!getBreederCountRoute) {
|
|
throw new Error("Undefined route " + getBreederCountRoute);
|
|
}
|
|
window.axios
|
|
.get(getBreederCountRoute)
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
.then((response: { data: any; }) => {
|
|
countPerDepartment.value = response.data;
|
|
})
|
|
.catch(ajaxErrorHandler);
|
|
return;
|
|
}
|
|
|
|
function getDepartmentBreederCount(departmentNumber: number) {
|
|
if (!countPerDepartment.value) {
|
|
return 0;
|
|
}
|
|
const departmentDatas = countPerDepartment.value.find(
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
(count: any) => {
|
|
return count.department === departmentNumber;
|
|
}
|
|
);
|
|
if (!departmentDatas || !departmentDatas.total) {
|
|
return 0;
|
|
}
|
|
return departmentDatas.total;
|
|
}
|
|
|
|
// * SWIPER
|
|
/**
|
|
* Init breeder slider.
|
|
* @return void
|
|
*/
|
|
function initBreederSwiper(): void {
|
|
const swiperHtmlElements = document.querySelectorAll(".swiper-breeders-map") as NodeListOf<HTMLElement>;
|
|
swiperHtmlElements.forEach((element) => {
|
|
swiper.value.push(new Swiper(element, {
|
|
modules: [Navigation],
|
|
grabCursor: true,
|
|
slidesPerView: 1,
|
|
spaceBetween: 20,
|
|
breakpoints: {
|
|
992: {
|
|
spaceBetween: 50
|
|
}
|
|
},
|
|
observer: true,
|
|
navigation: {
|
|
nextEl: ".swiper-btn-next",
|
|
prevEl: ".swiper-btn-prev",
|
|
},
|
|
}));
|
|
});
|
|
nextTick(() => {
|
|
if (swiper.value) {
|
|
swiper.value.forEach(swiperElement => {
|
|
swiperElement.on("slidesUpdated", () => {
|
|
setSwiperCount(swiperElement);
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Refresh the swiper in this Vue component
|
|
*
|
|
* @return void
|
|
*/
|
|
function refreshSwiper() {
|
|
if (swiper.value) {
|
|
swiper.value.forEach((swiperElement: Swiper) => {
|
|
swiperElement.update();
|
|
swiperElement.slideTo(0);
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Refresh the swiper in this Vue component
|
|
*
|
|
* @return void
|
|
*/
|
|
function setSwiperCount(swiper: Swiper) {
|
|
let i = 1;
|
|
for (const currentSlideSpan of document.querySelectorAll(".breeder-map-component .breeder-card-current")) {
|
|
currentSlideSpan.textContent = String(i);
|
|
i++;
|
|
if (i > swiper.slides.length) {
|
|
i = 1;
|
|
}
|
|
}
|
|
|
|
for (const totalSlideSpan of document.querySelectorAll(".breeder-map-component .breeder-card-total")) {
|
|
totalSlideSpan.textContent = String(swiper.slides.length);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
// * MAP
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
function addGeoJsonLayerClickEvent(feature: any, layer: L.Layer) {
|
|
layer.on("click", (e) => {
|
|
if (
|
|
selectedDepartment.value == feature.properties.code ||
|
|
getDepartmentBreederCount(feature.properties.code) === 0
|
|
) {
|
|
return;
|
|
}
|
|
|
|
const clickedDepartmentElement = e.originalEvent.target as HTMLElement;
|
|
if (clickedDepartmentElement) {
|
|
resetDepartmentColors();
|
|
colorClickedDepartment(clickedDepartmentElement);
|
|
}
|
|
|
|
selectedDepartment.value = feature.properties.code;
|
|
getDepartmentBreeders();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Set a class on the given HTMLElement to make it active.
|
|
*
|
|
* @return void
|
|
*/
|
|
function colorClickedDepartment(clickedDepartmentHtml: HTMLElement) {
|
|
clickedDepartmentHtml.classList.add("active-department");
|
|
}
|
|
|
|
/**
|
|
* Remove the active-department class on all department elements.
|
|
*
|
|
* @return void
|
|
*/
|
|
function resetDepartmentColors() {
|
|
for(const departmentElement of document.querySelectorAll(".breeder-map-component #map .active-department") as NodeListOf<HTMLElement>) {
|
|
departmentElement.classList.remove("active-department");
|
|
}
|
|
}
|
|
|
|
function initMap() {
|
|
const map = L.map("map").setView([
|
|
46.3626304852, 2.33629344655
|
|
], 5);
|
|
|
|
const tileUrl = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png";
|
|
|
|
const osm = L.tileLayer(tileUrl, {
|
|
minZoom: 5,
|
|
maxZoom: 10
|
|
});
|
|
|
|
osm.addTo(map);
|
|
|
|
const departments = new L.GeoJSON(
|
|
departmentGeoJson as GeoJsonObject,
|
|
{
|
|
style: function (feature) {
|
|
if (String(feature?.properties.code) === String(selectedDepartment.value)) {
|
|
return {
|
|
className: "active-department department",
|
|
weight: 1,
|
|
};
|
|
}
|
|
return {
|
|
className: "department",
|
|
weight: 1,
|
|
};
|
|
},
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
onEachFeature: (feature: any, layer: any) => {
|
|
if (!countPerDepartment.value) {
|
|
return;
|
|
}
|
|
|
|
const departmentBreederCount = getDepartmentBreederCount(feature.properties.code);
|
|
|
|
if (departmentBreederCount && departmentBreederCount !== 0) {
|
|
const divIconHtml = document.createElement("p");
|
|
divIconHtml.textContent = String(departmentBreederCount);
|
|
|
|
const breederCountMarkerIcon = L.divIcon({
|
|
html: divIconHtml,
|
|
className: "breeder-count-marker",
|
|
});
|
|
const center = layer.getBounds().getCenter();
|
|
const marker = L.marker(center, {icon: breederCountMarkerIcon});
|
|
marker.addTo(map);
|
|
}
|
|
|
|
addGeoJsonLayerClickEvent(feature, layer);
|
|
}
|
|
});
|
|
departments.addTo(map);
|
|
|
|
map.on("zoomend", () => {
|
|
hideCountMarker(map);
|
|
});
|
|
map.whenReady(() => {
|
|
hideCountMarker(map);
|
|
});
|
|
}
|
|
|
|
function hideCountMarker(map: L.Map) {
|
|
const countMarkers = document.querySelectorAll(".breeder-map-component #map .breeder-count-marker");
|
|
if (map.getZoom() < 6) {
|
|
for (const breederCountElement of countMarkers) {
|
|
breederCountElement.classList.add("d-none");
|
|
}
|
|
} else {
|
|
for (const breederCountElement of countMarkers) {
|
|
breederCountElement.classList.remove("d-none");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Return error message after the ajax call.
|
|
* @param e any
|
|
* @return void
|
|
*/
|
|
function ajaxErrorHandler(e: unknown): void {
|
|
let message = __("Une erreur est survenue");
|
|
|
|
if (axios.isAxiosError<LaravelValidationErrorResponse>(e)) {
|
|
if (e.response) {
|
|
if (e.response.status === 422) {
|
|
message = parseLaravelErrorMessages.methods.parseValidationErrors(
|
|
e.response?.data?.errors ?? {}
|
|
) || message;
|
|
if (window.vueDebug) {
|
|
console.warn(e.response.data.errors);
|
|
}
|
|
} else if (e.response.status === 419) {
|
|
console.log(__("Votre session a expiré, la page va être rechargée"));
|
|
setTimeout(() => window.location.reload(), 2000);
|
|
} else if (e.response.status === 403) {
|
|
console.log(__("Vous n'êtes pas autorisé a effectuer cette action"));
|
|
setTimeout(() => window.location.reload(), 2000);
|
|
} else {
|
|
if (window.vueDebug) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
} else if (e.request) {
|
|
console.error("Aucune réponse reçue :", e.request);
|
|
} else {
|
|
console.error("Erreur de configuration de la requête :", e.message);
|
|
}
|
|
}
|
|
console.log(message);
|
|
}
|
|
</script>
|
|
|
|
<style lang="scss" scopped>
|
|
@import "bootstrap/scss/functions";
|
|
@import "bootstrap/scss/variables";
|
|
@import "bootstrap/scss/mixins";
|
|
@import "swiper/css";
|
|
@import "swiper/css/navigation";
|
|
|
|
.breeder-map-component {
|
|
.swiper-btns {
|
|
width: 2rem;
|
|
height: 2rem;
|
|
}
|
|
.swiper-btn-prev {
|
|
left: 0;
|
|
transform: translateY(-50%) rotate(180deg);
|
|
@include media-breakpoint-up(xxl) {
|
|
left: 0;
|
|
}
|
|
}
|
|
.swiper-btn-next {
|
|
right: 0;
|
|
@include media-breakpoint-up(xxl) {
|
|
right: 0;
|
|
}
|
|
}
|
|
|
|
#map {
|
|
height: 360px;
|
|
@include media-breakpoint-up(lg) {
|
|
min-height: 500px;
|
|
height: 100%;
|
|
}
|
|
|
|
.department {
|
|
color: #fff;
|
|
}
|
|
|
|
.active-department {
|
|
fill: #422B30;
|
|
fill-opacity: 0.8;
|
|
// color: "#422B30";
|
|
}
|
|
|
|
.breeder-count-marker {
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
width: 1.5rem !important;
|
|
height: 1.5rem !important;
|
|
background-color: #422B30;
|
|
color: #fff;
|
|
border-radius: 100%;
|
|
font-family: BourtonBase;
|
|
|
|
p {
|
|
margin: 0;
|
|
font-size: 1rem;
|
|
line-height: 2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</style>
|