Add htmx custom triggers

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-07-22 18:30:13 -06:00
parent a8a2aae4a8
commit 7ce76cb421
4 changed files with 221 additions and 1 deletions
+33
View File
@@ -0,0 +1,33 @@
(function () {
const triggers = {
ctm_alert: function (evt) {
const message = decodeURIComponent(evt.detail.value);
alert(message);
},
ctm_alert_with_refresh: function (evt) {
const message = decodeURIComponent(evt.detail.value);
alert(message);
location.reload();
},
ctm_toast_success: function (evt) {
const message = decodeURIComponent(evt.detail.value);
toaster.success(message);
},
ctm_toast_error: function (evt) {
const message = decodeURIComponent(evt.detail.value);
toaster.error(message);
},
ctm_toast_success_infinite: function (evt) {
const message = decodeURIComponent(evt.detail.value);
toaster.successInfinite(message);
},
ctm_toast_error_infinite: function (evt) {
const message = decodeURIComponent(evt.detail.value);
toaster.errorInfinite(message);
},
}
for (const key in triggers) {
document.addEventListener(key, triggers[key])
}
})()
+79
View File
@@ -0,0 +1,79 @@
package htmx
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/labstack/echo/v4"
)
// setCustomTrigger will set a custom trigger.
// Is the basic helper for all the other custom triggers.
func setCustomTrigger(c echo.Context, key string, value string) {
value = strings.ReplaceAll(value, `"`, `\"`)
value = url.PathEscape(value)
s := fmt.Sprintf(`{"%s": "%s"}`, key, value)
ServerSetTrigger(c, s)
}
// RespondAlert shows an alert.
func RespondAlert(c echo.Context, message string) error {
ServerSetReswap(c, "none")
setCustomTrigger(c, "ctm_alert", message)
return c.NoContent(http.StatusOK)
}
// RespondAlertWithRefresh shows an alert and refreshes the page using HTMX.
func RespondAlertWithRefresh(c echo.Context, message string) error {
ServerSetReswap(c, "none")
setCustomTrigger(c, "ctm_alert_with_refresh", message)
return c.NoContent(http.StatusOK)
}
// RespondToastSuccess reswaps the HTMX to none and shows an success message
// inside a toast.
func RespondToastSuccess(c echo.Context, message string) error {
ServerSetReswap(c, "none")
setCustomTrigger(c, "ctm_toast_success", message)
return c.NoContent(http.StatusOK)
}
// RespondToastError reswaps the HTMX to none and shows an error message inside
// a toast.
func RespondToastError(c echo.Context, message string) error {
ServerSetReswap(c, "none")
setCustomTrigger(c, "ctm_toast_error", message)
return c.NoContent(http.StatusOK)
}
// RespondToastSuccessInfinite reswaps the HTMX to none and shows an success
// message inside an infinite toast.
func RespondToastSuccessInfinite(c echo.Context, message string) error {
ServerSetReswap(c, "none")
setCustomTrigger(c, "ctm_toast_success_infinite", message)
return c.NoContent(http.StatusOK)
}
// RespondToastErrorInfinite reswaps the HTMX to none and shows an error message
// inside an infinite toast.
func RespondToastErrorInfinite(c echo.Context, message string) error {
ServerSetReswap(c, "none")
setCustomTrigger(c, "ctm_toast_error_infinite", message)
return c.NoContent(http.StatusOK)
}
// RespondRedirect redirects the user to the given URL using HTMX.
func RespondRedirect(c echo.Context, url string) error {
ServerSetReswap(c, "none")
ServerSetRedirect(c, url)
return c.NoContent(http.StatusOK)
}
// RespondRefresh refreshes the page using HTMX.
func RespondRefresh(c echo.Context) error {
ServerSetReswap(c, "none")
ServerSetRefresh(c)
return c.NoContent(http.StatusOK)
}
+106
View File
@@ -0,0 +1,106 @@
package htmx
import (
"github.com/labstack/echo/v4"
)
// ServerIsBoosted indicates that the request is via an element using hx-boost.
func ServerIsBoosted(c echo.Context) bool {
return c.Request().Header.Get("HX-Boosted") != ""
}
// ServerGetCurrentURL of the browser.
func ServerGetCurrentURL(c echo.Context) string {
return c.Request().Header.Get("HX-Current-URL")
}
// ServerIsHistoryRestoreRequest returns whether the request is for history restoration
// after a miss in the local history cache.
func ServerIsHistoryRestoreRequest(c echo.Context) bool {
return c.Request().Header.Get("HX-History-Restore-Request") == "true"
}
// ServerGetPrompt from the user response to an hx-prompt.
// See https://htmx.org/attributes/hx-prompt
func ServerGetPrompt(c echo.Context) string {
return c.Request().Header.Get("HX-Prompt")
}
// ServerIsRequest returns whether this is a HTMX request.
func ServerIsRequest(c echo.Context) bool {
return c.Request().Header.Get("HX-Request") == "true"
}
// ServerGetTarget returns the id of the target element if it exists.
func ServerGetTarget(c echo.Context) string {
return c.Request().Header.Get("HX-Target")
}
// ServerGetTriggerName returns the name of the triggered element if it exists.
func ServerGetTriggerName(c echo.Context) string {
return c.Request().Header.Get("HX-Trigger-Name")
}
// ServerGetTrigger returns the id of the triggered element if it exists.
func ServerGetTrigger(c echo.Context) string {
return c.Request().Header.Get("HX-Trigger")
}
// ServerSetLocation allows you to do a client-side redirect that does
// not do a full page reload.
// See https://htmx.org/headers/hx-location
func ServerSetLocation(c echo.Context, v string) {
c.Response().Header().Set("HX-Location", v)
}
// ServerSetPushURL pushes a new URL into the history stack.
// See https://htmx.org/headers/hx-push-url
func ServerSetPushURL(c echo.Context, v string) {
c.Response().Header().Set("HX-Push-Url", v)
}
// ServerSetRedirect can be used to do a client-side redirect to a new location.
func ServerSetRedirect(c echo.Context, v string) {
c.Response().Header().Set("HX-Redirect", v)
}
// ServerSetRefresh will make the client side do a a full refresh of the page.
func ServerSetRefresh(c echo.Context) {
c.Response().Header().Set("HX-Refresh", "true")
}
// ServerSetReplaceURL replaces the current URL in the location bar.
// See https://htmx.org/headers/hx-replace-url
func ServerSetReplaceURL(c echo.Context, v string) {
c.Response().Header().Set("HX-Replace-Url", v)
}
// ServerSetReswap allows you to specify how the response will be swapped.
// See https://htmx.org/attributes/hx-swap
func ServerSetReswap(c echo.Context, v string) {
c.Response().Header().Set("HX-Reswap", v)
}
// ServerSetRetarget sets a CSS selector that updates the target of the
// content update to a different element on the page.
func ServerSetRetarget(c echo.Context, v string) {
c.Response().Header().Set("HX-Retarget", v)
}
// ServerSetTrigger allows you to trigger client side events.
// See https://htmx.org/headers/hx-trigger
func ServerSetTrigger(c echo.Context, v string) {
c.Response().Header().Set("HX-Trigger", v)
}
// ServerSetTriggerAfterSettle allows you to trigger client side events.
// See https://htmx.org/headers/hx-trigger
func ServerSetTriggerAfterSettle(c echo.Context, v string) {
c.Response().Header().Set("HX-Trigger-After-Settle", v)
}
// ServerSetTriggerAfterSwap allows you to trigger client side events.
// See https://htmx.org/headers/hx-trigger
func ServerSetTriggerAfterSwap(c echo.Context, v string) {
c.Response().Header().Set("HX-Trigger-After-Swap", v)
}
+3 -1
View File
@@ -25,8 +25,10 @@ func Auth(params AuthParams) gomponents.Node {
html.Link(html.Rel("shortcut icon"), html.Href("/favicon.ico")),
html.Link(html.Rel("stylesheet"), html.Href("/css/style.css")),
html.Script(html.Src("/libs/alpinejs/alpinejs-3.14.1.min.js"), html.Defer()),
html.Script(html.Src("/libs/htmx/htmx-2.0.1.min.js"), html.Defer()),
html.Script(html.Src("/js/htmx-triggers.js")),
html.Script(html.Src("/libs/alpinejs/alpinejs-3.14.1.min.js"), html.Defer()),
html.Script(html.Src("/libs/theme-change/theme-change-2.0.2.min.js")),
html.Link(html.Rel("stylesheet"), html.Href("/libs/notyf/notyf-3.10.0.min.css")),