main.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. jQuery(function ($) {
  2. console.log('salut');
  3. // MENU BURGER
  4. const burger = document.getElementById("block-burger");
  5. const burgertitle = document.getElementById("block-burger-menu");
  6. if (burger && burgertitle) {
  7. burgertitle.addEventListener("click", function () {
  8. burger.classList.toggle('opened');
  9. });
  10. }
  11. // FAQ — réponses
  12. const answers = document.getElementsByClassName("field--name-field-reponse");
  13. const fichiers = document.getElementsByClassName("field--name-field-fichiers");
  14. const liens = document.getElementsByClassName("field--name-field-liens");
  15. const ressources = document.getElementsByClassName("field--name-field-ress");
  16. const questions = document.getElementsByClassName("field--name-field-question");
  17. for (let i = 0; i < questions.length; i++) {
  18. const q = questions[i];
  19. q.addEventListener("click", function () {
  20. // Réponses
  21. Array.from(answers).forEach(a => a.classList.remove("opened"));
  22. const r = this.parentNode.querySelector(".field--name-field-reponse");
  23. if (r) r.classList.add("opened");
  24. // Fichiers
  25. Array.from(fichiers).forEach(f => f.classList.remove("opened"));
  26. const f = this.parentNode.querySelector(".field--name-field-fichiers");
  27. if (f) f.classList.add("opened");
  28. // Liens
  29. Array.from(liens).forEach(l => l.classList.remove("opened"));
  30. const l = this.parentNode.querySelector(".field--name-field-liens");
  31. if (l) l.classList.add("opened");
  32. // Ressources
  33. Array.from(ressources).forEach(r => r.classList.remove("opened"));
  34. const res = this.parentNode.querySelector(".field--name-field-ress");
  35. if (res) res.classList.add("opened");
  36. });
  37. }
  38. // SLIDESHOW INIT
  39. $('.path-frontpage .view-actus-blocks-pages .view-content .view-type-slide .views-row-wrapper').slick({
  40. slidesToShow: 1,
  41. dots: true,
  42. arrows: true,
  43. centerMode: true,
  44. responsive: [{
  45. breakpoint: 810,
  46. settings: {
  47. slidesToShow: 1,
  48. adaptiveHeight: true,
  49. arrows: false,
  50. draggable: true,
  51. centerMode: true,
  52. }
  53. }]
  54. });
  55. $('.page-node-type-actualite .block-entity-fieldnodefield-images .field--type-image').slick({
  56. dots: true,
  57. arrows: true,
  58. adaptiveHeight: true,
  59. responsive: [{
  60. breakpoint: 800,
  61. settings: { adaptiveHeight: true }
  62. }]
  63. });
  64. $('.page-node-type-projet .block-entity-fieldnodefield-photo .field--type-image').slick({
  65. slidesToShow: 1,
  66. dots: true,
  67. arrows: false,
  68. draggable: true,
  69. adaptiveHeight: true,
  70. responsive: [{
  71. breakpoint: 800,
  72. settings: { adaptiveHeight: true }
  73. }]
  74. });
  75. ///// fusion views-type-slide de class identique ////////////
  76. const seen = new Set();
  77. $('.view-type-slide').each(function () {
  78. const $slide = $(this);
  79. const classes = $slide.attr('class').split(/\s+/);
  80. const typeClass = classes.find(cls => cls.startsWith('type-'));
  81. if (typeClass) {
  82. if (seen.has(typeClass)) {
  83. // Trouver le premier slide avec cette classe
  84. const $target = $('.view-type-slide.' + typeClass).first();
  85. // Déplacer les rows de la slide actuelle vers la première
  86. $slide.find('.views-row').appendTo($target.find('.views-row-wrapper'));
  87. // Supprimer la slide en double
  88. $slide.remove();
  89. } else {
  90. seen.add(typeClass);
  91. }
  92. }
  93. });
  94. if (!$('.view-content').hasClass('filtered')) {
  95. $('.path-ressources .view:not(.view-partenaires) .view-content .view-type-slide .views-row-wrapper').slick({
  96. slidesToShow: 3,
  97. dots: false,
  98. arrows: true,
  99. infinite: false,
  100. centerMode: false,
  101. draggable: true,
  102. responsive: [{
  103. breakpoint: 810,
  104. settings: {
  105. slidesToShow: 1,
  106. dots: true,
  107. arrows: false,
  108. draggable: true,
  109. centerMode: true,
  110. }
  111. }]
  112. });
  113. }
  114. // Classes media → .wrapper-ressource
  115. $(".wrapper-ressource").each(function () {
  116. const media = $(this).find(".field--name-field-type-de-media").text().trim();
  117. const className = media
  118. .toLowerCase()
  119. .normalize("NFD").replace(/[\u0300-\u036f]/g, "")
  120. .replace(/[^a-z0-9]+/g, '-')
  121. .replace(/(^-|-$)/g, '');
  122. $(this).addClass('type-media-' + className);
  123. });
  124. console.log("classses media");
  125. // MASQUER TYPE DE RESSOURCE DOUBLON
  126. if (document.body.classList.contains("path-ressources")) {
  127. $(".view-type-slide").each(function () {
  128. const h3Content = $(this).find("h3").text().trim();
  129. console.log(h3Content);
  130. $(this).find(".field--name-field-type-de-ressource .field__item a").each(function () {
  131. if ($(this).text().trim() === h3Content) {
  132. $(this).hide();
  133. }
  134. });
  135. });
  136. }
  137. // Ouvrir les liens externes dans un nouvel onglet
  138. document.querySelectorAll('a[href^="http"]').forEach(link => {
  139. if (!link.href.includes(location.hostname)) {
  140. link.setAttribute('target', '_blank');
  141. link.setAttribute('rel', 'noopener noreferrer');
  142. }
  143. });
  144. // Scroll automatique au filtre
  145. if (document.body.classList.contains("path-projets")) {
  146. const form = document.querySelector(".views-exposed-form");
  147. if (form) form.setAttribute("action", form.action.split("#")[0] + "#filtres");
  148. if (window.location.hash === "#filtres") {
  149. const target = document.getElementById("filtres");
  150. if (target) {
  151. const offset = 300;
  152. const top = target.getBoundingClientRect().top + window.pageYOffset - offset;
  153. window.scrollTo({ top: top, behavior: "smooth" });
  154. }
  155. }
  156. }
  157. });
  158. /////////////////start diaporama ressource //////////
  159. document.addEventListener('DOMContentLoaded', function () {
  160. // Vérifie que le body a la classe souhaitée
  161. if (!document.body.classList.contains('type-media-images---photos')) return;
  162. // Attendre que les éléments HTML soient bien présents
  163. const interval = setInterval(() => {
  164. const mainImage = document.getElementById('mainImage');
  165. const prevArrow = document.getElementById('prevArrow');
  166. const nextArrow = document.getElementById('nextArrow');
  167. const caption = document.getElementById('caption');
  168. const thumbsContainer = document.getElementById('thumbnails');
  169. const imagesInDom = document.querySelectorAll('.carousel-items .carousel-item img');
  170. if (mainImage && prevArrow && nextArrow && caption && thumbsContainer && imagesInDom.length > 0) {
  171. clearInterval(interval); // Tous les éléments sont là, on lance le carrousel
  172. initCarousel({ mainImage, prevArrow, nextArrow, caption, thumbsContainer });
  173. }
  174. }, 100);
  175. });
  176. function initCarousel({ mainImage, prevArrow, nextArrow, caption, thumbsContainer }) {
  177. const images = [];
  178. // Adapter l'orientation des vignettes si écran < 810px
  179. function applyResponsiveThumbnailsLayout() {
  180. if (window.innerWidth < 810) {
  181. thumbsContainer.style.flexDirection = 'row';
  182. thumbsContainer.style.flexWrap = 'nowrap';
  183. thumbsContainer.style.overflowX = 'auto';
  184. thumbsContainer.style.overflowY = 'hidden';
  185. thumbsContainer.style.display = 'flex';
  186. thumbsContainer.style.justifyContent = 'center';
  187. } else {
  188. thumbsContainer.style.flexDirection = 'column';
  189. thumbsContainer.style.overflowY = 'auto';
  190. thumbsContainer.style.overflowX = 'hidden';
  191. }
  192. }
  193. applyResponsiveThumbnailsLayout();
  194. window.addEventListener('resize', applyResponsiveThumbnailsLayout);
  195. document.querySelectorAll('.carousel-items .carousel-item img').forEach((img) => {
  196. images.push({
  197. src: img.getAttribute('src'),
  198. caption: img.getAttribute('alt') || ''
  199. });
  200. });
  201. if (!images.length) return;
  202. let currentIndex = 0;
  203. // function scrollThumbnailToCenter(index) {
  204. // const thumbnails = document.querySelectorAll('.thumbnails img');
  205. // const activeThumb = thumbnails[index];
  206. // if (activeThumb && thumbsContainer) {
  207. // const containerHeight = thumbsContainer.clientHeight;
  208. // const thumbOffsetTop = activeThumb.offsetTop;
  209. // const thumbHeight = activeThumb.offsetHeight;
  210. // const scrollTarget = thumbOffsetTop - (containerHeight / 2) + (thumbHeight / 2);
  211. // thumbsContainer.scrollTo({ top: scrollTarget, behavior: 'smooth' });
  212. // }
  213. // }
  214. function scrollThumbnailToCenter(index) {
  215. const thumbnails = document.querySelectorAll('.thumbnails img');
  216. const activeThumb = thumbnails[index];
  217. if (activeThumb && thumbsContainer) {
  218. const isMobile = window.innerWidth < 810;
  219. if (isMobile) {
  220. // Scroll horizontal
  221. const containerWidth = thumbsContainer.clientWidth;
  222. const thumbOffsetLeft = activeThumb.offsetLeft;
  223. const thumbWidth = activeThumb.offsetWidth;
  224. const scrollTarget = thumbOffsetLeft - (containerWidth / 2) + (thumbWidth / 2);
  225. thumbsContainer.scrollTo({ left: scrollTarget, behavior: 'smooth' });
  226. } else {
  227. // Scroll vertical
  228. const containerHeight = thumbsContainer.clientHeight;
  229. const thumbOffsetTop = activeThumb.offsetTop;
  230. const thumbHeight = activeThumb.offsetHeight;
  231. const scrollTarget = thumbOffsetTop - (containerHeight / 2) + (thumbHeight / 2);
  232. thumbsContainer.scrollTo({ top: scrollTarget, behavior: 'smooth' });
  233. }
  234. }
  235. }
  236. function showImage(index) {
  237. mainImage.src = images[index].src;
  238. caption.textContent = images[index].caption;
  239. const thumbnails = document.querySelectorAll('.thumbnails img');
  240. thumbnails.forEach(img => img.classList.remove('active'));
  241. if (thumbnails[index]) {
  242. thumbnails[index].classList.add('active');
  243. scrollThumbnailToCenter(index);
  244. }
  245. }
  246. prevArrow.addEventListener('click', () => {
  247. currentIndex = (currentIndex - 1 + images.length) % images.length;
  248. showImage(currentIndex);
  249. });
  250. nextArrow.addEventListener('click', () => {
  251. currentIndex = (currentIndex + 1) % images.length;
  252. showImage(currentIndex);
  253. });
  254. images.forEach((img, index) => {
  255. const thumb = document.createElement('img');
  256. thumb.src = img.src;
  257. thumb.alt = img.caption;
  258. thumb.addEventListener('click', () => {
  259. currentIndex = index;
  260. showImage(index);
  261. });
  262. thumbsContainer.appendChild(thumb);
  263. });
  264. showImage(currentIndex);
  265. const thumbPrev = document.getElementById('thumbPrev');
  266. const thumbNext = document.getElementById('thumbNext');
  267. // ✅ Masquer les flèches de thumbnails si pas assez d’images pour scroller
  268. const thumbHeight = 70; // approx : image + gap
  269. const visibleCount = Math.floor(thumbsContainer.clientHeight / thumbHeight);
  270. if (images.length <= visibleCount) {
  271. if (thumbPrev) thumbPrev.style.display = 'none';
  272. if (thumbNext) thumbNext.style.display = 'none';
  273. }
  274. // Boucle haut/bas avec scroll
  275. thumbPrev.addEventListener('click', () => {
  276. if (thumbsContainer.scrollTop <= 0) {
  277. thumbsContainer.scrollTo({ top: thumbsContainer.scrollHeight, behavior: 'smooth' });
  278. } else {
  279. thumbsContainer.scrollBy({ top: -150, behavior: 'smooth' });
  280. }
  281. });
  282. thumbNext.addEventListener('click', () => {
  283. const maxScrollTop = thumbsContainer.scrollHeight - thumbsContainer.clientHeight;
  284. if (thumbsContainer.scrollTop >= maxScrollTop - 1) {
  285. thumbsContainer.scrollTo({ top: 0, behavior: 'smooth' });
  286. } else {
  287. thumbsContainer.scrollBy({ top: 150, behavior: 'smooth' });
  288. }
  289. });
  290. }
  291. /////////////////end diaporama ressource //////////
  292. //////////////////////////start classe en JS si des filtres sont présents///////////
  293. document.addEventListener('DOMContentLoaded', () => {
  294. if (window.location.search.length > 0) {
  295. document.querySelector('.view-content')?.classList.add('filtered');
  296. document.querySelectorAll('.tout-voir').forEach(btn => btn.remove());
  297. }
  298. });
  299. //////////////////////////end classe en JS si des filtres sont présents///////////
  300. // ////////////////// start tronquage sous titre //////////////////
  301. document.addEventListener("DOMContentLoaded", function () {
  302. const maxLength = 80;
  303. document.querySelectorAll('.view-base-de-donnees .wrapper-ressource .field--name-field-sous-titre a').forEach(function (element) {
  304. const fullText = element.textContent.trim();
  305. if (fullText.length > maxLength) {
  306. const truncated = fullText.slice(0, maxLength).trim() + '...';
  307. element.textContent = truncated;
  308. }
  309. });
  310. });
  311. // //////////////////end tronquage sous titre //////////////////
  312. ////////////////// start boutons media ////////////////
  313. document.addEventListener("DOMContentLoaded", function () {
  314. const mediaButtons = document.querySelectorAll('.buttons-filtres-ressources a');
  315. mediaButtons.forEach(button => {
  316. button.addEventListener('click', function (e) {
  317. e.preventDefault();
  318. // Récupérer l’ID media depuis l’URL du bouton
  319. const url = new URL(button.href);
  320. const mediaID = url.searchParams.get('field_type_de_media_target_id');
  321. // Appliquer la valeur dans le <select>
  322. const select = document.getElementById('edit-field-type-de-media-target-id');
  323. if (select) {
  324. select.value = mediaID;
  325. // Simuler le clic sur le bouton "Appliquer"
  326. const submit = document.querySelector('input#edit-submit-base-de-donnees');
  327. if (submit) {
  328. submit.click();
  329. }
  330. }
  331. });
  332. });
  333. });
  334. ////////////////// end boutons media ////////////////
  335. ////////////////// start bouton "Tout voir" ////////////////
  336. document.addEventListener("DOMContentLoaded", function () {
  337. const toutVoirButtons = document.querySelectorAll('.tout-voir');
  338. toutVoirButtons.forEach(button => {
  339. button.addEventListener('click', function () {
  340. const tid = this.dataset.tid;
  341. const select = document.getElementById('edit-field-type-de-ressource-target-id');
  342. if (!select) return;
  343. const optionExists = Array.from(select.options).some(o => o.value === tid);
  344. if (!optionExists) {
  345. alert("Le type de ressource sélectionné n’est pas disponible.");
  346. return;
  347. }
  348. select.value = tid;
  349. select.dispatchEvent(new Event('change'));
  350. const form = select.closest('form');
  351. if (form) {
  352. const submitButton = form.querySelector('input[type="submit"]');
  353. if (submitButton) {
  354. submitButton.click();
  355. } else {
  356. form.submit();
  357. }
  358. }
  359. });
  360. });
  361. });
  362. ////////////////// end bouton "Tout voir" ////////////////
  363. ////////////// start croix clear input recherche ////////////
  364. document.addEventListener("DOMContentLoaded", function () {
  365. const input = document.getElementById("edit-combine");
  366. const clearBtn = document.querySelector('.form-item-combine .clear-input');
  367. const form = document.querySelector("form.views-exposed-form");
  368. if (input && clearBtn && form) {
  369. // Affiche la croix si contenu présent
  370. input.addEventListener('input', () => {
  371. clearBtn.style.display = input.value ? 'block' : 'none';
  372. });
  373. // Efface et soumet le formulaire
  374. clearBtn.addEventListener('click', () => {
  375. input.value = '';
  376. clearBtn.style.display = 'none';
  377. input.focus();
  378. form.submit(); // déclenche la recherche sans rechargement manuel
  379. });
  380. // Affiche la croix au chargement si une valeur est déjà présente
  381. if (input.value) {
  382. clearBtn.style.display = 'block';
  383. }
  384. }
  385. });
  386. ////////////// end croix clear input recherche ////////////