Bug 40659: (follow-up) prevent Javascript errors when SVG missing

Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
David Cook 2026-04-21 04:34:15 +00:00 committed by Lucas Gass
parent 609c11afc4
commit 16288c9187
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
1 changed files with 35 additions and 28 deletions

View File

@ -1,34 +1,41 @@
document.addEventListener("DOMContentLoaded", function () {
let barcodeNumber;
let barcodeFormat;
const svgElement = document.getElementById("patron-barcode");
var barcodeNumber = svgElement.dataset.barcode;
var barcodeFormat = svgElement.dataset.barcodeFormat;
if (svgElement) {
barcodeNumber = svgElement.dataset.barcode;
barcodeFormat = svgElement.dataset.barcodeFormat;
if (barcodeNumber && barcodeFormat) {
try {
// Generate the barcode SVG
let svg = bwipjs.toSVG({
bcid: barcodeFormat, // Barcode type
text: barcodeNumber, // Text to encode
padding: 0,
height: 15,
});
// Add the generated SVG to the barcode container
if (barcodeFormat == "qrcode") {
document
.getElementById("barcode-container")
.classList.add("qrcode");
}
document.getElementById("barcode-container").innerHTML = svg;
} catch (error) {
console.error(error);
try {
// Generate the barcode SVG
let svg = bwipjs.toSVG({
bcid: barcodeFormat, // Barcode type
text: barcodeNumber, // Text to encode
padding: 0,
height: 15,
});
// Add the generated SVG to the barcode container
if (barcodeFormat == "qrcode") {
document
.getElementById("barcode-container")
.classList.add("qrcode");
const p_node = document.createElement("p");
const strong_node = document.createElement("strong");
strong_node.textContent = __("Error: ");
const span_node = document.createElement("span");
span_node.textContent = __("Unable to generate barcode");
span_node.setAttribute("id", "barcode-gen-error");
p_node.appendChild(strong_node);
p_node.appendChild(span_node);
document
.getElementById("barcode-container")
.replaceChildren(p_node);
}
}
document.getElementById("barcode-container").innerHTML = svg;
} catch (error) {
console.error(error);
const p_node = document.createElement("p");
const strong_node = document.createElement("strong");
strong_node.textContent = __("Error: ");
const span_node = document.createElement("span");
span_node.textContent = __("Unable to generate barcode");
span_node.setAttribute("id", "barcode-gen-error");
p_node.appendChild(strong_node);
p_node.appendChild(span_node);
document.getElementById("barcode-container").replaceChildren(p_node);
}
});