Skip to content

Commit

Permalink
Add aHtmx function to avoid typing the htmx attributes manually on links
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Sep 1, 2023
1 parent a4f5250 commit 0b746b7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.html.*
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import net.perfectdreams.galleryofdreams.backend.utils.FanArtUtils
import net.perfectdreams.galleryofdreams.backend.utils.aHtmx
import net.perfectdreams.galleryofdreams.backend.utils.websiteLocaleIdPath
import net.perfectdreams.galleryofdreams.common.data.FanArtArtistX
import net.perfectdreams.i18nhelper.core.I18nContext
Expand All @@ -16,8 +17,7 @@ fun FlowContent.fanArtArtist(
fanArtCount: Long
) {
div {
a(classes = "entry", href = "/${i18nContext.websiteLocaleIdPath}/artists/${artist.slug}") {
attributes["hx-target"] = "#content"
aHtmx(classes = "entry", href = "/${i18nContext.websiteLocaleIdPath}/artists/${artist.slug}", hxTarget = "#content") {
attributes["power-close-sidebar"] = "true"

val url = FanArtUtils.getArtistAvatarUrl(dssBaseUrl, namespace, artist, 32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.datetime.toLocalDateTime
import kotlinx.html.*
import net.perfectdreams.galleryofdreams.backend.GalleryOfDreamsBackend
import net.perfectdreams.galleryofdreams.backend.utils.FanArtUtils
import net.perfectdreams.galleryofdreams.backend.utils.aHtmx
import net.perfectdreams.galleryofdreams.backend.utils.icon
import net.perfectdreams.galleryofdreams.backend.utils.websiteLocaleIdPath
import net.perfectdreams.galleryofdreams.common.MediaTypeUtils
Expand All @@ -13,9 +14,7 @@ import net.perfectdreams.galleryofdreams.common.i18n.I18nKeysData
import net.perfectdreams.i18nhelper.core.I18nContext

fun FlowContent.fanArtCard(m: GalleryOfDreamsBackend, i18nContext: I18nContext, dssBaseUrl: String, namespace: String, fanArtArtist: FanArtArtistX, fanArt: FanArt) {
a(classes = "fan-art-card", href = "/${i18nContext.websiteLocaleIdPath}/artists/${fanArtArtist.slug}/${fanArt.slug}") {
attributes["hx-target"] = "#content"

aHtmx(classes = "fan-art-card", href = "/${i18nContext.websiteLocaleIdPath}/artists/${fanArtArtist.slug}/${fanArt.slug}", hxTarget = "#content") {
div(classes = "fan-art-info-card") {
div(classes = "fan-art-tags") {
for (tag in fanArt.tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ package net.perfectdreams.galleryofdreams.backend.utils

import io.ktor.server.application.*
import io.ktor.server.request.*
import kotlinx.html.*

val ApplicationCall.htmxElementTarget: String?
get() = this.request.header("HX-Target")
get() = this.request.header("HX-Target")

/**
* Anchor
*/
@HtmlTagMarker
inline fun FlowOrInteractiveOrPhrasingContent.aHtmx(href : String, hxTarget : String, classes : String? = null, crossinline block : A.() -> Unit = {}) = a(href = href, classes = classes) {
attributes["hx-get"] = href
attributes["hx-push-url"] = "true"
attributes["hx-target"] = hxTarget
block()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.perfectdreams.galleryofdreams.backend.components.fanArtArtist
import net.perfectdreams.galleryofdreams.backend.tables.FanArtArtists
import net.perfectdreams.galleryofdreams.backend.utils.FanArtArtistSortOrder
import net.perfectdreams.galleryofdreams.backend.utils.FanArtArtistWithFanArtCount
import net.perfectdreams.galleryofdreams.backend.utils.aHtmx
import net.perfectdreams.galleryofdreams.backend.utils.websiteLocaleIdPath
import net.perfectdreams.galleryofdreams.common.i18n.I18nKeysData
import net.perfectdreams.i18nhelper.core.I18nContext
Expand Down Expand Up @@ -103,8 +104,7 @@ abstract class DashboardView(
attributes["hx-preserve"] = "true"

div(classes = "entries") {
a(classes = "entry", href = "/${i18nContext.websiteLocaleIdPath}/") {
attributes["hx-target"] = "#content"
aHtmx(classes = "entry", href = "/${i18nContext.websiteLocaleIdPath}/", hxTarget = "#content") {
attributes["power-close-sidebar"] = "true"

text(i18nContext.get(I18nKeysData.AboutTheFanArts))
Expand Down Expand Up @@ -168,8 +168,7 @@ abstract class DashboardView(

hr(classes = "divider")

a(classes = "entry", href = "/${i18nContext.websiteLocaleIdPath}/fan-arts") {
attributes["hx-target"] = "#content"
aHtmx(classes = "entry", href = "/${i18nContext.websiteLocaleIdPath}/fan-arts", hxTarget = "#content") {
attributes["power-close-sidebar"] = "true"

text(i18nContext.get(I18nKeysData.ViewAllFanArts(totalFanArtCount)))
Expand Down

0 comments on commit 0b746b7

Please sign in to comment.