Skip to content

Commit 1af6093

Browse files
committed
building assets
1 parent f7f8c20 commit 1af6093

File tree

3 files changed

+49
-68
lines changed

3 files changed

+49
-68
lines changed

src/Autocomplete/assets/dist/controller.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ export default class extends Controller {
3333
private mutationObserver;
3434
private isObserving;
3535
private hasLoadedChoicesPreviously;
36+
private originalOptions;
3637
initialize(): void;
3738
connect(): void;
39+
initializeTomSelect(): void;
3840
disconnect(): void;
3941
private getMaxOptions;
4042
get selectElement(): HTMLSelectElement | null;
@@ -43,9 +45,10 @@ export default class extends Controller {
4345
get preload(): string | boolean;
4446
private resetTomSelect;
4547
private changeTomSelectDisabledState;
46-
private updateTomSelectPlaceholder;
4748
private startMutationObserver;
4849
private stopMutationObserver;
4950
private onMutations;
5051
private requiresLiveIgnore;
52+
private createOptionsDataStructure;
53+
private areOptionsEquivalent;
5154
}

src/Autocomplete/assets/dist/controller.js

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class default_1 extends Controller {
2929
_default_1_instances.add(this);
3030
this.isObserving = false;
3131
this.hasLoadedChoicesPreviously = false;
32+
this.originalOptions = [];
3233
}
3334
initialize() {
3435
if (this.requiresLiveIgnore()) {
@@ -49,6 +50,15 @@ class default_1 extends Controller {
4950
}
5051
}
5152
connect() {
53+
if (this.selectElement) {
54+
this.originalOptions = this.createOptionsDataStructure(this.selectElement);
55+
}
56+
this.initializeTomSelect();
57+
}
58+
initializeTomSelect() {
59+
if (this.selectElement) {
60+
this.selectElement.setAttribute('data-skip-morph', '');
61+
}
5262
if (this.urlValue) {
5363
this.tomSelect = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createAutocompleteWithRemoteData).call(this, this.urlValue, this.hasMinCharactersValue ? this.minCharactersValue : null);
5464
return;
@@ -97,9 +107,12 @@ class default_1 extends Controller {
97107
resetTomSelect() {
98108
if (this.tomSelect) {
99109
this.stopMutationObserver();
100-
this.tomSelect.clearOptions();
101-
this.tomSelect.settings.maxOptions = this.getMaxOptions();
102-
this.tomSelect.sync();
110+
const currentHtml = this.element.innerHTML;
111+
const currentValue = this.tomSelect.getValue();
112+
this.tomSelect.destroy();
113+
this.element.innerHTML = currentHtml;
114+
this.initializeTomSelect();
115+
this.tomSelect.setValue(currentValue);
103116
this.startMutationObserver();
104117
}
105118
}
@@ -113,22 +126,6 @@ class default_1 extends Controller {
113126
}
114127
this.startMutationObserver();
115128
}
116-
updateTomSelectPlaceholder() {
117-
const input = this.element;
118-
let placeholder = input.getAttribute('placeholder') || input.getAttribute('data-placeholder');
119-
if (!placeholder && !this.tomSelect.allowEmptyOption) {
120-
const option = input.querySelector('option[value=""]');
121-
if (option) {
122-
placeholder = option.textContent;
123-
}
124-
}
125-
if (placeholder) {
126-
this.stopMutationObserver();
127-
this.tomSelect.settings.placeholder = placeholder;
128-
this.tomSelect.control_input.setAttribute('placeholder', placeholder);
129-
this.startMutationObserver();
130-
}
131-
}
132129
startMutationObserver() {
133130
if (!this.isObserving && this.mutationObserver) {
134131
this.mutationObserver.observe(this.element, {
@@ -147,74 +144,51 @@ class default_1 extends Controller {
147144
}
148145
}
149146
onMutations(mutations) {
150-
const addedOptionElements = [];
151-
const removedOptionElements = [];
152-
let hasAnOptionChanged = false;
153147
let changeDisabledState = false;
154-
let changePlaceholder = false;
155148
mutations.forEach((mutation) => {
156149
switch (mutation.type) {
157-
case 'childList':
158-
if (mutation.target instanceof HTMLOptionElement) {
159-
if (mutation.target.value === '') {
160-
changePlaceholder = true;
161-
break;
162-
}
163-
hasAnOptionChanged = true;
164-
break;
165-
}
166-
mutation.addedNodes.forEach((node) => {
167-
if (node instanceof HTMLOptionElement) {
168-
if (removedOptionElements.includes(node)) {
169-
removedOptionElements.splice(removedOptionElements.indexOf(node), 1);
170-
return;
171-
}
172-
addedOptionElements.push(node);
173-
}
174-
});
175-
mutation.removedNodes.forEach((node) => {
176-
if (node instanceof HTMLOptionElement) {
177-
if (addedOptionElements.includes(node)) {
178-
addedOptionElements.splice(addedOptionElements.indexOf(node), 1);
179-
return;
180-
}
181-
removedOptionElements.push(node);
182-
}
183-
});
184-
break;
185150
case 'attributes':
186-
if (mutation.target instanceof HTMLOptionElement) {
187-
hasAnOptionChanged = true;
188-
break;
189-
}
190151
if (mutation.target === this.element && mutation.attributeName === 'disabled') {
191152
changeDisabledState = true;
192153
break;
193154
}
194155
break;
195-
case 'characterData':
196-
if (mutation.target instanceof Text && mutation.target.parentElement instanceof HTMLOptionElement) {
197-
if (mutation.target.parentElement.value === '') {
198-
changePlaceholder = true;
199-
break;
200-
}
201-
hasAnOptionChanged = true;
202-
}
203156
}
204157
});
205-
if (hasAnOptionChanged || addedOptionElements.length > 0 || removedOptionElements.length > 0) {
158+
const newOptions = this.selectElement ? this.createOptionsDataStructure(this.selectElement) : [];
159+
const areOptionsEquivalent = this.areOptionsEquivalent(newOptions);
160+
console.log('are options equivalent?', areOptionsEquivalent);
161+
if (!areOptionsEquivalent) {
162+
this.originalOptions = newOptions;
206163
this.resetTomSelect();
207164
}
208165
if (changeDisabledState) {
209166
this.changeTomSelectDisabledState(this.formElement.disabled);
210167
}
211-
if (changePlaceholder) {
212-
this.updateTomSelectPlaceholder();
213-
}
214168
}
215169
requiresLiveIgnore() {
216170
return this.element instanceof HTMLSelectElement && this.element.multiple;
217171
}
172+
createOptionsDataStructure(selectElement) {
173+
return Array.from(selectElement.options).map((option) => {
174+
const optgroup = option.closest('optgroup');
175+
return {
176+
value: option.value,
177+
text: option.text,
178+
group: optgroup ? optgroup.label : null,
179+
};
180+
});
181+
}
182+
areOptionsEquivalent(newOptions) {
183+
if (this.originalOptions.length !== newOptions.length) {
184+
return false;
185+
}
186+
const normalizeOption = (option) => `${option.value}-${option.text}-${option.group}`;
187+
const originalOptionsSet = new Set(this.originalOptions.map(normalizeOption));
188+
const newOptionsSet = new Set(newOptions.map(normalizeOption));
189+
return (originalOptionsSet.size === newOptionsSet.size &&
190+
[...originalOptionsSet].every((option) => newOptionsSet.has(option)));
191+
}
218192
}
219193
_default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _default_1_getCommonConfig() {
220194
const plugins = {};

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,10 @@ function executeMorphdom(rootFromElement, rootToElement, modifiedFieldElements,
12641264
fromEl.removeAttribute('parent-live-id-changed');
12651265
return true;
12661266
}
1267+
if (fromEl.hasAttribute('data-skip-morph')) {
1268+
fromEl.innerHTML = toEl.innerHTML;
1269+
return false;
1270+
}
12671271
return !fromEl.hasAttribute('data-live-ignore');
12681272
},
12691273
beforeNodeRemoved(node) {

0 commit comments

Comments
 (0)