visionforge/ui/ring/src/main/kotlin/space.kscience.visionforge.ring/ringThreeControls.kt

134 lines
4.1 KiB
Kotlin
Raw Normal View History

2021-05-09 15:49:18 +03:00
package space.kscience.visionforge.ring
2021-06-14 16:42:02 +03:00
import kotlinx.css.*
2021-05-09 15:49:18 +03:00
import kotlinx.css.properties.border
import kotlinx.html.js.onClickFunction
import org.w3c.dom.events.Event
import org.w3c.files.Blob
import org.w3c.files.BlobPropertyBag
import react.*
2021-06-04 20:01:50 +03:00
import react.dom.attrs
2021-05-09 15:49:18 +03:00
import react.dom.button
import ringui.island.ringIsland
import ringui.tabs.ringSmartTabs
import ringui.tabs.ringTab
import space.kscience.dataforge.meta.withDefault
import space.kscience.dataforge.names.Name
import space.kscience.visionforge.Vision
2021-06-06 20:57:39 +03:00
import space.kscience.visionforge.encodeToString
2021-05-09 15:49:18 +03:00
import space.kscience.visionforge.react.flexColumn
import space.kscience.visionforge.react.flexRow
import space.kscience.visionforge.react.propertyEditor
2021-06-13 21:24:02 +03:00
import space.kscience.visionforge.react.visionTree
2021-05-09 15:49:18 +03:00
import space.kscience.visionforge.solid.specifications.Canvas3DOptions
import styled.css
2021-06-14 16:42:02 +03:00
import styled.styledDiv
2021-05-09 15:49:18 +03:00
internal fun saveData(event: Event, fileName: String, mimeType: String = "text/plain", dataBuilder: () -> String) {
event.stopPropagation();
event.preventDefault();
val fileSaver = kotlinext.js.require("file-saver")
val blob = Blob(arrayOf(dataBuilder()), BlobPropertyBag("$mimeType;charset=utf-8"))
fileSaver.saveAs(blob, fileName)
}
2021-06-06 20:57:39 +03:00
internal fun RBuilder.canvasControls(options: Canvas3DOptions, vision: Vision?): ReactElement {
2021-05-09 15:49:18 +03:00
return child(CanvasControls) {
attrs {
2021-06-06 20:57:39 +03:00
this.options = options
this.vision = vision
2021-05-09 15:49:18 +03:00
}
}
}
internal external interface CanvasControlsProps : RProps {
2021-06-06 20:57:39 +03:00
public var options: Canvas3DOptions
public var vision: Vision?
2021-05-09 15:49:18 +03:00
}
internal val CanvasControls: FunctionalComponent<CanvasControlsProps> = functionalComponent("CanvasControls") { props ->
flexColumn {
flexRow {
css {
border(1.px, BorderStyle.solid, Color.blue)
padding(4.px)
}
2021-06-06 20:57:39 +03:00
props.vision?.let { vision ->
button {
+"Export"
attrs {
onClickFunction = {
val json = vision.encodeToString()
2021-05-09 15:49:18 +03:00
saveData(it, "object.json", "text/json") {
json
}
2021-06-06 20:57:39 +03:00
2021-05-09 15:49:18 +03:00
}
}
}
}
}
propertyEditor(
2021-06-06 20:57:39 +03:00
ownProperties = props.options,
allProperties = props.options.withDefault(Canvas3DOptions.descriptor.defaultMeta),
2021-05-09 15:49:18 +03:00
descriptor = Canvas3DOptions.descriptor,
expanded = false
)
}
}
public external interface ThreeControlsProps : RProps {
2021-06-06 20:57:39 +03:00
public var canvasOptions: Canvas3DOptions
public var vision: Vision?
2021-05-09 15:49:18 +03:00
public var selected: Name?
2021-06-13 21:24:02 +03:00
public var onSelect: (Name?) -> Unit
2021-06-14 16:42:02 +03:00
public var additionalTabs: Map<String, RBuilder.() -> Unit>
2021-05-09 15:49:18 +03:00
}
@JsExport
public val ThreeControls: FunctionalComponent<ThreeControlsProps> = functionalComponent { props ->
2021-06-14 16:42:02 +03:00
ringSmartTabs("Tree") {
2021-06-13 21:24:02 +03:00
props.vision?.let {
ringTab("Tree") {
2021-06-14 16:42:02 +03:00
styledDiv {
css {
height = 100.pct
overflowY = Overflow.auto
2021-06-08 18:45:03 +03:00
}
2021-06-14 16:42:02 +03:00
ringIsland("Vision tree") {
visionTree(it, props.selected, props.onSelect)
2021-06-08 18:45:03 +03:00
}
2021-05-09 15:49:18 +03:00
}
}
}
2021-06-14 16:42:02 +03:00
ringTab("Settings") {
ringIsland("Canvas configuration") {
canvasControls(props.canvasOptions, props.vision)
}
}
props.additionalTabs.forEach { (name, handler) ->
ringTab(name){
handler()
}
}
2021-05-09 15:49:18 +03:00
}
}
public fun RBuilder.ringThreeControls(
2021-06-06 20:57:39 +03:00
canvasOptions: Canvas3DOptions,
vision: Vision?,
2021-05-09 15:49:18 +03:00
selected: Name?,
2021-06-13 21:24:02 +03:00
onSelect: (Name?) -> Unit = {},
2021-06-14 16:42:02 +03:00
additionalTabs: Map<String, RBuilder.() -> Unit>? = null
2021-05-09 15:49:18 +03:00
): ReactElement = child(ThreeControls) {
attrs {
2021-06-06 20:57:39 +03:00
this.canvasOptions = canvasOptions
this.vision = vision
2021-05-09 15:49:18 +03:00
this.selected = selected
this.onSelect = onSelect
2021-06-14 16:42:02 +03:00
this.additionalTabs = additionalTabs?: emptyMap()
2021-05-09 15:49:18 +03:00
}
}