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

153 lines
4.8 KiB
Kotlin
Raw Normal View History

2021-05-09 15:49:18 +03:00
package space.kscience.visionforge.ring
import kotlinx.css.*
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 react.dom.h2
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.dataforge.names.isEmpty
import space.kscience.visionforge.Vision
import space.kscience.visionforge.VisionGroup
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.objectTree
import space.kscience.visionforge.react.propertyEditor
import space.kscience.visionforge.solid.specifications.Canvas3DOptions
import styled.css
import styled.styledDiv
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?
public var onSelect: (Name) -> Unit
}
@JsExport
public val ThreeControls: FunctionalComponent<ThreeControlsProps> = functionalComponent { props ->
2021-06-08 18:45:03 +03:00
ringSmartTabs(if (props.selected != null) "Properties" else "Tree") {
2021-05-09 15:49:18 +03:00
ringTab("Canvas") {
ringIsland("Canvas configuration") {
2021-06-06 20:57:39 +03:00
canvasControls(props.canvasOptions, props.vision)
2021-05-09 15:49:18 +03:00
}
}
ringTab("Tree") {
2021-06-08 18:45:03 +03:00
flexColumn {
2021-05-09 15:49:18 +03:00
css {
border(1.px, BorderStyle.solid, Color.lightGray)
padding(10.px)
2021-06-08 18:45:03 +03:00
flexGrow = 1.0
flexWrap = FlexWrap.wrap
2021-05-09 15:49:18 +03:00
}
h2 { +"Object tree" }
styledDiv {
css {
2021-06-08 18:45:03 +03:00
overflowY = Overflow.auto
flexGrow = 1.0
2021-05-09 15:49:18 +03:00
}
2021-06-06 20:57:39 +03:00
props.vision?.let {
2021-05-09 15:49:18 +03:00
objectTree(it, props.selected, props.onSelect)
}
}
}
}
2021-06-08 18:45:03 +03:00
if (props.selected != null) {
ringTab("Properties") {
props.selected.let { selected ->
val selectedObject: Vision? = when {
selected == null -> null
selected.isEmpty() -> props.vision
else -> (props.vision as? VisionGroup)?.get(selected)
}
if (selectedObject != null) {
ringPropertyEditor(selectedObject, key = selected)
}
2021-05-09 15:49:18 +03:00
}
}
}
props.children()
}
}
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?,
onSelect: (Name) -> Unit = {},
builder: RBuilder.() -> Unit = {},
): 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
}
builder()
}