-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
080dc9c
commit 98f9ddf
Showing
11 changed files
with
160 additions
and
14 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
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 @@ | ||
build |
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,18 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
id("org.jlleitschuh.gradle.ktlint") | ||
id("org.jetbrains.kotlinx.kover") | ||
alias(libs.plugins.maven.publish) | ||
} | ||
|
||
applyBasicSetup() | ||
|
||
kotlin { | ||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
api(projects.core) | ||
} | ||
} | ||
} | ||
} |
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,2 @@ | ||
POM_NAME=Javascript | ||
POM_ARTIFACT_ID=javascript |
47 changes: 47 additions & 0 deletions
47
javascript/js/src/dev/programadorthi/routing/javascript/JavascriptRouting.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,47 @@ | ||
package dev.programadorthi.routing.javascript | ||
|
||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.core.application | ||
import dev.programadorthi.routing.core.call | ||
import kotlinx.browser.window | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.dom.clear | ||
import org.w3c.dom.Element | ||
import org.w3c.dom.PopStateEvent | ||
import kotlin.js.Json | ||
|
||
internal const val METHOD_KEY = "method" | ||
internal const val URI_KEY = "uri" | ||
|
||
public fun render( | ||
routing: Routing, | ||
root: Element, | ||
) { | ||
with(routing.application) { | ||
routingFlow = MutableStateFlow(null) | ||
|
||
launch { | ||
routingFlow.collect { child -> | ||
if (child != null) { | ||
root.clear() | ||
root.appendChild(child) | ||
} | ||
} | ||
} | ||
} | ||
|
||
window.onpopstate = { event -> | ||
onPopState(routing = routing, event = event) | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST_TO_EXTERNAL_INTERFACE") | ||
private fun onPopState(routing: Routing, event: PopStateEvent) { | ||
val json = event.state as? Json ?: return | ||
val uri = json[URI_KEY] as? String | ||
val method = json[METHOD_KEY] as? String | ||
if (uri.isNullOrBlank() || method.isNullOrBlank()) return | ||
// TODO: add route method and parameters support | ||
routing.call(uri = uri) | ||
} |
22 changes: 22 additions & 0 deletions
22
javascript/js/src/dev/programadorthi/routing/javascript/JavascriptRoutingAttribute.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,22 @@ | ||
package dev.programadorthi.routing.javascript | ||
|
||
import dev.programadorthi.routing.core.application.Application | ||
import dev.programadorthi.routing.core.application.ApplicationCall | ||
import io.ktor.util.AttributeKey | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import org.w3c.dom.Node | ||
|
||
internal val JavascriptRoutingAttributeKey: AttributeKey<MutableStateFlow<Node?>> = | ||
AttributeKey("JavascriptRoutingAttributeKey") | ||
|
||
internal var Application.routingFlow: MutableStateFlow<Node?> | ||
get() = attributes[JavascriptRoutingAttributeKey] | ||
internal set(value) { | ||
attributes.put(JavascriptRoutingAttributeKey, value) | ||
} | ||
|
||
internal var ApplicationCall.destination: Node? | ||
get() = application.routingFlow.value | ||
internal set(value) { | ||
application.routingFlow.value = value | ||
} |
41 changes: 41 additions & 0 deletions
41
javascript/js/src/dev/programadorthi/routing/javascript/JavascriptRoutingBuilder.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,41 @@ | ||
package dev.programadorthi.routing.javascript | ||
|
||
import dev.programadorthi.routing.core.Route | ||
import dev.programadorthi.routing.core.RouteMethod | ||
import dev.programadorthi.routing.core.application.ApplicationCall | ||
import dev.programadorthi.routing.core.application.call | ||
import dev.programadorthi.routing.core.route | ||
import io.ktor.util.KtorDsl | ||
import io.ktor.util.pipeline.PipelineContext | ||
import kotlinx.browser.window | ||
import org.w3c.dom.Element | ||
import kotlin.js.json | ||
|
||
@KtorDsl | ||
public fun Route.jsRoute( | ||
path: String, | ||
name: String? = null, | ||
body: PipelineContext<Unit, ApplicationCall>.() -> Element, | ||
): Route = route(path = path, name = name) { jsRoute(body) } | ||
|
||
@KtorDsl | ||
public fun Route.jsRoute( | ||
path: String, | ||
method: RouteMethod, | ||
name: String? = null, | ||
body: PipelineContext<Unit, ApplicationCall>.() -> Element, | ||
): Route = route(path = path, name = name, method = method) { jsRoute(body) } | ||
|
||
@KtorDsl | ||
public fun Route.jsRoute( | ||
body: PipelineContext<Unit, ApplicationCall>.() -> Element, | ||
) { | ||
handle { | ||
call.destination = body(this) | ||
val data = json( | ||
METHOD_KEY to call.routeMethod.value, | ||
URI_KEY to call.uri, | ||
) | ||
window.history.pushState(data = data, title = "", url = call.uri) | ||
} | ||
} |
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