Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Timelist.vue « timelist « plugins « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58af224c398f3434c240b2c625e3c11981262034 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!--
 Open MCT, Copyright (c) 2014-2022, 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.
-->

<template>
<div
    ref="timelistHolder"
    class="c-timelist"
>
    <list-view
        :items="planActivities"
        :header-items="headerItems"
        :default-sort="defaultSort"
        class="sticky"
    />
</div>
</template>

<script>
import {getValidatedData} from "../plan/util";
import ListView from '../../ui/components/List/ListView.vue';
import {getPreciseDuration} from "../../utils/duration";
import ticker from 'utils/clock/Ticker';
import {SORT_ORDER_OPTIONS} from "./constants";

import moment from "moment";
import { v4 as uuid } from 'uuid';

const SCROLL_TIMEOUT = 10000;
const ROW_HEIGHT = 30;
const TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss:SSS';
const headerItems = [
    {
        defaultDirection: true,
        isSortable: true,
        property: 'start',
        name: 'Start Time',
        format: function (value, object) {
            return `${moment(value).format(TIME_FORMAT)}Z`;
        }
    }, {
        defaultDirection: true,
        isSortable: true,
        property: 'end',
        name: 'End Time',
        format: function (value, object) {
            return `${moment(value).format(TIME_FORMAT)}Z`;
        }
    }, {
        defaultDirection: false,
        property: 'duration',
        name: 'Time To/From',
        format: function (value) {
            let result;
            if (value < 0) {
                result = `-${getPreciseDuration(Math.abs(value))}`;
            } else if (value > 0) {
                result = `+${getPreciseDuration(value)}`;
            } else {
                result = 'Now';
            }

            return result;
        }
    }, {
        defaultDirection: true,
        property: 'name',
        name: 'Activity'
    }
];

const defaultSort = {
    property: 'start',
    defaultDirection: true
};

