-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close: #660 - Defines constants for HTMX Request Header and HTMX Response Headers - Defines HTMXResponse for out of band swaps. - Defines HtmxRequestUtils Allows to bind a HttpRequestHeaders to a controller method.
- Loading branch information
Showing
40 changed files
with
1,391 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
To integrate with https://htmx.org[HTMX], add the following dependency: | ||
|
||
dependency:micronaut-views-htmx[groupId="io.micronaut.views"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
You can bind api:views.htmx.http.HtmxRequestHeaders[] in a controller method. | ||
|
||
In the following example, the parameter is bound if the request is an HTMX request. If it is not an HTMX request, | ||
the parameter is bound as null. | ||
|
||
snippet::io.micronaut.views.docs.htmx.HtmxTest[tags="htmxRequestHeaders",indent=0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The class api:views.htmx.http.HtmxResponseHeaders[] defines constants for the https://htmx.org/reference/#response_headers[HTMX Response headers]. | ||
|
||
snippet::io.micronaut.views.docs.htmx.HtmxTest[tags="htmxResponseHeaders",indent=0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
You can return an API:views.htmx.HtmxResponse[] in a controller method to render multiple views in a single HTMX response—for example, to do https://htmx.org/docs/#oob_swaps[Out Of Band Swaps]. | ||
|
||
snippet::io.micronaut.views.docs.htmx.HtmxTest[tags="outOfBandSwaps",indent=0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
test-suite-groovy/src/test/groovy/io/micronaut/views/docs/htmx/HtmxTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io.micronaut.views.docs.htmx | ||
|
||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.core.annotation.NonNull | ||
import io.micronaut.core.annotation.Nullable | ||
import io.micronaut.core.util.StringUtils | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Post | ||
import io.micronaut.http.client.BlockingHttpClient | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import io.micronaut.views.ModelAndView | ||
import io.micronaut.views.docs.turbo.Fruit | ||
import io.micronaut.views.htmx.http.HtmxRequestHeaders | ||
import io.micronaut.views.htmx.http.HtmxResponse | ||
import io.micronaut.views.htmx.http.HtmxResponseHeaders | ||
import jakarta.inject.Inject | ||
import spock.lang.Specification | ||
|
||
@Property(name = "micronaut.views.soy.enabled", value = StringUtils.FALSE) | ||
@Property(name = "micronaut.security.enabled", value = StringUtils.FALSE) | ||
@Property(name = "spec.name", value = "HtmxRequestHeadersTest") | ||
@MicronautTest | ||
class HtmxTest extends Specification { | ||
|
||
@Inject | ||
@Client("/") | ||
HttpClient httpClient | ||
|
||
void testHtmxRequestHeaders() { | ||
given: | ||
BlockingHttpClient client = httpClient.toBlocking() | ||
|
||
when: | ||
String html = client.retrieve(HttpRequest.GET("/fruits/htmx").header("HX-Request", StringUtils.TRUE)) | ||
then: | ||
'<h1>fruit: Apple</h1>\n<h2>color: Red</h2>\n' == html | ||
when: | ||
html = client.retrieve(HttpRequest.GET("/fruits/htmx")) | ||
then: | ||
html.contains("<!DOCTYPE html>") | ||
} | ||
|
||
void testOutOfBandSwaps() { | ||
given: | ||
BlockingHttpClient client = httpClient.toBlocking() | ||
when: | ||
String html = client.retrieve(HttpRequest.POST("/fruits/htmx", Collections.emptyMap()).header("HX-Request", StringUtils.TRUE)); | ||
then: | ||
'<h1>fruit: Apple</h1>\n<h2>color: Red</h2>\n<div id="message" hx-swap-oob="true">Swap me directly!</div>' == html | ||
} | ||
|
||
void htmxResponseHeaders() { | ||
given: | ||
BlockingHttpClient client = httpClient.toBlocking(); | ||
|
||
when: | ||
HttpResponse<?> response = client.exchange(HttpRequest.GET("/fruits/htmx/responseHeaders").header("HX-Request", StringUtils.TRUE)); | ||
|
||
then: | ||
StringUtils.TRUE == response.getHeaders().get("HX-Refresh") | ||
} | ||
|
||
@Requires(property = "spec.name", value = "HtmxRequestHeadersTest") | ||
@Controller("/fruits/htmx") | ||
static class HtmxRequestHeadersController { | ||
//tag::htmxRequestHeaders[] | ||
@Get | ||
ModelAndView<Map<String, Object>> index(@Nullable HtmxRequestHeaders htmxRequestHeaders) { | ||
Map<String, Object> model = [fruit: new Fruit("Apple", "Red")] | ||
if (htmxRequestHeaders != null) { | ||
return new ModelAndView<>("fruit", model) | ||
} | ||
new ModelAndView<>("fruits", model) | ||
} | ||
//end::htmxRequestHeaders[] | ||
|
||
//tag::outOfBandSwaps[] | ||
@Post | ||
HtmxResponse<?> outOfBandSwaps(@NonNull HtmxRequestHeaders htmxRequestHeaders) { | ||
HtmxResponse.builder() | ||
.modelAndView(new ModelAndView<>("fruit", [fruit: new Fruit("Apple", "Red")])) | ||
.modelAndView(new ModelAndView<>("swap", [:])) | ||
.build() | ||
} | ||
//end::outOfBandSwaps[] | ||
|
||
//tag::htmxResponseHeaders[] | ||
@Get("/responseHeaders") | ||
HttpResponse<?> htmxResponseHeaders(@NonNull HtmxRequestHeaders htmxRequestHeaders) { | ||
HttpResponse.ok().header(HtmxResponseHeaders.HX_REFRESH, StringUtils.TRUE); | ||
} | ||
//end::htmxResponseHeaders[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div id="message" hx-swap-oob="true">Swap me directly!</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
test-suite-kotlin/src/test/kotlin/io/micronaut/views/docs/htmx/HtmxTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.micronaut.views.docs.htmx | ||
|
||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.core.annotation.NonNull | ||
import io.micronaut.core.util.StringUtils | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Post | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import io.micronaut.views.ModelAndView | ||
import io.micronaut.views.docs.turbo.Fruit | ||
import io.micronaut.views.htmx.http.HtmxRequestHeaders | ||
import io.micronaut.views.htmx.http.HtmxResponse | ||
import io.micronaut.views.htmx.http.HtmxResponseHeaders | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
|
||
@Property(name = "micronaut.views.soy.enabled", value = StringUtils.FALSE) | ||
@Property(name = "micronaut.security.enabled", value = StringUtils.FALSE) | ||
@Property(name = "spec.name", value = "HtmxRequestHeadersTest") | ||
@MicronautTest | ||
internal class HtmxTest { | ||
@Test | ||
fun testHtmxRequestHeaders(@Client("/") httpClient: HttpClient) { | ||
val client = httpClient.toBlocking() | ||
var html = client.retrieve(HttpRequest.GET<Any>("/fruits/htmx").header("HX-Request", StringUtils.TRUE)) | ||
assertEquals( | ||
""" | ||
<h1>fruit: Apple</h1> | ||
<h2>color: Red</h2> | ||
""".trimIndent(), html | ||
) | ||
html = client.retrieve(HttpRequest.GET<Any>("/fruits/htmx")) | ||
assertTrue(html.contains("<!DOCTYPE html>")) | ||
} | ||
|
||
@Test | ||
fun testOutOfBandSwaps(@Client("/") httpClient: HttpClient) { | ||
val client = httpClient.toBlocking() | ||
val html = client.retrieve( | ||
HttpRequest.POST("/fruits/htmx", emptyMap<Any, Any>()).header("HX-Request", StringUtils.TRUE) | ||
) | ||
assertEquals( | ||
""" | ||
<h1>fruit: Apple</h1> | ||
<h2>color: Red</h2> | ||
<div id="message" hx-swap-oob="true">Swap me directly!</div> | ||
""".trimIndent(), html | ||
) | ||
} | ||
|
||
@Test | ||
fun htmxResponseHeaders(@Client("/") httpClient: HttpClient) { | ||
val client = httpClient.toBlocking() | ||
val response: HttpResponse<*> = client.exchange<Any, Any>( | ||
HttpRequest.GET<Any>("/fruits/htmx/responseHeaders").header("HX-Request", StringUtils.TRUE) | ||
) | ||
assertEquals( | ||
StringUtils.TRUE, | ||
response.headers["HX-Refresh"] | ||
) | ||
} | ||
|
||
@Requires(property = "spec.name", value = "HtmxRequestHeadersTest") | ||
@Controller("/fruits/htmx") | ||
internal class HtmxRequestHeadersController { | ||
//tag::htmxRequestHeaders[] | ||
@Get | ||
fun index(htmxRequestHeaders: HtmxRequestHeaders?): ModelAndView<Map<String, Any>> { | ||
val model = mapOf("fruit" to Fruit("Apple", "Red")) | ||
if (htmxRequestHeaders != null) { | ||
return ModelAndView("fruit", model) | ||
} | ||
return ModelAndView("fruits", model) | ||
} | ||
//end::htmxRequestHeaders[] | ||
|
||
//tag::outOfBandSwaps[] | ||
@Post | ||
fun outOfBandSwaps(htmxRequestHeaders: HtmxRequestHeaders): HtmxResponse<*> { | ||
return HtmxResponse.builder<Any>() | ||
.modelAndView(ModelAndView("fruit", mapOf("fruit" to Fruit("Apple", "Red")))) | ||
.modelAndView(ModelAndView("swap", emptyMap<Any, Any>())) | ||
.build() | ||
} | ||
//end::outOfBandSwaps[] | ||
|
||
//tag::htmxResponseHeaders[] | ||
@Get("/responseHeaders") | ||
fun htmxResponseHeaders(htmxRequestHeaders: @NonNull HtmxRequestHeaders): HttpResponse<*> { | ||
return HttpResponse.ok<Any>().header(HtmxResponseHeaders.HX_REFRESH, StringUtils.TRUE) | ||
} | ||
//end::htmxResponseHeaders[] | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div id="message" hx-swap-oob="true">Swap me directly!</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.