@@ -22,11 +22,15 @@ jQuery(document).ready(function () {
2222 checkUnicityTranslation ( jQuery ( this ) . val ( ) ) ;
2323 } ) ;
2424
25+ // Store search timeout globally to avoid conflicts
26+ let searchTimeout = null ;
27+
2528 function loadPostParentAjax ( post_type , current_value ) {
2629 jQuery ( "select#post_parent_js" ) . load ( ajaxurl , {
2730 'action' : 'load_original_content' ,
2831 'post_type' : post_type ,
29- 'current_value' : current_value
32+ 'current_value' : current_value ,
33+ 'limit' : 100
3034 } , function ( response , status , xhr ) {
3135 if ( status == "error" ) {
3236 alert ( "Sorry but an error occured with AJAX method" ) ;
@@ -35,12 +39,103 @@ jQuery(document).ready(function () {
3539 if ( translationSelect !== null ) {
3640 translationSelect . destroy ( ) ;
3741 }
42+
3843 // Initialize Choices.js after options are loaded
39- translationSelect = new Choices ( jQuery ( '#post_parent_js' ) [ 0 ] , {
44+ const selectElement = jQuery ( '#post_parent_js' ) [ 0 ] ;
45+ translationSelect = new Choices ( selectElement , {
4046 searchEnabled : true ,
4147 itemSelectText : '' ,
48+ searchChoices : false , // Disable local search, we'll use AJAX
49+ shouldSort : true ,
50+ shouldSortItems : true
4251 } ) ;
52+
53+ // Wait for Choices.js to render, then attach search handler
54+ setTimeout ( function ( ) {
55+ attachSearchHandler ( post_type , current_value ) ;
56+ } , 100 ) ;
57+
58+ syncSelectParentBox ( ) ;
59+ }
60+ } ) ;
61+ }
62+
63+ function attachSearchHandler ( post_type , current_value ) {
64+ // Find the search input in Choices.js
65+ const choicesContainer = jQuery ( '#post_parent_js' ) . closest ( '.choices' ) ;
66+ const searchInput = choicesContainer . find ( 'input[type="text"]' ) ;
67+
68+ // Remove any existing handlers
69+ searchInput . off ( 'input.ajaxSearch' ) ;
70+
71+ // Attach new search handler
72+ searchInput . on ( 'input.ajaxSearch' , function ( ) {
73+ const searchTerm = jQuery ( this ) . val ( ) . trim ( ) ;
74+
75+ // Clear previous timeout
76+ if ( searchTimeout ) {
77+ clearTimeout ( searchTimeout ) ;
78+ }
79+
80+ // If search is cleared, reload initial list
81+ if ( searchTerm . length < 2 ) {
82+ loadPostParentAjax ( post_type , current_value ) ;
83+ return ;
84+ }
85+
86+ // Debounce search requests (wait 300ms after user stops typing)
87+ searchTimeout = setTimeout ( function ( ) {
88+ loadPostParentAjaxSearch ( post_type , current_value , searchTerm ) ;
89+ } , 300 ) ;
90+ } ) ;
91+ }
92+
93+ function loadPostParentAjaxSearch ( post_type , current_value , searchTerm ) {
94+ jQuery . ajax ( {
95+ url : ajaxurl ,
96+ type : 'POST' ,
97+ data : {
98+ 'action' : 'load_original_content' ,
99+ 'post_type' : post_type ,
100+ 'current_value' : current_value ,
101+ 'search' : searchTerm ,
102+ 'limit' : 100
103+ } ,
104+ success : function ( response ) {
105+ // Parse the response to get options
106+ const tempSelect = jQuery ( '<select>' ) . html ( response ) ;
107+ const options = tempSelect . find ( 'option' ) ;
108+
109+ // Destroy and recreate Choices instance with new options
110+ if ( translationSelect !== null ) {
111+ translationSelect . destroy ( ) ;
112+ }
113+
114+ // Clear and repopulate select
115+ const selectElement = jQuery ( '#post_parent_js' ) [ 0 ] ;
116+ selectElement . innerHTML = '' ;
117+ options . each ( function ( ) {
118+ selectElement . appendChild ( this ) ;
119+ } ) ;
120+
121+ // Reinitialize Choices.js
122+ translationSelect = new Choices ( selectElement , {
123+ searchEnabled : true ,
124+ itemSelectText : '' ,
125+ searchChoices : false ,
126+ shouldSort : true ,
127+ shouldSortItems : true
128+ } ) ;
129+
130+ // Reattach search handler
131+ setTimeout ( function ( ) {
132+ attachSearchHandler ( post_type , current_value ) ;
133+ } , 100 ) ;
134+
43135 syncSelectParentBox ( ) ;
136+ } ,
137+ error : function ( ) {
138+ alert ( "Sorry but an error occured with AJAX search method" ) ;
44139 }
45140 } ) ;
46141 }
0 commit comments