export default {
    components: {
        ListView
    },
    inject: ['openmct', 'domainObject', 'path', 'composition'],
    data() {
        this.planObjects = [];

        return {
            viewBounds: undefined,
            height: 0,
            planActivities: [],
            headerItems: headerItems,
            defaultSort: defaultSort
        };
    },
    mounted() {
        this.isEditing = this.openmct.editor.isEditing();
        this.timestamp = Date.now();
        this.getPlanDataAndSetConfig(this.domainObject);

        this.unlisten = this.openmct.objects.observe(this.domainObject, 'selectFile', this.planFileUpdated);
        this.unlistenConfig = this.openmct.objects.observe(this.domainObject, 'configuration', this.setViewFromConfig);
        this.removeStatusListener = this.openmct.status.observe(this.domainObject.identifier, this.setStatus);
        this.status = this.openmct.status.get(this.domainObject.identifier);
        this.unlistenTicker = ticker.listen(this.clearPreviousActivities);
        this.openmct.editor.on('isEditing', this.setEditState);

        this.deferAutoScroll = _.debounce(this.deferAutoScroll, 500);
        this.$el.parentElement.addEventListener('scroll', this.deferAutoScroll, true);

        if (this.composition) {
            this.composition.on('add', this.addToComposition);
            this.composition.on('remove', this.removeItem);
            this.composition.load();
        }
    },
    beforeDestroy() {
        if (this.unlisten) {
            this.unlisten();
        }

        if (this.unlistenConfig) {
            this.unlistenConfig();
        }

        if (this.unlistenTicker) {
            this.unlistenTicker();
        }

        if (this.removeStatusListener) {
            this.removeStatusListener();
        }

        this.openmct.editor.off('isEditing', this.setEditState);

        this.$el.parentElement.removeEventListener('scroll', this.deferAutoScroll, true);
        if (this.clearAutoScrollDisabledTimer) {
            clearTimeout(this.clearAutoScrollDisabledTimer);
        }

        if (this.composition) {
            this.composition.off('add', this.addToComposition);
            this.composition.off('remove', this.removeItem);
        }
    },
    methods: {
        planFileUpdated(selectFile) {
            this.getPlanData({
                selectFile,
                sourceMap: this.domainObject.sourceMap
            });
        },
        getPlanDataAndSetConfig(mutatedObject) {
            this.getPlanData(mutatedObject);
            this.setViewFromConfig(mutatedObject.configuration);
        },
        setViewFromConfig(configuration) {
            if (this.isEditing) {
                this.filterValue = configuration.filter;
                this.hideAll = false;
                this.showAll = true;
                this.listActivities();
            } else {
                this.filterValue = configuration.filter;
                this.setSort();
                this.setViewBounds();
                this.listActivities();
            }
        },
        addItem(domainObject) {
            this.planObjects = [domainObject];
            this.resetPlanData();
            if (domainObject.type === 'plan') {
                this.getPlanDataAndSetConfig({
                    ...this.domainObject,
                    selectFile: domainObject.selectFile
                });
            }
        },
        addToComposition(telemetryObject) {
            if (this.planObjects.length > 0) {
                this.confirmReplacePlan(telemetryObject);
            } else {
                this.addItem(telemetryObject);
            }
        },
        confirmReplacePlan(telemetryObject) {
            const dialog = this.openmct.overlays.dialog({
                iconClass: 'alert',
                message: 'This action will replace the current plan. Do you want to continue?',
                buttons: [
                    {
                        label: 'Ok',
                        emphasis: true,
                        callback: () => {
                            const oldTelemetryObject = this.planObjects[0];
                            this.removeFromComposition(oldTelemetryObject);
                            this.addItem(telemetryObject);
                            dialog.dismiss();
                        }
                    },
                    {
                        label: 'Cancel',
                        callback: () => {
                            this.removeFromComposition(telemetryObject);
                            dialog.dismiss();
                        }
                    }
                ]
            });
        },
        removeFromComposition(telemetryObject) {
            this.composition.remove(telemetryObject);
        },
        removeItem() {
            this.planObjects = [];
            this.resetPlanData();
        },
        resetPlanData() {
            this.planData = {};
        },
        getPlanData(domainObject) {
            this.planData = getValidatedData(domainObject);
        },
        setViewBounds() {
            const pastEventsIndex = this.domainObject.configuration.pastEventsIndex;
            const currentEventsIndex = this.domainObject.configuration.currentEventsIndex;
            const futureEventsIndex = this.domainObject.configuration.futureEventsIndex;
            const pastEventsDuration = this.domainObject.configuration.pastEventsDuration;
            const pastEventsDurationIndex = this.domainObject.configuration.pastEventsDurationIndex;
            const futureEventsDuration = this.domainObject.configuration.futureEventsDuration;
            const futureEventsDurationIndex = this.domainObject.configuration.futureEventsDurationIndex;

            if (pastEventsIndex === 0 && futureEventsIndex === 0 && currentEventsIndex === 0) {
                //don't show all events
                this.showAll = false;
                this.viewBounds = undefined;
                this.hideAll = true;

                return;
            }

            this.hideAll = false;

            if (pastEventsIndex === 1 && futureEventsIndex === 1 && currentEventsIndex === 1) {
                //show all events
                this.showAll = true;
                this.viewBounds = undefined;

                return;
            }

            this.showAll = false;

            this.viewBounds = {};

            this.noCurrent = currentEventsIndex === 0;

            if (pastEventsIndex !== 1) {
                const pastDurationInMS = this.getDurationInMilliSeconds(pastEventsDuration, pastEventsDurationIndex);
                this.viewBounds.pastEnd = (timestamp) => {
                    if (pastEventsIndex === 2) {
                        return timestamp - pastDurationInMS;
                    } else if (pastEventsIndex === 0) {
                        return timestamp + 1;
                    }
                };
            }

            if (futureEventsIndex !== 1) {
                const futureDurationInMS = this.getDurationInMilliSeconds(futureEventsDuration, futureEventsDurationIndex);
                this.viewBounds.futureStart = (timestamp) => {
                    if (futureEventsIndex === 2) {
                        return timestamp + futureDurationInMS;
                    } else if (futureEventsIndex === 0) {
                        return 0;
                    }
                };
            }
        },
        getDurationInMilliSeconds(duration, durationIndex) {
            if (duration > 0) {
                if (durationIndex === 0) {
                    return duration * 1000;
                } else if (durationIndex === 1) {
                    return duration * 60 * 1000;
                } else if (durationIndex === 2) {
                    return duration * 60 * 60 * 1000;
                }
            }
        },
        listActivities() {
            let groups = Object.keys(this.planData);
            let activities = [];

            groups.forEach((key) => {
                activities = activities.concat(this.planData[key]);
            });
            activities = activities.filter(this.filterActivities);
            activities = this.applyStyles(activities);
            this.setScrollTop();
            // sort by start time
            this.planActivities = activities.sort(this.sortByStartTime);
        },
        clearPreviousActivities(time) {
            if (time instanceof Date) {
                this.timestamp = time.getTime();
            } else {
                this.timestamp = time;
            }

            this.listActivities();
        },
        filterActivities(activity, index) {

            const hasFilterMatch = this.filterByName(activity.name);

            if (hasFilterMatch === false || this.hideAll === true) {
                return false;
            }

            if (this.showAll === true) {
                return true;
            }

            //current event or future start event or past end event
            const isCurrent = (this.noCurrent === false && this.timestamp >= activity.start && this.timestamp <= activity.end);
            const isPast = (this.timestamp > activity.end && (this.viewBounds.pastEnd === undefined || activity.end >= this.viewBounds.pastEnd(this.timestamp)));
            const isFuture = (this.timestamp < activity.start && (this.viewBounds.futureStart === undefined || activity.start <= this.viewBounds.futureStart(this.timestamp)));

            return isCurrent || isPast || isFuture;
        },
        filterByName(name) {
            const filters = this.filterValue.split(',');

            return filters.some((search => {
                const normalized = search.trim().toLowerCase();
                const regex = new RegExp(normalized);

                return regex.test(name.toLowerCase());
            }));
        },
        applyStyles(activities) {
            let firstCurrentActivityIndex = -1;
            let currentActivitiesCount = 0;
            const styledActivities = activities.map((activity, index) => {
                if (this.timestamp >= activity.start && this.timestamp <= activity.end) {
                    activity.cssClass = '--is-current';
                    if (firstCurrentActivityIndex < 0) {
                        firstCurrentActivityIndex = index;
                    }

                    currentActivitiesCount = currentActivitiesCount + 1;
                } else if (this.timestamp < activity.start) {
                    activity.cssClass = '--is-future';
                } else {
                    activity.cssClass = '--is-past';
                }

                if (!activity.key) {
                    activity.key = uuid();
                }

                activity.duration = activity.start - this.timestamp;

                return activity;
            });

            this.firstCurrentActivityIndex = firstCurrentActivityIndex;
            this.currentActivitiesCount = currentActivitiesCount;

            return styledActivities;
        },
        canAutoScroll() {
            //this distinguishes between programmatic vs user-triggered scroll events
            this.autoScrolled = (this.dontAutoScroll !== true);

            return this.autoScrolled;
        },
        resetScroll() {
            if (this.canAutoScroll() === false) {
                return;
            }

            this.firstCurrentActivityIndex = -1;
            this.currentActivitiesCount = 0;
            this.$el.parentElement.scrollTo({top: 0});
            this.autoScrolled = false;
        },
        setScrollTop() {
            //scroll to somewhere mid-way of the current activities
            if (this.firstCurrentActivityIndex > -1) {
                if (this.canAutoScroll() === false) {
                    return;
                }

                const scrollOffset = this.currentActivitiesCount > 0 ? Math.floor(this.currentActivitiesCount / 2) : 0;
                this.$el.parentElement.scrollTo({
                    top: ROW_HEIGHT * (this.firstCurrentActivityIndex + scrollOffset),
                    behavior: "smooth"
                });
                this.autoScrolled = false;
            } else {
                this.resetScroll();
            }
        },
        deferAutoScroll() {
            //if this is not a user-triggered event, don't defer auto scrolling
            if (this.autoScrolled) {
                this.autoScrolled = false;

                return;
            }

            this.dontAutoScroll = true;
            const self = this;
            if (this.clearAutoScrollDisabledTimer) {
                clearTimeout(this.clearAutoScrollDisabledTimer);
            }

            this.clearAutoScrollDisabledTimer = setTimeout(() => {
                self.dontAutoScroll = false;
                self.setScrollTop();
            }, SCROLL_TIMEOUT);
        },
        setSort() {
            const sortOrder = SORT_ORDER_OPTIONS[this.domainObject.configuration.sortOrderIndex];
            const property = sortOrder.property;
            const direction = sortOrder.direction.toLowerCase() === 'asc';
            this.defaultSort = {
                property,
                defaultDirection: direction
            };
        },
        sortByStartTime(a, b) {
            const numA = parseInt(a.start, 10);
            const numB = parseInt(b.start, 10);

            return numA - numB;
        },
        setStatus(status) {
            this.status = status;
        },
        setEditState(isEditing) {
            this.isEditing = isEditing;
            this.setViewFromConfig(this.domainObject.configuration);
        }
    }
};
</script>