-
Notifications
You must be signed in to change notification settings - Fork 167
/
representation-element.ts
191 lines (158 loc) · 4.82 KB
/
representation-element.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* @file Representation Element
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import { Signal } from 'signals'
import { Color } from 'three'
import Stage from '../stage/stage'
import Representation from '../representation/representation'
import Component from './component'
import Element, { ElementDefaultParameters, ElementSignals } from './element'
export const RepresentationElementDefaultParameters = Object.assign({
visible: true
}, ElementDefaultParameters)
export type RepresentationElementParameters = typeof RepresentationElementDefaultParameters
export interface RepresentationElementSignals extends ElementSignals {
visibilityChanged: Signal // on visibility change
parametersChanged: Signal // on parameters change
}
/**
* Element wrapping a {@link Representation} object
*/
class RepresentationElement extends Element {
signals: RepresentationElementSignals
parameters: RepresentationElementParameters
get defaultParameters() { return RepresentationElementDefaultParameters }
repr: Representation
/**
* Create representation component
* @param {Stage} stage - stage object the component belongs to
* @param {Representation} repr - representation object to wrap
* @param {RepresentationParameters} [params] - component parameters
* @param {Component} [parent] - parent component
*/
constructor (stage: Stage, repr: Representation, params: Partial<RepresentationElementParameters> = {}, readonly parent: Component) {
super(stage, Object.assign({ name: repr.type }, params))
this.signals = Object.assign({
visibilityChanged: new Signal(),
parametersChanged: new Signal()
}, this.signals)
this.setRepresentation(repr)
}
get visible () { return this.parameters.visible }
/**
* Component type
* @type {String}
*/
get type () { return 'representation' }
getType () {
return this.repr.type
}
setRepresentation (repr: Representation) {
this._disposeRepresentation()
this.repr = repr
// this.name = repr.type;
this.stage.tasks.listen(this.repr.tasks)
this.updateVisibility()
}
_disposeRepresentation () {
if (this.repr) {
this.stage.tasks.unlisten(this.repr.tasks)
this.repr.dispose()
}
}
dispose () {
if (this.parent && this.parent.hasRepresentation(this)) {
this.parent.removeRepresentation(this)
} else {
this._disposeRepresentation()
this.signals.disposed.dispatch()
}
}
/**
* Set the visibility of the component, takes parent visibility into account
* @param {Boolean} value - visibility flag
* @return {RepresentationElement} this object
*/
setVisibility (value: boolean) {
this.parameters.visible = value
this.updateVisibility()
this.signals.visibilityChanged.dispatch(this.parameters.visible)
return this
}
getVisibility () {
if (this.parent) {
return this.parent.parameters.visible && this.parameters.visible
} else {
return this.parameters.visible
}
}
/**
* Toggle visibility of the component, takes parent visibility into account
* @return {RepresentationElement} this object
*/
toggleVisibility () {
return this.setVisibility(!this.parameters.visible)
}
updateVisibility () {
this.repr.setVisibility(this.getVisibility())
}
/**
* Set selection
* @param {Object} what - flags indicating what attributes to update
* @param {Boolean} what.position - update position attribute
* @param {Boolean} what.color - update color attribute
* @param {Boolean} what.radius - update radius attribute
* @return {RepresentationElement} this object
*/
update (what: any) { // TODO
(this.repr as any).update(what) // TODO
return this
}
build (params?: any) { // TODO
this.repr.build(params)
return this
}
/**
* Set selection
* @param {String} string - selection string
* @return {RepresentationElement} this object
*/
setSelection (string: string) {
const repr: any = this.repr // TODO
if (repr.setSelection) {
repr.setSelection(string)
}
return this
}
/**
* Set representation parameters
* @param {RepresentationParameters} params - parameter object
* @return {RepresentationElement} this object
*/
setParameters (params: any) { // TODO
this.repr.setParameters(params)
this.signals.parametersChanged.dispatch(
this.repr.getParameters()
)
return this
}
/**
* Get representation parameters
* @return {RepresentationParameters} parameter object
*/
getParameters () {
return this.repr.getParameters()
}
/**
* Set color
* @param {String|Color|Hex} value - color value
* @return {RepresentationElement} this object
*/
setColor (value: string|number|Color) {
this.repr.setColor(value)
return this
}
}
export default RepresentationElement