django_coreprotect/static/coreprotect/js/main.js

78 lines
1.9 KiB
JavaScript
Raw Normal View History

(function() {
flatpickr('[type="date"]', {
altInput: true,
altFormat: "F j, Y at H:i",
dateFormat: "U",
enableTime: true,
});
})();
function ajax(method, url, callback) {
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4
callback(xmlhttp.status, xmlhttp.responseText);
}
};
xmlhttp.open(method, url, true);
xmlhttp.send();
}
function formURI(formData) {
let pairs = [];
for (let data of formData.entries()) {
pairs.push(encodeURIComponent(data[0]) + '=' + encodeURIComponent(data[1]))
}
return pairs.join("&").replace(/%20/g, '+');
}
function elementHTML(id, html) {
document.getElementById(id).innerHTML = html
}
function downloadCSV(csv, filename) {
let csvFile;
let downloadLink;
// CSV file
csvFile = new Blob([csv], {type: "text/csv"});
// Download link
downloadLink = document.createElement("a");
// File name
downloadLink.download = filename;
// Create a link to the file
downloadLink.href = window.URL.createObjectURL(csvFile);
// Hide download link
downloadLink.style.display = "none";
// Add the link to DOM
document.body.appendChild(downloadLink);
// Click download link
downloadLink.click();
}
function exportTableToCSV(filename, elementId) {
let csv = [];
let selector = "table tr";
if (elementId !== "") {
selector = "#" + elementId + " tr"
}
let rows = document.querySelectorAll(selector);
for (let i = 0; i < rows.length; i++) {
let row = [], cols = rows[i].querySelectorAll("td, th");
for (let j = 0; j < cols.length; j++)
row.push('"' + cols[j].innerText + '"');
csv.push(row.join(","));
}
// Download CSV file
downloadCSV(csv.join("\n"), filename);
}