aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/pl')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/Application.kt86
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/HMR.kt (renamed from src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt)2
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/hmr/ApplicationBase.kt41
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/panel/Root.kt39
4 files changed, 125 insertions, 43 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/Application.kt b/src/main/kotlin/pl/treksoft/kvision/Application.kt
new file mode 100644
index 00000000..34d3e8cb
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/Application.kt
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2017-present Robert Jaros
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package pl.treksoft.kvision
+
+import pl.treksoft.kvision.panel.Root
+import kotlin.browser.document
+
+/**
+ * Base class for KVision applications.
+ */
+abstract class Application {
+
+ /**
+ * Starting point for an application.
+ */
+ open fun start() {}
+
+ /**
+ * Starting point for an application with the state managed by Hot Module Replacement (HMR).
+ * @param state Initial state for Hot Module Replacement (HMR).
+ */
+ open fun start(state: Map<String, Any>) {
+ start()
+ }
+
+ /**
+ * Ending point for an application.
+ * @return final state for Hot Module Replacement (HMR).
+ */
+ open fun dispose(): Map<String, Any> {
+ return mapOf()
+ }
+}
+
+/**
+ * Main function for creating KVision applications.
+ */
+fun startApplication(builder: () -> Application) {
+
+ fun start(state: dynamic): Application? {
+ val application = builder()
+ @Suppress("UnsafeCastFromDynamic")
+ application.start(state?.appState ?: emptyMap())
+ return application
+ }
+
+ var application: Application? = null
+
+ val state: dynamic = module.hot?.let { hot ->
+ hot.accept()
+
+ hot.dispose { data ->
+ Root.disposeAllRoots()
+ data.appState = application?.dispose()
+ application = null
+ }
+
+ hot.data
+ }
+
+ if (document.body != null) {
+ application = start(state)
+ } else {
+ application = null
+ document.addEventListener("DOMContentLoaded", { application = start(state) })
+ }
+}
diff --git a/src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt b/src/main/kotlin/pl/treksoft/kvision/HMR.kt
index 62cde4d9..27f8d5ed 100644
--- a/src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/HMR.kt
@@ -19,7 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-package pl.treksoft.kvision.hmr
+package pl.treksoft.kvision
/**
* Helper variable for Hot Module Replacement (HMR).
diff --git a/src/main/kotlin/pl/treksoft/kvision/hmr/ApplicationBase.kt b/src/main/kotlin/pl/treksoft/kvision/hmr/ApplicationBase.kt
deleted file mode 100644
index 3183f71a..00000000
--- a/src/main/kotlin/pl/treksoft/kvision/hmr/ApplicationBase.kt
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2017-present Robert Jaros
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-package pl.treksoft.kvision.hmr
-
-/**
- * Base interface for applications.
- *
- * Base interface for applications supporting Hot Module Replacement (HMR).
- */
-interface ApplicationBase {
- /**
- * Starting point for an application.
- * @param state Initial state between Hot Module Replacement (HMR).
- */
- fun start(state: Map<String, Any>)
-
- /**
- * Ending point for an application.
- * @return final state for Hot Module Replacement (HMR).
- */
- fun dispose(): Map<String, Any>
-}
diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt
index e0c70ac4..ce86c678 100644
--- a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt
@@ -24,6 +24,7 @@ package pl.treksoft.kvision.panel
import com.github.snabbdom.VNode
import com.github.snabbdom.h
import org.w3c.dom.HTMLElement
+import pl.treksoft.kvision.Application
import pl.treksoft.kvision.KVManager
import pl.treksoft.kvision.core.StringBoolPair
import pl.treksoft.kvision.core.Style
@@ -40,6 +41,7 @@ import pl.treksoft.kvision.utils.snOpt
*
* @constructor
* @param id ID attribute of element in the main HTML file
+ * @param element HTML element in the DOM tree
* @param fixed if false, the container is rendered with Bootstrap "container-fluid" class,
* otherwise it's rendered with "container" class (default is false)
* @param init an initializer extension function
@@ -160,13 +162,16 @@ class Root(
/**
* @suppress internal function
*/
- fun shutdown() {
+ fun disposeAllRoots() {
roots.forEach { it.dispose() }
roots.clear()
}
internal val roots: MutableList<Root> = mutableListOf()
+ /**
+ * @suppress internal function
+ */
fun getFirstRoot(): Root? {
return if (roots.isNotEmpty())
roots[0]
@@ -174,6 +179,9 @@ class Root(
null
}
+ /**
+ * @suppress internal function
+ */
fun getLastRoot(): Root? {
return if (roots.isNotEmpty())
roots[roots.size - 1]
@@ -181,12 +189,41 @@ class Root(
null
}
+ /**
+ * @suppress internal function
+ */
fun addModal(modal: Widget) {
modals.add(modal)
}
+ /**
+ * @suppress internal function
+ */
fun removeModal(modal: Widget) {
modals.remove(modal)
}
+
+ /**
+ * Create new Root container based on ID
+ * @param id ID attribute of element in the main HTML file
+ * @param fixed if false, the container is rendered with Bootstrap "container-fluid" class,
+ * otherwise it's rendered with "container" class (default is false)
+ * @param init an initializer extension function
+ */
+ fun Application.root(id: String, fixed: Boolean = false, init: Root.() -> Unit) {
+ Root(id, fixed = fixed, init = init)
+ }
+
+ /**
+ * Create new Root container based on HTML element
+ * @param element HTML element in the DOM tree
+ * @param fixed if false, the container is rendered with Bootstrap "container-fluid" class,
+ * otherwise it's rendered with "container" class (default is false)
+ * @param init an initializer extension function
+ */
+ fun Application.root(element: HTMLElement, fixed: Boolean = false, init: Root.() -> Unit) {
+ Root(element = element, fixed = fixed, init = init)
+ }
+
}
}