2020-06-13 22:39:17 +02:00
|
|
|
var MapGuesser = {
|
2021-03-19 22:59:09 +01:00
|
|
|
isSecure: window.location.protocol === 'https:',
|
2020-06-25 14:02:39 +02:00
|
|
|
cookiesAgreed: false,
|
|
|
|
sessionAvailableHooks: {},
|
|
|
|
|
|
|
|
initGoogleAnalitics: function () {
|
|
|
|
if (typeof GOOGLE_ANALITICS_ID === 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global site tag (gtag.js) - Google Analytics
|
|
|
|
var script = document.createElement('script');
|
|
|
|
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + GOOGLE_ANALITICS_ID;
|
|
|
|
script.async = true;
|
|
|
|
|
|
|
|
document.head.appendChild(script);
|
|
|
|
|
|
|
|
window.dataLayer = window.dataLayer || [];
|
2020-06-25 19:14:46 +02:00
|
|
|
function gtag() { dataLayer.push(arguments); }
|
2020-06-25 14:02:39 +02:00
|
|
|
gtag('js', new Date());
|
|
|
|
gtag('config', GOOGLE_ANALITICS_ID);
|
|
|
|
},
|
|
|
|
|
|
|
|
agreeCookies: function () {
|
|
|
|
if (MapGuesser.cookiesAgreed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var expirationDate = new Date(new Date().getTime() + 20 * 365 * 24 * 60 * 60 * 1000).toUTCString();
|
|
|
|
document.cookie = 'COOKIES_CONSENT=1; expires=' + expirationDate + '; path=/';
|
|
|
|
|
|
|
|
MapGuesser.initGoogleAnalitics();
|
|
|
|
MapGuesser.httpRequest('GET', '/startSession.json', function () {
|
|
|
|
ANTI_CSRF_TOKEN = this.response.antiCsrfToken;
|
|
|
|
|
|
|
|
for (var hookId in MapGuesser.sessionAvailableHooks) {
|
|
|
|
if (!MapGuesser.sessionAvailableHooks.hasOwnProperty(hookId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapGuesser.sessionAvailableHooks[hookId]();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
MapGuesser.cookiesAgreed = true;
|
|
|
|
},
|
|
|
|
|
2020-06-13 22:39:17 +02:00
|
|
|
httpRequest: function (method, url, callback, data) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.onload = callback;
|
|
|
|
|
|
|
|
xhr.open(method, url, true);
|
|
|
|
|
2020-06-18 16:20:45 +02:00
|
|
|
xhr.responseType = 'json';
|
|
|
|
|
2020-06-13 22:39:17 +02:00
|
|
|
if (method === 'POST') {
|
|
|
|
if (typeof data === 'undefined') {
|
|
|
|
data = new FormData();
|
|
|
|
}
|
|
|
|
|
|
|
|
data.append('anti_csrf_token', ANTI_CSRF_TOKEN);
|
|
|
|
|
|
|
|
xhr.send(data);
|
|
|
|
} else {
|
|
|
|
xhr.send();
|
|
|
|
}
|
2020-06-14 02:30:15 +02:00
|
|
|
},
|
|
|
|
|
2020-06-25 22:34:32 +02:00
|
|
|
setOnsubmitForForm: function (form) {
|
2020-06-25 19:14:46 +02:00
|
|
|
form.onsubmit = function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
document.getElementById('loading').style.visibility = 'visible';
|
|
|
|
|
|
|
|
var formData = new FormData(form);
|
|
|
|
var formError = form.getElementsByClassName('formError')[0];
|
2020-06-25 22:34:32 +02:00
|
|
|
var pageLeaveOnSuccess = form.dataset.redirectOnSuccess || form.dataset.reloadOnSuccess;
|
2020-06-25 19:14:46 +02:00
|
|
|
|
|
|
|
MapGuesser.httpRequest('POST', form.action, function () {
|
|
|
|
if (!pageLeaveOnSuccess) {
|
|
|
|
document.getElementById('loading').style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.response.error) {
|
|
|
|
if (pageLeaveOnSuccess) {
|
|
|
|
document.getElementById('loading').style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
formError.style.display = 'block';
|
|
|
|
formError.innerHTML = this.response.error.errorText;
|
2023-09-25 21:19:32 +02:00
|
|
|
if (typeof grecaptcha !== 'undefined') {
|
|
|
|
grecaptcha.reset();
|
|
|
|
}
|
2020-06-25 19:14:46 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 22:34:32 +02:00
|
|
|
if (this.response.redirect) {
|
|
|
|
window.location.replace(this.response.redirect.target);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 19:14:46 +02:00
|
|
|
if (!pageLeaveOnSuccess) {
|
|
|
|
formError.style.display = 'none';
|
|
|
|
form.reset();
|
|
|
|
} else {
|
2020-06-25 22:34:32 +02:00
|
|
|
if (form.dataset.redirectOnSuccess) {
|
|
|
|
window.location.replace(form.dataset.redirectOnSuccess);
|
|
|
|
} else if (form.dataset.reloadOnSuccess) {
|
2020-06-25 19:14:46 +02:00
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, formData);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-14 02:30:15 +02:00
|
|
|
showModal: function (id) {
|
|
|
|
document.getElementById(id).style.visibility = 'visible';
|
|
|
|
document.getElementById('cover').style.visibility = 'visible';
|
|
|
|
},
|
|
|
|
|
|
|
|
showModalWithContent: function (title, body, extraButtons) {
|
|
|
|
if (typeof extraButtons === 'undefined') {
|
|
|
|
extraButtons = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
MapGuesser.showModal('modal');
|
|
|
|
|
|
|
|
document.getElementById('modalTitle').textContent = title;
|
|
|
|
|
|
|
|
if (typeof body === 'object') {
|
|
|
|
document.getElementById('modalText').appendChild(body);
|
|
|
|
} else {
|
|
|
|
document.getElementById('modalText').textContent = body;
|
|
|
|
}
|
|
|
|
|
|
|
|
var buttons = document.getElementById('modalButtons');
|
|
|
|
buttons.textContent = '';
|
|
|
|
|
2020-06-14 11:31:50 +02:00
|
|
|
for (var i = 0; i < extraButtons.length; i++) {
|
|
|
|
var extraButton = extraButtons[i];
|
2020-06-14 02:30:15 +02:00
|
|
|
var button = document.createElement(extraButton.type);
|
|
|
|
|
|
|
|
if (extraButton.type === 'a') {
|
|
|
|
button.classList.add('button');
|
|
|
|
}
|
|
|
|
|
2020-06-14 11:31:50 +02:00
|
|
|
for (var i = 0; i < extraButton.classNames.length; i++) {
|
|
|
|
button.classList.add(extraButton.classNames[i]);
|
2020-06-14 02:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
button.classList.add('marginTop');
|
|
|
|
button.classList.add('marginRight');
|
|
|
|
button.textContent = extraButton.text;
|
|
|
|
|
|
|
|
if (extraButton.type === 'a') {
|
|
|
|
button.href = extraButton.href;
|
|
|
|
} else if (extraButton.type === 'button') {
|
|
|
|
button.onclick = extraButton.onclick;
|
|
|
|
}
|
|
|
|
|
|
|
|
buttons.appendChild(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeButton = document.createElement('button');
|
|
|
|
|
|
|
|
closeButton.classList.add('gray');
|
|
|
|
closeButton.classList.add('marginTop');
|
2020-06-14 17:16:47 +02:00
|
|
|
closeButton.textContent = 'Close';
|
2020-06-14 02:30:15 +02:00
|
|
|
closeButton.onclick = function () {
|
|
|
|
MapGuesser.hideModal();
|
|
|
|
};
|
|
|
|
|
|
|
|
buttons.appendChild(closeButton);
|
|
|
|
},
|
|
|
|
|
|
|
|
hideModal: function () {
|
|
|
|
var modals = document.getElementsByClassName('modal');
|
|
|
|
|
2020-06-14 11:31:50 +02:00
|
|
|
for (var i = 0; i < modals.length; i++) {
|
|
|
|
modals[i].style.visibility = 'hidden';
|
2020-06-14 02:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('cover').style.visibility = 'hidden';
|
2020-06-14 20:15:53 +02:00
|
|
|
},
|
|
|
|
|
2023-09-24 00:40:16 +02:00
|
|
|
observeInput: function (form, observedInputs) {
|
|
|
|
var anyChanged = false;
|
|
|
|
|
|
|
|
for (var i = 0; i < observedInputs.length; i++) {
|
|
|
|
var input = form.elements[observedInputs[i]];
|
|
|
|
if (input.type === 'checkbox') {
|
|
|
|
if (input.defaultChecked !== input.checked) {
|
|
|
|
anyChanged = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (input.defaultValue !== input.value) {
|
|
|
|
anyChanged = true;
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 20:15:53 +02:00
|
|
|
}
|
2023-09-24 00:40:16 +02:00
|
|
|
|
|
|
|
form.elements['submit_button'].disabled = !anyChanged;
|
2020-06-25 19:14:46 +02:00
|
|
|
},
|
|
|
|
|
2020-06-25 22:34:32 +02:00
|
|
|
observeInputsInForm: function (form, observedInputs) {
|
2020-06-25 19:14:46 +02:00
|
|
|
for (var i = 0; i < observedInputs.length; i++) {
|
|
|
|
var input = form.elements[observedInputs[i]];
|
|
|
|
|
|
|
|
switch (input.tagName) {
|
|
|
|
case 'INPUT':
|
|
|
|
case 'TEXTAREA':
|
|
|
|
input.oninput = function () {
|
2023-09-24 00:40:16 +02:00
|
|
|
MapGuesser.observeInput(form, observedInputs);
|
2020-06-25 19:14:46 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'SELECT':
|
|
|
|
input.onchange = function () {
|
2023-09-24 00:40:16 +02:00
|
|
|
MapGuesser.observeInput(form, observedInputs);
|
2020-06-25 19:14:46 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
form.onreset = function () {
|
2023-09-24 00:40:16 +02:00
|
|
|
form.elements['submit_button'].disabled = true;
|
2020-06-25 19:14:46 +02:00
|
|
|
}
|
2020-06-13 22:39:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
var anchors = document.getElementsByTagName('a');
|
|
|
|
for (var i = 0; i < anchors.length; i++) {
|
|
|
|
var a = anchors[i];
|
|
|
|
if (a.href !== 'javascript:;' && a.target !== '_blank') {
|
|
|
|
a.onclick = function () {
|
|
|
|
document.getElementById('loading').style.visibility = 'visible';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 02:30:15 +02:00
|
|
|
|
2020-06-25 22:34:32 +02:00
|
|
|
var forms = document.getElementsByTagName('form');
|
|
|
|
for (var i = 0; i < forms.length; i++) {
|
|
|
|
var form = forms[i];
|
|
|
|
|
|
|
|
if (form.dataset.noSubmit) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapGuesser.setOnsubmitForForm(form);
|
|
|
|
|
|
|
|
if (form.dataset.observeInputs) {
|
|
|
|
MapGuesser.observeInputsInForm(form, form.dataset.observeInputs.split(','));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 02:30:15 +02:00
|
|
|
document.getElementById('cover').onclick = function () {
|
|
|
|
MapGuesser.hideModal();
|
|
|
|
};
|
2020-06-28 17:56:47 +02:00
|
|
|
|
|
|
|
if (COOKIES_CONSENT) {
|
|
|
|
MapGuesser.initGoogleAnalitics();
|
|
|
|
} else {
|
|
|
|
// we don't want user to agree cookies when clicking on the notice itself
|
|
|
|
document.getElementById('cookiesNotice').onclick = function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
document.getElementById('agreeCookiesButton').onclick = function () {
|
|
|
|
MapGuesser.agreeCookies();
|
|
|
|
|
|
|
|
document.getElementById('cookiesNotice').style.display = 'none';
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onclick = function () {
|
|
|
|
MapGuesser.agreeCookies();
|
|
|
|
};
|
|
|
|
}
|
2020-06-13 22:39:17 +02:00
|
|
|
})();
|