@@ -27,14 +27,13 @@ class default_1 extends Controller {
27
27
constructor ( ) {
28
28
super ( ...arguments ) ;
29
29
_default_1_instances . add ( this ) ;
30
+ this . isObserving = false ;
30
31
}
31
32
initialize ( ) {
32
- this . element . setAttribute ( 'data-live-ignore' , '' ) ;
33
- if ( this . element . id ) {
34
- const label = document . querySelector ( `label[for="${ this . element . id } "]` ) ;
35
- if ( label ) {
36
- label . setAttribute ( 'data-live-ignore' , '' ) ;
37
- }
33
+ if ( ! this . mutationObserver ) {
34
+ this . mutationObserver = new MutationObserver ( ( mutations ) => {
35
+ this . onMutations ( mutations ) ;
36
+ } ) ;
38
37
}
39
38
}
40
39
connect ( ) {
@@ -47,11 +46,15 @@ class default_1 extends Controller {
47
46
return ;
48
47
}
49
48
this . tomSelect = __classPrivateFieldGet ( this , _default_1_instances , "m" , _default_1_createAutocomplete ) . call ( this ) ;
49
+ this . startMutationObserver ( ) ;
50
50
}
51
51
disconnect ( ) {
52
- this . tomSelect . revertSettings . innerHTML = this . element . innerHTML ;
52
+ this . stopMutationObserver ( ) ;
53
53
this . tomSelect . destroy ( ) ;
54
54
}
55
+ getMaxOptions ( ) {
56
+ return this . selectElement ? this . selectElement . options . length : 50 ;
57
+ }
55
58
get selectElement ( ) {
56
59
if ( ! ( this . element instanceof HTMLSelectElement ) ) {
57
60
return null ;
@@ -79,6 +82,123 @@ class default_1 extends Controller {
79
82
}
80
83
return this . preloadValue ;
81
84
}
85
+ resetTomSelect ( ) {
86
+ if ( this . tomSelect ) {
87
+ this . stopMutationObserver ( ) ;
88
+ this . tomSelect . clearOptions ( ) ;
89
+ this . tomSelect . settings . maxOptions = this . getMaxOptions ( ) ;
90
+ this . tomSelect . sync ( ) ;
91
+ this . startMutationObserver ( ) ;
92
+ }
93
+ }
94
+ changeTomSelectDisabledState ( isDisabled ) {
95
+ this . stopMutationObserver ( ) ;
96
+ if ( isDisabled ) {
97
+ this . tomSelect . disable ( ) ;
98
+ }
99
+ else {
100
+ this . tomSelect . enable ( ) ;
101
+ }
102
+ this . startMutationObserver ( ) ;
103
+ }
104
+ updateTomSelectPlaceholder ( ) {
105
+ const input = this . element ;
106
+ let placeholder = input . getAttribute ( 'placeholder' ) || input . getAttribute ( 'data-placeholder' ) ;
107
+ if ( ! placeholder && ! this . tomSelect . allowEmptyOption ) {
108
+ const option = input . querySelector ( 'option[value=""]' ) ;
109
+ if ( option ) {
110
+ placeholder = option . textContent ;
111
+ }
112
+ }
113
+ if ( placeholder ) {
114
+ this . stopMutationObserver ( ) ;
115
+ this . tomSelect . control_input . setAttribute ( 'placeholder' , placeholder ) ;
116
+ this . startMutationObserver ( ) ;
117
+ }
118
+ }
119
+ startMutationObserver ( ) {
120
+ if ( ! this . isObserving ) {
121
+ this . mutationObserver . observe ( this . element , {
122
+ childList : true ,
123
+ subtree : true ,
124
+ attributes : true ,
125
+ characterData : true ,
126
+ } ) ;
127
+ this . isObserving = true ;
128
+ }
129
+ }
130
+ stopMutationObserver ( ) {
131
+ if ( this . isObserving ) {
132
+ this . mutationObserver . disconnect ( ) ;
133
+ this . isObserving = false ;
134
+ }
135
+ }
136
+ onMutations ( mutations ) {
137
+ const addedOptionElements = [ ] ;
138
+ const removedOptionElements = [ ] ;
139
+ let hasAnOptionChanged = false ;
140
+ let changeDisabledState = false ;
141
+ let changePlaceholder = false ;
142
+ mutations . forEach ( ( mutation ) => {
143
+ switch ( mutation . type ) {
144
+ case 'childList' :
145
+ if ( mutation . target instanceof HTMLOptionElement ) {
146
+ if ( mutation . target . value === '' ) {
147
+ changePlaceholder = true ;
148
+ break ;
149
+ }
150
+ hasAnOptionChanged = true ;
151
+ break ;
152
+ }
153
+ mutation . addedNodes . forEach ( ( node ) => {
154
+ if ( node instanceof HTMLOptionElement ) {
155
+ if ( removedOptionElements . includes ( node ) ) {
156
+ removedOptionElements . splice ( removedOptionElements . indexOf ( node ) , 1 ) ;
157
+ return ;
158
+ }
159
+ addedOptionElements . push ( node ) ;
160
+ }
161
+ } ) ;
162
+ mutation . removedNodes . forEach ( ( node ) => {
163
+ if ( node instanceof HTMLOptionElement ) {
164
+ if ( addedOptionElements . includes ( node ) ) {
165
+ addedOptionElements . splice ( addedOptionElements . indexOf ( node ) , 1 ) ;
166
+ return ;
167
+ }
168
+ removedOptionElements . push ( node ) ;
169
+ }
170
+ } ) ;
171
+ break ;
172
+ case 'attributes' :
173
+ if ( mutation . target instanceof HTMLOptionElement ) {
174
+ hasAnOptionChanged = true ;
175
+ break ;
176
+ }
177
+ if ( mutation . target === this . element && mutation . attributeName === 'disabled' ) {
178
+ changeDisabledState = true ;
179
+ break ;
180
+ }
181
+ break ;
182
+ case 'characterData' :
183
+ if ( mutation . target instanceof Text && mutation . target . parentElement instanceof HTMLOptionElement ) {
184
+ if ( mutation . target . parentElement . value === '' ) {
185
+ changePlaceholder = true ;
186
+ break ;
187
+ }
188
+ hasAnOptionChanged = true ;
189
+ }
190
+ }
191
+ } ) ;
192
+ if ( hasAnOptionChanged || addedOptionElements . length > 0 || removedOptionElements . length > 0 ) {
193
+ this . resetTomSelect ( ) ;
194
+ }
195
+ if ( changeDisabledState ) {
196
+ this . changeTomSelectDisabledState ( ( this . formElement . disabled ) ) ;
197
+ }
198
+ if ( changePlaceholder ) {
199
+ this . updateTomSelectPlaceholder ( ) ;
200
+ }
201
+ }
82
202
}
83
203
_default_1_instances = new WeakSet ( ) , _default_1_getCommonConfig = function _default_1_getCommonConfig ( ) {
84
204
const plugins = { } ;
@@ -103,10 +223,6 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def
103
223
onItemAdd : ( ) => {
104
224
this . tomSelect . setTextboxValue ( '' ) ;
105
225
} ,
106
- onInitialize : function ( ) {
107
- const tomSelect = this ;
108
- tomSelect . wrapper . setAttribute ( 'data-live-ignore' , '' ) ;
109
- } ,
110
226
closeAfterSelect : true ,
111
227
} ;
112
228
if ( ! this . selectElement && ! this . urlValue ) {
@@ -115,12 +231,12 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def
115
231
return __classPrivateFieldGet ( this , _default_1_instances , "m" , _default_1_mergeObjects ) . call ( this , config , this . tomSelectOptionsValue ) ;
116
232
} , _default_1_createAutocomplete = function _default_1_createAutocomplete ( ) {
117
233
const config = __classPrivateFieldGet ( this , _default_1_instances , "m" , _default_1_mergeObjects ) . call ( this , __classPrivateFieldGet ( this , _default_1_instances , "m" , _default_1_getCommonConfig ) . call ( this ) , {
118
- maxOptions : this . selectElement ? this . selectElement . options . length : 50 ,
234
+ maxOptions : this . getMaxOptions ( ) ,
119
235
} ) ;
120
236
return __classPrivateFieldGet ( this , _default_1_instances , "m" , _default_1_createTomSelect ) . call ( this , config ) ;
121
237
} , _default_1_createAutocompleteWithHtmlContents = function _default_1_createAutocompleteWithHtmlContents ( ) {
122
238
const config = __classPrivateFieldGet ( this , _default_1_instances , "m" , _default_1_mergeObjects ) . call ( this , __classPrivateFieldGet ( this , _default_1_instances , "m" , _default_1_getCommonConfig ) . call ( this ) , {
123
- maxOptions : this . selectElement ? this . selectElement . options . length : 50 ,
239
+ maxOptions : this . getMaxOptions ( ) ,
124
240
score : ( search ) => {
125
241
const scoringFunction = this . tomSelect . getScoreFunction ( search ) ;
126
242
return ( item ) => {
@@ -183,9 +299,11 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def
183
299
} , _default_1_mergeObjects = function _default_1_mergeObjects ( object1 , object2 ) {
184
300
return Object . assign ( Object . assign ( { } , object1 ) , object2 ) ;
185
301
} , _default_1_createTomSelect = function _default_1_createTomSelect ( options ) {
186
- this . dispatchEvent ( 'pre-connect' , { options } ) ;
302
+ const preConnectPayload = { options } ;
303
+ this . dispatchEvent ( 'pre-connect' , preConnectPayload ) ;
187
304
const tomSelect = new TomSelect ( this . formElement , options ) ;
188
- this . dispatchEvent ( 'connect' , { tomSelect, options } ) ;
305
+ const connectPayload = { tomSelect, options } ;
306
+ this . dispatchEvent ( 'connect' , connectPayload ) ;
189
307
return tomSelect ;
190
308
} ;
191
309
default_1 . values = {
0 commit comments