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. --- build.gradle | 3 +- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 2 +- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 100 --------------------- kvision-modules/kvision-testutils/build.gradle | 60 +++++++++++++ .../kotlin/pl/treksoft/kvision/test/TestUtil.kt | 100 +++++++++++++++++++++ settings.gradle | 3 +- 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 +++++++- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 2 +- 21 files changed, 337 insertions(+), 1093 deletions(-) delete mode 100644 kvision-modules/kvision-bootstrap-datetime/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-bootstrap-select/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-bootstrap-spinner/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-bootstrap-upload/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-chart/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-redux-kotlin/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-richtext/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt delete mode 100644 kvision-modules/kvision-tabulator/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt create mode 100644 kvision-modules/kvision-testutils/build.gradle create mode 100644 kvision-modules/kvision-testutils/src/test/kotlin/pl/treksoft/kvision/test/TestUtil.kt 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 diff --git a/build.gradle b/build.gradle index da4ab01a..39321f5c 100644 --- a/build.gradle +++ b/build.gradle @@ -185,7 +185,8 @@ dokka { 'kvision-modules/kvision-server-ktor/src/main/kotlin', 'kvision-modules/kvision-server-spring-boot/src/main/kotlin', 'kvision-modules/kvision-cordova/src/main/kotlin', - 'kvision-modules/kvision-electron/src/main/kotlin') + 'kvision-modules/kvision-electron/src/main/kotlin', + 'kvision-modules/kvision-testutils/src/main/kotlin') classpath = [new File("dokka/kvision-dokka-helper.jar")] outputFormat = 'html' outputDirectory = "$buildDir/kdoc" diff --git a/kvision-modules/kvision-bootstrap-datetime/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-bootstrap-datetime/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-bootstrap-datetime/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-bootstrap-select/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-bootstrap-select/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-bootstrap-select/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-bootstrap-spinner/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-bootstrap-spinner/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-bootstrap-spinner/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-bootstrap-upload/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-bootstrap-upload/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-bootstrap-upload/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-bootstrap/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-bootstrap/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt index fc3d5242..d0b05cce 100644 --- a/kvision-modules/kvision-bootstrap/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ b/kvision-modules/kvision-bootstrap/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt @@ -65,7 +65,7 @@ interface DomSpec : TestSpec { val div = document.getElementById("pretest") div?.let { jQuery(it).remove() } jQuery(".modal-backdrop").remove() - Root.shutdown() + Root.disposeAllRoots() } fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { diff --git a/kvision-modules/kvision-chart/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-chart/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-chart/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-datacontainer/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-redux-kotlin/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-redux-kotlin/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-redux-kotlin/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-richtext/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-richtext/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-richtext/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-tabulator/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/kvision-modules/kvision-tabulator/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt deleted file mode 100644 index 13c8531b..00000000 --- a/kvision-modules/kvision-tabulator/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ /dev/null @@ -1,100 +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 test.pl.treksoft.kvision - -import org.w3c.dom.Element -import pl.treksoft.jquery.jQuery -import pl.treksoft.kvision.core.Widget -import pl.treksoft.kvision.panel.Root -import kotlin.browser.document -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -interface TestSpec { - fun beforeTest() - - fun afterTest() - - fun run(code: () -> Unit) { - beforeTest() - code() - afterTest() - } -} - -interface SimpleSpec : TestSpec { - - override fun beforeTest() { - } - - override fun afterTest() { - } - -} - -interface DomSpec : TestSpec { - - override fun beforeTest() { - val fixture = "
" + - "
" - document.body?.insertAdjacentHTML("afterbegin", fixture) - } - - override fun afterTest() { - val div = document.getElementById("pretest") - div?.let { jQuery(it).remove() } - jQuery(".modal-backdrop").remove() - Root.shutdown() - } - - fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { - if (expected != null && actual != null) { - val exp = jQuery(expected) - val act = jQuery(actual) - val result = exp[0]?.isEqualNode(act[0]) - if (result == true) { - assertTrue(result == true, message) - } else { - assertEquals(expected, actual, message) - } - } else { - assertEquals(expected, actual, message) - } - } -} - -interface WSpec : DomSpec { - - fun runW(code: (widget: Widget, element: Element?) -> Unit) { - run { - val root = Root("test", fixed = true) - val widget = Widget() - widget.id = "test_id" - root.add(widget) - val element = document.getElementById("test_id") - code(widget, element) - } - } - -} - -external fun require(name: String): dynamic diff --git a/kvision-modules/kvision-testutils/build.gradle b/kvision-modules/kvision-testutils/build.gradle new file mode 100644 index 00000000..1bf2dcf0 --- /dev/null +++ b/kvision-modules/kvision-testutils/build.gradle @@ -0,0 +1,60 @@ +apply plugin: 'kotlin-platform-js' + +task cleanLibs(type: Delete) { + delete 'build/js', 'build/libs' +} + +if (project.gradle.startParameter.taskNames.contains("jar")) { + compileKotlin2Js.dependsOn 'cleanLibs' +} + +jar { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE +} + +task testJar(type: Jar, dependsOn: testClasses) { + baseName = "${project.archivesBaseName}-test" + from sourceSets.test.output +} + +configurations { + tests +} + +artifacts { + tests testJar +} + +publishing { + publications { + mavenProject(MavenPublication) { + artifact testJar { + classifier 'tests' + } + } + } +} + +compileKotlin2Js { + kotlinOptions.metaInfo = true + kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js" + kotlinOptions.sourceMap = !production + kotlinOptions.moduleKind = 'umd' +} + +compileTestKotlin2Js { + kotlinOptions.metaInfo = true + kotlinOptions.outputFile = "$project.buildDir.path/js-tests/${project.name}-tests.js" + kotlinOptions.sourceMap = !production + kotlinOptions.moduleKind = 'umd' +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion" + compile "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion" + if (!project.gradle.startParameter.taskNames.contains("generatePomFileForMavenProjectPublication")) { + compile project(":kvision-modules:kvision-base") + } else { + compile rootProject + } +} diff --git a/kvision-modules/kvision-testutils/src/test/kotlin/pl/treksoft/kvision/test/TestUtil.kt b/kvision-modules/kvision-testutils/src/test/kotlin/pl/treksoft/kvision/test/TestUtil.kt new file mode 100644 index 00000000..c32faa70 --- /dev/null +++ b/kvision-modules/kvision-testutils/src/test/kotlin/pl/treksoft/kvision/test/TestUtil.kt @@ -0,0 +1,100 @@ +/* + * 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.test + +import org.w3c.dom.Element +import pl.treksoft.jquery.jQuery +import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.panel.Root +import kotlin.browser.document +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +interface TestSpec { + fun beforeTest() + + fun afterTest() + + fun run(code: () -> Unit) { + beforeTest() + code() + afterTest() + } +} + +interface SimpleSpec : TestSpec { + + override fun beforeTest() { + } + + override fun afterTest() { + } + +} + +interface DomSpec : TestSpec { + + override fun beforeTest() { + val fixture = "
" + + "
" + document.body?.insertAdjacentHTML("afterbegin", fixture) + } + + override fun afterTest() { + val div = document.getElementById("pretest") + div?.let { jQuery(it).remove() } + jQuery(".modal-backdrop").remove() + Root.disposeAllRoots() + } + + fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { + if (expected != null && actual != null) { + val exp = jQuery(expected) + val act = jQuery(actual) + val result = exp[0]?.isEqualNode(act[0]) + if (result == true) { + assertTrue(result == true, message) + } else { + assertEquals(expected, actual, message) + } + } else { + assertEquals(expected, actual, message) + } + } +} + +interface WSpec : DomSpec { + + fun runW(code: (widget: Widget, element: Element?) -> Unit) { + run { + val root = Root("test", fixed = true) + val widget = Widget() + widget.id = "test_id" + root.add(widget) + val element = document.getElementById("test_id") + code(widget, element) + } + } + +} + +external fun require(name: String): dynamic diff --git a/settings.gradle b/settings.gradle index 316306ce..414c8b8b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -28,4 +28,5 @@ include 'kvision-modules:kvision-base', 'kvision-modules:kvision-server-ktor', 'kvision-modules:kvision-server-spring-boot', 'kvision-modules:kvision-electron', - 'kvision-modules:kvision-cordova' + 'kvision-modules:kvision-cordova', + 'kvision-modules:kvision-testutils' 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) + } + } } diff --git a/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt index 13c8531b..9da4dfb4 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt @@ -63,7 +63,7 @@ interface DomSpec : TestSpec { val div = document.getElementById("pretest") div?.let { jQuery(it).remove() } jQuery(".modal-backdrop").remove() - Root.shutdown() + Root.disposeAllRoots() } fun assertEqualsHtml(expected: String?, actual: String?, message: String?) { -- cgit