This commit is contained in:
Mark Gregory
2026-07-07 16:35:28 +01:00
parent 323dabac5a
commit fc27aa7318
3 changed files with 64 additions and 71 deletions
+32 -46
View File
@@ -143,23 +143,21 @@ function renderWmsCharts() {
function livewireComponentFor(element) {
if (!element || !window.Livewire) {
let current = element;
while (current && current !== document.documentElement) {
if (current.hasAttribute && current.hasAttribute('wire:id')) {
break;
}
current = current.parentElement;
}
if (!current || !window.Livewire) {
return null;
}
let componentRoot = null;
try {
componentRoot = element.closest('[wire\\:id]');
} catch (error) {
componentRoot = null;
}
if (!componentRoot) {
return null;
}
return window.Livewire.find(componentRoot.getAttribute('wire:id'));
return window.Livewire.find(current.getAttribute('wire:id'));
}
function setLivewireProperty(element, model, value) {
@@ -177,15 +175,12 @@ function setLivewireProperty(element, model, value) {
*
* @returns {void}
*/
function initialiseSelect2(root = document) {
function initialiseSelect2() {
if (!window.jQuery?.fn?.select2) {
return;
}
const $root = root instanceof window.Element ? $(root) : $(document);
const selector = 'select[data-helpdesk-select2], select[data-wms-select2], select.wms-select2-control';
$root.find(selector).add($root.is(selector) ? $root : $()).each(function () {
$('[data-helpdesk-select2], [data-wms-select2], .wms-select2-control').each(function () {
const select = this;
const $select = $(select);
@@ -199,28 +194,36 @@ function initialiseSelect2(root = document) {
$select.find('option[value=""]').first().text() ||
(isMultiple ? 'Select options' : 'Select option');
if (!isMultiple && $select.find('option[value=""]').length === 0) {
$select.prepend(new Option('', '', false, false));
}
$select.select2({
theme: 'wms',
width: '100%',
minimumResultsForSearch: 0,
allowClear: !isMultiple,
closeOnSelect: !isMultiple,
dropdownAutoWidth: false,
placeholder,
sorter(data) {
return data.sort((first, second) => first.text.localeCompare(second.text, undefined, {
sensitivity: 'base',
ignorePunctuation: true,
}));
return data.sort((first, second) => {
const firstText = (first.text || '').trim();
const secondText = (second.text || '').trim();
if (!first.id) return -1;
if (!second.id) return 1;
return firstText.localeCompare(secondText, undefined, {
sensitivity: 'base',
ignorePunctuation: true,
});
});
},
});
$select.off('change.wms-select2-livewire');
$select.on('change.wms-select2-livewire', function () {
setLivewireProperty(this, this.dataset.wireModel, $select.val());
this.dispatchEvent(new Event('change', { bubbles: true }));
});
});
}
@@ -236,28 +239,11 @@ function bootUi() {
}
document.addEventListener('DOMContentLoaded', bootUi);
window.addEventListener('load', bootUi);
window.addEventListener('pageshow', bootUi);
document.addEventListener('livewire:navigated', bootUi);
document.addEventListener('livewire:init', () => {
Livewire.hook('morphed', ({ el }) => {
initialiseSelect2(el || document);
Livewire.hook('morphed', () => {
initialiseSelect2();
});
});
const select2Observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node instanceof window.Element) {
initialiseSelect2(node);
}
});
});
});
select2Observer.observe(document.documentElement, {
childList: true,
subtree: true,
});