1'use strict'; 2 3(function ($, Drupal, once) { 4 Drupal.behaviors.geolocation = { 5 selectors: { 6 form: 'form-renseigner-adresse', 7 inputAddr: '[name=adresse]', 8 btnGeoloc: '[data-geolocation]', 9 }, 10 11 attach: function (context, settings) { 12 const behaviors = Drupal.behaviors.geolocation; 13 14 $(once('div-' + behaviors.selectors.form, '#' + behaviors.selectors.form, context)).each(function () { 15 const form = $(this); 16 let geolocBtn = form.find(behaviors.selectors.btnGeoloc); 17 18 geolocBtn.hide(); 19 20 form.find(behaviors.selectors.btnGeoloc).click(function () { 21 behaviors.getAddressData(form); 22 }); 23 24 form.find(behaviors.selectors.inputAddr).on('focus blur input', function (event) { 25 switch (event.type) { 26 case 'focus': 27 geolocBtn.show(); 28 break; 29 case 'input': 30 if ($(this).val() === '') { 31 setTimeout(function () { 32 geolocBtn.show(); 33 }, 600); 34 } else if (event.type === 'input') { 35 geolocBtn.hide(); 36 } 37 break; 38 case 'blur': 39 if ($(event.relatedTarget).attr('data-geolocation') === undefined) { 40 geolocBtn.hide(); 41 } 42 break; 43 } 44 }); 45 }); 46 }, 47 48 /** 49 * 50 * @param form 51 * The serach form. 52 */ 53 getAddressData(form) { 54 const behaviors = Drupal.behaviors.geolocation; 55 56 if (navigator.geolocation) { 57 navigator.geolocation.getCurrentPosition( 58 function (position) { 59 const latitude = position.coords.latitude; 60 const longitude = position.coords.longitude; 61 const url = `https://data.geopf.fr/geocodage/reverse?lat=${latitude}&lon=${longitude}`; 62 63 // Make an AJAX request to the API. 64 $.ajax({ 65 url: url, 66 method: 'GET', 67 success: function(data) { 68 // Check if the response contains location information. 69 if (data && data.features && data.features.length > 0) { 70 const prop = data.features[0].properties; 71 let dataForm = { 72 'city': prop.city ?? false, 73 'type': prop.type ?? false, 74 'propertiesType': prop.type ?? false, 75 'codeInsee': prop.citycode ?? false, 76 'lon': longitude, 77 'lat': latitude, 78 }; 79 80 // Format 'adresse' value. 81 if (dataForm['city'] && prop.postcode) { 82 dataForm['adresse'] = prop.type === 'municipality' ? 83 `${prop.postcode}, ${dataForm['city']}` : 84 `${prop.name}, ${prop.postcode} ${dataForm['city']}`; 85 } 86 87 // Assign values to the form fields. 88 $.each(dataForm, function(field, value) { 89 if (value) { 90 $(`[name=${field}]`).val(value).change(); 91 } 92 }); 93 94 // Enable the submit button. 95 $(form).find('[id=submit-adresse]').attr('disabled', false); 96 $(form).find('[id=submit-adresse]').attr('aria-disabled', false); 97 } else { 98 console.error('No data found for these coordinates.'); 99 } 100 }, 101 error: function(xhr, status, error) { 102 console.error('Error while retrieving data:', error); 103 } 104 }); 105 }, 106 function (error) { 107 console.error('Geolocation error:', error.message); 108 }, 109 { 110 enableHighAccuracy: true, 111 timeout: 10000, 112 maximumAge: 0, 113 }, 114 ); 115 } else { 116 console.error("Geolocation is not supported by this browser."); 117 } 118 119 form.find(behaviors.selectors.btnGeoloc).hide(); 120 }, 121 122 }; 123})(jQuery, Drupal, once);
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.