| 1 |
/** |
| 2 |
* Facet Meta/ACF widget behavior. |
| 3 |
* |
| 4 |
* Dispatches filter changes through a custom event consumed by the faceted |
| 5 |
* filters system. |
| 6 |
*/ |
| 7 |
(function ($) { |
| 8 |
"use strict"; |
| 9 |
|
| 10 |
const FLAG = "kingAddonsFacetMetaBound"; |
| 11 |
|
| 12 |
/** |
| 13 |
* Dispatch filter updates. |
| 14 |
* |
| 15 |
* @param {string} queryId Query identifier. |
| 16 |
* @param {Object} payload Filters payload. |
| 17 |
* @returns {void} |
| 18 |
*/ |
| 19 |
const dispatch = (queryId, payload) => { |
| 20 |
const event = new CustomEvent("kingaddons:filters:apply", { |
| 21 |
detail: { |
| 22 |
queryId, |
| 23 |
filters: payload, |
| 24 |
}, |
| 25 |
}); |
| 26 |
document.dispatchEvent(event); |
| 27 |
window.dispatchEvent(event); |
| 28 |
}; |
| 29 |
|
| 30 |
/** |
| 31 |
* Bind delegated listeners once per page. |
| 32 |
* |
| 33 |
* @returns {void} |
| 34 |
*/ |
| 35 |
const bindOnce = () => { |
| 36 |
if (window[FLAG]) { |
| 37 |
return; |
| 38 |
} |
| 39 |
window[FLAG] = true; |
| 40 |
|
| 41 |
document.addEventListener("input", (event) => { |
| 42 |
const target = event.target; |
| 43 |
if (!(target instanceof Element)) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
const wrapper = target.closest(".ka-facet-meta"); |
| 48 |
if (!wrapper) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
const queryId = wrapper.dataset.kaFiltersQueryId || ""; |
| 53 |
const key = wrapper.dataset.kaMetaKey || ""; |
| 54 |
if (!queryId || !key) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( |
| 59 |
target.classList.contains("ka-facet-meta__input") || |
| 60 |
target.classList.contains("ka-facet-meta__select") |
| 61 |
) { |
| 62 |
dispatch(queryId, { |
| 63 |
meta: { [key]: target.value ? [target.value] : [] }, |
| 64 |
}); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if ( |
| 69 |
target.classList.contains("ka-facet-meta__range-min") || |
| 70 |
target.classList.contains("ka-facet-meta__range-max") |
| 71 |
) { |
| 72 |
const minEl = wrapper.querySelector(".ka-facet-meta__range-min"); |
| 73 |
const maxEl = wrapper.querySelector(".ka-facet-meta__range-max"); |
| 74 |
dispatch(queryId, { |
| 75 |
meta: { |
| 76 |
[key]: { |
| 77 |
min: minEl ? minEl.value : "", |
| 78 |
max: maxEl ? maxEl.value : "", |
| 79 |
}, |
| 80 |
}, |
| 81 |
}); |
| 82 |
} |
| 83 |
}); |
| 84 |
}; |
| 85 |
|
| 86 |
$(window).on("elementor/frontend/init", function () { |
| 87 |
elementorFrontend.hooks.addAction( |
| 88 |
"frontend/element_ready/facet_meta.default", |
| 89 |
function () { |
| 90 |
bindOnce(); |
| 91 |
} |
| 92 |
); |
| 93 |
}); |
| 94 |
|
| 95 |
document.addEventListener("DOMContentLoaded", bindOnce); |
| 96 |
})(jQuery); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|