From 780c340a481676e27398dccf4503e8f0b9c63060 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sun, 13 Oct 2019 17:04:42 +0200 Subject: New Application class to reduce applications boilerplate (#92). New testutils module. --- src/main/kotlin/pl/treksoft/kvision/Application.kt | 86 ++++++++++++++++++++++ src/main/kotlin/pl/treksoft/kvision/HMR.kt | 47 ++++++++++++ .../pl/treksoft/kvision/hmr/ApplicationBase.kt | 41 ----------- src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt | 47 ------------ src/main/kotlin/pl/treksoft/kvision/panel/Root.kt | 39 +++++++++- 5 files changed, 171 insertions(+), 89 deletions(-) create mode 100644 src/main/kotlin/pl/treksoft/kvision/Application.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/HMR.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/hmr/ApplicationBase.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt (limited to 'src/main/kotlin/pl/treksoft') 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) { + start() + } + + /** + * Ending point for an application. + * @return final state for Hot Module Replacement (HMR). + */ + open fun dispose(): Map { + 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.kt b/src/main/kotlin/pl/treksoft/kvision/HMR.kt new file mode 100644 index 00000000..27f8d5ed --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/HMR.kt @@ -0,0 +1,47 @@ +/* + * 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 + +/** + * Helper variable for Hot Module Replacement (HMR). + */ +external val module: Module + +/** + * Helper interface for Hot Module Replacement (HMR). + */ +external interface Module { + val hot: Hot? +} + +/** + * Helper interface for Hot Module Replacement (HMR). + */ +external interface Hot { + val data: dynamic + + fun accept() + fun accept(dependency: String, callback: () -> Unit) + fun accept(dependencies: Array, callback: (updated: Array) -> Unit) + + fun dispose(callback: (data: dynamic) -> Unit) +} 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) - - /** - * Ending point for an application. - * @return final state for Hot Module Replacement (HMR). - */ - fun dispose(): Map -} diff --git a/src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt b/src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt deleted file mode 100644 index 62cde4d9..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/hmr/HMR.kt +++ /dev/null @@ -1,47 +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 - -/** - * Helper variable for Hot Module Replacement (HMR). - */ -external val module: Module - -/** - * Helper interface for Hot Module Replacement (HMR). - */ -external interface Module { - val hot: Hot? -} - -/** - * Helper interface for Hot Module Replacement (HMR). - */ -external interface Hot { - val data: dynamic - - fun accept() - fun accept(dependency: String, callback: () -> Unit) - fun accept(dependencies: Array, callback: (updated: Array) -> Unit) - - fun dispose(callback: (data: dynamic) -> Unit) -} 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 = 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) + } + } } -- cgit