Skip to content

Commit

Permalink
Add reswap() and retarget() http functions (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Nov 9, 2022
1 parent 7137be2 commit 79d8827
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ History

Thanks to Chris Tapper in `PR #264 <https://github.com/adamchainz/django-htmx/pull/264>`__.

* Add ``django_htmx.http.reswap()`` for setting the ``HX-Reswap`` header added in `htmx 1.8.0 <https://htmx.org/posts/2022-07-12-htmx-1.8.0-is-released/>`__.

* Add ``django_htmx.http.retarget()`` for setting the ``HX-Retarget`` header added in `htmx 1.6.1 <https://htmx.org/posts/2021-11-22-htmx-1.6.1-is-released/>`__.

1.12.2 (2022-08-31)
-------------------

Expand Down
20 changes: 18 additions & 2 deletions docs/http.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,30 @@ HTTP
.. autofunction:: push_url

Modify the |HX-Push-Url header|__ of ``response`` to push a URL into the browser location history, and return the response.
``url`` should be the URL to push, or ``False`` to prevent the location history from being updated.
Set the |HX-Push-Url header|__ of ``response`` and return the response.
This header makes htmx push the given URL into the browser location history,
``url`` should be the (relative) URL to push, or ``False`` to prevent the location history from being updated.

.. |HX-Push-Url header| replace:: ``HX-Push-Url`` header
__ https://htmx.org/headers/hx-push-url/

Calling ``push_url`` multiple times for the same ``response`` will replace the value of the header.

.. autofunction:: reswap

Set the |HX-Reswap header|__ of ``response``, and return the response.
This header overrides the `swap method <https://htmx.org/attributes/hx-swap/>`__ that htmx will use.

.. |HX-Reswap header| replace:: ``HX-Reswap`` header
__ https://htmx.org/reference/#response_headers

.. autofunction:: retarget

Set the |HX-Retarget header|__ of ``response`` to override element that htmx will swap the content into, and return the response.

.. |HX-Retarget header| replace:: ``HX-Retarget`` header
__ https://htmx.org/reference/#response_headers

.. autofunction:: trigger_client_event

Modify the |HX-Trigger headers|__ of ``response`` to trigger client-side events, and return the response.
Expand Down
22 changes: 22 additions & 0 deletions src/django_htmx/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ def push_url(response: _HttpResponse, url: str | Literal[False]) -> _HttpRespons
return response


def reswap(
response: _HttpResponse,
method: Literal[
"innerHTML",
"outerHTML",
"beforebegin",
"afterbegin",
"beforeend",
"afterend",
"delete",
"none",
],
) -> _HttpResponse:
response["HX-Reswap"] = method
return response


def retarget(response: _HttpResponse, target: str) -> _HttpResponse:
response["HX-Retarget"] = target
return response


def trigger_client_event(
response: _HttpResponse,
name: str,
Expand Down
28 changes: 26 additions & 2 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from django_htmx.http import HttpResponseClientRefresh
from django_htmx.http import HttpResponseStopPolling
from django_htmx.http import push_url
from django_htmx.http import reswap
from django_htmx.http import retarget
from django_htmx.http import trigger_client_event


Expand Down Expand Up @@ -52,18 +54,40 @@ class PushUrlTests(SimpleTestCase):
def test_success(self):
response = HttpResponse()

push_url(response, "/index.html")
response2 = push_url(response, "/index.html")

assert response2 is response
assert response["HX-Push-Url"] == "/index.html"

def test_success_false(self):
response = HttpResponse()

push_url(response, False)
response2 = push_url(response, False)

assert response2 is response
assert response["HX-Push-Url"] == "false"


class ReswapTests(SimpleTestCase):
def test_success(self):
response = HttpResponse()

response2 = reswap(response, "outerHTML")

assert response2 is response
assert response["HX-Reswap"] == "outerHTML"


class RetargetTests(SimpleTestCase):
def test_success(self):
response = HttpResponse()

response2 = retarget(response, "#heading")

assert response2 is response
assert response["HX-Retarget"] == "#heading"


class TriggerClientEventTests(SimpleTestCase):
def test_fail_bad_after_value(self):
response = HttpResponse()
Expand Down

0 comments on commit 79d8827

Please sign in to comment.