-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathJsFrontendUtil.kt
43 lines (36 loc) · 1.45 KB
/
JsFrontendUtil.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Copyright (c) 2021. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package org.jetbrains.letsPlot.frontend
import jetbrains.datalore.plot.PlotHtmlHelper
import kotlinx.browser.document
import kotlinx.dom.createElement
import org.jetbrains.letsPlot.Figure
import org.jetbrains.letsPlot.intern.toSpec
import org.w3c.dom.*
object JsFrontendUtil {
fun createPlotDiv(plot: Figure): HTMLDivElement {
val spec = plot.toSpec()
val html = PlotHtmlHelper.getStaticDisplayHtmlForRawSpec(spec)
val divElement = document.createElement("div")
setInnerHtml(divElement, html)
return divElement as HTMLDivElement
}
fun setInnerHtml(element: Element, html: String) {
element.innerHTML = html
// Force browser to execute scripts in the "html".
val allScripts = element.querySelectorAll("script")
for (i in 0 until allScripts.length) {
val oldScript = allScripts.get(i) as Element
val newScript = document.createElement("script") {
this is HTMLScriptElement
oldScript.attributes.asList().forEach { attr ->
this.setAttribute(attr.name, attr.value)
}
this.appendChild(document.createTextNode(oldScript.innerHTML))
}
(oldScript.parentNode as Element).replaceChild(newScript, oldScript)
}
}
}