- Keeping it simple, just html element replace

- Updated Kotlin version
This commit is contained in:
2019-11-15 19:06:02 +01:00
parent 2b1f3fa5a4
commit 29f570cfc1
7 changed files with 46 additions and 608 deletions

View File

@@ -1,6 +1,8 @@
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
@@ -8,10 +10,7 @@ import org.w3c.dom.css.CSSStyleDeclaration
import kotlin.browser.document
fun HtmlBlockTag.include(component: Komponent) {
val consumer = this.consumer
if (consumer is KompConsumer) {
consumer.appendKomponent(component)
}
component.render(this.consumer as TagConsumer<HTMLElement>)
}
enum class UpdateStrategy {
@@ -21,16 +20,15 @@ enum class UpdateStrategy {
abstract class Komponent {
var element: Node? = null
var kompElement: KompElement? = null
val declaredStyles: MutableMap<String, CSSStyleDeclaration> = HashMap()
open fun create(): KompElement {
val result = render(KompConsumer(this))
open fun create(): HTMLElement {
val result = render(document.create)
return result
}
abstract fun render(consumer: KompConsumer): KompElement
abstract fun render(consumer: TagConsumer<HTMLElement>): HTMLElement
open fun declareStyle(className: String, block: CSSStyleDeclaration.() -> Unit) {
val style = (document.createElement("div") as HTMLDivElement).style
@@ -46,20 +44,9 @@ abstract class Komponent {
val newElement = create()
val replacedElement = if (updateStrategy == UpdateStrategy.REPLACE) {
//val replacedElement = replaceNode(newElement, element)
element.parentNode?.replaceChild(newElement, element)
replaceNode(newElement, element)
} else if (kompElement != null) {
kompElement?.let {
DomDiffer.replaceDiff(it, newElement, element)
}
} else {
newElement.create()
}
kompElement = newElement
this.element = replacedElement
this.element = newElement
}
}
@@ -67,22 +54,6 @@ abstract class Komponent {
refresh()
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class.js != other::class.js) return false
other as Komponent
if (kompElement != other.kompElement) return false
return true
}
override fun hashCode(): Int {
var result = kompElement?.hashCode() ?: 0
return result
}
companion object {
var logRenderEvent = false
@@ -90,25 +61,6 @@ abstract class Komponent {
var logEquals = false
var updateStrategy = UpdateStrategy.DOM_DIFF
fun replaceNode(newKomponent: KompElement, oldElement: Node): Node {
val newElement = newKomponent.create()
if (logReplaceEvent) {
console.log("Replace", oldElement, newElement)
}
val parent = oldElement.parentElement ?: throw IllegalStateException("oldElement has no parent! $oldElement")
parent.replaceChild(newElement, oldElement)
newKomponent.komponent?.also {
it.kompElement = newKomponent
it.element = newElement
}
return newElement
}
fun removeElement(element: Node) {
val parent = element.parentElement ?: throw IllegalArgumentException("Element has no parent!?")
@@ -119,28 +71,16 @@ abstract class Komponent {
parent.removeChild(element)
}
fun appendElement(element: Node, kompElement: KompElement) {
val newElement = kompElement.create()
if (logReplaceEvent) {
console.log("Append", newElement)
}
element.appendChild(newElement)
}
fun create(parent: HTMLElement, component: Komponent, insertAsFirst: Boolean = false) {
val element = component.create()
component.kompElement = component.create()
val element = component.kompElement?.create()
if (element != null) {
if (insertAsFirst && parent.childElementCount > 0) {
parent.insertBefore(element, parent.firstChild)
} else {
parent.appendChild(element)
}
component.element = element
if (insertAsFirst && parent.childElementCount > 0) {
parent.insertBefore(element, parent.firstChild)
} else {
parent.appendChild(element)
}
component.element = element
}
}
}