Skip to content

Instantly share code, notes, and snippets.

@AngelMunoz
Created January 15, 2024 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngelMunoz/1a4a9cc7b49e821cfc7ff4b8b6a9822a to your computer and use it in GitHub Desktop.
Save AngelMunoz/1a4a9cc7b49e821cfc7ff4b8b6a9822a to your computer and use it in GitHub Desktop.
A few non exhaustive Htmx bindings and helpers for ktor and kotlin.html
package me.tunaxor.htmx
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import kotlinx.html.CommonAttributeGroupFacade
enum class HxSwap {
InnerHTML,
OuterHTML,
BeforeBegin,
AfterBegin,
BeforeEnd,
AfterEnd,
Delete,
None
}
val HxSwap.asString: String
get() {
return when (this) {
HxSwap.InnerHTML -> "innerHTML"
HxSwap.OuterHTML -> "outerHTML"
HxSwap.BeforeBegin -> "beforebegin"
HxSwap.AfterBegin -> "afterbegin"
HxSwap.BeforeEnd -> "beforeend"
HxSwap.AfterEnd -> "afterend"
HxSwap.Delete -> "delete"
HxSwap.None -> "none"
}
}
fun CommonAttributeGroupFacade.hxGet(url: String) {
attributes["hx-get"] = url
}
fun CommonAttributeGroupFacade.hxPost(url: String) {
attributes["hx-post"] = url
}
fun CommonAttributeGroupFacade.hxPut(url: String) {
attributes["hx-put"] = url
}
fun CommonAttributeGroupFacade.hxPatch(url: String) {
attributes["hx-patch"] = url
}
fun CommonAttributeGroupFacade.hxDelete(url: String) {
attributes["hx-delete"] = url
}
fun CommonAttributeGroupFacade.hxIndicator(indicator: String) {
attributes["hx-indicator"] = indicator
}
fun CommonAttributeGroupFacade.hxSwap(swap: HxSwap) {
attributes["hx-swap"] = swap.asString
}
fun CommonAttributeGroupFacade.hxTarget(selector: String) {
attributes["hx-target"] = selector
}
fun CommonAttributeGroupFacade.hxTrigger(trigger: String) {
attributes["hx-trigger"] = trigger
}
fun CommonAttributeGroupFacade.hxSync(sync: String) {
attributes["hx-sync"] = sync
}
fun CommonAttributeGroupFacade.hxSelect(selector: String) {
attributes["hx-select"] = selector
}
fun CommonAttributeGroupFacade.hxSwapOob(shouldSwap: Boolean) {
attributes["hx-swap-oob"] = "$shouldSwap"
}
fun CommonAttributeGroupFacade.hxPreserve(selector: String) {
attributes["hx-preserve"] = selector
}
fun CommonAttributeGroupFacade.hxEncoding(encoding: String) {
attributes["hx-encoding"] = encoding
}
fun CommonAttributeGroupFacade.hxVars(vars: String) {
attributes["hx-vars"] = vars
}
fun CommonAttributeGroupFacade.hxConfirm(confirm: String) {
attributes["hx-confirm"] = confirm
}
fun CommonAttributeGroupFacade.hxPrompt(prompt: String) {
attributes["hx-prompt"] = prompt
}
fun CommonAttributeGroupFacade.hxBoost(shouldBoost: Boolean) {
attributes["hx-boost"] = "$shouldBoost"
}
fun CommonAttributeGroupFacade.hxWs(url: String) {
attributes["hx-ws"] = url
}
fun CommonAttributeGroupFacade.hxSse(url: String) {
attributes["hx-sse"] = url
}
fun CommonAttributeGroupFacade.hxPushUrl(shouldPush: Boolean) {
attributes["hx-push-url"] = "$shouldPush"
}
fun CommonAttributeGroupFacade.hxHistoryElt(shouldHistory: Boolean) {
attributes["hx-history-elt"] = "$shouldHistory"
}
fun CommonAttributeGroupFacade.hxExt(extension: String) {
attributes["hx-ext"] = extension
}
fun CommonAttributeGroupFacade.hxOn(event: String, handler: String) {
attributes["hx-on:$event"] = handler
}
fun CommonAttributeGroupFacade.hxSelectOob(selector: String) {
attributes["hx-select-oob"] = selector
}
fun CommonAttributeGroupFacade.hxDisable(shouldDisable: Boolean) {
attributes["hx-disable"] = "$shouldDisable"
}
fun CommonAttributeGroupFacade.hxDisabledElt(selector: String) {
attributes["hx-disabled-elt"] = selector
}
fun CommonAttributeGroupFacade.hxDisinherit(disinherit: String) {
attributes["hx-disinherit"] = disinherit
}
fun CommonAttributeGroupFacade.hxHeaders(headers: String) {
attributes["hx-headers"] = headers
}
fun CommonAttributeGroupFacade.hxInclude(include: String) {
attributes["hx-include"] = include
}
fun CommonAttributeGroupFacade.hxParams(params: String) {
attributes["hx-params"] = params
}
fun CommonAttributeGroupFacade.hxValidate(validate: String) {
attributes["hx-validate"] = validate
}
fun ApplicationCall.hxBoosted(): Boolean {
return request.header("HX-Boosted") == "true"
}
fun ApplicationCall.hxCurrentUrl(): String? {
return request.header("HX-Current-Url")
}
fun ApplicationCall.hxHistoryRestoreRequest(): Boolean {
return request.header("HX-History-Restore-Request") == "true"
}
fun ApplicationCall.xhPrompt(): String? {
return request.header("HX-Prompt")
}
fun ApplicationCall.hxRequest(): Boolean {
return request.header("HX-Request") == "true"
}
fun ApplicationCall.hxTarget(): String? {
return request.header("HX-Target")
}
fun ApplicationCall.hxTriggerName(): String? {
return request.header("HX-Trigger-Name")
}
fun ApplicationCall.hxTrigger(): String? {
return request.header("HX-Trigger")
}
fun ApplicationCall.hxLocation(url: String) {
response.header("HX-Location", url)
}
fun ApplicationCall.xhPushUrl(url: String) {
response.header("HX-Push-Url", url)
}
fun ApplicationCall.hxRedirect(shouldRedrect: Boolean) {
response.header("HX-Redirect", "$shouldRedrect")
}
fun ApplicationCall.hxRefresh(shouldRefresh: Boolean) {
response.header("HX-Refresh", "$shouldRefresh")
}
fun ApplicationCall.hxReplaceUrl(url: String) {
response.header("HX-Replace-Url", url)
}
fun ApplicationCall.hxReswap(swap: HxSwap) {
response.header("HX-Reswap", swap.asString)
}
fun ApplicationCall.hxRetarget(selector: String) {
response.header("HX-Retarget", selector)
}
fun ApplicationCall.xhReselect(selector: String) {
response.header("HX-Reselect", selector)
}
fun ApplicationCall.hxTrigger(trigger: String) {
response.header("HX-Trigger", trigger)
}
fun ApplicationCall.hxTriggerAfterSettle(trigger: String) {
response.header("HX-Trigger-After-Settle", trigger)
}
fun ApplicationCall.hxTriggerAfterSwap(trigger: String) {
response.header("HX-Trigger-After-Swap", trigger)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment