drupal-quartiersdedemain/web/modules/custom/q2d_mod/assets/js/carte-interactive-qdd.js

104 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-11-05 21:45:42 +01:00
// Sélection des éléments principaux
2024-10-01 21:57:47 +02:00
const svgElement = document.querySelector('#sites-map-container svg');
const popup = document.querySelector('#sites-map-container #popup');
const popupContent = document.querySelector('#sites-map-container #popup-content');
const modalBackground = document.querySelector('#sites-map-container #modal-background');
// Vérifiez si la page contient la classe '.node-type-site'
const isSitePage = document.body.classList.contains('node-type-site');
2024-11-07 09:20:04 +01:00
const isLessitesPage = document.querySelector('#lessites') !== null;
2024-10-01 21:57:47 +02:00
// Fonction pour afficher le popup
2024-10-07 14:55:03 +02:00
function showPopup(content, x, y, isLeftHalf) {
2024-11-05 21:45:42 +01:00
if (isSitePage || isLessitesPage) return;
2024-10-01 21:57:47 +02:00
popupContent.innerHTML = content;
popup.style.display = 'block';
2024-10-04 14:21:09 +02:00
modalBackground.style.display = 'block';
2024-10-01 21:57:47 +02:00
}
// Fonction pour fermer le popup
function closePopup() {
popup.style.display = 'none';
2024-11-07 09:20:04 +01:00
modalBackground.style.display = 'none';
2024-10-01 21:57:47 +02:00
}
2024-11-05 21:45:42 +01:00
// Gérer le survol des cercles
2024-10-07 14:55:03 +02:00
svgElement.addEventListener('mouseover', function(event) {
2024-10-01 21:57:47 +02:00
if (event.target.classList.contains('site-link')) {
const content = event.target.getAttribute('data-content');
2024-11-07 09:20:04 +01:00
const dataId = event.target.getAttribute('data-id');
2024-11-05 21:45:42 +01:00
const correspondingRow = document.querySelector(`.views-row[data-id="${dataId}"]`);
2024-10-08 21:10:45 +02:00
showPopup(content);
2024-11-07 09:20:04 +01:00
if (correspondingRow) correspondingRow.style.border = '2px solid red';
2024-10-01 21:57:47 +02:00
}
if (event.target.tagName === 'circle') {
2024-10-08 11:47:01 +02:00
event.target.setAttribute('fill', 'red');
}
2024-10-01 21:57:47 +02:00
});
2024-11-05 21:45:42 +01:00
// Fermer le popup et retirer la bordure rouge lorsque la souris quitte le cercle
2024-10-07 14:55:03 +02:00
svgElement.addEventListener('mouseout', function(event) {
if (event.target.classList.contains('site-link')) {
2024-10-01 21:57:47 +02:00
closePopup();
2024-11-05 21:45:42 +01:00
const dataId = event.target.getAttribute('data-id');
const correspondingRow = document.querySelector(`.views-row[data-id="${dataId}"]`);
2024-11-07 09:20:04 +01:00
if (correspondingRow) correspondingRow.style.border = 'none';
2024-10-01 21:57:47 +02:00
}
if (event.target.tagName === 'circle') {
2024-11-07 09:20:04 +01:00
event.target.setAttribute('fill', 'black');
2024-10-08 11:47:01 +02:00
}
2024-10-01 21:57:47 +02:00
});
2024-10-08 14:44:25 +02:00
2024-11-07 09:20:04 +01:00
// Fonction pour recalculer les positions des sites
2024-10-08 14:44:25 +02:00
function recalculateSitePositions() {
const allSites = document.querySelectorAll('.site-link');
2024-11-07 09:20:04 +01:00
allSites.forEach((site) => {
const geofield = site.getAttribute('data-geofield');
2024-10-08 14:44:25 +02:00
const lon = parseFloat(geofield.split(',')[0]);
const lat = parseFloat(geofield.split(',')[1]);
2024-11-07 09:20:04 +01:00
const x = Math.round((lon - lonLeft) / (lonRight - lonLeft) * vp_w);
const y = Math.round((latTop - lat) / (latTop - latBottom) * vp_h);
2024-10-08 14:44:25 +02:00
site.setAttribute('transform', `translate(${x}, ${y})`);
});
}
window.addEventListener('resize', function() {
recalculateSitePositions();
if (popup.style.display === 'block') {
const currentPopupRect = popup.getBoundingClientRect();
const rect = svgElement.getBoundingClientRect();
showPopup(popupContent.innerHTML, currentPopupRect.left - rect.left, currentPopupRect.top - rect.top);
}
});
2024-10-08 15:21:09 +02:00
// Gérer le clic ou le toucher des cercles
function handleCircleClick(content) {
showPopup(content, event.clientX, event.clientY, event.target.getAttribute('data-left-half') === 'true');
}
2024-11-07 09:20:04 +01:00
// Écouter les événements de clic sur le SVG
2024-10-08 15:21:09 +02:00
svgElement.addEventListener('click', function(event) {
2024-11-07 09:20:04 +01:00
if (event.target.classList.contains('site-link')) {
2024-10-08 15:21:09 +02:00
const content = event.target.getAttribute('data-content');
handleCircleClick(content);
2024-11-07 09:20:04 +01:00
} else if (event.target.tagName === 'circle') {
const circleId = event.target.id; // Récupérer l'ID du cercle cliqué
if (circleId.startsWith('site-')) {
const pageId = circleId.split('-')[1]; // Extraire l'ID numérique du site
window.location.href = `sites/node/${pageId}`; // Rediriger vers la page correspondante
}
2024-10-08 15:21:09 +02:00
}
2024-11-07 09:20:04 +01:00
2024-10-08 15:21:09 +02:00
});
2024-10-08 21:10:45 +02:00
2024-11-07 09:20:04 +01:00
// Mise en évidence du point rouge correspondant à la page courante
const body = document.querySelector('body');
2024-11-07 09:20:04 +01:00
const pageIdMatch = body.className.match(/node-id-(\d+)/);
if (pageIdMatch) {
const pageId = pageIdMatch[1];
const matchingCircle = document.querySelector(`#site-${pageId} circle`);
if (matchingCircle) matchingCircle.setAttribute('fill', 'red');
}