From a981c71416f69461f858510c2a8f79b9920e8838 Mon Sep 17 00:00:00 2001 From: xhaggi Date: Mon, 28 Aug 2023 17:00:44 +0200 Subject: [PATCH] Add annotation support for HX-Reselect --- README.md | 1 + .../boot/mvc/HtmxHandlerInterceptor.java | 8 +++++++ .../htmx/spring/boot/mvc/HxReselect.java | 24 +++++++++++++++++++ .../boot/mvc/HtmxHandlerInterceptorTest.java | 7 ++++++ .../htmx/spring/boot/mvc/TestController.java | 7 ++++++ 5 files changed, 47 insertions(+) create mode 100644 htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReselect.java diff --git a/README.md b/README.md index 92bf0fb4..c703c2a8 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ The following annotations are currently supported: * [@HxRedirect](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRedirect.html) * [@HxRefresh](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRefresh.html) * [@HxReplaceUrl](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReplaceUrl.html) +* [@HxReselect](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReselect.html) * [@HxReswap](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReswap.html) * [@HxRetarget](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRetarget.html) * [@HxTrigger](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxTrigger.html) diff --git a/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java b/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java index 910489fc..0fe27791 100644 --- a/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java +++ b/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptor.java @@ -36,6 +36,7 @@ public boolean preHandle(HttpServletRequest request, setHxReplaceUrl(response, method); setHxReswap(response, method); setHxRetarget(response, method); + setHxReselect(response, method); setHxTrigger(response, method); setHxRefresh(response, method); setVary(request, response); @@ -97,6 +98,13 @@ private void setHxRetarget(HttpServletResponse response, Method method) { } } + private void setHxReselect(HttpServletResponse response, Method method) { + HxReselect methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxReselect.class); + if (methodAnnotation != null) { + response.setHeader(HX_RESELECT.getValue(), methodAnnotation.value()); + } + } + private void setHxTrigger(HttpServletResponse response, Method method) { HxTrigger methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxTrigger.class); if (methodAnnotation != null) { diff --git a/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReselect.java b/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReselect.java new file mode 100644 index 00000000..08bc4769 --- /dev/null +++ b/htmx-spring-boot/src/main/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReselect.java @@ -0,0 +1,24 @@ +package io.github.wimdeblauwe.htmx.spring.boot.mvc; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to specify a CSS selector that allows you to choose which part + * of the response is used to be swapped in. + * + * @see HX-Retarget + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface HxReselect { + + /** + * A CSS selector that allows you to choose which part of the response is used to be swapped in. + *

Overrides an existing hx-select on the triggering element. + */ + String value(); + +} diff --git a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java index ebf4e880..ef98d93a 100644 --- a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java +++ b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerInterceptorTest.java @@ -131,4 +131,11 @@ public void testHxRetarget() throws Exception { .andExpect(header().string("HX-Retarget", "#target")); } + @Test + public void testHxReselect() throws Exception { + mockMvc.perform(get("/hx-reselect")) + .andExpect(status().isOk()) + .andExpect(header().string("HX-Reselect", "#target")); + } + } diff --git a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java index 9f103595..e9dd1433 100644 --- a/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java +++ b/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/TestController.java @@ -112,4 +112,11 @@ public String hxRetarget() { return ""; } + @GetMapping("/hx-reselect") + @HxReselect("#target") + @ResponseBody + public String hxReselect() { + return ""; + } + }