Skip to content
Snippets Groups Projects
Commit 3f843d4d authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

Issue #3399268 by Masami, yas: Refactor to native JavaScript from jQuery (cloud_geocoder.js)

parent 204e3b90
No related branches found
No related tags found
No related merge requests found
(function ($, drupalSettings) { (function (drupalSettings) {
'use strict'; 'use strict';
let countryId = 'edit-field-location-country'; let countryId = 'edit-field-location-country';
...@@ -28,25 +28,29 @@ ...@@ -28,25 +28,29 @@
return; return;
} }
let country = $('#' + countryId).val(); let countryInput = document.getElementById(countryId);
let city = $('#' + cityId).val(); let cityInput = document.getElementById(cityId);
if (country === '_none' || city.trim().length === 0) { if (countryInput.value === '_none' || cityInput.value.trim().length === 0) {
return; return;
} }
let url = drupalSettings.cloud.geocoder_url; let url = drupalSettings.cloud.geocoder_url;
url = url.replace('country', country); url = url.replace('country', countryInput.value);
url = url.replace('city', city); url = url.replace('city', cityInput.value);
$.ajax({ let xhr = new XMLHttpRequest();
async: false, xhr.open('GET', url, false);
url: url, xhr.setRequestHeader('Content-Type', 'application/json');
dataType: 'json', xhr.onload = function() {
success: function success(data) { if (xhr.status === 200) {
let data = JSON.parse(xhr.responseText);
if (data && data.latitude && data.longitude) { if (data && data.latitude && data.longitude) {
let step = $('#' + latitudeId).attr('step'); let latitudeInput = document.getElementById(latitudeId);
if (step && $.isNumeric(step)) { let longitudeInput = document.getElementById(longitudeId);
step = Number.parseFloat(step);
let step = latitudeInput.getAttribute('step');
if (step && !isNaN(step)) {
step = parseFloat(step);
} }
else { else {
step = 0.000001; step = 0.000001;
...@@ -54,31 +58,29 @@ ...@@ -54,31 +58,29 @@
step = 1 / step; step = 1 / step;
let latitude = Math.round(data.latitude * step) / step; let latitude = Math.round(data.latitude * step) / step;
let longitude = Math.round(data.longitude * step) / step; let longitude = Math.round(data.longitude * step) / step;
$('#' + latitudeId).val(latitude); latitudeInput.value = latitude;
$('#' + longitudeId).val(longitude); longitudeInput.value = longitude;
} }
} }
}); };
xhr.send();
}; };
$('#' + countryId).change(function () { let countryInput = document.getElementById(countryId);
setGeolocation(); if (countryInput) {
}); countryInput.addEventListener('change', setGeolocation);
countryInput.addEventListener('blur', setGeolocation);
$('#' + countryId).blur(function () { }
setGeolocation();
});
$('#' + cityId).change(function () {
setGeolocation();
});
$('#' + cityId).blur(function () { let cityInput = document.getElementById(cityId);
setGeolocation(); if (cityInput) {
}); cityInput.addEventListener('change', setGeolocation);
cityInput.addEventListener('blur', setGeolocation);
}
$('form').submit(function () { let form = document.querySelector('form');
setGeolocation(); if (form) {
}); form.addEventListener('submit', setGeolocation);
}
}(jQuery, drupalSettings)); }(drupalSettings));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment