From e4a57abbb0343e38f95208c133a894c74c318ff9 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Wed, 13 Aug 2025 08:32:04 +0000 Subject: [PATCH] Bug 40643: Prevent keypress buildup in circulation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We have reports of progressive slowdown on the circulation page during long sessions. A likely cause might be a block in `circ/circulation.tt` that attaches a new keypress handler to `#barcode` on every submit. Some submits do not navigate (e.g. waiting holds modal, quick slip on empty barcode), and pages restored from the bfcache keep previously attached handlers. Over time, listeners accumulate and the UI becomes sluggish. This patch installs a single, namespaced keypress handler at page load, guarded by an in-flight flag toggled on submit. The handler shows the “Barcode submitted” modal and prevents typing only while a submit is in flight. The flag is reset on bfcache restores via `pageshow`. This preserves existing UX while keeping the listener count constant. Test plan: 1. Before applying the patch, open circ/circulation.pl for any patron. 2. In the browser console, run: (function () { const el = $('#barcode')[0]; const dataFn = jQuery._data || jQuery.data; const ev = (dataFn && dataFn(el, 'events')) || {}; return (ev.keypress || []).length; })() Note the keypress handler count. 3. Enter a barcode and submit. Use the browser Back button to return. 4. Re-run the IIFE from step 2. The count has increased. 5. Repeat steps 3–4 a few times and observe the count continues to grow. 6. Apply the patch. 7. Reload the page and re-run step 2; count should be ~1. 8. Enter a barcode and submit. Use Back to return. Re-run step 2. The count remains ~1 across submits and back/forward navigations. 9. Confirm UX is preserved: - While submit is in flight, pressing keys in `#barcode` shows the “Barcode submitted” modal and prevents typing. - Waiting holds flow still shows its modal and behaves as before. - Submitting with an empty barcode still triggers the quick slip behavior per system preference and does not add handlers. 10. Optional: Simulate scanner behavior by pressing Enter rapidly after submit. Verify the handler count remains stable. --- .../prog/en/modules/circ/circulation.tt | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt index 1ca767f7295..29aa6532b97 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt @@ -1152,6 +1152,18 @@ var newin = window.open(link, 'popup', 'width=600,height=400,resizable=1,toolbar=0,scrollbars=1,top'); } $(document).ready(function() { + let barcodeSubmitInFlight = false; + $('#barcode').off('keypress.preventDouble').on('keypress.preventDouble', function(e) { + if (barcodeSubmitInFlight) { + $('#barcodeSubmittedModal').modal('show'); + e.preventDefault(); + } + }); + window.addEventListener('pageshow', function(event) { + if (event.persisted) { + barcodeSubmitInFlight = false; + } + }); [% IF waiting_holds.count > 0 %] $('#circ-warnwaitingholds-modal .btn-primary').on('click',function() { $('#mainform').submit(); @@ -1178,11 +1190,8 @@ [% END %] $('#mainform').on('submit',function() { - if ($("#barcode") && $("#barcode").val()) { - $('#barcode').on('keypress',function(event) { - $('#barcodeSubmittedModal').modal('show'); - event.preventDefault(); } - ); + if ($("#barcode").length && $("#barcode").val()) { + barcodeSubmitInFlight = true; } }); -- 2.50.1