2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-03-07 01:28:32 +01:00
|
|
|
import {Controller} from "@hotwired/stimulus";
|
2022-08-13 22:41:54 +02:00
|
|
|
//import * as ZXing from "@zxing/library";
|
|
|
|
|
|
2025-01-04 18:09:56 +01:00
|
|
|
import {Html5QrcodeScanner, Html5Qrcode} from "@part-db/html5-qrcode";
|
2026-01-16 22:42:20 +13:00
|
|
|
import { generateCsrfToken, generateCsrfHeaders } from "../csrf_protection_controller";
|
2022-03-07 01:28:32 +01:00
|
|
|
|
|
|
|
|
/* stimulusFetch: 'lazy' */
|
2022-08-13 22:41:54 +02:00
|
|
|
|
2026-01-16 13:49:26 +13:00
|
|
|
export default class extends Controller {
|
2022-08-13 22:41:54 +02:00
|
|
|
_scanner = null;
|
2026-01-16 13:49:26 +13:00
|
|
|
_submitting = false;
|
2026-01-16 22:42:20 +13:00
|
|
|
_lastDecodedText = "";
|
2022-03-07 01:28:32 +01:00
|
|
|
|
|
|
|
|
connect() {
|
2026-01-16 13:49:26 +13:00
|
|
|
// Prevent double init if connect fires twice
|
|
|
|
|
if (this._scanner) return;
|
2022-03-07 01:28:32 +01:00
|
|
|
|
2026-01-16 22:42:20 +13:00
|
|
|
this.bindModeToggles();
|
|
|
|
|
|
2025-01-06 00:49:16 +01:00
|
|
|
//This function ensures, that the qrbox is 70% of the total viewport
|
|
|
|
|
let qrboxFunction = function(viewfinderWidth, viewfinderHeight) {
|
|
|
|
|
let minEdgePercentage = 0.7; // 70%
|
|
|
|
|
let minEdgeSize = Math.min(viewfinderWidth, viewfinderHeight);
|
|
|
|
|
let qrboxSize = Math.floor(minEdgeSize * minEdgePercentage);
|
|
|
|
|
return {
|
|
|
|
|
width: qrboxSize,
|
|
|
|
|
height: qrboxSize
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 22:41:54 +02:00
|
|
|
//Try to get the number of cameras. If the number is 0, then the promise will fail, and we show the warning dialog
|
2026-01-16 13:49:26 +13:00
|
|
|
Html5Qrcode.getCameras().catch(() => {
|
|
|
|
|
document.getElementById("scanner-warning")?.classList.remove("d-none");
|
2022-08-13 22:41:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this._scanner = new Html5QrcodeScanner(this.element.id, {
|
2025-01-04 16:57:41 +01:00
|
|
|
fps: 10,
|
2025-01-06 00:49:16 +01:00
|
|
|
qrbox: qrboxFunction,
|
|
|
|
|
experimentalFeatures: {
|
|
|
|
|
//This option improves reading quality on android chrome
|
2026-01-16 13:49:26 +13:00
|
|
|
useBarCodeDetectorIfSupported: true,
|
|
|
|
|
},
|
2022-08-13 22:41:54 +02:00
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
this._scanner.render(this.onScanSuccess.bind(this));
|
|
|
|
|
}
|
2022-03-07 01:28:32 +01:00
|
|
|
|
2025-01-04 18:10:26 +01:00
|
|
|
disconnect() {
|
2026-01-16 13:49:26 +13:00
|
|
|
// If we already stopped/cleared before submit, nothing to do.
|
|
|
|
|
const scanner = this._scanner;
|
|
|
|
|
this._scanner = null;
|
|
|
|
|
this._submitting = false;
|
2026-01-16 22:42:20 +13:00
|
|
|
this._lastDecodedText = "";
|
|
|
|
|
this.unbindModeToggles();
|
2026-01-16 13:49:26 +13:00
|
|
|
|
|
|
|
|
if (!scanner) return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const p = scanner.clear?.();
|
|
|
|
|
if (p && typeof p.then === "function") p.catch(() => {});
|
|
|
|
|
} catch (_) {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2025-01-04 18:10:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 22:42:20 +13:00
|
|
|
/**
|
|
|
|
|
* Add events to Mode checkboxes so they both can't be selected at the same time
|
|
|
|
|
*/
|
|
|
|
|
bindModeToggles() {
|
|
|
|
|
const info = document.getElementById("scan_dialog_info_mode");
|
|
|
|
|
const aug = document.getElementById("scan_dialog_augmented_mode");
|
|
|
|
|
if (!info || !aug) return;
|
|
|
|
|
|
|
|
|
|
const onInfoChange = () => {
|
|
|
|
|
if (info.checked) aug.checked = false;
|
|
|
|
|
};
|
|
|
|
|
const onAugChange = () => {
|
|
|
|
|
if (aug.checked) info.checked = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info.addEventListener("change", onInfoChange);
|
|
|
|
|
aug.addEventListener("change", onAugChange);
|
|
|
|
|
|
|
|
|
|
// Save references so we can remove listeners on disconnect
|
|
|
|
|
this._onInfoChange = onInfoChange;
|
|
|
|
|
this._onAugChange = onAugChange;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unbindModeToggles() {
|
|
|
|
|
const info = document.getElementById("scan_dialog_info_mode");
|
|
|
|
|
const aug = document.getElementById("scan_dialog_augmented_mode");
|
|
|
|
|
if (!info || !aug) return;
|
|
|
|
|
|
|
|
|
|
if (this._onInfoChange) info.removeEventListener("change", this._onInfoChange);
|
|
|
|
|
if (this._onAugChange) aug.removeEventListener("change", this._onAugChange);
|
|
|
|
|
|
|
|
|
|
this._onInfoChange = null;
|
|
|
|
|
this._onAugChange = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-16 13:49:26 +13:00
|
|
|
async onScanSuccess(decodedText) {
|
2026-01-16 22:42:20 +13:00
|
|
|
if (!decodedText) return;
|
|
|
|
|
|
|
|
|
|
const normalized = String(decodedText).trim();
|
|
|
|
|
|
|
|
|
|
// If we already handled this exact barcode and it's still showing, ignore.
|
|
|
|
|
if (normalized === this._lastDecodedText) return;
|
|
|
|
|
|
|
|
|
|
// If a request/submit is in-flight, ignore scans.
|
2026-01-16 13:49:26 +13:00
|
|
|
if (this._submitting) return;
|
2026-01-16 22:42:20 +13:00
|
|
|
|
|
|
|
|
// Mark as handled immediately (prevents spam even if callback fires repeatedly)
|
|
|
|
|
this._lastDecodedText = normalized;
|
2026-01-16 13:49:26 +13:00
|
|
|
this._submitting = true;
|
|
|
|
|
|
2022-08-13 22:41:54 +02:00
|
|
|
//Put our decoded Text into the input box
|
2026-01-16 13:49:26 +13:00
|
|
|
const input = document.getElementById("scan_dialog_input");
|
|
|
|
|
if (input) input.value = decodedText;
|
|
|
|
|
|
2026-01-16 22:42:20 +13:00
|
|
|
const augmented = !!document.getElementById("scan_dialog_augmented_mode")?.checked;
|
|
|
|
|
|
|
|
|
|
// If augmented mode: do NOT submit the form.
|
|
|
|
|
if (augmented) {
|
|
|
|
|
try {
|
|
|
|
|
await this.lookupAndRender(decodedText);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn("[barcode_scan] augmented lookup failed", e);
|
|
|
|
|
// Allow retry on failure by clearing last decoded text
|
|
|
|
|
this._lastDecodedText = "";
|
|
|
|
|
} finally {
|
|
|
|
|
// allow scanning again
|
|
|
|
|
this._submitting = false;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Non-augmented: Stop scanner BEFORE submitting to avoid camera transition races
|
2026-01-16 13:49:26 +13:00
|
|
|
try {
|
|
|
|
|
if (this._scanner?.clear) {
|
|
|
|
|
await this._scanner.clear();
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {
|
|
|
|
|
// ignore
|
|
|
|
|
} finally {
|
|
|
|
|
this._scanner = null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 22:41:54 +02:00
|
|
|
//Submit form
|
2026-01-16 13:49:26 +13:00
|
|
|
document.getElementById("scan_dialog_form")?.requestSubmit();
|
2022-03-07 01:28:32 +01:00
|
|
|
}
|
2026-01-16 22:42:20 +13:00
|
|
|
|
|
|
|
|
async lookupAndRender(decodedText) {
|
|
|
|
|
const form = document.getElementById("scan_dialog_form");
|
|
|
|
|
if (!form) return;
|
|
|
|
|
|
|
|
|
|
// Ensure the hidden csrf field has been converted from placeholder -> real token + cookie set
|
|
|
|
|
generateCsrfToken(form);
|
|
|
|
|
|
|
|
|
|
const mode =
|
|
|
|
|
document.querySelector('input[name="scan_dialog[mode]"]:checked')?.value ?? "";
|
|
|
|
|
|
|
|
|
|
const body = new URLSearchParams();
|
|
|
|
|
body.set("input", decodedText);
|
|
|
|
|
if (mode !== "") body.set("mode", mode);
|
|
|
|
|
|
|
|
|
|
const headers = {
|
|
|
|
|
"Accept": "text/html",
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
|
|
|
...generateCsrfHeaders(form), // adds the special CSRF header Symfony expects (if enabled)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resp = await fetch(this.element.dataset.augmentedUrl, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers,
|
|
|
|
|
body: body.toString(),
|
|
|
|
|
credentials: "same-origin",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const html = await resp.text();
|
|
|
|
|
|
|
|
|
|
const el = document.getElementById("scan-augmented-result");
|
|
|
|
|
if (el) el.innerHTML = html;
|
|
|
|
|
}
|
2026-01-19 10:14:17 +13:00
|
|
|
}
|