Skip to content

Commit

Permalink
Merge pull request #62 from Runek00/hx-reswap
Browse files Browse the repository at this point in the history
Issue #56: add reswap to HtmxResponse
  • Loading branch information
wimdeblauwe authored Aug 23, 2023
2 parents c84b09b + 5a2e956 commit 26f1bda
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/main/java/io/github/wimdeblauwe/hsbt/mvc/HtmxResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down Expand Up @@ -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}.
* @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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -272,4 +288,8 @@ String getHeaderRedirect() {
String getHeaderPushHistory() {
return headerPushHistory;
}

public String getHeaderReswap() {
return headerReswap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> triggers,
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/github/wimdeblauwe/hsbt/mvc/HxSwapType.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/**
Expand Down

0 comments on commit 26f1bda

Please sign in to comment.