From 0482e157196c0cd8ca58e7c123028c1d54f88336 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 6 May 2020 23:23:50 +0200 Subject: New dedicated classes for NAV, OL, UL, LI html elements. --- src/main/kotlin/pl/treksoft/kvision/html/Li.kt | 66 ++++++++ src/main/kotlin/pl/treksoft/kvision/html/List.kt | 166 --------------------- .../kotlin/pl/treksoft/kvision/html/ListTag.kt | 166 +++++++++++++++++++++ src/main/kotlin/pl/treksoft/kvision/html/Nav.kt | 66 ++++++++ src/main/kotlin/pl/treksoft/kvision/html/Ol.kt | 59 ++++++++ src/main/kotlin/pl/treksoft/kvision/html/Ul.kt | 59 ++++++++ 6 files changed, 416 insertions(+), 166 deletions(-) create mode 100644 src/main/kotlin/pl/treksoft/kvision/html/Li.kt delete mode 100644 src/main/kotlin/pl/treksoft/kvision/html/List.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/html/ListTag.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/html/Nav.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/html/Ol.kt create mode 100644 src/main/kotlin/pl/treksoft/kvision/html/Ul.kt diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Li.kt b/src/main/kotlin/pl/treksoft/kvision/html/Li.kt new file mode 100644 index 00000000..75157d60 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Li.kt @@ -0,0 +1,66 @@ +/* + * 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.html + +import pl.treksoft.kvision.core.Container + +/** + * Simple component rendered as *li*. + * + * @constructor + * @param content element text + * @param rich determines if [content] can contain HTML code + * @param align content align + * @param classes a set of CSS class names + * @param init an initializer extension function + */ +open class Li( + content: String? = null, + rich: Boolean = false, + align: Align? = null, + classes: Set = setOf(), + init: (Li.() -> Unit)? = null +) : + Tag(TAG.LI, content, rich, align, classes) { + + init { + @Suppress("LeakingThis") + init?.invoke(this) + } +} + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.li( + content: String? = null, + rich: Boolean = false, + align: Align? = null, + classes: Set = setOf(), + init: (Li.() -> Unit)? = null +): Li { + val li = Li(content, rich, align, classes).apply { init?.invoke(this) } + this.add(li) + return li +} diff --git a/src/main/kotlin/pl/treksoft/kvision/html/List.kt b/src/main/kotlin/pl/treksoft/kvision/html/List.kt deleted file mode 100644 index cdd18cfc..00000000 --- a/src/main/kotlin/pl/treksoft/kvision/html/List.kt +++ /dev/null @@ -1,166 +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.html - -import com.github.snabbdom.VNode -import com.github.snabbdom.h -import pl.treksoft.kvision.KVManager -import pl.treksoft.kvision.core.Component -import pl.treksoft.kvision.core.Container -import pl.treksoft.kvision.core.StringBoolPair -import pl.treksoft.kvision.panel.SimplePanel -import pl.treksoft.kvision.utils.snClasses -import pl.treksoft.kvision.utils.snOpt - -/** - * HTML list types. - */ -enum class ListType(internal val tagName: String) { - UL("ul"), - OL("ol"), - UNSTYLED("ul"), - INLINE("ul"), - DL("dl"), - DL_HORIZ("dl") -} - -/** - * HTML list component. - * - * The list component can be populated directly from *elements* parameter or manually by adding - * any [Component] to the container. - * - * @constructor - * @param type list type - * @param elements optional list of elements - * @param rich determines if [elements] can contain HTML code - * @param classes a set of CSS class names - * @param init an initializer extension function - */ -open class ListTag( - type: ListType, elements: List? = null, rich: Boolean = false, - classes: Set = setOf(), init: (ListTag.() -> Unit)? = null -) : SimplePanel(classes) { - /** - * List type. - */ - var type by refreshOnUpdate(type) - /** - * List of elements. - */ - var elements by refreshOnUpdate(elements) - /** - * Determines if [elements] can contain HTML code. - */ - var rich by refreshOnUpdate(rich) - - init { - @Suppress("LeakingThis") - init?.invoke(this) - } - - override fun render(): VNode { - val childrenElements = when (type) { - ListType.UL, ListType.OL, ListType.UNSTYLED, ListType.INLINE -> elements?.map { el -> - element("li", el, rich, type == ListType.INLINE) - } - ListType.DL, ListType.DL_HORIZ -> elements?.mapIndexed { index, el -> - element(if (index % 2 == 0) "dt" else "dd", el, rich, false) - } - }?.toTypedArray() - return if (childrenElements != null) { - render(type.tagName, childrenElements + childrenVNodes()) - } else { - render(type.tagName, childrenVNodes()) - } - } - - @Suppress("ComplexCondition", "ComplexMethod") - override fun childrenVNodes(): Array { - val childrenElements = children.filter { it.visible } - val res = when (type) { - ListType.UL, ListType.OL, ListType.UNSTYLED, ListType.INLINE -> childrenElements.map { v -> - if (v is Tag && v.type == TAG.LI /*|| v is DropDown && v.forNavbar*/) { - v.renderVNode() - } else { - if (type == ListType.INLINE) { - val opt = snOpt { - `class` = snClasses(listOf("list-inline-item" to true)) - } - h("li", opt, arrayOf(v.renderVNode())) - } else { - h("li", arrayOf(v.renderVNode())) - } - } - } - ListType.DL, ListType.DL_HORIZ -> childrenElements.mapIndexed { index, v -> - if (v is Tag && v.type == TAG.LI /*|| v is DropDown && v.forNavbar*/) { - v.renderVNode() - } else { - h(if (index % 2 == 0) "dt" else "dd", arrayOf(v.renderVNode())) - } - } - } - return res.toTypedArray() - } - - private fun element(name: String, value: String, rich: Boolean, inline: Boolean): VNode { - val translatedValue = translate(value) - val opt = if (inline) { - snOpt { - `class` = snClasses(listOf("list-inline-item" to true)) - } - } else { - snOpt {} - } - return if (rich) { - h(name, opt, arrayOf(KVManager.virtualize("$translatedValue"))) - } else { - h(name, opt, translatedValue) - } - } - - override fun getSnClass(): List { - val cl = super.getSnClass().toMutableList() - @Suppress("NON_EXHAUSTIVE_WHEN") - when (type) { - ListType.UNSTYLED -> cl.add("list-unstyled" to true) - ListType.INLINE -> cl.add("list-inline" to true) - ListType.DL_HORIZ -> cl.add("dl-horizontal" to true) - } - return cl - } -} - -/** - * DSL builder extension function. - * - * It takes the same parameters as the constructor of the built component. - */ -fun Container.listTag( - type: ListType, elements: List? = null, rich: Boolean = false, - classes: Set = setOf(), init: (ListTag.() -> Unit)? = null -): ListTag { - val listTag = ListTag(type, elements, rich, classes, init) - this.add(listTag) - return listTag -} diff --git a/src/main/kotlin/pl/treksoft/kvision/html/ListTag.kt b/src/main/kotlin/pl/treksoft/kvision/html/ListTag.kt new file mode 100644 index 00000000..cdd18cfc --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/ListTag.kt @@ -0,0 +1,166 @@ +/* + * 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.html + +import com.github.snabbdom.VNode +import com.github.snabbdom.h +import pl.treksoft.kvision.KVManager +import pl.treksoft.kvision.core.Component +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.core.StringBoolPair +import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.utils.snClasses +import pl.treksoft.kvision.utils.snOpt + +/** + * HTML list types. + */ +enum class ListType(internal val tagName: String) { + UL("ul"), + OL("ol"), + UNSTYLED("ul"), + INLINE("ul"), + DL("dl"), + DL_HORIZ("dl") +} + +/** + * HTML list component. + * + * The list component can be populated directly from *elements* parameter or manually by adding + * any [Component] to the container. + * + * @constructor + * @param type list type + * @param elements optional list of elements + * @param rich determines if [elements] can contain HTML code + * @param classes a set of CSS class names + * @param init an initializer extension function + */ +open class ListTag( + type: ListType, elements: List? = null, rich: Boolean = false, + classes: Set = setOf(), init: (ListTag.() -> Unit)? = null +) : SimplePanel(classes) { + /** + * List type. + */ + var type by refreshOnUpdate(type) + /** + * List of elements. + */ + var elements by refreshOnUpdate(elements) + /** + * Determines if [elements] can contain HTML code. + */ + var rich by refreshOnUpdate(rich) + + init { + @Suppress("LeakingThis") + init?.invoke(this) + } + + override fun render(): VNode { + val childrenElements = when (type) { + ListType.UL, ListType.OL, ListType.UNSTYLED, ListType.INLINE -> elements?.map { el -> + element("li", el, rich, type == ListType.INLINE) + } + ListType.DL, ListType.DL_HORIZ -> elements?.mapIndexed { index, el -> + element(if (index % 2 == 0) "dt" else "dd", el, rich, false) + } + }?.toTypedArray() + return if (childrenElements != null) { + render(type.tagName, childrenElements + childrenVNodes()) + } else { + render(type.tagName, childrenVNodes()) + } + } + + @Suppress("ComplexCondition", "ComplexMethod") + override fun childrenVNodes(): Array { + val childrenElements = children.filter { it.visible } + val res = when (type) { + ListType.UL, ListType.OL, ListType.UNSTYLED, ListType.INLINE -> childrenElements.map { v -> + if (v is Tag && v.type == TAG.LI /*|| v is DropDown && v.forNavbar*/) { + v.renderVNode() + } else { + if (type == ListType.INLINE) { + val opt = snOpt { + `class` = snClasses(listOf("list-inline-item" to true)) + } + h("li", opt, arrayOf(v.renderVNode())) + } else { + h("li", arrayOf(v.renderVNode())) + } + } + } + ListType.DL, ListType.DL_HORIZ -> childrenElements.mapIndexed { index, v -> + if (v is Tag && v.type == TAG.LI /*|| v is DropDown && v.forNavbar*/) { + v.renderVNode() + } else { + h(if (index % 2 == 0) "dt" else "dd", arrayOf(v.renderVNode())) + } + } + } + return res.toTypedArray() + } + + private fun element(name: String, value: String, rich: Boolean, inline: Boolean): VNode { + val translatedValue = translate(value) + val opt = if (inline) { + snOpt { + `class` = snClasses(listOf("list-inline-item" to true)) + } + } else { + snOpt {} + } + return if (rich) { + h(name, opt, arrayOf(KVManager.virtualize("$translatedValue"))) + } else { + h(name, opt, translatedValue) + } + } + + override fun getSnClass(): List { + val cl = super.getSnClass().toMutableList() + @Suppress("NON_EXHAUSTIVE_WHEN") + when (type) { + ListType.UNSTYLED -> cl.add("list-unstyled" to true) + ListType.INLINE -> cl.add("list-inline" to true) + ListType.DL_HORIZ -> cl.add("dl-horizontal" to true) + } + return cl + } +} + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.listTag( + type: ListType, elements: List? = null, rich: Boolean = false, + classes: Set = setOf(), init: (ListTag.() -> Unit)? = null +): ListTag { + val listTag = ListTag(type, elements, rich, classes, init) + this.add(listTag) + return listTag +} diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Nav.kt b/src/main/kotlin/pl/treksoft/kvision/html/Nav.kt new file mode 100644 index 00000000..31addd95 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Nav.kt @@ -0,0 +1,66 @@ +/* + * 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.html + +import pl.treksoft.kvision.core.Container + +/** + * Simple component rendered as *nav*. + * + * @constructor + * @param content element text + * @param rich determines if [content] can contain HTML code + * @param align content align + * @param classes a set of CSS class names + * @param init an initializer extension function + */ +open class Nav( + content: String? = null, + rich: Boolean = false, + align: Align? = null, + classes: Set = setOf(), + init: (Nav.() -> Unit)? = null +) : + Tag(TAG.NAV, content, rich, align, classes) { + + init { + @Suppress("LeakingThis") + init?.invoke(this) + } +} + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.nav( + content: String? = null, + rich: Boolean = false, + align: Align? = null, + classes: Set = setOf(), + init: (Nav.() -> Unit)? = null +): Nav { + val nav = Nav(content, rich, align, classes).apply { init?.invoke(this) } + this.add(nav) + return nav +} diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Ol.kt b/src/main/kotlin/pl/treksoft/kvision/html/Ol.kt new file mode 100644 index 00000000..79aa93fb --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Ol.kt @@ -0,0 +1,59 @@ +/* + * 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.html + +import pl.treksoft.kvision.core.Container + +/** + * Simple component rendered as *ol*. + * + * @constructor + * @param elements optional list of elements + * @param rich determines if [elements] can contain HTML code + * @param classes a set of CSS class names + * @param init an initializer extension function + */ +open class Ol( + elements: List? = null, rich: Boolean = false, + classes: Set = setOf(), init: (Ol.() -> Unit)? = null +) : + ListTag(ListType.OL, elements, rich, classes) { + + init { + @Suppress("LeakingThis") + init?.invoke(this) + } +} + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.ol( + elements: List? = null, rich: Boolean = false, + classes: Set = setOf(), init: (Ol.() -> Unit)? = null +): Ol { + val ol = Ol(elements, rich, classes).apply { init?.invoke(this) } + this.add(ol) + return ol +} diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Ul.kt b/src/main/kotlin/pl/treksoft/kvision/html/Ul.kt new file mode 100644 index 00000000..9673f56d --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/html/Ul.kt @@ -0,0 +1,59 @@ +/* + * 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.html + +import pl.treksoft.kvision.core.Container + +/** + * Simple component rendered as *ul*. + * + * @constructor + * @param elements optional list of elements + * @param rich determines if [elements] can contain HTML code + * @param classes a set of CSS class names + * @param init an initializer extension function + */ +open class Ul( + elements: List? = null, rich: Boolean = false, + classes: Set = setOf(), init: (Ul.() -> Unit)? = null +) : + ListTag(ListType.UL, elements, rich, classes) { + + init { + @Suppress("LeakingThis") + init?.invoke(this) + } +} + +/** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ +fun Container.ul( + elements: List? = null, rich: Boolean = false, + classes: Set = setOf(), init: (Ul.() -> Unit)? = null +): Ul { + val ul = Ul(elements, rich, classes).apply { init?.invoke(this) } + this.add(ul) + return ul +} -- cgit