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

TableRowCollection.js « collections « telemetryTable « plugins « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0dd2d911582c5bc38a733235cae97d2e6915d22 (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
/*****************************************************************************
 * 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.
 *****************************************************************************/

define(
    [
        'lodash',
        'EventEmitter'
    ],
    function (
        _,
        EventEmitter
    ) {
        /**
         * @constructor
         */
        class TableRowCollection extends EventEmitter {
            constructor() {
                super();

                this.rows = [];
                this.columnFilters = {};
                this.addRows = this.addRows.bind(this);
                this.removeRowsByObject = this.removeRowsByObject.bind(this);
                this.removeRowsByData = this.removeRowsByData.bind(this);

                this.clear = this.clear.bind(this);
            }

            removeRowsByObject(keyString) {
                let removed = [];

                this.rows = this.rows.filter((row) => {
                    if (row.objectKeyString === keyString) {
                        removed.push(row);

                        return false;
                    } else {
                        return true;
                    }
                });

                this.emit('remove', removed);
            }

            addRows(rows, type = 'add') {
                if (this.sortOptions === undefined) {
                    throw 'Please specify sort options';
                }

                let isFilterTriggeredReset = type === 'filter';
                let anyActiveFilters = Object.keys(this.columnFilters).length > 0;
                let rowsToAdd = !anyActiveFilters ? rows : rows.filter(this.matchesFilters, this);

                // if type is filter, then it's a reset of all rows,
                // need to wipe current rows
                if (isFilterTriggeredReset) {
                    this.rows = [];
                }

                this.sortAndMergeRows(rowsToAdd);

                // we emit filter no matter what to trigger
                // an update of visible rows
                if (rowsToAdd.length > 0 || isFilterTriggeredReset) {
                    this.emit(type, rowsToAdd);
                }
            }

            sortAndMergeRows(rows) {
                const sortedRowsToAdd = this.sortCollection(rows);

                if (this.rows.length === 0) {
                    this.rows = sortedRowsToAdd;

                    return;
                }

                const firstIncomingRow = sortedRowsToAdd[0];
                const lastIncomingRow = sortedRowsToAdd[sortedRowsToAdd.length - 1];
                const firstExistingRow = this.rows[0];
                const lastExistingRow = this.rows[this.rows.length - 1];

                if (this.firstRowInSortOrder(lastIncomingRow, firstExistingRow)
                    === lastIncomingRow
                ) {
                    this.rows = [...sortedRowsToAdd, ...this.rows];
                } else if (this.firstRowInSortOrder(lastExistingRow, firstIncomingRow)
                    === lastExistingRow
                ) {
                    this.rows = [...this.rows, ...sortedRowsToAdd];
                } else {
                    this.mergeSortedRows(sortedRowsToAdd);
                }
            }

            sortCollection(rows) {
                const sortedRows = _.orderBy(
                    rows,
                    row => row.getParsedValue(this.sortOptions.key), this.sortOptions.direction
                );

                return sortedRows;
            }

            mergeSortedRows(rows) {
                const mergedRows = [];
                let i = 0;
                let j = 0;

                while (i < this.rows.length && j < rows.length) {
                    const existingRow = this.rows[i];
                    const incomingRow = rows[j];

                    if (this.firstRowInSortOrder(existingRow, incomingRow) === existingRow) {
                        mergedRows.push(existingRow);
                        i++;
                    } else {
                        mergedRows.push(incomingRow);
                        j++;
                    }
                }

                // tail of existing rows is all that is left to merge
                if (i < this.rows.length) {
                    for (i; i < this.rows.length; i++) {
                        mergedRows.push(this.rows[i]);
                    }
                }

                // tail of incoming rows is all that is left to merge
                if (j < rows.length) {
                    for (j; j < rows.length; j++) {
                        mergedRows.push(rows[j]);
                    }
                }

                this.rows = mergedRows;
            }

            firstRowInSortOrder(row1, row2) {
                const val1 = this.getValueForSortColumn(row1);
                const val2 = this.getValueForSortColumn(row2);

                if (this.sortOptions.direction === 'asc') {
                    return val1 <= val2 ? row1 : row2;
                } else {
                    return val1 >= val2 ? row1 : row2;
                }
            }

            removeRowsByData(data) {
                let removed = [];

                this.rows = this.rows.filter((row) => {
                    if (data.includes(row.fullDatum)) {
                        removed.push(row);

                        return false;
                    } else {
                        return true;
                    }
                });

                this.emit('remove', removed);
            }

            /**
             * Sorts the telemetry collection based on the provided sort field
             * specifier. Subsequent inserts are sorted to maintain specified sport
             * order.
             *
             * @example
             * // First build some mock telemetry for the purpose of an example
             * let now = Date.now();
             * let telemetry = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(function (value) {
             *     return {
             *         // define an object property to demonstrate nested paths
             *         timestamp: {
             *             ms: now - value * 1000,
             *             text:
             *         },
             *         value: value
             *     }
             * });
             * let collection = new TelemetryCollection();
             *
             * collection.add(telemetry);
             *
             * // Sort by telemetry value
             * collection.sortBy({
             *  key: 'value', direction: 'asc'
             * });
             *
             * // Sort by ms since epoch
             * collection.sort({
             *  key: 'timestamp.ms',
             *  direction: 'asc'
             * });
             *
             * // Sort by 'text' attribute, descending
             * collection.sort("timestamp.text");
             *
             *
             * @param {object} sortOptions An object specifying a sort key, and direction.
             */
            sortBy(sortOptions) {
                if (arguments.length > 0) {
                    this.sortOptions = sortOptions;
                    this.rows = _.orderBy(this.rows, (row) => row.getParsedValue(sortOptions.key), sortOptions.direction);

                    this.emit('sort');
                }

                // Return duplicate to avoid direct modification of underlying object
                return Object.assign({}, this.sortOptions);
            }

            setColumnFilter(columnKey, filter) {
                filter = filter.trim().toLowerCase();
                let wasBlank = this.columnFilters[columnKey] === undefined;
                let isSubset = this.isSubsetOfCurrentFilter(columnKey, filter);

                if (filter.length === 0) {
                    delete this.columnFilters[columnKey];
                } else {
                    this.columnFilters[columnKey] = filter;
                }

                if (isSubset || wasBlank) {
                    this.rows = this.rows.filter(this.matchesFilters, this);
                    this.emit('filter');
                } else {
                    this.emit('resetRowsFromAllData');
                }

            }

            setColumnRegexFilter(columnKey, filter) {
                filter = filter.trim();
                this.columnFilters[columnKey] = new RegExp(filter);

                this.emit('resetRowsFromAllData');
            }

            getColumnMapForObject(objectKeyString) {
                let columns = this.configuration.getColumns();

                if (columns[objectKeyString]) {
                    return columns[objectKeyString].reduce((map, column) => {
                        map[column.getKey()] = column;

                        return map;
                    }, {});
                }

                return {};
            }

            // /**
            //  * @private
            //  */
            isSubsetOfCurrentFilter(columnKey, filter) {
                if (this.columnFilters[columnKey] instanceof RegExp) {
                    return false;
                }

                return this.columnFilters[columnKey]
                    && filter.startsWith(this.columnFilters[columnKey])
                    // startsWith check will otherwise fail when filter cleared
                    // because anyString.startsWith('') === true
                    && filter !== '';
            }

            /**
             * @private
             */
            matchesFilters(row) {
                let doesMatchFilters = true;
                Object.keys(this.columnFilters).forEach((key) => {
                    if (!doesMatchFilters || !this.rowHasColumn(row, key)) {
                        return false;
                    }

                    let formattedValue = row.getFormattedValue(key);
                    if (formattedValue === undefined) {
                        return false;
                    }

                    if (this.columnFilters[key] instanceof RegExp) {
                        doesMatchFilters = this.columnFilters[key].test(formattedValue);
                    } else {
                        doesMatchFilters = formattedValue.toLowerCase().indexOf(this.columnFilters[key]) !== -1;
                    }
                });

                return doesMatchFilters;
            }

            rowHasColumn(row, key) {
                return Object.prototype.hasOwnProperty.call(row.columns, key);
            }

            getRows() {
                return this.rows;
            }

            getRowsLength() {
                return this.rows.length;
            }

            getValueForSortColumn(row) {
                return row.getParsedValue(this.sortOptions.key);
            }

            clear() {
                let removedRows = this.rows;
                this.rows = [];

                this.emit('remove', removedRows);
            }

            destroy() {
                this.removeAllListeners();
            }
        }

        return TableRowCollection;
    });