From c54fa7d8ac88a09682bebf57529d58c817ce50a8 Mon Sep 17 00:00:00 2001
From: Jan Kissig <jkissig@th-wildau.de>
Date: Tue, 10 Oct 2023 13:36:04 +0200
Subject: [PATCH] Bug_35012

test plan:
a) build a new basket + order line using "item create on placing an order"
b) in the item creation dialog (neworderempty.pl) please use the webbrowsers dev tools to analyze the [...] button next to subfield d (Date acquired) and inspect the events that get fired by clicking
c) notice the Clicktag_952_subfield_d_* event gets fired twice.
d) apply patch and reload page
e) there should be only 1 click handler left (besides the jquery one), which points to the Clicktag_952_subfield_* function
---
 Koha/FrameworkPlugin.pm | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/Koha/FrameworkPlugin.pm b/Koha/FrameworkPlugin.pm
index 2331bfa6b3..a91ca22c54 100644
--- a/Koha/FrameworkPlugin.pm
+++ b/Koha/FrameworkPlugin.pm
@@ -329,7 +329,15 @@ sub _add_binding {
         #click event applies to buttonDot
 
     if( $pars =~ /^(e|ev|event)$/i ) { # new style event handler assumed
-        $bind= qq|    \$("#$ctl").$ev(\{id: '$id'\}, $fname);\n|;
+        
+        if ($ev eq "click"){
+          # remove already registered click listeners
+          $bind= qq|    \$("#$ctl").off('click');\n|;
+          $bind.= qq|    \$("#$ctl").$ev(\{id: '$id'\}, $fname);\n|;
+        } else {
+          $bind= qq|    \$("#$ctl").$ev(\{id: '$id'\}, $fname);\n|;
+        }
+        
         $script='';
     } elsif( $fname eq 'noclick' ) { # no click: return false, no scroll
         $bind= qq|    \$("#$ctl").$ev(function () { return false; });\n|;
@@ -379,9 +387,12 @@ sub _merge_script {
 $script
 function BindEvents$id() {
 $bind
+
 }
 \$(document).ready(function() {
     BindEvents$id();
+
+
 });
 </script>
 HERE
-- 
2.34.1