VisionForge window binding

This commit is contained in:
Alexander Nozik 2021-03-07 23:00:19 +03:00
parent 8060933859
commit 1a5a207fb7
7 changed files with 24 additions and 13 deletions

View File

@ -20,7 +20,7 @@ allprojects {
} }
group = "space.kscience" group = "space.kscience"
version = "0.2.0-dev-8" version = "0.2.0-dev-9"
} }
subprojects { subprojects {

View File

@ -43,7 +43,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
} }
script { script {
type = "text/javascript" type = "text/javascript"
unsafe { +"window.renderVisionsAt(\"$id\");" } unsafe { +"VisionForge.renderVisionsAt(\"$id\");" }
} }
} }

View File

@ -62,6 +62,7 @@ kotlin {
val jvmMain by getting{ val jvmMain by getting{
dependencies { dependencies {
api(project(":visionforge-server")) api(project(":visionforge-server"))
api("ch.qos.logback:logback-classic:1.2.3")
implementation("com.github.Ricky12Awesome:json-schema-serialization:0.6.6") implementation("com.github.Ricky12Awesome:json-schema-serialization:0.6.6")
} }
} }

View File

@ -6,9 +6,9 @@ import space.kscience.visionforge.solid.three.useThreeJs
@DFExperimental @DFExperimental
fun main(): Unit = VisionForge.run{ fun main(): Unit = VisionForge.run{
console.info("Starting VisionForge context")
usePlotly() usePlotly()
useThreeJs() useThreeJs()
window.asDynamic()["VisionForge"] = VisionForge
renderVisionsInWindow() renderVisionsInWindow()
window.asDynamic()["renderVisionsAt"] = ::renderVisionsAt
window.asDynamic()["renderVisionsInWindow"] = ::renderVisionsInWindow
} }

View File

@ -40,7 +40,7 @@ internal class GdmlForJupyter : JupyterIntegration() {
} }
script { script {
type = "text/javascript" type = "text/javascript"
unsafe { +"window.renderVisionsAt(\"$id\");" } unsafe { +"VisionForge.renderVisionsAt(\"$id\");" }
} }
} }

View File

@ -2,26 +2,36 @@ package space.kscience.visionforge
import kotlinx.browser.document import kotlinx.browser.document
import kotlinx.browser.window import kotlinx.browser.window
import org.w3c.dom.COMPLETE
import org.w3c.dom.Document
import org.w3c.dom.DocumentReadyState
import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Context
import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.misc.DFExperimental
private fun whenDocumentLoaded(block: Document.() -> Unit): Unit {
if(document.readyState == DocumentReadyState.COMPLETE){
block(document)
} else {
document.addEventListener("DOMContentLoaded", { block(document) })
}
}
@JsExport @JsExport
@DFExperimental @DFExperimental
public actual object VisionForge { public actual object VisionForge {
/** /**
* Render all visions in this [window] using current global state of [VisionForge] * Render all visions in this [window] using current global state of [VisionForge]
*/ */
public fun renderVisionsInWindow() { public fun renderVisionsInWindow(): Unit = whenDocumentLoaded {
window.onload = { visionClient.renderAllVisions()
visionClient.renderAllVisions()
}
} }
/** /**
* Render all visions in an element with a given [id] * Render all visions in an element with a given [id]
*/ */
public fun renderVisionsAt(id: String) { public fun renderVisionsAt(id: String): Unit = whenDocumentLoaded {
val element = document.getElementById(id) val element = getElementById(id)
if (element != null) { if (element != null) {
visionClient.renderAllVisionsAt(element) visionClient.renderAllVisionsAt(element)
} else { } else {

View File

@ -7,8 +7,8 @@ import space.kscience.visionforge.solid.three.useThreeJs
@DFExperimental @DFExperimental
public fun main(): Unit = VisionForge.run { public fun main(): Unit = VisionForge.run {
console.info("Starting VisionForge context")
useThreeJs() useThreeJs()
window.asDynamic()["VisionForge"] = VisionForge
renderVisionsInWindow() renderVisionsInWindow()
window.asDynamic()["renderVisionsAt"] = ::renderVisionsAt
window.asDynamic()["renderVisionsInWindow"] = ::renderVisionsInWindow
} }