main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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') || 'Image sans légende'
  181. });
  182. });
  183. if (!images.length) return;
  184. let currentIndex = 0;
  185. function showImage(index) {
  186. mainImage.src = images[index].src;
  187. caption.textContent = images[index].caption;
  188. const thumbnails = document.querySelectorAll('.thumbnails img');
  189. thumbnails.forEach(img => img.classList.remove('active'));
  190. if (thumbnails[index]) {
  191. thumbnails[index].classList.add('active');
  192. // thumbnails[index].scrollIntoView({ block: 'center', behavior: 'smooth' });
  193. }
  194. }
  195. prevArrow.addEventListener('click', () => {
  196. currentIndex = (currentIndex - 1 + images.length) % images.length;
  197. showImage(currentIndex);
  198. });
  199. nextArrow.addEventListener('click', () => {
  200. currentIndex = (currentIndex + 1) % images.length;
  201. showImage(currentIndex);
  202. });
  203. images.forEach((img, index) => {
  204. const thumb = document.createElement('img');
  205. thumb.src = img.src;
  206. thumb.alt = img.caption;
  207. thumb.addEventListener('click', () => {
  208. currentIndex = index;
  209. showImage(index);
  210. });
  211. thumbsContainer.appendChild(thumb);
  212. });
  213. showImage(currentIndex);
  214. const thumbPrev = document.getElementById('thumbPrev');
  215. const thumbNext = document.getElementById('thumbNext');
  216. // Boucle haut/bas avec scroll
  217. thumbPrev.addEventListener('click', () => {
  218. if (thumbsContainer.scrollTop <= 0) {
  219. thumbsContainer.scrollTo({ top: thumbsContainer.scrollHeight, behavior: 'smooth' });
  220. } else {
  221. thumbsContainer.scrollBy({ top: -150, behavior: 'smooth' });
  222. }
  223. });
  224. thumbNext.addEventListener('click', () => {
  225. const maxScrollTop = thumbsContainer.scrollHeight - thumbsContainer.clientHeight;
  226. if (thumbsContainer.scrollTop >= maxScrollTop - 1) {
  227. thumbsContainer.scrollTo({ top: 0, behavior: 'smooth' });
  228. } else {
  229. thumbsContainer.scrollBy({ top: 150, behavior: 'smooth' });
  230. }
  231. });
  232. }
  233. /////////////////end diaporama ressource //////////
  234. //////////////////////////start classe en JS si des filtres sont présents///////////
  235. document.addEventListener('DOMContentLoaded', () => {
  236. if (window.location.search.length > 0) {
  237. document.querySelector('.view-content')?.classList.add('filtered');
  238. document.querySelectorAll('.tout-voir').forEach(btn => btn.remove());
  239. }
  240. });
  241. //////////////////////////end classe en JS si des filtres sont présents///////////
  242. // ////////////////// start tronquage sous titre //////////////////
  243. document.addEventListener("DOMContentLoaded", function () {
  244. const maxLength = 80;
  245. document.querySelectorAll('.view-base-de-donnees .wrapper-ressource .field--name-field-sous-titre a').forEach(function (element) {
  246. const fullText = element.textContent.trim();
  247. if (fullText.length > maxLength) {
  248. const truncated = fullText.slice(0, maxLength).trim() + '...';
  249. element.textContent = truncated;
  250. }
  251. });
  252. });
  253. // //////////////////end tronquage sous titre //////////////////
  254. ////////////////// start boutons media ////////////////
  255. document.addEventListener("DOMContentLoaded", function () {
  256. const mediaButtons = document.querySelectorAll('.buttons-filtres-ressources a');
  257. mediaButtons.forEach(button => {
  258. button.addEventListener('click', function (e) {
  259. e.preventDefault();
  260. // Récupérer l’ID media depuis l’URL du bouton
  261. const url = new URL(button.href);
  262. const mediaID = url.searchParams.get('field_type_de_media_target_id');
  263. // Appliquer la valeur dans le <select>
  264. const select = document.getElementById('edit-field-type-de-media-target-id');
  265. if (select) {
  266. select.value = mediaID;
  267. // Simuler le clic sur le bouton "Appliquer"
  268. const submit = document.querySelector('input#edit-submit-base-de-donnees');
  269. if (submit) {
  270. submit.click();
  271. }
  272. }
  273. });
  274. });
  275. });
  276. ////////////////// end boutons media ////////////////
  277. ////////////// start croix clear input recherche ////////////
  278. document.addEventListener("DOMContentLoaded", function () {
  279. const input = document.getElementById("edit-combine");
  280. const clearBtn = document.querySelector('.form-item-combine .clear-input');
  281. const form = document.querySelector("form.views-exposed-form");
  282. if (input && clearBtn && form) {
  283. // Affiche la croix si contenu présent
  284. input.addEventListener('input', () => {
  285. clearBtn.style.display = input.value ? 'block' : 'none';
  286. });
  287. // Efface et soumet le formulaire
  288. clearBtn.addEventListener('click', () => {
  289. input.value = '';
  290. clearBtn.style.display = 'none';
  291. input.focus();
  292. form.submit(); // déclenche la recherche sans rechargement manuel
  293. });
  294. // Affiche la croix au chargement si une valeur est déjà présente
  295. if (input.value) {
  296. clearBtn.style.display = 'block';
  297. }
  298. }
  299. });
  300. ////////////// end croix clear input recherche ////////////