drupal-caravane/web/themes/custom/caravane/assets/js/main.js

253 lines
8.5 KiB
JavaScript
Raw Normal View History

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import '../scss/main.scss'
import Modale from './vuejs/Modale.vue'
2024-09-29 21:36:21 +02:00
import VueImageZoomer from 'vue-image-zoomer'
import 'vue-image-zoomer/dist/style.css';
import { useContentStore } from './stores/content';
import { useMapStore } from './stores/mapState';
2024-08-05 21:08:09 +02:00
import router from './router/router';
// Working with the history API
// https://developer.mozilla.org/en-US/docs/Web/API/History_API/Working_with_the_History_API
2024-07-16 17:01:09 +02:00
// /**
// * @file
// * reha behaviors.
// * https://www.drupal.org/docs/drupal-apis/javascript-api/javascript-api-overview
// */
// (function (Drupal) {
2024-07-16 17:01:09 +02:00
// 'use strict';
// Drupal.behaviors.reha = {
// attach: function (context, settings) {
// console.log('It works!');
// }
// };
// } (Drupal));
(function ($, Drupal, drupalSettings) {
2024-07-16 17:01:09 +02:00
const CaravaneTheme = function () {
const _is_front = drupalSettings.path.isFront;
console.log('drupalSettings', drupalSettings);
2024-07-16 17:01:09 +02:00
function init () {
console.log('CaravaneTheme init()');
initVues();
toggleMenu();
2024-07-16 17:01:09 +02:00
}
function initVues(){
initVueContentModale();
2024-10-15 10:09:10 +02:00
2024-07-16 17:01:09 +02:00
}
function initVueContentModale(){
2024-09-29 21:36:21 +02:00
const app = createApp(Modale)
.use(createPinia()).use(router)
.use(VueImageZoomer);
const store = useContentStore();
const mapStore = useMapStore();
app.mount('#content-modale');
setHamburgerWhenLogged();
Drupal.behaviors.customLeafletInteraction = {
attach: function(context, settings) {
$(context).on('leafletMapInit', function (e, settings, map, mapid, markers) {
mapStore.defaultMapCenter = map.getCenter();
mapStore.maxZoom = settings.settings.maxZoom;
mapStore.defaultZoom = settings.settings.minZoom;
2024-10-15 10:09:10 +02:00
initFirstLoadRouting(store, map);
processEtapeLinks(store, map);
processStaticLinks(store, map);
processHeaderLogo(store, map);
setupEtapeMapPopup(store, map);
});
}
}
}
2024-10-15 10:09:10 +02:00
function initFirstLoadRouting(store, map){
var decoupled_origin = JSON.parse(window.localStorage.getItem('decoupled_origin'));
console.log('decoupled_origin', decoupled_origin);
if(decoupled_origin && decoupled_origin.entity_id){
// Si c'était moi je ne ferais qu'une seule function fetchdata capable de dealer avec les différent type de contenus
switch (decoupled_origin.entity_bundle) {
case 'etape':
store.fetchEtapeData(decoupled_origin.entity_id, map);
break;
case 'static':
store.fetchEtapeData(decoupled_origin.entity_id, map);
break;
}
router.push({
// name: decoupled_origin.entity_bundle,
path: decoupled_origin.url,
// params: {
// title: decoupled_origin.entity_uuid
// },
// props: {
// nid: decoupled_origin.entity_id
// }
});
// reset the storage
window.localStorage.removeItem("decoupled_origin");
}
}
function onClickContentLink(e, store, map, category){
2024-07-16 17:01:09 +02:00
e.preventDefault();
let a;
if (e.target.tagName !== 'IMG') {
const li = e.target.closest('li');
a = li.querySelector('a');
} else {
a = e.target.closest('a');
}
2024-10-15 10:09:10 +02:00
let nid = a.dataset.nid;
2024-07-16 17:01:09 +02:00
if (category === 'etape') {
store.fetchEtapeData(nid, map);
} else if (category === 'static') {
if (nid) {
store.fetchStaticData(nid, map);
} else {
store.emptyAll(null, map);
}
}
2024-07-16 17:01:09 +02:00
return null;
}
function processStaticLinks(store, map) {
let general_link_fields = document.querySelectorAll('#menu > ul > li > a');
for (let i =1; i < general_link_fields.length; i ++) {
2024-10-15 10:09:10 +02:00
let general_link_path = general_link_fields[i].getAttribute('data-drupal-link-system-path');
const match = [...general_link_path.match(/^node\/(\d+)$/)];
if (match) {
const nid = match[1];
general_link_fields[i].setAttribute('data-nid', parseInt(nid));
general_link_fields[i].addEventListener('click', (e) => onClickContentLink(e, store, map, 'static'));
}
}
}
function processHeaderLogo(store, map) {
const logo = document.querySelector('#block-caravane-logocaravane a');
logo.addEventListener('click', (e) => onClickContentLink(e, store, map, 'static'));
}
2024-07-16 17:01:09 +02:00
function processEtapeLinks(store, map) {
let etape_li = document.querySelectorAll('#etapes-liste li');
etape_li.forEach((li) => {
2024-10-15 10:09:10 +02:00
let etape_link = li.querySelector('a.etape-link');
let nid = etape_link.dataset.nid;
2024-07-16 17:01:09 +02:00
if (nid) {
li.addEventListener('click', (e) => onClickContentLink(e, store, map, 'etape'));
2024-07-16 17:01:09 +02:00
}
2024-10-15 10:09:10 +02:00
let couleur = etape_link.dataset.couleur;
let iconElements = li.querySelectorAll('.icone-arret > div');
for (let element of iconElements) {
element.style.backgroundColor = couleur;
}
2024-07-29 23:39:35 +02:00
})
}
function toggleMenu() {
const menuButton = document.querySelector('#block-caravane-mainnavigation > #menu');
const menuContainer = document.querySelector('#block-caravane-mainnavigation > #menu > ul');
const menuTitle = document.querySelector('#menu-title');
const menuBurger = document.querySelector('#hamburger');
const menuH2 = document.querySelector('#menu > h2');
2024-07-31 02:13:40 +02:00
menuButton.addEventListener('click', (e) => {
setTimeout(() => {
menuContainer.classList.toggle('open');
menuTitle.classList.toggle('open');
menuBurger.classList.toggle('open');
menuH2.classList.toggle('open');
}, 50);
})
document.addEventListener('click', (e) => {
if (!menuContainer.contains(e.target) && !menuBurger.contains(e.target)) {
menuContainer.classList.remove('open');
menuTitle.classList.remove('open');
menuBurger.classList.remove('open');
menuH2.classList.remove('open');
}
2024-07-16 17:01:09 +02:00
})
}
function setHamburgerWhenLogged() {
if (drupalSettings.user.uid != 0) {
const menuBurger = document.querySelector('#hamburger');
const menuTitle = document.querySelector('#menu-title');
const menuContainer = document.querySelector('#block-caravane-mainnavigation > #menu > ul');
const header = document.querySelector('.dialog-off-canvas-main-canvas');
const headerTop = header.getBoundingClientRect().top;
menuTitle.style.top = `${headerTop}px`;
menuBurger.style.top = `${headerTop}px`;
menuContainer.style.paddingTop = `${headerTop}px`;
}
}
function setupEtapeMapPopup(store, map) {
const icons = document.querySelectorAll('.leaflet-map-divicon');
for (let icon of icons) {
const colorContainer = icon.querySelector('.couleur');
let colorDivs = colorContainer.querySelectorAll('.separated-content');
let color;
colorDivs.forEach((div) => {
if (div.innerText.startsWith('<div class="snippets-description">')) {
color = div.innerText;
}
});
color = color.substring(color.indexOf('>') + 1, color.indexOf('<', color.indexOf('>') + 1)).trim();
const nid = icon.querySelector('.nid');
const nidValue = nid.querySelector('.separated-content').innerText;
icon.addEventListener('click', function(event) {
store.fetchEtapeData(nidValue, map);
});
colorContainer.style.display = "none";
nid.style.display = "none";
const iconElements = icon.querySelectorAll('div');
for (let iconElement of iconElements) {
iconElement.style.backgroundColor = color;
}
icon.removeAttribute('title');
icon.addEventListener('mouseenter', function (event) {
icon.style.transform = `${icon.style.transform} scale(1.1)`;
const popup = document.querySelector('.leaflet-tooltip-center > div');
popup.style.opacity = "1";
});
icon.addEventListener('mouseleave', function (event) {
icon.style.transform = icon.style.transform.split(' ')[0] + icon.style.transform.split(' ')[1] + icon.style.transform.split(' ')[2];
})
}
}
2024-07-16 17:01:09 +02:00
init()
} // end CaravaneTheme()
CaravaneTheme()
})(jQuery, Drupal, drupalSettings)