From 1a5a207fb71da199e5eb30dddb443b194400733a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 7 Mar 2021 23:00:19 +0300 Subject: [PATCH] VisionForge window binding --- build.gradle.kts | 2 +- .../VisionForgePlayGroundForJupyter.kt | 2 +- demo/playground/build.gradle.kts | 1 + .../src/jsMain/kotlin/playgroundMain.kt | 4 ++-- .../src/main/kotlin/GdmlForJupyter.kt | 2 +- .../kscience/visionforge/visionForgeJs.kt | 22 ++++++++++++++----- .../visionforge/three/server/jsMain.kt | 4 ++-- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c3f88591..3e30285f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,7 +20,7 @@ allprojects { } group = "space.kscience" - version = "0.2.0-dev-8" + version = "0.2.0-dev-9" } subprojects { diff --git a/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt b/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt index 58304bd2..7b277b71 100644 --- a/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt +++ b/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt @@ -43,7 +43,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { } script { type = "text/javascript" - unsafe { +"window.renderVisionsAt(\"$id\");" } + unsafe { +"VisionForge.renderVisionsAt(\"$id\");" } } } diff --git a/demo/playground/build.gradle.kts b/demo/playground/build.gradle.kts index 3159194c..733bc7c2 100644 --- a/demo/playground/build.gradle.kts +++ b/demo/playground/build.gradle.kts @@ -62,6 +62,7 @@ kotlin { val jvmMain by getting{ dependencies { api(project(":visionforge-server")) + api("ch.qos.logback:logback-classic:1.2.3") implementation("com.github.Ricky12Awesome:json-schema-serialization:0.6.6") } } diff --git a/demo/playground/src/jsMain/kotlin/playgroundMain.kt b/demo/playground/src/jsMain/kotlin/playgroundMain.kt index a49fb3b2..4ecf33f3 100644 --- a/demo/playground/src/jsMain/kotlin/playgroundMain.kt +++ b/demo/playground/src/jsMain/kotlin/playgroundMain.kt @@ -6,9 +6,9 @@ import space.kscience.visionforge.solid.three.useThreeJs @DFExperimental fun main(): Unit = VisionForge.run{ + console.info("Starting VisionForge context") usePlotly() useThreeJs() + window.asDynamic()["VisionForge"] = VisionForge renderVisionsInWindow() - window.asDynamic()["renderVisionsAt"] = ::renderVisionsAt - window.asDynamic()["renderVisionsInWindow"] = ::renderVisionsInWindow } \ No newline at end of file diff --git a/jupyter/visionforge-gdml-jupyter/src/main/kotlin/GdmlForJupyter.kt b/jupyter/visionforge-gdml-jupyter/src/main/kotlin/GdmlForJupyter.kt index 5c8c32d0..16b7c9f9 100644 --- a/jupyter/visionforge-gdml-jupyter/src/main/kotlin/GdmlForJupyter.kt +++ b/jupyter/visionforge-gdml-jupyter/src/main/kotlin/GdmlForJupyter.kt @@ -40,7 +40,7 @@ internal class GdmlForJupyter : JupyterIntegration() { } script { type = "text/javascript" - unsafe { +"window.renderVisionsAt(\"$id\");" } + unsafe { +"VisionForge.renderVisionsAt(\"$id\");" } } } diff --git a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/visionForgeJs.kt b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/visionForgeJs.kt index fbcca4ec..28e2ef66 100644 --- a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/visionForgeJs.kt +++ b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/visionForgeJs.kt @@ -2,26 +2,36 @@ package space.kscience.visionforge import kotlinx.browser.document 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.misc.DFExperimental + +private fun whenDocumentLoaded(block: Document.() -> Unit): Unit { + if(document.readyState == DocumentReadyState.COMPLETE){ + block(document) + } else { + document.addEventListener("DOMContentLoaded", { block(document) }) + } +} + @JsExport @DFExperimental public actual object VisionForge { /** * Render all visions in this [window] using current global state of [VisionForge] */ - public fun renderVisionsInWindow() { - window.onload = { - visionClient.renderAllVisions() - } + public fun renderVisionsInWindow(): Unit = whenDocumentLoaded { + visionClient.renderAllVisions() } /** * Render all visions in an element with a given [id] */ - public fun renderVisionsAt(id: String) { - val element = document.getElementById(id) + public fun renderVisionsAt(id: String): Unit = whenDocumentLoaded { + val element = getElementById(id) if (element != null) { visionClient.renderAllVisionsAt(element) } else { diff --git a/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/space/kscience/visionforge/three/server/jsMain.kt b/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/space/kscience/visionforge/three/server/jsMain.kt index 9ed1536e..48460a92 100644 --- a/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/space/kscience/visionforge/three/server/jsMain.kt +++ b/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/space/kscience/visionforge/three/server/jsMain.kt @@ -7,8 +7,8 @@ import space.kscience.visionforge.solid.three.useThreeJs @DFExperimental public fun main(): Unit = VisionForge.run { + console.info("Starting VisionForge context") useThreeJs() + window.asDynamic()["VisionForge"] = VisionForge renderVisionsInWindow() - window.asDynamic()["renderVisionsAt"] = ::renderVisionsAt - window.asDynamic()["renderVisionsInWindow"] = ::renderVisionsInWindow } \ No newline at end of file