1(function ($, Drupal) { 2 'use strict'; 3 4 console.log('TripPlannerCsrf: Script loaded'); 5 6 /** 7 * Gestionnaire de token CSRF pour le planificateur de voyage. 8 */ 9 Drupal.TripPlannerCsrf = { 10 token: null, 11 fetchPromise: null, 12 initialized: false, 13 14 /** 15 * Obtenir l'endpoint token avec le préfixe de langue. 16 */ 17 getTokenEndpoint: function () { 18 var langCode = drupalSettings.path.currentLanguage || 'fr'; 19 return '/' + langCode + '/api/trip-planner/token'; 20 }, 21 22 /** 23 * Ajouter le token à une URL. 24 */ 25 addTokenToUrl: function (url, token) { 26 var separator = url.indexOf('?') !== -1 ? '&' : '?'; 27 return url + separator + 'token=' + encodeURIComponent(token); 28 }, 29 30 /** 31 * Initialiser la délégation d'événements (une seule fois). 32 */ 33 init: function () { 34 if (this.initialized) { 35 console.log('TripPlannerCsrf: Already initialized, skipping'); 36 return; 37 } 38 this.initialized = true; 39 40 var self = this; 41 42 self.csrf = new TQCsrf(self.getTokenEndpoint()); 43 44 // Délégation d'événements sur le document pour capturer tous les clics 45 // sur les liens .trip-planner-ajax, même ceux ajoutés dynamiquement. 46 console.log('TripPlannerCsrf: Event delegation initialized'); 47 console.log('TripPlannerCsrf: Found ' + $('a.trip-planner-ajax').length + ' links on page'); 48 $(document).on('click', 'a.trip-planner-ajax', function (e) { 49 console.log('TripPlannerCsrf: Click intercepted on', this.href); 50 e.preventDefault(); 51 e.stopPropagation(); 52 53 var $link = $(this); 54 var href = $link.attr('href'); 55 56 // Récupérer le token puis exécuter l'AJAX. 57 self.csrf.fetchToken().then(function (token) { 58 var newHref = self.addTokenToUrl(href, token); 59 60 // Nul besoin de reinitialiser le jeton car ceci est fait par 61 // self.csrf.fetchToken(). 62 63 // Créer et exécuter une requête AJAX Drupal. 64 var ajaxSettings = { 65 url: newHref, 66 event: 'trip-planner-execute', 67 element: $link[0], 68 progress: { 69 type: 'throbber', 70 message: '' 71 } 72 }; 73 74 Drupal.ajax(ajaxSettings).execute(); 75 76 }).catch(function (error) { 77 console.error('Could not fetch CSRF token:', error); 78 }); 79 80 return false; 81 }); 82 } 83 }; 84 85 /** 86 * Behavior pour initialiser le gestionnaire CSRF. 87 */ 88 Drupal.behaviors.tripPlannerCsrf = { 89 attach: function (context) { 90 console.log('TripPlannerCsrf: Behavior attach called'); 91 // Initialiser une seule fois la délégation d'événements. 92 Drupal.TripPlannerCsrf.init(); 93 } 94 }; 95 96})(jQuery, Drupal);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.