main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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: 3,
  106. arrows: true,
  107. draggable: true,
  108. centerMode: false,
  109. }
  110. }]
  111. });
  112. }
  113. // Classes media → .wrapper-ressource
  114. $(".wrapper-ressource").each(function () {
  115. const media = $(this).find(".field--name-field-type-de-media").text().trim();
  116. const className = media
  117. .toLowerCase()
  118. .normalize("NFD").replace(/[\u0300-\u036f]/g, "")
  119. .replace(/[^a-z0-9]+/g, '-')
  120. .replace(/(^-|-$)/g, '');
  121. $(this).addClass('type-media-' + className);
  122. });
  123. console.log("classses media");
  124. // MASQUER TYPE DE RESSOURCE DOUBLON
  125. if (document.body.classList.contains("path-ressources")) {
  126. $(".view-type-slide").each(function () {
  127. const h3Content = $(this).find("h3").text().trim();
  128. console.log(h3Content);
  129. $(this).find(".field--name-field-type-de-ressource .field__item a").each(function () {
  130. if ($(this).text().trim() === h3Content) {
  131. $(this).hide();
  132. }
  133. });
  134. });
  135. }
  136. // Ouvrir les liens externes dans un nouvel onglet
  137. document.querySelectorAll('a[href^="http"]').forEach(link => {
  138. if (!link.href.includes(location.hostname)) {
  139. link.setAttribute('target', '_blank');
  140. link.setAttribute('rel', 'noopener noreferrer');
  141. }
  142. });
  143. // Scroll automatique au filtre
  144. if (document.body.classList.contains("path-projets")) {
  145. const form = document.querySelector(".views-exposed-form");
  146. if (form) form.setAttribute("action", form.action.split("#")[0] + "#filtres");
  147. if (window.location.hash === "#filtres") {
  148. const target = document.getElementById("filtres");
  149. if (target) {
  150. const offset = 300;
  151. const top = target.getBoundingClientRect().top + window.pageYOffset - offset;
  152. window.scrollTo({ top: top, behavior: "smooth" });
  153. }
  154. }
  155. }
  156. });
  157. /////////////////start diaporama ressource //////////
  158. document.addEventListener('DOMContentLoaded', function () {
  159. // Vérifie que le body a la classe souhaitée
  160. if (!document.body.classList.contains('type-media-images---photos')) return;
  161. // Attendre que les éléments HTML soient bien présents
  162. const interval = setInterval(() => {
  163. const mainImage = document.getElementById('mainImage');
  164. const prevArrow = document.getElementById('prevArrow');
  165. const nextArrow = document.getElementById('nextArrow');
  166. const caption = document.getElementById('caption');
  167. const thumbsContainer = document.getElementById('thumbnails');
  168. const imagesInDom = document.querySelectorAll('.carousel-items .carousel-item img');
  169. if (mainImage && prevArrow && nextArrow && caption && thumbsContainer && imagesInDom.length > 0) {
  170. clearInterval(interval); // Tous les éléments sont là, on lance le carrousel
  171. initCarousel({ mainImage, prevArrow, nextArrow, caption, thumbsContainer });
  172. }
  173. }, 100); // vérifie toutes les 100ms
  174. });
  175. function initCarousel({ mainImage, prevArrow, nextArrow, caption, thumbsContainer }) {
  176. const images = [];
  177. document.querySelectorAll('.carousel-items .carousel-item img').forEach((img) => {
  178. images.push({
  179. src: img.getAttribute('src'),
  180. caption: img.getAttribute('alt') || ''
  181. });
  182. });
  183. if (!images.length) return;
  184. let currentIndex = 0;
  185. function scrollThumbnailToCenter(index) {
  186. const thumbnails = document.querySelectorAll('.thumbnails img');
  187. const activeThumb = thumbnails[index];
  188. if (activeThumb && thumbsContainer) {
  189. const containerHeight = thumbsContainer.clientHeight;
  190. const thumbOffsetTop = activeThumb.offsetTop;
  191. const thumbHeight = activeThumb.offsetHeight;
  192. const scrollTarget = thumbOffsetTop - (containerHeight / 2) + (thumbHeight / 2);
  193. thumbsContainer.scrollTo({ top: scrollTarget, behavior: 'smooth' });
  194. }
  195. }
  196. function showImage(index) {
  197. mainImage.src = images[index].src;
  198. caption.textContent = images[index].caption;
  199. const thumbnails = document.querySelectorAll('.thumbnails img');
  200. thumbnails.forEach(img => img.classList.remove('active'));
  201. if (thumbnails[index]) {
  202. thumbnails[index].classList.add('active');
  203. scrollThumbnailToCenter(index); // 👈 centrage de la miniature active
  204. }
  205. }
  206. prevArrow.addEventListener('click', () => {
  207. currentIndex = (currentIndex - 1 + images.length) % images.length;
  208. showImage(currentIndex);
  209. });
  210. nextArrow.addEventListener('click', () => {
  211. currentIndex = (currentIndex + 1) % images.length;
  212. showImage(currentIndex);
  213. });
  214. images.forEach((img, index) => {
  215. const thumb = document.createElement('img');
  216. thumb.src = img.src;
  217. thumb.alt = img.caption;
  218. thumb.addEventListener('click', () => {
  219. currentIndex = index;
  220. showImage(index);
  221. });
  222. thumbsContainer.appendChild(thumb);
  223. });
  224. showImage(currentIndex);
  225. const thumbPrev = document.getElementById('thumbPrev');
  226. const thumbNext = document.getElementById('thumbNext');
  227. // Boucle haut/bas avec scroll
  228. thumbPrev.addEventListener('click', () => {
  229. if (thumbsContainer.scrollTop <= 0) {
  230. thumbsContainer.scrollTo({ top: thumbsContainer.scrollHeight, behavior: 'smooth' });
  231. } else {
  232. thumbsContainer.scrollBy({ top: -150, behavior: 'smooth' });
  233. }
  234. });
  235. thumbNext.addEventListener('click', () => {
  236. const maxScrollTop = thumbsContainer.scrollHeight - thumbsContainer.clientHeight;
  237. if (thumbsContainer.scrollTop >= maxScrollTop - 1) {
  238. thumbsContainer.scrollTo({ top: 0, behavior: 'smooth' });
  239. } else {
  240. thumbsContainer.scrollBy({ top: 150, behavior: 'smooth' });
  241. }
  242. });
  243. }
  244. /////////////////end diaporama ressource //////////
  245. //////////////////////////start classe en JS si des filtres sont présents///////////
  246. document.addEventListener('DOMContentLoaded', () => {
  247. if (window.location.search.length > 0) {
  248. document.querySelector('.view-content')?.classList.add('filtered');
  249. document.querySelectorAll('.tout-voir').forEach(btn => btn.remove());
  250. }
  251. });
  252. //////////////////////////end classe en JS si des filtres sont présents///////////
  253. // ////////////////// start tronquage sous titre //////////////////
  254. document.addEventListener("DOMContentLoaded", function () {
  255. const maxLength = 80;
  256. document.querySelectorAll('.view-base-de-donnees .wrapper-ressource .field--name-field-sous-titre a').forEach(function (element) {
  257. const fullText = element.textContent.trim();
  258. if (fullText.length > maxLength) {
  259. const truncated = fullText.slice(0, maxLength).trim() + '...';
  260. element.textContent = truncated;
  261. }
  262. });
  263. });
  264. // //////////////////end tronquage sous titre //////////////////
  265. ////////////////// start boutons media ////////////////
  266. document.addEventListener("DOMContentLoaded", function () {
  267. const mediaButtons = document.querySelectorAll('.buttons-filtres-ressources a');
  268. mediaButtons.forEach(button => {
  269. button.addEventListener('click', function (e) {
  270. e.preventDefault();
  271. // Récupérer l’ID media depuis l’URL du bouton
  272. const url = new URL(button.href);
  273. const mediaID = url.searchParams.get('field_type_de_media_target_id');
  274. // Appliquer la valeur dans le <select>
  275. const select = document.getElementById('edit-field-type-de-media-target-id');
  276. if (select) {
  277. select.value = mediaID;
  278. // Simuler le clic sur le bouton "Appliquer"
  279. const submit = document.querySelector('input#edit-submit-base-de-donnees');
  280. if (submit) {
  281. submit.click();
  282. }
  283. }
  284. });
  285. });
  286. });
  287. ////////////////// end boutons media ////////////////
  288. ////////////// start croix clear input recherche ////////////
  289. document.addEventListener("DOMContentLoaded", function () {
  290. const input = document.getElementById("edit-combine");
  291. const clearBtn = document.querySelector('.form-item-combine .clear-input');
  292. const form = document.querySelector("form.views-exposed-form");
  293. if (input && clearBtn && form) {
  294. // Affiche la croix si contenu présent
  295. input.addEventListener('input', () => {
  296. clearBtn.style.display = input.value ? 'block' : 'none';
  297. });
  298. // Efface et soumet le formulaire
  299. clearBtn.addEventListener('click', () => {
  300. input.value = '';
  301. clearBtn.style.display = 'none';
  302. input.focus();
  303. form.submit(); // déclenche la recherche sans rechargement manuel
  304. });
  305. // Affiche la croix au chargement si une valeur est déjà présente
  306. if (input.value) {
  307. clearBtn.style.display = 'block';
  308. }
  309. }
  310. });
  311. ////////////// end croix clear input recherche ////////////