From c843c229cdccad1ffacd1920f77bd7c72fb750cb Mon Sep 17 00:00:00 2001 From: Runek Date: Thu, 10 Aug 2023 21:03:45 +0200 Subject: [PATCH 1/2] Issue #56: add reswap to HtmxResponse --- .../wimdeblauwe/hsbt/mvc/HtmxResponse.java | 20 +++++++++++++++ .../hsbt/mvc/HtmxResponseHeader.java | 3 ++- .../hsbt/mvc/HtmxViewHandlerInterceptor.java | 3 +++ .../wimdeblauwe/hsbt/mvc/HxSwapType.java | 25 +++++++++++++++++++ .../hsbt/mvc/HtmxResponseTest.java | 4 ++- 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/wimdeblauwe/hsbt/mvc/HxSwapType.java diff --git a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java index 835a88e5..f22ad24e 100644 --- a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java +++ b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java @@ -30,6 +30,7 @@ final public class HtmxResponse { private boolean headerRefresh; private String headerRedirect; private String headerPushHistory; + private String headerReswap; public HtmxResponse() { this.templates = new LinkedHashSet<>(); @@ -170,6 +171,18 @@ public HtmxResponse retarget(String cssSelector) { return this; } + /** + * Set a new swap to specify how the response will be swapped + * + * @param swapType must not be {@literal null} or empty. + * @return same HtmxResponse for chaining + */ + public HtmxResponse reswap(HxSwapType swapType) { + Assert.notNull(swapType, "swapType should not be null"); + this.headerReswap = swapType.getValue(); + return this; + } + /** * * @param otherResponse Another HtmxResponse that will be merged into this response. @@ -199,6 +212,9 @@ public HtmxResponse and(HtmxResponse otherResponse){ if(otherResponse.getHeaderRedirect() != null) { this.headerRedirect = otherResponse.getHeaderRedirect(); } + if(otherResponse.getHeaderReswap() != null) { + this.headerReswap = otherResponse.getHeaderReswap(); + } return this; } @@ -272,4 +288,8 @@ String getHeaderRedirect() { String getHeaderPushHistory() { return headerPushHistory; } + + public String getHeaderReswap() { + return headerReswap; + } } diff --git a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseHeader.java b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseHeader.java index 18eda123..e83d0c52 100644 --- a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseHeader.java +++ b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseHeader.java @@ -7,7 +7,8 @@ public enum HtmxResponseHeader { HX_RETARGET("HX-Retarget"), HX_TRIGGER("HX-Trigger"), HX_TRIGGER_AFTER_SETTLE("HX-Trigger-After-Settle"), - HX_TRIGGER_AFTER_SWAP("HX-Trigger-After-Swap"); + HX_TRIGGER_AFTER_SWAP("HX-Trigger-After-Swap"), + HX_RESWAP("HX-Reswap"); private final String value; diff --git a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxViewHandlerInterceptor.java b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxViewHandlerInterceptor.java index e1fa82be..ef670aa8 100644 --- a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxViewHandlerInterceptor.java +++ b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxViewHandlerInterceptor.java @@ -111,6 +111,9 @@ private void addHxHeaders(HtmxResponse htmxResponse, HttpServletResponse respons if (htmxResponse.getHeaderRetarget() != null) { response.setHeader(HtmxResponseHeader.HX_RETARGET.getValue(), htmxResponse.getHeaderRetarget()); } + if (htmxResponse.getHeaderReswap() != null) { + response.setHeader(HtmxResponseHeader.HX_RESWAP.getValue(), htmxResponse.getHeaderReswap()); + } } private void setTriggerHeader(HxTriggerLifecycle triggerHeader, Map triggers, diff --git a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HxSwapType.java b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HxSwapType.java new file mode 100644 index 00000000..1ea35b0e --- /dev/null +++ b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HxSwapType.java @@ -0,0 +1,25 @@ +package io.github.wimdeblauwe.hsbt.mvc; + +public enum HxSwapType { + + INNER_HTML("innerHTML"), + OUTER_HTML("outerHTML"), + BEFORE_BEGIN("beforebegin"), + AFTER_BEGIN("afterbegin"), + BEFORE_END("beforeend"), + AFTER_END("afterend"), + DELETE("delete"), + NONE("none"); + + + private final String value; + + HxSwapType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + +} diff --git a/src/test/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseTest.java b/src/test/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseTest.java index 56a6f6f2..18bab5fa 100644 --- a/src/test/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseTest.java +++ b/src/test/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponseTest.java @@ -72,12 +72,14 @@ public void extraHxHeaders() { sut.pushHistory("/a/history") .browserRedirect("/a/new/page") .browserRefresh(true) - .retarget("#theThing"); + .retarget("#theThing") + .reswap(HxSwapType.AFTER_BEGIN); assertThat(sut.getHeaderPushHistory()).isEqualTo("/a/history"); assertThat(sut.getHeaderRedirect()).isEqualTo("/a/new/page"); assertThat(sut.getHeaderRefresh()).isTrue(); assertThat(sut.getHeaderRetarget()).isEqualTo("#theThing"); + assertThat(sut.getHeaderReswap()).isEqualTo("afterbegin"); } /** From 5a2e95658bdb0ac10e77586cf87ddf4e3cd59009 Mon Sep 17 00:00:00 2001 From: Runek00 Date: Tue, 15 Aug 2023 00:00:51 +0200 Subject: [PATCH 2/2] Fix in Javadoc --- src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java index f22ad24e..d1835c19 100644 --- a/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java +++ b/src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java @@ -174,7 +174,7 @@ public HtmxResponse retarget(String cssSelector) { /** * Set a new swap to specify how the response will be swapped * - * @param swapType must not be {@literal null} or empty. + * @param swapType must not be {@literal null}. * @return same HtmxResponse for chaining */ public HtmxResponse reswap(HxSwapType swapType) {