getnamingo-registry/cp/resources/views/partials/js-users.twig
2024-02-14 13:48:33 +02:00

117 lines
No EOL
4.7 KiB
Twig

<script src="/assets/js/tabulator.min.js" defer></script>
<script src="/assets/js/tabler.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.6.0/jspdf.plugin.autotable.min.js"></script>
<script>
var table;
document.addEventListener("DOMContentLoaded", function(){
function userLinkFormatter(cell){
var value = cell.getValue();
return `<span style="font-weight:bold;">${value}</a>`;
}
function statusFormatter(cell) {
var statusArray = cell.getValue();
if (statusArray && Array.isArray(statusArray)) {
return statusArray.map(item => item.status).join(', ');
}
return "";
}
function roleLabelFormatter(cell) {
var value = cell.getValue();
if (value === 0) {
return '<span class="status status-purple">Administrator</span>';
} else if (value === 4) {
return '<span class="status status-indigo">Registrar</span>';
}
return value; // If the value is neither 0 nor 4, return it as is
}
function verifiedFormatter(cell) {
var value = cell.getValue();
if (value === false) {
return '<span class="status status-orange">Pending</span>';
} else if (value === true) {
return '<span class="status status-green">ok</span>';
} else {
return '<span class="status status-dark">?</span>';
}
return value;
}
function statusBadgeFormatter(cell) {
var value = cell.getValue();
if (value === 0) {
return '<span class="status status-green">ok</span>';
} else {
return '<span class="status status-red">Trouble</span>';
}
}
table = new Tabulator("#userTable", {
ajaxURL:"/api/records/users", // Set the URL for your JSON data
ajaxConfig:"GET",
pagination:"local",
paginationSize:10,
ajaxResponse:function(url, params, response){
return response.records;
},
layout:"fitDataFill",
responsiveLayout: "collapse",
responsiveLayoutCollapseStartOpen:false,
resizableColumns:false,
columns:[
{formatter:"responsiveCollapse", width:30, minWidth:30, hozAlign:"center", resizable:false, headerSort:false, responsive:0},
{title:"{{ __('Name') }}", field:"username", width:200, headerSort:true, formatter: userLinkFormatter, responsive:0},
{title:"{{ __('Email') }}", field:"email", width:300, headerSort:true, responsive:2},
{title:"{{ __('Roles') }}", field:"roles_mask", width:200, headerSort:true, formatter: roleLabelFormatter, responsive:2},
{title:"{{ __('Verified') }}", field:"verified", width:200, headerSort:true, formatter: verifiedFormatter, responsive:2},
{title:"{{ __('Status') }}", field:"status", width:200, headerSort:true, formatter: statusBadgeFormatter, responsive:2},
],
placeholder:function(){
return this.getHeaderFilters().length ? "No Matching Data" : "{{ __('No Data') }}"; //set placeholder based on if there are currently any header filters
}
});
var searchInput = document.getElementById("search-input");
searchInput.addEventListener("input", function () {
var term = searchInput.value.toLowerCase();
if (term) { // Only apply filter when there's a term to search for
table.setFilter(function (data) {
return (
String(data.username).toLowerCase().includes(term) ||
String(data.email).toLowerCase().includes(term) ||
String(data.roles_mask).toString().toLowerCase().includes(term) ||
String(data.verified).toLowerCase().includes(term) ||
String(data.status).toLowerCase().includes(term)
);
});
} else {
table.clearFilter(); // Clear the filter when the search box is emptied
}
});
});
function downloadCSV() {
table.download("csv", "data.csv");
}
function downloadJSON() {
table.download("json", "data.json");
}
function downloadXLSX() {
table.download("xlsx", "data.xlsx", {sheetName:"My Users"});
}
function downloadPDF() {
table.download("pdf", "data.pdf", {
orientation:"portrait",
title:"My Users",
jsPDF:{unit:"mm", format:"a4", orientation:"p"}
});
}
</script>