From 7e095b7322c7dc831e8e3481848257dcda99d467 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sun, 21 Jan 2018 20:36:13 +0100 Subject: Hello World example --- .../src/main/kotlin/com/example/Helloworld.kt | 32 +++++++++++++++++ .../helloworld/src/main/kotlin/com/example/Main.kt | 41 ++++++++++++++++++++++ examples/helloworld/src/main/web/index.html | 19 ++++++++++ .../test/kotlin/test/com/example/HelloworldSpec.kt | 21 +++++++++++ .../src/test/kotlin/test/com/example/TestUtil.kt | 32 +++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 examples/helloworld/src/main/kotlin/com/example/Helloworld.kt create mode 100644 examples/helloworld/src/main/kotlin/com/example/Main.kt create mode 100644 examples/helloworld/src/main/web/index.html create mode 100644 examples/helloworld/src/test/kotlin/test/com/example/HelloworldSpec.kt create mode 100644 examples/helloworld/src/test/kotlin/test/com/example/TestUtil.kt (limited to 'examples/helloworld/src') diff --git a/examples/helloworld/src/main/kotlin/com/example/Helloworld.kt b/examples/helloworld/src/main/kotlin/com/example/Helloworld.kt new file mode 100644 index 00000000..61e75482 --- /dev/null +++ b/examples/helloworld/src/main/kotlin/com/example/Helloworld.kt @@ -0,0 +1,32 @@ +package com.example + +import pl.treksoft.kvision.ApplicationBase +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.html.TAG +import pl.treksoft.kvision.html.Tag +import pl.treksoft.kvision.panel.FLEXDIR +import pl.treksoft.kvision.panel.FLEXJUSTIFY +import pl.treksoft.kvision.panel.FlexPanel +import pl.treksoft.kvision.utils.px + +class Helloworld : ApplicationBase() { + + override fun start(state: Map) { + val root = Root("helloworld") + val panel = FlexPanel(FLEXDIR.ROW, justify = FLEXJUSTIFY.CENTER) + val hello = Tag(TAG.DIV, "Hello world!", classes = setOf("helloworld")).apply { + marginTop = 50.px() + + } + panel.add(hello) + root.add(panel) + } + + override fun dispose(): Map { + return mapOf() + } + + companion object { + val css = require("./css/style.css") + } +} diff --git a/examples/helloworld/src/main/kotlin/com/example/Main.kt b/examples/helloworld/src/main/kotlin/com/example/Main.kt new file mode 100644 index 00000000..fba5f87a --- /dev/null +++ b/examples/helloworld/src/main/kotlin/com/example/Main.kt @@ -0,0 +1,41 @@ +package com.example + +import pl.treksoft.kvision.ApplicationBase +import pl.treksoft.kvision.core.KVManager +import pl.treksoft.kvision.module +import kotlin.browser.document + +external fun require(name: String): dynamic + +fun main(args: Array) { + var application: ApplicationBase? = null + + val state: dynamic = module.hot?.let { hot -> + hot.accept() + + hot.dispose { data -> + data.appState = application?.dispose() + KVManager.shutdown() + application = null + } + + hot.data + } + + if (document.body != null) { + application = start(state) + } else { + KVManager.init() + application = null + document.addEventListener("DOMContentLoaded", { application = start(state) }) + } +} + +fun start(state: dynamic): ApplicationBase? { + if (document.getElementById("helloworld") == null) return null + val application = Helloworld() + @Suppress("UnsafeCastFromDynamic") + application.start(state?.appState ?: emptyMap()) + return application +} + diff --git a/examples/helloworld/src/main/web/index.html b/examples/helloworld/src/main/web/index.html new file mode 100644 index 00000000..8d540e95 --- /dev/null +++ b/examples/helloworld/src/main/web/index.html @@ -0,0 +1,19 @@ + + + + + + + KVision Showcase + + + + + + +
+ + diff --git a/examples/helloworld/src/test/kotlin/test/com/example/HelloworldSpec.kt b/examples/helloworld/src/test/kotlin/test/com/example/HelloworldSpec.kt new file mode 100644 index 00000000..8054c000 --- /dev/null +++ b/examples/helloworld/src/test/kotlin/test/com/example/HelloworldSpec.kt @@ -0,0 +1,21 @@ +package test.com.example + +import com.example.Helloworld +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertTrue + +class HelloworldSpec : DomSpec { + + @Test + fun render() { + run { + Helloworld().start(mapOf()) + val element = document.getElementById("helloworld") + assertTrue( + element?.innerHTML?.contains("Hello world!") ?: false, + "Application should render Hello world! text" + ) + } + } +} diff --git a/examples/helloworld/src/test/kotlin/test/com/example/TestUtil.kt b/examples/helloworld/src/test/kotlin/test/com/example/TestUtil.kt new file mode 100644 index 00000000..c5ec014f --- /dev/null +++ b/examples/helloworld/src/test/kotlin/test/com/example/TestUtil.kt @@ -0,0 +1,32 @@ +package test.com.example + +import pl.treksoft.jquery.jQuery +import kotlin.browser.document + +interface TestSpec { + fun beforeTest() + + fun afterTest() + + fun run(code: () -> Unit) { + beforeTest() + code() + afterTest() + } +} + +interface DomSpec : TestSpec { + + override fun beforeTest() { + val fixture = "
" + + "
" + document.body?.insertAdjacentHTML("afterbegin", fixture) + } + + override fun afterTest() { + val div = document.getElementById("pretest") + div?.remove() + jQuery(`object` = ".modal-backdrop").remove() + } + +} -- cgit