From 88bdd397cfb328e94cfde371c76289003ea59563 Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Thu, 25 Jan 2024 11:50:19 +0000 Subject: [PATCH] Use w-swap's reflect event in w-drilldown and w-link --- client/src/controllers/DrilldownController.ts | 19 ---------------- client/src/controllers/LinkController.ts | 22 +++++++++++-------- .../shared/headers/slim_header.html | 3 ++- wagtail/admin/widgets/button.py | 2 +- 4 files changed, 16 insertions(+), 30 deletions(-) diff --git a/client/src/controllers/DrilldownController.ts b/client/src/controllers/DrilldownController.ts index f1c4886e8b..f50dfc63be 100644 --- a/client/src/controllers/DrilldownController.ts +++ b/client/src/controllers/DrilldownController.ts @@ -76,25 +76,6 @@ export class DrilldownController extends Controller { }); } - updateParams(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() !== '' && !key.startsWith('_w_')) { - // Check if the value is not empty after trimming white space - filteredParams.append(key, value); - } - }); - const queryString = `?${filteredParams.toString()}`; - window.history.replaceState(null, '', queryString); - } - this.updateCount(); - } - open(e: MouseEvent) { const toggle = (e.target as HTMLElement)?.closest( 'button', diff --git a/client/src/controllers/LinkController.ts b/client/src/controllers/LinkController.ts index b828870205..b1f928c7a2 100644 --- a/client/src/controllers/LinkController.ts +++ b/client/src/controllers/LinkController.ts @@ -19,7 +19,7 @@ export class LinkController extends Controller { } connect() { - this.setParamsFromLocation(); + this.setParams(); } get reflectAll() { @@ -33,11 +33,13 @@ export class LinkController extends Controller { const sourceParams = url.searchParams; sourceParams.forEach((value, key) => { + // Skip the key if if ( - key.startsWith('_w_') || // Wagtail internal - // Delete the key if we want to preserve it from the current URL, or - // if we don't want to reflect it to the new URL + // it's a Wagtail internal param, or + key.startsWith('_w_') || + // we want to preserve it from the current URL, or this.preserveKeysValue.includes(key) || + // we don't want to reflect it to the new URL (!reflectAll && !this.reflectKeysValue.includes(key)) ) { return; @@ -57,12 +59,14 @@ export class LinkController extends Controller { this.element.setAttribute(this.attrNameValue, currentUrl.toString()); } - setParamsFromSwapRequest(e: CustomEvent<{ requestUrl?: string }>) { + setParams(e?: CustomEvent<{ requestUrl?: string }>) { + if (!e) { + this.setParamsFromURL(new URL(window.location.href)); + return; + } + if (!e.detail?.requestUrl) return; + this.setParamsFromURL(new URL(e.detail.requestUrl, window.location.href)); } - - setParamsFromLocation() { - this.setParamsFromURL(new URL(window.location.href)); - } } diff --git a/wagtail/admin/templates/wagtailadmin/shared/headers/slim_header.html b/wagtail/admin/templates/wagtailadmin/shared/headers/slim_header.html index cae299f69d..8e878a4d50 100644 --- a/wagtail/admin/templates/wagtailadmin/shared/headers/slim_header.html +++ b/wagtail/admin/templates/wagtailadmin/shared/headers/slim_header.html @@ -71,6 +71,7 @@ data-action="change->w-swap#submitLazy input->w-swap#submitLazy" data-w-swap-src-value="{{ search_url }}" data-w-swap-target-value="#listing-results" + data-w-swap-reflect-value="true" > {% if search_form %} {% for field in search_form %} @@ -83,7 +84,7 @@ id="filters-drilldown" class="w-drilldown" data-controller="w-drilldown" - data-action="w-swap:success@document->w-drilldown#updateParams w-dropdown:hide->w-drilldown#delayedClose w-dropdown:clickaway->w-drilldown#preventOutletClickaway" + data-action="w-swap:success@document->w-drilldown#updateCount w-dropdown:hide->w-drilldown#delayedClose w-dropdown:clickaway->w-drilldown#preventOutletClickaway" data-w-drilldown-count-attr-value="data-w-active-filter-name" data-w-drilldown-w-action-outlet="[data-w-active-filter-name][data-controller='w-action']" data-w-drilldown-w-dropdown-outlet="#filters-drilldown [data-controller='w-dropdown']" diff --git a/wagtail/admin/widgets/button.py b/wagtail/admin/widgets/button.py index 5b3b45263d..9297bd15a0 100644 --- a/wagtail/admin/widgets/button.py +++ b/wagtail/admin/widgets/button.py @@ -111,7 +111,7 @@ class HeaderButton(Button): { "data-controller": "w-tooltip w-link", "data-w-tooltip-content-value": label, - "data-action": "w-swap:success@document->w-link#setParamsFromSwapRequest", + "data-action": "w-swap:reflect@document->w-link#setParams", "aria-label": label, } )