0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-25 05:02:57 +01:00

Use data-action to listen to w-swap:success in DrilldownController

and rename the method to updateParamsCount
This commit is contained in:
Sage Abdullah 2024-01-22 22:14:14 +00:00
parent a1e3f755e5
commit 7ba218a094
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
2 changed files with 18 additions and 25 deletions

View File

@ -8,38 +8,31 @@ export class DrilldownController extends Controller<HTMLElement> {
};
declare activeSubmenuValue: string;
declare swapSuccessListener: (e: Event) => void;
declare readonly menuTarget: HTMLElement;
declare readonly toggleTargets: HTMLButtonElement[];
connect() {
this.updateToggleCounts();
this.swapSuccessListener = (e: Event) => {
const swapEvent = e as CustomEvent<{ requestUrl: string }>;
if ((e.target as HTMLElement)?.id === 'listing-results') {
const params = new URLSearchParams(
swapEvent.detail?.requestUrl.split('?')[1],
);
const filteredParams = new URLSearchParams();
params.forEach((value, key) => {
if (value.trim() !== '') {
// Check if the value is not empty after trimming white space
filteredParams.append(key, value);
}
});
const queryString = '?' + filteredParams.toString();
this.updateToggleCounts();
window.history.replaceState(null, '', queryString);
}
};
document.addEventListener('w-swap:success', this.swapSuccessListener);
}
disconnect() {
document.removeEventListener('w-swap:success', this.swapSuccessListener);
updateParamsCount(e: Event) {
const swapEvent = e as CustomEvent<{ requestUrl: string }>;
if ((e.target as HTMLElement)?.id === 'listing-results') {
const params = new URLSearchParams(
swapEvent.detail?.requestUrl.split('?')[1],
);
const filteredParams = new URLSearchParams();
params.forEach((value, key) => {
if (value.trim() !== '') {
// Check if the value is not empty after trimming white space
filteredParams.append(key, value);
}
});
const queryString = '?' + filteredParams.toString();
this.updateToggleCounts();
window.history.replaceState(null, '', queryString);
}
}
open(e: MouseEvent) {

View File

@ -79,7 +79,7 @@
{% endif %}
{% if filters %}
<div class="w-drilldown" data-controller="w-drilldown">
<div class="w-drilldown" data-controller="w-drilldown" data-action="w-swap:success@document->w-drilldown#updateParamsCount">
{% fragment as toggle_suffix %}
<span class="w-drilldown__count" hidden></span>
{% endfragment %}