summaryrefslogtreecommitdiff
path: root/indoteknik_custom/static
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-08-21 16:04:43 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-08-21 16:04:43 +0700
commitf5619faaf199e89d753d8e9a0814b1f7894051ab (patch)
tree520c5ec0db0bb52dd852c53455994a0fc2cd9863 /indoteknik_custom/static
parent2026e1244712658e4ed2fa6182a778bb91910d5d (diff)
<Miqdad> fix
Diffstat (limited to 'indoteknik_custom/static')
-rw-r--r--indoteknik_custom/static/src/js/check_product_barcode.js97
1 files changed, 66 insertions, 31 deletions
diff --git a/indoteknik_custom/static/src/js/check_product_barcode.js b/indoteknik_custom/static/src/js/check_product_barcode.js
index d81d630a..2fddc616 100644
--- a/indoteknik_custom/static/src/js/check_product_barcode.js
+++ b/indoteknik_custom/static/src/js/check_product_barcode.js
@@ -1,41 +1,76 @@
-odoo.define('indoteknik_custom.prevent_manual_typing', function (require) {
- "use strict";
+odoo.define('indoteknik_custom.buffered_scanner', function (require) {
+ 'use strict';
+ console.log('✅ Indoteknik_Custom JS Loaded');
- console.log("✅ Custom JS from indoteknik_custom loaded!");
+ var GAP_MS = 120;
+ var MIN_LEN = 3;
+ var COMMIT_TIMEOUT = 180;
+ var buffer = '';
+ var last = 0;
+ var timer = null;
- const THRESHOLD_MS = 500;
- let lastTime = 0;
- let burstCount = 0;
+ function reset() {
+ buffer = '';
+ last = 0;
+ if (timer) { clearTimeout(timer); timer = null; }
+ }
- function isScannerLike(now) {
- const gap = now - lastTime;
- lastTime = now;
- if (gap < THRESHOLD_MS) {
- burstCount += 1;
- } else {
- burstCount = 1;
- }
- return burstCount >= 2;
+ function isCodeProduct(el) {
+ return el && el instanceof HTMLInputElement && el.name === 'code_product';
+ }
+
+ function commit() {
+ var el = document.activeElement;
+ if (!isCodeProduct(el)) { reset(); return; }
+ if (buffer.length >= MIN_LEN) {
+ el.value = buffer;
+ el.dispatchEvent(new Event('input', { bubbles: true }));
+ el.dispatchEvent(new Event('change', { bubbles: true }));
+ }
+ reset();
+ }
+
+ document.addEventListener('keydown', function (e) {
+ var el = document.activeElement;
+ if (!isCodeProduct(el)) return;
+
+ var key = e.key;
+
+ // ENTER mengakhiri scan
+ if (key === 'Enter') {
+ e.preventDefault();
+ commit();
+ return;
}
- document.addEventListener('keydown', function (e) {
- const t = e.target;
- if (!(t instanceof HTMLInputElement)) return;
+ // abaikan tombol kontrol (Shift, Tab, Arrow, Backspace, dll.)
+ if (key.length !== 1) return;
- // pastikan hanya field code_product
- if (t.name !== 'code_product') return;
+ var now = performance.now();
+ var gap = now - (last || now);
+ last = now;
- const now = performance.now();
- const scanner = isScannerLike(now);
+ if (!buffer || gap <= GAP_MS) {
+ // bagian dari "scan cepat" → tangani sendiri (hindari karakter hilang)
+ e.preventDefault();
+ buffer += key;
- // enter tetap boleh (scanner biasanya akhiri Enter)
- if (e.key === "Enter") return;
+ if (timer) clearTimeout(timer);
+ timer = setTimeout(function () {
+ // auto-commit jika scanner tidak mengirim Enter
+ commit();
+ }, COMMIT_TIMEOUT);
+ } else {
+ // jeda besar → kemungkinan manual. Kalau mau benar-benar melarang,
+ // buka komentar 2 baris di bawah.
+ // e.preventDefault();
+ // e.stopPropagation();
+ reset(); // keluar dari mode buffer agar manual normal
+ }
+ }, true);
- // kalau bukan scanner → blok manual ketikan
- if (!scanner) {
- e.preventDefault();
- e.stopPropagation();
- }
- }, true);
-});
+ document.addEventListener('focusin', function (e) {
+ if (isCodeProduct(e.target)) reset();
+ }, true);
+}); \ No newline at end of file