Jacob Otto
04/21/2021, 8:10 AMJan Weidenhaupt
04/21/2021, 9:47 AMHTMLElement
e.g. to their divIcon
function. Did you try something similar to this?
// simulate external leaflet API
object L {
fun <E: HTMLElement> divIcon(element: E) {
// here leaflet can do everything with the given HTMLElement
// e.g. appending text to the given HTMLElement
element.appendChild(document.createTextNode("Hello World!"))
}
}
// small wrapper Component to support fritz2 HTML DSL
fun RenderContext.divIcon(content: RenderContext.() -> Unit) {
L.divIcon(div {
content()
}.domNode)
}
fun main() {
// calling the wrapped leaflet component
// and using a fritz2 component inside
render {
divIcon {
icon { fromTheme { fritz2 } }
}
}
}
In fritz2 all our standard HTML elements e.g. div()
have the property domNode
which gives you the corresponding native HTMLElement
which external libraries like leaflet can use.
I hope I get your right and my approach work for you. If not please let me know 😉Jacob Otto
04/21/2021, 11:49 AMJacob Otto
04/22/2021, 10:37 AMfun RenderContext.test(){
icon { fromTheme { fritz2 } }
}
to a HTMLElement, so that I can use it in the leaflet context like this:
L.marker(...).setIcon( L.divIcon( test() ) )
I created a kotlin data class for the leaflet DivIcon to be able to easily handle all the icon options dynamically in my webapp and then I create a JS object out of it to display it with leaflet. Currently my data class has a string containing the html code and this would be nice to change to HTMLElement and use a fritz2 DSL there. I dont know if I am making it overcomplicated, but that is my current setup to deal with the kotlin <-> JS handover.Jan Weidenhaupt
04/23/2021, 12:38 PMfun RenderContext.stringify(content: RenderContext.() -> Unit): String {
return Div(job = this.job).apply {
content()
}.domNode.innerHTML
}
fun main() {
render {
pre {
+stringify {
icon { fromTheme { fritz2 } }
}
}
}
}
You need to create a wrapper function (like test()
). You can then use the fritz2 components functions inside the content
parameter. Internally a Div()
gets created to get access to the underlying domNode
and innerHTML
which presents the HTML generated by the fritz2 component (here icon()
).
I hope that helps 😉Jacob Otto
04/27/2021, 4:57 PMfun stringify(content: RenderContext.() -> Unit): String {
return Div(job = Job(parent = null)).apply {
content()
}.domNode.innerHTML
}
This allows me to use the string everywhere, also outside the RenderContext. 🙂
Does that make sense or is it not ideal to define the job like this without parent?Jan Weidenhaupt
04/27/2021, 5:13 PMJob()
everytime you call stringify()
function. Instead of a String
you can also use the domNode
directly. This should be more performant ;)Jacob Otto
04/27/2021, 5:22 PM