getnamingo-registry/cp/resources/views/partials/js-users.twig
2025-02-04 10:12:28 +02:00

160 lines
No EOL
6.8 KiB
Twig

<script src="/assets/js/tabulator.min.js" defer></script>
<script src="/assets/js/tabler.min.js" defer></script>
<script src="/assets/js/xlsx.full.min.js" defer></script>
<script src="/assets/js/jspdf.umd.min.js" defer></script>
<script src="/assets/js/jspdf.plugin.autotable.min.js" defer></script>
<script>
var table;
document.addEventListener("DOMContentLoaded", function(){
function userLinkFormatter(cell){
var value = cell.getValue();
return `<span style="font-weight:bold;">${value}</a>`;
}
function actionsFormatter(cell, formatterParams, onRendered) {
return `
<a class="btn btn-outline-primary btn-icon update-btn" href="/user/update/${cell.getRow().getData().username}" title="{{ __('Manage User') }}"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1"></path><path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z"></path><path d="M16 5l3 3"></path></svg></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>';
} else if (value === 6) {
return '<span class="status status-azure">Registrar Assistant</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>';
}
}
var searchTerm = ""; // global variable to hold the search term
function updateSearchTerm(term) {
searchTerm = term;
table.replaceData();
}
table = new Tabulator("#userTable", {
pagination: true,
paginationMode: "remote",
paginationSize: 10,
sortMode: "remote",
ajaxURL: "/api/records/users",
ajaxURLGenerator: function(url, config, params) {
var queryParts = [];
// Handle search term
if (searchTerm) {
queryParts.push("filter1=username,cs," + encodeURIComponent(searchTerm));
queryParts.push("filter2=email,cs," + encodeURIComponent(searchTerm));
queryParts.push("filter3=roles_mask,cs," + encodeURIComponent(searchTerm));
}
// Handle sorting from Tabulator
if (params.sort && params.sort.length > 0) {
var sorter = params.sort[0]; // single-column sorting
var sortField = encodeURIComponent(sorter.field);
var sortDir = (sorter.dir === "asc" ? "asc" : "desc");
queryParts.push("order=" + sortField + "," + sortDir);
} else {
// fallback default order if no sorters
queryParts.push("order=id,desc");
}
// Include pagination parameters
if (params.page) {
queryParts.push("page=" + params.page + "," + params.size);
}
return url + "?" + queryParts.join("&");
},
ajaxResponse: function(url, params, response) {
if (response && Array.isArray(response.records) && typeof response.results === 'number') {
var lastPage = Math.ceil(response.results / this.options.paginationSize);
return {
last_page: lastPage, // Calculated total number of pages
data: response.records, // Data for the current page
};
} else {
console.error('Unexpected response format', response);
return { last_page: 1, data: [] };
}
},
layout:"fitDataFill",
responsiveLayout: "collapse",
responsiveLayoutCollapseStartOpen:false,
resizableColumns:false,
placeholder: "{{ __('No Data') }}",
columns:[
{formatter:"responsiveCollapse", width:30, minWidth:30, hozAlign:"center", resizable:false, headerSort:false, responsive:0},
{title:"{{ __('Name') }}", field:"username", width:200, resizable:false, headerSort:true, formatter: userLinkFormatter, responsive:0},
{title:"{{ __('Email') }}", field:"email", width:300, resizable:false, headerSort:true, responsive:2},
{title:"{{ __('Roles') }}", field:"roles_mask", width:200, resizable:false, headerSort:true, formatter: roleLabelFormatter, responsive:2},
{title:"{{ __('Verified') }}", field:"verified", width:150, resizable:false, headerSort:true, formatter: verifiedFormatter, responsive:2},
{title:"{{ __('Status') }}", field:"status", width:150, resizable:false, headerSort:true, formatter: statusBadgeFormatter, responsive:2},
{title: "{{ __('Actions') }}", formatter: actionsFormatter, responsive:0, headerSort: false, download:false, hozAlign: "center", cellClick:function(e, cell){ e.stopPropagation(); }},
]
});
var searchInput = document.getElementById("search-input");
let searchTimeout;
searchInput.addEventListener("input", function () {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => {
updateSearchTerm(searchInput.value);
}, 300); // 300ms delay
});
});
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>