-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathFigureModelJs.kt
121 lines (101 loc) · 4.71 KB
/
FigureModelJs.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) 2024. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.logging.PortableLogging
import org.jetbrains.letsPlot.commons.registration.Registration
import org.jetbrains.letsPlot.core.interact.event.ToolEventDispatcher
import org.jetbrains.letsPlot.core.plot.builder.interact.FigureImplicitInteractionSpecs
import org.jetbrains.letsPlot.core.plot.builder.interact.tools.FigureModelHelper
import org.jetbrains.letsPlot.core.spec.front.SpecOverrideUtil
import org.jetbrains.letsPlot.core.util.sizing.SizingOption
import org.jetbrains.letsPlot.core.util.sizing.SizingPolicy
import org.jetbrains.letsPlot.platf.w3c.jsObject.dynamicFromAnyQ
import org.jetbrains.letsPlot.platf.w3c.jsObject.dynamicObjectToMap
import org.jetbrains.letsPlot.platf.w3c.jsObject.dynamicToAnyQ
import org.w3c.dom.HTMLElement
@OptIn(ExperimentalJsExport::class)
@JsName("FigureModel")
@JsExport
class FigureModelJs internal constructor(
private val processedPlotSpec: Map<String, Any>,
private val wrapperElement: HTMLElement,
private val containerSize: () -> DoubleVector,
private var sizingPolicy: SizingPolicy,
private val messageHandler: MessageHandler,
private var toolEventDispatcher: ToolEventDispatcher,
private var figureRegistration: Registration?,
) {
private var toolEventCallback: ((dynamic) -> Unit)? = null
private var currSpecOverrideList: List<Map<String, Any>> = emptyList()
fun onToolEvent(callback: (dynamic) -> Unit) {
toolEventCallback = callback
toolEventDispatcher.initToolEventCallback { event -> handleToolEvent(event) }
// Make snsure that 'implicit' interaction activated.
deactivateInteractions(origin = ToolEventDispatcher.ORIGIN_FIGURE_IMPLICIT)
activateInteractions(
origin = ToolEventDispatcher.ORIGIN_FIGURE_IMPLICIT,
interactionSpecListJs = dynamicFromAnyQ(FigureImplicitInteractionSpecs.LIST)
)
}
fun updateView(specOverrideJs: dynamic = null, optionsJs: dynamic = null) {
// view options update (just 'sizing' at the moment).
val options: Map<String, Any>? = if (optionsJs != null) {
dynamicObjectToMap(optionsJs)
} else {
null
}
val sizingOptionsUpdate = options?.get(SizingOption.KEY)
if (sizingOptionsUpdate is Map<*, *>) {
sizingPolicy = sizingPolicy.withUpdate(sizingOptionsUpdate)
}
// plot specs update.
val specOverride: Map<String, Any>? = if (specOverrideJs != null) {
dynamicObjectToMap(specOverrideJs)
} else {
null
}
currSpecOverrideList = FigureModelHelper.updateSpecOverrideList(
specOverrideList = currSpecOverrideList,
newSpecOverride = specOverride
)
val currentInteractions = toolEventDispatcher.deactivateAllSilently()
figureRegistration?.dispose()
figureRegistration = null
val plotSpec = SpecOverrideUtil.applySpecOverride(processedPlotSpec, currSpecOverrideList)
// LOG.info { "New sizing policy: $sizingPolicy" }
val newFigureModel = buildPlotFromProcessedSpecsIntern(
plotSpec,
wrapperElement,
containerSize,
sizingPolicy,
messageHandler,
)
if (newFigureModel == null) return // something went wrong.
// Grab properties and discard just created another figure model
figureRegistration = newFigureModel.figureRegistration
toolEventDispatcher = newFigureModel.toolEventDispatcher
toolEventDispatcher.initToolEventCallback { event -> handleToolEvent(event) }
// Re-activate interactions
currentInteractions.forEach { (origin, interactionSpec) ->
toolEventDispatcher.activateInteractions(origin, interactionSpec)
}
}
fun activateInteractions(origin: String, interactionSpecListJs: dynamic) {
val interactionSpecList = dynamicToAnyQ(interactionSpecListJs)
require(interactionSpecList is List<*>) { "Interaction spec list expected but was: $interactionSpecListJs" }
@Suppress("UNCHECKED_CAST")
interactionSpecList as List<Map<String, Any>>
toolEventDispatcher.activateInteractions(origin, interactionSpecList)
}
fun deactivateInteractions(origin: String) {
toolEventDispatcher.deactivateInteractions(origin)
}
private fun handleToolEvent(event: Map<String, Any>) {
toolEventCallback?.invoke(dynamicFromAnyQ(event))
}
companion object {
private val LOG = PortableLogging.logger("FigureModelJs")
}
}