custom dynamic sites svg map
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
@font-face {
|
||||
font-family: 'gilroy-semibold';
|
||||
src: url('fonts/Gilroy/gilroy-semibold-webfont.woff') format('woff');
|
||||
}
|
||||
#sites-map-container {
|
||||
background-color: #c3cef2;
|
||||
text-align: center;
|
||||
}
|
||||
/* #sites-map-container .circle {
|
||||
fill: black;
|
||||
stroke: white;
|
||||
stroke-width: 0.7;
|
||||
} */
|
||||
|
||||
/* Changer la couleur au survol (hover) */
|
||||
#sites-map-container .site-link:hover{
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#sites-map-container .site-link:hover circle.circle {
|
||||
fill: #f7002b!important;
|
||||
}
|
||||
|
||||
/* Styles du popup (caché par défaut) */
|
||||
#sites-map-container #popup {
|
||||
font-family: 'gilroy-semibold';
|
||||
display: none;
|
||||
position: fixed;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
#sites-map-container strong{
|
||||
color: #f7002b;
|
||||
font-family: 'gilroy-semibold';
|
||||
}
|
||||
#sites-map-container a{
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
color: white;
|
||||
background: black;
|
||||
text-transform: uppercase;
|
||||
font-size: 0.8rem;
|
||||
padding-left: 0.5rem;
|
||||
text-decoration: none;
|
||||
margin-top: 0.7rem;
|
||||
height: 1.3rem;
|
||||
}
|
||||
#sites-map-container a:after{
|
||||
display: inline-flex;
|
||||
content: url("../img/noun-arrow-to-right.svg");
|
||||
padding-right: 0.2rem;
|
||||
padding-left: 0.2rem;
|
||||
|
||||
}
|
0
web/modules/custom/q2d_mod/assets/css/fontface.css
Normal file
0
web/modules/custom/q2d_mod/assets/css/fontface.css
Normal file
Binary file not shown.
@@ -0,0 +1,91 @@
|
||||
// Sélection des éléments au préalable pour éviter de les rechercher plusieurs fois
|
||||
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');
|
||||
console.log('svgElement', svgElement);
|
||||
|
||||
// Fonction pour afficher le popup
|
||||
function showPopup(content, x, y) {
|
||||
console.log('showPopup', content, x, y);
|
||||
|
||||
popupContent.innerHTML = content;
|
||||
|
||||
// Récupérer la position de l'élément SVG seulement une fois
|
||||
const rect = svgElement.getBoundingClientRect();
|
||||
|
||||
// Calculer la position du popup par rapport à l'élément SVG
|
||||
const popupX = rect.left + x + 120; // Décalage de 120px à droite
|
||||
const popupY = rect.top + y + 20; // Décalage de 20px en dessous
|
||||
|
||||
// Dimensions de la fenêtre et du popup
|
||||
const viewportWidth = window.innerWidth;
|
||||
const viewportHeight = window.innerHeight;
|
||||
const popupWidth = popup.offsetWidth;
|
||||
const popupHeight = popup.offsetHeight;
|
||||
|
||||
// Vérification des bordures droite et gauche
|
||||
if (popupX + popupWidth > viewportWidth) {
|
||||
popupX = viewportWidth - popupWidth - 10; // Ajuster pour qu'il ne dépasse pas à droite
|
||||
}
|
||||
if (popupX < 0) {
|
||||
popupX = 10; // S'assurer que le popup ne dépasse pas à gauche
|
||||
}
|
||||
|
||||
// Vérification des bordures inférieure et supérieure
|
||||
if (popupY + popupHeight > viewportHeight) {
|
||||
popupY = viewportHeight - popupHeight - 10; // Ajuster pour qu'il ne dépasse pas en bas
|
||||
}
|
||||
if (popupY < 0) {
|
||||
popupY = 10; // S'assurer que le popup ne dépasse pas en haut
|
||||
}
|
||||
|
||||
popup.style.left = `${popupX}px`;
|
||||
popup.style.top = `${popupY}px`;
|
||||
popup.style.display = 'block';
|
||||
modalBackground.style.display = 'block'; // Activer le fond modal si nécessaire
|
||||
}
|
||||
|
||||
// Recalculer la position du popup lors du redimensionnement de la fenêtre
|
||||
window.addEventListener('resize', function() {
|
||||
if (popup.style.display === 'block') {
|
||||
// Recalculer les coordonnées et ajuster la position
|
||||
const currentPopupRect = popup.getBoundingClientRect();
|
||||
showPopup(popupContent.innerHTML, currentPopupRect.left, currentPopupRect.top);
|
||||
}
|
||||
});
|
||||
|
||||
// Fonction pour fermer le popup
|
||||
function closePopup() {
|
||||
popup.style.display = 'none';
|
||||
modalBackground.style.display = 'none'; // Désactiver le fond modal
|
||||
}
|
||||
|
||||
// Ajout d'un écouteur d'événement sur l'élément SVG pour tous les cercles
|
||||
svgElement.addEventListener('click', function(event) {
|
||||
console.log('svgElement click', event.target);
|
||||
|
||||
if (event.target.classList.contains('site-link')) {
|
||||
// Récupérer le contenu personnalisé de l'attribut data-content
|
||||
const content = event.target.getAttribute('data-content');
|
||||
|
||||
// Récupérer la position du cercle cliqué par rapport à l'élément SVG
|
||||
const rect = svgElement.getBoundingClientRect();
|
||||
const circleX = event.clientX - rect.left;
|
||||
const circleY = event.clientY - rect.top;
|
||||
|
||||
// Afficher le popup avec le contenu spécifique
|
||||
showPopup(content, circleX, circleY);
|
||||
|
||||
// Empêcher la propagation de l'événement pour éviter la fermeture immédiate
|
||||
event.stopPropagation();
|
||||
}
|
||||
});
|
||||
|
||||
// Ferme le popup quand on clique n'importe où sur la page, sauf sur le popup
|
||||
document.addEventListener('click', function(event) {
|
||||
// Vérifier si le clic s'est produit en dehors du popup
|
||||
if (popup.style.display === 'block' && !popup.contains(event.target)) {
|
||||
closePopup();
|
||||
}
|
||||
});
|
7
web/modules/custom/q2d_mod/q2d_mod.libraries.yml
Normal file
7
web/modules/custom/q2d_mod/q2d_mod.libraries.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
sites_map_block:
|
||||
css:
|
||||
theme:
|
||||
assets/css/fontface.css: {}
|
||||
assets/css/carte-interactive-qdd.css: {}
|
||||
js:
|
||||
assets/js/carte-interactive-qdd.js: {}
|
@@ -5,3 +5,13 @@
|
||||
* Primary module hooks for reha module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function q2d_mod_theme() {
|
||||
return array(
|
||||
'svg_mapsites' => array(
|
||||
'variables' => array('vpw' => null, 'vph' => null, 'sites' => []),
|
||||
),
|
||||
);
|
||||
}
|
113
web/modules/custom/q2d_mod/src/Plugin/Block/SitesMap.php
Normal file
113
web/modules/custom/q2d_mod/src/Plugin/Block/SitesMap.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\q2d_mod\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Block\Attribute\Block;
|
||||
// use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\Core\Link;
|
||||
use Drupal\Core\Render\Markup;
|
||||
|
||||
/**
|
||||
* Provides a 'Sites Map' Block.
|
||||
* @Block(
|
||||
* id = "sitesmap_block",
|
||||
* admin_label = @Translation("Sites map Block"),
|
||||
* )
|
||||
*/
|
||||
class SitesMap extends BlockBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
$return = null;
|
||||
|
||||
$allSites = \Drupal::entityTypeManager()->getStorage('node')
|
||||
->loadByProperties(['type' => 'site', 'status' => 1]);
|
||||
|
||||
$sites_paths = "";
|
||||
|
||||
$vp_w = 400;
|
||||
$vp_h = 400;
|
||||
|
||||
// Coordonnées géographiques des coins de la carte (France)
|
||||
$latTop = 52.0; // Nord-Ouest (coin supérieur gauche)
|
||||
$lonLeft = -6.0;
|
||||
$latBottom = 40.0; // Sud-Est (coin inférieur droit)
|
||||
$lonRight = 11.0;
|
||||
|
||||
|
||||
foreach($allSites as $index => $site){
|
||||
$title = $site->get('title')->getString();
|
||||
$subtitle = $site->get('field_sous_titre')->getString();
|
||||
|
||||
$link_options = ['absolute' => FALSE, 'attributes' => ['class' => 'site-link']];
|
||||
$site_link_object = Link::createFromRoute("voir le site", 'entity.node.canonical', ['node' => $site->id()], $link_options);
|
||||
$link = $site_link_object->toString()->getGeneratedLink();
|
||||
|
||||
$datacontent = htmlspecialchars("<strong>$title</strong><br>$subtitle<br>$link");
|
||||
|
||||
$geofield = $site->get('field_geofield')->get(0);
|
||||
$lon = $geofield->lon;
|
||||
$lat = $geofield->lat;
|
||||
|
||||
$x = round(($lon - $lonLeft) / ($lonRight - $lonLeft) * $vp_w);
|
||||
$y = round(($latTop - $lat) / ($latTop - $latBottom) * $vp_h);
|
||||
|
||||
$r = 10;
|
||||
$m = -$r+2;
|
||||
$l = $r*2-4;
|
||||
$sites_paths .= <<<SVGSITEPATH
|
||||
<g
|
||||
id="site-$index"
|
||||
transform="
|
||||
translate($x,$y)">
|
||||
<circle
|
||||
id="path83" class="circle"
|
||||
cx="0" cy="0" r="$r"
|
||||
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;" />
|
||||
<path
|
||||
id="path84"
|
||||
d="m 0,$m v $l"
|
||||
style="fill:none;stroke:#ffffff;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1" />
|
||||
<path
|
||||
id="path85"
|
||||
d="m $m,0 h $l"
|
||||
style="fill:none;stroke:#ffffff;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1" />
|
||||
<circle
|
||||
class="site-link"
|
||||
data-content="$datacontent"
|
||||
cx="0" cy="0" r="$r"
|
||||
style="fill:#000000;fill-opacity:0;fill-rule:nonzero;" />
|
||||
</g>
|
||||
SVGSITEPATH;
|
||||
}
|
||||
|
||||
|
||||
$return = [
|
||||
'#cache' => [
|
||||
'max-age' => 0,
|
||||
],
|
||||
'svg_mapsites' => [
|
||||
'#theme' => 'svg_mapsites',
|
||||
'#sites' => $sites_paths,
|
||||
'#vpw' => $vp_w,
|
||||
'#vph' => $vp_h,
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
'q2d_mod/sites_map_block',
|
||||
],
|
||||
],
|
||||
]
|
||||
];
|
||||
|
||||
return $return;
|
||||
// return [
|
||||
// '#markup' => $this->t('Hello, Sites Map!'),
|
||||
// ];
|
||||
}
|
||||
public function getCacheMaxAge() {
|
||||
return 0;
|
||||
}
|
||||
}
|
50
web/modules/custom/q2d_mod/templates/svg-mapsites.html.twig
Normal file
50
web/modules/custom/q2d_mod/templates/svg-mapsites.html.twig
Normal file
@@ -0,0 +1,50 @@
|
||||
<div id="sites-map-container">
|
||||
{# <svg {{ svg_attributes }}>
|
||||
<path {{ path_attributes }}></path>
|
||||
</svg> #}
|
||||
<svg
|
||||
width="{{ vpw }}"
|
||||
height="{{ vph }}"
|
||||
viewBox="0 0 100% 400px"
|
||||
version="1.1"
|
||||
id="svg1"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg">
|
||||
|
||||
<g
|
||||
id="layer1"
|
||||
transform="
|
||||
translate(-25.237685,-27.066503)
|
||||
scale(2 2)"
|
||||
>
|
||||
<g
|
||||
id="g87"
|
||||
transform="matrix(0.26458333,0,0,0.26458333,-117.12499,-462.76799)"
|
||||
>
|
||||
<path
|
||||
d="M 22.94948,0 24.34889,-18.0149 0,-25.72302 5.65439,-72.56693 20.36713,-85.65977 31.67962,-51.59929 30.80613,-4.91625 Z"
|
||||
style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none"
|
||||
transform="matrix(1.3333333,0,0,-1.3333333,1086.6494,2370.4282)"
|
||||
id="path86" />
|
||||
<path
|
||||
d="M 213.91022,-8.56159 245.88833,0 c 0,0 58.73522,-78.05274 188.63529,-102.77609 0,0 -13.72994,-23.9062 -20.7684,-68.77233 l -46.76509,-59.13289 22.59883,4.10628 9.98215,-59.15478 -15.22238,-6.69194 c 0,0 21.10627,-42.02694 33.11738,-51.15904 l -49.61728,-51.89156 c 0,0 -54.37257,40.3184 -83.08495,23.05862 -26.13166,-16.38332 -20.26669,-47.73592 -20.26669,-47.73592 l -84.48157,7.5286 -73.42912,23.87167 c 0,0 13.68285,68.86637 13.5616,120.85908 l 13.70164,-16.03089 -8.31311,25.90453 c -7.71795,24.04996 -30.44003,42.76792 -30.59939,44.58536 -3.6948,42.13802 -38.37183,51.8082 -82.82932,56.0101 L 0,-120.94655 l 48.38804,11.38921 63.90371,-13.15789 c 0,0 -1.62874,19.5427 -17.78042,51.54499 l 20.25382,7.61331 c 0,0 16.51357,-27.83033 25.80184,-26.51089 4.8781,0.69253 23.95695,21.70897 32.12568,26.76126 7.32295,4.5292 35.79484,10.58956 36.55389,13.534 2.94469,10.87069 4.66366,41.21097 4.66366,41.21097 z"
|
||||
style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none"
|
||||
transform="matrix(1.3333333,0,0,-1.3333333,543.93475,1857.6088)"
|
||||
id="path87" />
|
||||
</g>
|
||||
</g>
|
||||
<g
|
||||
id="layer2">
|
||||
{{ sites|raw }}
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
<!-- Fond du modal -->
|
||||
<div id="modal-background" class="modal-background"></div>
|
||||
|
||||
<!-- Popup -->
|
||||
<div id="popup" class="popup">
|
||||
<p id="popup-content">Hello! I am a popup!</p>
|
||||
<!--<button onclick="closePopup()">Close</button>-->
|
||||
</div>
|
||||
</div>
|
Reference in New Issue
Block a user