33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-11-15 17:47:11 +00:00
jedchecker/media/com_jedchecker/js/script.js

107 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-01-15 14:55:31 +00:00
(() => {
2022-01-16 12:31:21 +00:00
'use strict';
2022-01-15 15:33:10 +00:00
let jedOptions;
try {
2022-01-16 12:31:21 +00:00
jedOptions = JSON.parse(document.getElementById('jed-rules-json').innerHTML);
2022-01-19 16:43:31 +00:00
if (!jedOptions) throw new Error();
2022-01-15 15:33:10 +00:00
} catch (e) {
throw new Error('Initialization data is missing');
}
2022-01-16 12:31:21 +00:00
const add_validation = () => {
2022-01-15 15:33:10 +00:00
// Loop over them and prevent submission
2022-01-19 16:45:05 +00:00
[...document.querySelectorAll('.needs-validation')].forEach(form => {
2022-01-15 15:33:10 +00:00
form.addEventListener('submit', (event) => {
const form = event.target;
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}
function check(url, rule) {
fetch(`${url}index.php?option=com_jedchecker&task=police.check&format=raw&rule=${rule}`)
2022-01-16 12:31:21 +00:00
.then(response => {
if (!response.ok) {
throw new Error(`${response.status} ${response.statusCode}`);
}
return response.text();
})
2022-01-15 15:33:10 +00:00
.then(data => {
2022-01-16 12:31:21 +00:00
const sidebar = document.getElementById(`jed-${rule}`);
const target = document.getElementById(`police-check-result-${rule}`);
2022-01-15 15:33:10 +00:00
target.innerHTML = data;
2022-01-19 16:44:46 +00:00
const error = target.querySelectorAll('.alert-danger').length;
2022-01-16 12:31:21 +00:00
[...sidebar.querySelectorAll('.badge.bg-danger')].forEach(el => el.textContent = error || '');
2022-01-15 15:33:10 +00:00
2022-01-19 16:44:46 +00:00
const warning = target.querySelectorAll('.alert-warning').length;
2022-01-16 12:31:21 +00:00
[...sidebar.querySelectorAll('.badge.bg-warning')].forEach(el => el.textContent = warning || '');
2022-01-15 15:33:10 +00:00
2022-01-19 16:44:46 +00:00
const compat = target.querySelectorAll('.alert-secondary').length;
2022-01-16 12:31:21 +00:00
[...sidebar.querySelectorAll('.badge.bg-secondary')].forEach(el => el.textContent = compat || '');
2022-01-15 15:33:10 +00:00
2022-01-19 16:44:46 +00:00
const info = target.querySelectorAll('.alert-info').length;
2022-01-16 12:31:21 +00:00
[...sidebar.querySelectorAll('.badge.bg-info')].forEach(el => el.textContent = info || '');
2022-01-15 15:33:10 +00:00
2022-01-19 16:44:46 +00:00
const success = target.querySelectorAll('.alert-success').length;
2022-01-19 16:42:49 +00:00
[...sidebar.querySelectorAll('.badge.bg-success')].forEach(el => el.classList.toggle('hidden', !success));
2022-01-15 15:33:10 +00:00
2022-01-16 12:31:21 +00:00
[...sidebar.querySelectorAll('.spinner-border')].forEach(el => el.classList.add('hidden'));
2022-01-15 15:33:10 +00:00
})
.catch(error => {
2022-01-16 12:31:21 +00:00
console.error(error);
const sidebar = document.getElementById(`jed-${rule}`);
const target = document.getElementById(`police-check-result-${rule}`);
2022-01-15 15:33:10 +00:00
2022-01-16 12:31:21 +00:00
target.innerHTML = `<span class="text-danger"><b>${error.name}:</b> ${error.message}</span>`;
2022-01-15 15:33:10 +00:00
2022-01-16 12:31:21 +00:00
[...sidebar.querySelectorAll('.badge.bg-danger')].forEach(el => el.textContent = '?');
2022-01-19 16:44:11 +00:00
[...sidebar.querySelectorAll('.badge.bg-warning, .badge.bg-secondary, .badge.bg-info')].forEach(el => el.textContent = '');
[...sidebar.querySelectorAll('.badge.bg-success, .spinner-border')].forEach(el => el.classList.add('hidden'));
2022-01-15 15:33:10 +00:00
});
}
let jed_collapse_init = false;
window.Joomla.submitbutton = function (task) {
2022-01-16 12:31:21 +00:00
if (task === 'check') {
[...document.querySelectorAll('.jedchecker-results')].forEach(el => el.classList.remove('hidden'));
2022-01-19 16:44:11 +00:00
[...document.querySelectorAll('.jedchecker-results .badge:not(.bg-success)')].forEach(el => el.innerHTML = '');
[...document.querySelectorAll('.jedchecker-results .badge.bg-success')].forEach(el => el.classList.add('hidden'));
[...document.querySelectorAll('.jedchecker-results .spinner-border')].forEach(el => el.classList.remove('hidden'));
2022-01-17 08:48:38 +00:00
[...document.querySelectorAll('.police-check-result')].forEach(el => el.innerHTML = '<div class="text-center text-info"><span class="spinner-border"></span></div>');
2022-01-15 15:33:10 +00:00
if (!jed_collapse_init) {
2022-01-16 12:31:21 +00:00
[...document.querySelectorAll('.card-header[data-bs-toggle]')].forEach(el => {
el.classList.add('accordion');
2022-01-19 16:45:05 +00:00
el.classList.add('accordion-button');
el.classList.add('collapsed');
2022-01-15 15:33:10 +00:00
el.setAttribute('href', el.dataset.href);
});
new bootstrap.Collapse(document.getElementById('jedchecker-welcome'));
new bootstrap.Collapse(document.getElementById('jedchecker-contributors'));
jed_collapse_init = true;
}
2022-01-19 16:45:05 +00:00
jedOptions['rules'].forEach(rule => check(jedOptions['url'], rule));
2022-01-15 15:33:10 +00:00
} else {
Joomla.submitform(task);
}
}
2022-01-16 12:31:21 +00:00
document.getElementById('extension-upload').addEventListener('click', () => {
2022-01-15 15:33:10 +00:00
add_validation();
document.getElementById('jed_uploading_spinner').classList.remove('hidden');
Joomla.submitbutton('uploads.upload')
});
new bootstrap.Tooltip(document.getElementById('jedchecker'), {container: 'body', selector: '[data-bs-toggle=tooltip]'});
2022-01-15 14:55:31 +00:00
})();