diff --git a/build.gradle b/build.gradle index baaf0a0..3bc9e43 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'nl.astraeus' -version '0.1.7' +version '0.1.9-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'kotlin-dce-js' @@ -42,7 +42,6 @@ compile 'org.jetbrains.kotlinx:kotlinx-html-js:0.6.10' } -/* uploadArchives { //println 'user: ' + nexusUsername repositories { @@ -56,7 +55,6 @@ } } } -*/ compileKotlin2Js { kotlinOptions.sourceMap = true diff --git a/build.gradle b/build.gradle index baaf0a0..3bc9e43 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'nl.astraeus' -version '0.1.7' +version '0.1.9-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'kotlin-dce-js' @@ -42,7 +42,6 @@ compile 'org.jetbrains.kotlinx:kotlinx-html-js:0.6.10' } -/* uploadArchives { //println 'user: ' + nexusUsername repositories { @@ -56,7 +55,6 @@ } } } -*/ compileKotlin2Js { kotlinOptions.sourceMap = true diff --git a/komp.iml b/komp.iml index 140cd6f..dc22d6a 100644 --- a/komp.iml +++ b/komp.iml @@ -1,5 +1,5 @@ - + diff --git a/build.gradle b/build.gradle index baaf0a0..3bc9e43 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'nl.astraeus' -version '0.1.7' +version '0.1.9-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'kotlin-dce-js' @@ -42,7 +42,6 @@ compile 'org.jetbrains.kotlinx:kotlinx-html-js:0.6.10' } -/* uploadArchives { //println 'user: ' + nexusUsername repositories { @@ -56,7 +55,6 @@ } } } -*/ compileKotlin2Js { kotlinOptions.sourceMap = true diff --git a/komp.iml b/komp.iml index 140cd6f..dc22d6a 100644 --- a/komp.iml +++ b/komp.iml @@ -1,5 +1,5 @@ - + diff --git a/src/main/kotlin/nl/astraeus/komp/Komponent.kt b/src/main/kotlin/nl/astraeus/komp/Komponent.kt index 7fc127d..e5d7564 100644 --- a/src/main/kotlin/nl/astraeus/komp/Komponent.kt +++ b/src/main/kotlin/nl/astraeus/komp/Komponent.kt @@ -3,45 +3,43 @@ 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) + component.element = component.render(this.consumer as TagConsumer) } abstract class Komponent { var element: Node? = null - val declaredStyles: MutableMap = HashMap() open fun create(): HTMLElement { val result = render(document.create) + element = result + return result } abstract fun render(consumer: TagConsumer): 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 @@ 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 @@ } else { parent.appendChild(element) } - - component.element = element } } }