rvr-nextgen/public/static/js/rvr.js

278 lines
8.5 KiB
JavaScript
Raw Permalink Normal View History

2023-04-08 00:42:55 +02:00
var RVR = {
isSecure: window.location.protocol === 'https:',
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 || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', GOOGLE_ANALITICS_ID);
},
httpRequest: function (method, url, callback, data) {
var xhr = new XMLHttpRequest();
xhr.onload = callback;
xhr.open(method, url, true);
xhr.responseType = 'json';
if (method === 'POST') {
if (typeof data === 'undefined') {
data = new FormData();
}
data.append('anti_csrf_token', ANTI_CSRF_TOKEN);
xhr.send(data);
} else {
xhr.send();
}
},
setOnsubmitForForm: function (form) {
form.onsubmit = function (e) {
e.preventDefault();
document.getElementById('loading').style.visibility = 'visible';
var formData = new FormData(form);
var formError = document.getElementsByClassName('formError')[0];
2023-04-08 00:42:55 +02:00
var pageLeaveOnSuccess = form.dataset.redirectOnSuccess || form.dataset.reloadOnSuccess;
RVR.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-05-08 18:48:43 +02:00
if (typeof grecaptcha !== 'undefined') {
grecaptcha.reset();
}
2023-04-08 00:42:55 +02:00
return;
}
if (this.response.redirect) {
window.location.replace(this.response.redirect.target);
return;
}
if (!pageLeaveOnSuccess) {
formError.style.display = 'none';
form.reset();
} else {
if (form.dataset.redirectOnSuccess) {
window.location.replace(form.dataset.redirectOnSuccess);
} else if (form.dataset.reloadOnSuccess) {
window.location.reload();
}
}
}, formData);
}
},
setOnclickForFormConfirmation: function (button) {
button.onclick = function(e) {
e.preventDefault();
var self = this;
RVR.showModalWithContent('Confirmation', this.dataset.confirmation, [
{
type: 'button',
html: this.dataset.confirmationButton ? this.dataset.confirmationButton : this.innerHTML,
classNames: ['red'],
onclick: function() {
var event = new Event('submit', {'bubbles': true, 'cancelable': true});
self.form.dispatchEvent(event);
RVR.hideModal();
}
}
]);
};
},
2023-04-08 00:42:55 +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 = [];
}
RVR.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 = '';
for (var i = 0; i < extraButtons.length; i++) {
var extraButton = extraButtons[i];
var button = document.createElement(extraButton.type);
if (extraButton.type === 'a') {
button.classList.add('button');
}
for (var i = 0; i < extraButton.classNames.length; i++) {
button.classList.add(extraButton.classNames[i]);
}
button.classList.add('marginTop');
button.classList.add('marginRight');
if (typeof extraButton.html !== 'undefined') {
button.innerHTML = extraButton.html;
} else {
button.textContent = extraButton.text;
}
2023-04-08 00:42:55 +02:00
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');
closeButton.textContent = 'Close';
closeButton.onclick = function () {
RVR.hideModal();
};
buttons.appendChild(closeButton);
},
hideModal: function () {
var modals = document.getElementsByClassName('modal');
for (var i = 0; i < modals.length; i++) {
modals[i].style.visibility = 'hidden';
}
document.getElementById('cover').style.visibility = 'hidden';
},
2023-04-09 02:11:09 +02:00
observeInput: function (form, observedInputs) {
var anyChanged = false;
for (var i = 0; i < observedInputs.length; i++) {
var input = form.elements[observedInputs[i]];
2023-04-23 15:55:31 +02:00
if (input.type === 'checkbox') {
if (input.defaultChecked !== input.checked) {
anyChanged = true;
}
} else {
if (input.defaultValue !== input.value) {
anyChanged = true;
}
2023-04-09 02:11:09 +02:00
}
2023-04-08 00:42:55 +02:00
}
2023-04-09 02:11:09 +02:00
2023-05-02 02:21:49 +02:00
form.elements['submit_button'].disabled = !anyChanged;
2023-04-08 00:42:55 +02:00
},
observeInputsInForm: function (form, observedInputs) {
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-04-09 02:11:09 +02:00
RVR.observeInput(form, observedInputs);
2023-04-08 00:42:55 +02:00
};
break;
case 'SELECT':
input.onchange = function () {
2023-04-09 02:11:09 +02:00
RVR.observeInput(form, observedInputs);
2023-04-08 00:42:55 +02:00
};
break;
}
}
form.onreset = function () {
2023-05-02 02:21:49 +02:00
form.elements['submit_button'].disabled = true;
2023-04-08 00:42:55 +02:00
}
2023-04-16 03:31:40 +02:00
},
debounce: function(func, timeout = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
2023-04-08 00:42:55 +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';
}
}
}
var forms = document.getElementsByTagName('form');
for (var i = 0; i < forms.length; i++) {
var form = forms[i];
if (form.dataset.noSubmit) {
continue;
}
RVR.setOnsubmitForForm(form);
if (form.dataset.observeInputs) {
RVR.observeInputsInForm(form, form.dataset.observeInputs.split(','));
}
if (form.elements['submit_button'] && form.elements['submit_button'].dataset.confirmation) {
RVR.setOnclickForFormConfirmation(form.elements['submit_button']);
}
2023-04-08 00:42:55 +02:00
}
document.getElementById('cover').onclick = function () {
RVR.hideModal();
};
RVR.initGoogleAnalitics();
})();