Added functions to site.js

This commit is contained in:
2025-10-26 08:21:59 +01:00
parent 056d2321e4
commit c72e863b92

View File

@@ -1,4 +1,91 @@
// Please see documentation at https://learn.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.
function createToastContainer() {
const container = document.createElement('div');
container.id = 'toastContainer';
container.className = 'toast-container position-fixed bottom-0 end-0 p-3';
document.body.appendChild(container);
return container;
}
// Write your JavaScript code.
// Simple toast helper
function showToast(message, type) {
const toastContainer = document.getElementById('toastContainer') || createToastContainer();
const toast = document.createElement('div');
toast.className = `toast align-items-center text-white bg-${type} border-0`;
toast.role = 'alert';
toast.innerHTML = `
<div class="d-flex">
<div class="toast-body">${message}</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
</div>
`;
toastContainer.appendChild(toast);
const bsToast = new bootstrap.Toast(toast, { delay: 3000 });
bsToast.show();
toast.addEventListener('hidden.bs.toast', () => toast.remove());
}
function downloadBarcode(svgId, filename = "barcode.png") {
const svg = document.getElementById(svgId);
const serializer = new XMLSerializer();
const svgString = serializer.serializeToString(svg);
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
// Optionally set canvas size
const bbox = svg.getBBox();
canvas.width = bbox.width + 20; // padding
canvas.height = bbox.height + 20;
const img = new Image();
const svgBlob = new Blob([svgString], { type: "image/svg+xml;charset=utf-8" });
const url = URL.createObjectURL(svgBlob);
img.onload = () => {
ctx.drawImage(img, 0, 0);
URL.revokeObjectURL(url);
const pngUrl = canvas.toDataURL("image/png");
const a = document.createElement("a");
a.href = pngUrl;
a.download = filename;
a.click();
};
img.src = url;
}
function printBarcode(svgId) {
const svg = document.getElementById(svgId);
if (!svg) return;
// Serialize the SVG
const serializer = new XMLSerializer();
const svgString = serializer.serializeToString(svg);
// Open a new window for printing
const printWindow = window.open('', '_blank');
printWindow.document.write(`
<html>
<head>
<title>Print Barcode</title>
<style>
body { text-align: center; margin: 0; padding: 20px; }
svg { width: 300px; height: auto; }
</style>
</head>
<body>
${svgString}
</body>
</html>
`);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
}
function idToEAN13(id) {
const padded = id.toString().padStart(12, "0"); // 12 digits
return padded;
}