getnamingo-registry/cp/resources/views/partials/js-tokens.twig
2025-04-08 12:20:24 +03:00

150 lines
No EOL
6.9 KiB
Twig

<script src="/assets/js/tabulator.min.js" defer></script>
<script src="/assets/js/sweetalert2.min.js" defer></script>
<script src="/assets/js/tabler.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 copyableFormatter(cell) {
const displayName = cell.getValue();
return `<span style="cursor:text; font-weight:bold;"
onclick="window.getSelection().selectAllChildren(this)">
${displayName}
</span>`;
}
function typeStatusFormatter(cell) {
const type = cell.getValue();
let colorClass = "blue"; // default
switch (type) {
case "simple":
colorClass = "blue";
break;
case "advanced":
colorClass = "orange";
break;
case "complex":
colorClass = "red";
break;
default:
colorClass = "gray";
}
return `<span class="status status-${colorClass}">${type}</span>`;
}
function lifecycleStatusFormatter(cell) {
const status = cell.getValue();
let colorClass = "gray"; // fallback
switch (status) {
case "new":
colorClass = "green";
break;
case "active":
colorClass = "info";
break;
case "used":
colorClass = "yellow";
break;
case "deprecated":
colorClass = "red";
break;
default:
colorClass = "gray";
}
return `<span class="status status-${colorClass}">${status}</span>`;
}
function domainFormatter(cell) {
const value = cell.getValue();
if (value) {
return `<strong>${value}</strong>`;
} else {
return `<span class="status status-info">any</span>`;
}
}
function actionsFormatter(cell, formatterParams, onRendered) {
return `<a class="btn btn-outline-primary btn-icon update-btn" href="/registry/tokens/update/${cell.getRow().getData().token}" title="{{ __('Update Token') }}"><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>
<a class="btn btn-outline-danger btn-icon delete-btn" id="delete-btn" href="javascript:void(0);" data-delete-url="/registry/tokens/delete/${cell.getRow().getData().token}" title="{{ __('Delete Token') }}"><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="M4 7h16"></path><path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"></path><path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"></path><path d="M10 12l4 4m0 -4l-4 4"></path></svg></a>`;
}
table = new Tabulator("#tokenTable", {
ajaxURL:"/api/records/allocation_tokens", // Set the URL for your JSON data
ajaxConfig:"GET",
pagination:"local",
paginationSize: 10,
paginationSizeSelector:[10, 25, 50, 100],
paginationCounter:"rows",
paginationCounterElement:"#page-count",
clipboard:true,
clipboardPasteAction:"replace",
ajaxResponse:function(url, params, response){
return response.records;
},
layout:"fitColumns",
responsiveLayout: "collapse",
responsiveLayoutCollapseStartOpen:false,
resizableColumns:false,
placeholder: "{{ __('No Data') }}",
columns:[
{formatter:"responsiveCollapse", width:40, minWidth:40, hozAlign:"center", resizable:false, headerSort:false, responsive:0},
{title:"{{ __('Token') }}", field:"token", width:400, minWidth:200, headerSort:true, resizable:false, formatter: copyableFormatter, responsive:2},
{ title: "{{ __('Domain') }}", field: "domain_name", width:300, minWidth:80, headerSort:true, resizable:false, formatter: domainFormatter, responsive:2},
{title:"{{ __('Type') }}", field:"tokenType", width:160, minWidth:100, headerSort:true, resizable:false, formatter: typeStatusFormatter, responsive:2},
{title:"{{ __('Status') }}", field:"tokenStatus", width:160, minWidth:100, headerSort:true, resizable:false, formatter: lifecycleStatusFormatter, responsive:0},
{title: "{{ __('Actions') }}", formatter: actionsFormatter, resizable:false, headerSort:false, download:false, hozAlign: "center", responsive:0, cellClick: function(e, cell){
if (e.target.closest('.delete-btn')) {
e.preventDefault(); // Prevent the default link behavior
Swal.fire({
title: "{{ __('Are you sure you want to delete this token?') }}",
showCancelButton: true,
confirmButtonText: "{{ __('Confirm') }}"
}).then((result) => {
if (result.isConfirmed) {
let deleteUrl = e.target.closest('.delete-btn').getAttribute('data-delete-url');
window.location.href = deleteUrl;
}
});
}
}},
]
});
var searchInput = document.getElementById("search-input");
searchInput.addEventListener("input", function () {
var term = searchInput.value.toLowerCase();
if (term) { // Only apply the filter when there's a term to search for
table.setFilter(function (data) {
// Check if any of the fields contain the search term
return (
String(data.token).toLowerCase().includes(term) ||
String(data.domain_name).toLowerCase().includes(term) ||
String(data.tokenType).toLowerCase().includes(term) ||
String(data.tokenStatus).toLowerCase().includes(term)
);
});
} else {
table.clearFilter(); // Clear the filter when the search box is emptied
}
});
});
function downloadCSV() {
table.download("csv", "tokens.csv");
}
function downloadPDF() {
table.download("pdf", "tokens.pdf", {
orientation:"portrait",
title:"My Tokens"
});
}
</script>