From 93a116cb1f1040683cc7c45ebb427f6b65d8c657 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 7 Nov 2024 16:57:01 +0100
Subject: [PATCH] Bug 38310: WIP - Add Select2 to column filters

This is not working, the "clear" behaviour is not applied.
When 'x' is clicked the Select2 option list is supposed to hide.
---
 koha-tmpl/intranet-tmpl/prog/js/datatables.js |  2 ++
 koha-tmpl/intranet-tmpl/prog/js/select2.js    | 15 +++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
index 83d1253c8a2..13b9911fc45 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
@@ -783,6 +783,7 @@ function _dt_add_filters(table_node, table_dt, filters_options = {}) {
 
     if (!$(table_node).length) return;
 
+    $(table_node).find('thead tr:eq(1) select').select2('destroy');
     $(table_node).find('thead tr:eq(1)').remove(); // Remove if one exists already
     $(table_node).find('thead tr').clone().appendTo( $(table_node).find('thead') );
 
@@ -820,6 +821,7 @@ function _dt_add_filters(table_node, table_dt, filters_options = {}) {
                     o.appendTo(select);
                 });
                 $(th).html( select );
+                apply_select2(select);
             } else {
                 var title = $(th).text();
                 if ( existing_search ) {
diff --git a/koha-tmpl/intranet-tmpl/prog/js/select2.js b/koha-tmpl/intranet-tmpl/prog/js/select2.js
index bc11558e11f..946719ea8a9 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/select2.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/select2.js
@@ -27,9 +27,10 @@ $.fn.select2.defaults.set("language", {
     removeItem:function(){return __("Remove item"); }
 });
 
-$(document).ready(function(){
-    $(".select2").select2();
-    $(".select2").on("select2:clear", function () {
+function apply_select2(node){
+    $(node).select2();
+    $(node).on("select2:clear", function () {
+        console.log("clear");
         $(this).on("select2:opening.cancelOpen", function (evt) {
             evt.preventDefault();
 
@@ -37,8 +38,14 @@ $(document).ready(function(){
         });
     });
 
-    $(document).on("select2:open", function () {
+    /*$(document).on("select2:open", function () {
         document.querySelector(".select2-container--open .select2-search__field").focus();
+    });*/
+}
+
+$(document).ready(function(){
+    $(".select2").each(function(){
+        apply_select2(this);
     });
 });
 
-- 
2.34.1