-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathSeriesCollection.js
206 lines (187 loc) · 6.87 KB
/
SeriesCollection.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2024, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
import _ from 'lodash';
import Color from '@/ui/color/Color';
import ColorPalette from '@/ui/color/ColorPalette';
import Collection from './Collection.js';
import PlotSeries from './PlotSeries.js';
/**
* @extends {Collection<SeriesCollectionModelType, SeriesCollectionOptions>}
*/
export default class SeriesCollection extends Collection {
/**
@override
@param {import('./Model').ModelOptions<SeriesCollectionModelType, SeriesCollectionOptions>} options
*/
initialize(options) {
super.initialize(options);
this.modelClass = PlotSeries;
this.plot = options.plot;
this.openmct = options.openmct;
this.palette = options.palette || new ColorPalette();
this.listenTo(this, 'add', this.onSeriesAdd, this);
this.listenTo(this, 'remove', this.onSeriesRemove, this);
this.listenTo(this.plot, 'change:domainObject', this.trackPersistedConfig, this);
const domainObject = this.plot.get('domainObject');
if (domainObject.telemetry) {
this.addTelemetryObject(domainObject);
} else if (!this.openmct.objects.isMissing(domainObject)) {
this.watchTelemetryContainer(domainObject);
}
}
trackPersistedConfig(domainObject) {
domainObject.configuration.series.forEach(function (seriesConfig) {
const series = this.byIdentifier(seriesConfig.identifier);
if (series) {
series.persistedConfig = seriesConfig;
if (!series.persistedConfig.yAxisId) {
return;
}
if (series.get('yAxisId') !== series.persistedConfig.yAxisId) {
series.set('yAxisId', series.persistedConfig.yAxisId);
}
}
}, this);
}
watchTelemetryContainer(domainObject) {
if (domainObject.type === 'telemetry.plot.stacked') {
return;
}
const composition = this.openmct.composition.get(domainObject);
this.listenTo(composition, 'add', this.addTelemetryObject, this);
this.listenTo(composition, 'remove', this.removeTelemetryObject, this);
composition.load();
}
addTelemetryObject(domainObject, index) {
let seriesConfig = this.plot.getPersistedSeriesConfig(domainObject.identifier);
const filters = this.plot.getPersistedFilters(domainObject.identifier);
const plotObject = this.plot.get('domainObject');
if (!seriesConfig) {
seriesConfig = {
identifier: domainObject.identifier
};
if (plotObject.type === 'telemetry.plot.overlay') {
this.openmct.objects.mutate(
plotObject,
'configuration.series[' + this.size() + ']',
seriesConfig
);
seriesConfig = this.plot.getPersistedSeriesConfig(domainObject.identifier);
}
}
// Clone to prevent accidental mutation by ref.
seriesConfig = JSON.parse(JSON.stringify(seriesConfig));
if (!seriesConfig) {
throw 'not possible';
}
this.add(
new PlotSeries({
model: seriesConfig,
domainObject: domainObject,
openmct: this.openmct,
collection: this,
persistedConfig: this.plot.getPersistedSeriesConfig(domainObject.identifier),
filters: filters
})
);
}
removeTelemetryObject(identifier) {
const plotObject = this.plot.get('domainObject');
if (plotObject.type === 'telemetry.plot.overlay') {
const persistedIndex = plotObject.configuration.series.findIndex((s) => {
return _.isEqual(identifier, s.identifier);
});
const configIndex = this.models.findIndex((m) => {
return _.isEqual(m.domainObject.identifier, identifier);
});
/*
when cancelling out of edit mode, the config store and domain object are out of sync
thus it is necessary to check both and remove the models that are no longer in composition
*/
if (persistedIndex === -1) {
this.remove(this.at(configIndex));
} else {
this.remove(this.at(persistedIndex));
// Because this is triggered by a composition change, we have
// to defer mutation of our plot object, otherwise we might
// mutate an outdated version of the plotObject.
setTimeout(
function () {
const newPlotObject = this.plot.get('domainObject');
const cSeries = newPlotObject.configuration.series.slice();
cSeries.splice(persistedIndex, 1);
this.openmct.objects.mutate(newPlotObject, 'configuration.series', cSeries);
}.bind(this)
);
}
}
}
onSeriesAdd(series) {
let seriesColor = series.get('color');
if (seriesColor) {
if (!(seriesColor instanceof Color)) {
seriesColor = Color.fromHexString(seriesColor);
series.set('color', seriesColor);
}
this.palette.remove(seriesColor);
} else {
series.set('color', this.palette.getNextColor());
}
this.listenTo(series, 'change:color', this.updateColorPalette, this);
}
onSeriesRemove(series) {
this.palette.return(series.get('color'));
this.stopListening(series);
series.destroy();
}
updateColorPalette(newColor, oldColor) {
this.palette.remove(newColor);
const seriesWithColor = this.filter(function (series) {
return series.get('color') === newColor;
})[0];
if (!seriesWithColor) {
this.palette.return(oldColor);
}
}
byIdentifier(identifier) {
return this.filter(function (series) {
const seriesIdentifier = series.get('identifier');
return (
seriesIdentifier.namespace === identifier.namespace &&
seriesIdentifier.key === identifier.key
);
})[0];
}
destroy() {
super.destroy();
this.plot = undefined;
this.stopListening();
}
}
/**
@typedef {PlotSeries} SeriesCollectionModelType
*/
/**
@typedef {{
plot: import('./PlotConfigurationModel').default
}} SeriesCollectionOptions
*/