@@ -4,39 +4,51 @@ function sqlpage_select_dropdown() {
44 for ( const s of document . querySelectorAll (
55 "[data-pre-init=select-dropdown]" ,
66 ) ) {
7- s . removeAttribute ( "data-pre-init" ) ;
8- // See: https://github.com/orchidjs/tom-select/issues/716
9- // By default, TomSelect will not retain the focus if s is already focused
10- // This is a workaround to fix that
11- const is_focused = s === document . activeElement ;
12-
13- const tom = new TomSelect ( s , {
14- load : sqlpage_load_options_source ( s . dataset . options_source ) ,
15- valueField : "value" ,
16- labelField : "label" ,
17- searchField : "label" ,
18- create : s . dataset . create_new ,
19- maxOptions : null ,
20- onItemAdd : function ( ) {
21- this . setTextboxValue ( "" ) ;
22- this . refreshOptions ( ) ;
23- } ,
24- } ) ;
25- if ( is_focused ) tom . focus ( ) ;
26- s . form ?. addEventListener ( "reset" , async ( ) => {
27- // The reset event is fired before the form is reset, so we need to wait for the next event loop
28- await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
29- // Sync the options with the new reset value
30- tom . sync ( ) ;
31- // Wait for the options to be updated
32- await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
33- // "sync" also focuses the input, so we need to blur it to remove the focus
34- tom . blur ( ) ;
35- tom . close ( ) ;
36- } ) ;
7+ try {
8+ sqlpage_select_dropdown_individual ( s ) ;
9+ } catch ( e ) {
10+ console . error ( e ) ;
11+ }
3712 }
3813}
3914
15+ /**
16+ * Initialize a select dropdown for a single element
17+ * @param {HTMLSelectElement } s - The select element to initialize
18+ */
19+ function sqlpage_select_dropdown_individual ( s ) {
20+ s . removeAttribute ( "data-pre-init" ) ;
21+ // See: https://github.com/orchidjs/tom-select/issues/716
22+ // By default, TomSelect will not retain the focus if s is already focused
23+ // This is a workaround to fix that
24+ const is_focused = s === document . activeElement ;
25+
26+ const tom = new TomSelect ( s , {
27+ load : sqlpage_load_options_source ( s . dataset . options_source ) ,
28+ valueField : "value" ,
29+ labelField : "label" ,
30+ searchField : "label" ,
31+ create : s . dataset . create_new ,
32+ maxOptions : null ,
33+ onItemAdd : function ( ) {
34+ this . setTextboxValue ( "" ) ;
35+ this . refreshOptions ( ) ;
36+ } ,
37+ } ) ;
38+ if ( is_focused ) tom . focus ( ) ;
39+ s . form ?. addEventListener ( "reset" , async ( ) => {
40+ // The reset event is fired before the form is reset, so we need to wait for the next event loop
41+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
42+ // Sync the options with the new reset value
43+ tom . sync ( ) ;
44+ // Wait for the options to be updated
45+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
46+ // "sync" also focuses the input, so we need to blur it to remove the focus
47+ tom . blur ( ) ;
48+ tom . close ( ) ;
49+ } ) ;
50+ }
51+
4052function sqlpage_load_options_source ( options_source ) {
4153 if ( ! options_source ) return ;
4254 return async ( query , callback ) => {
0 commit comments