Fix refresh, update version to 0.1.8
This commit is contained in:
@@ -3,45 +3,43 @@ package nl.astraeus.komp
|
||||
import kotlinx.html.HtmlBlockTag
|
||||
import kotlinx.html.TagConsumer
|
||||
import kotlinx.html.dom.create
|
||||
import org.w3c.dom.HTMLDivElement
|
||||
import org.w3c.dom.HTMLElement
|
||||
import org.w3c.dom.Node
|
||||
import org.w3c.dom.css.CSSStyleDeclaration
|
||||
import kotlin.browser.document
|
||||
|
||||
fun HtmlBlockTag.include(component: Komponent) {
|
||||
component.render(this.consumer as TagConsumer<HTMLElement>)
|
||||
component.element = component.render(this.consumer as TagConsumer<HTMLElement>)
|
||||
}
|
||||
|
||||
abstract class Komponent {
|
||||
var element: Node? = null
|
||||
val declaredStyles: MutableMap<String, CSSStyleDeclaration> = HashMap()
|
||||
|
||||
open fun create(): HTMLElement {
|
||||
val result = render(document.create)
|
||||
|
||||
element = result
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
abstract fun render(consumer: TagConsumer<HTMLElement>): HTMLElement
|
||||
|
||||
open fun declareStyle(className: String, block: CSSStyleDeclaration.() -> Unit) {
|
||||
val style = (document.createElement("div") as HTMLDivElement).style
|
||||
block(style)
|
||||
declaredStyles[className] = style
|
||||
}
|
||||
|
||||
open fun refresh() {
|
||||
if (element == null) {
|
||||
console.log("Unable to refresh, element == null", this)
|
||||
}
|
||||
element?.let { element ->
|
||||
if (logRenderEvent) {
|
||||
console.log("Rendering", this)
|
||||
}
|
||||
|
||||
val oldElement = element
|
||||
val newElement = create()
|
||||
|
||||
element.parentNode?.replaceChild(newElement, element)
|
||||
|
||||
this.element = newElement
|
||||
if (logReplaceEvent) {
|
||||
console.log("Replacing", oldElement, newElement)
|
||||
}
|
||||
element.parentNode?.replaceChild(newElement, oldElement)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,21 +47,24 @@ abstract class Komponent {
|
||||
refresh()
|
||||
}
|
||||
|
||||
@JsName("remove")
|
||||
fun remove() {
|
||||
element?.let {
|
||||
val parent = it.parentElement ?: throw IllegalArgumentException("Element has no parent!?")
|
||||
|
||||
if (logReplaceEvent) {
|
||||
console.log("Remove", it)
|
||||
}
|
||||
|
||||
parent.removeChild(it)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
var logRenderEvent = false
|
||||
var logReplaceEvent = false
|
||||
|
||||
fun removeElement(element: Node) {
|
||||
val parent = element.parentElement ?: throw IllegalArgumentException("Element has no parent!?")
|
||||
|
||||
if (logReplaceEvent) {
|
||||
console.log("Remove", element)
|
||||
}
|
||||
|
||||
parent.removeChild(element)
|
||||
}
|
||||
|
||||
fun create(parent: HTMLElement, component: Komponent, insertAsFirst: Boolean = false) {
|
||||
val element = component.create()
|
||||
|
||||
@@ -72,8 +73,6 @@ abstract class Komponent {
|
||||
} else {
|
||||
parent.appendChild(element)
|
||||
}
|
||||
|
||||
component.element = element
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user