-
Notifications
You must be signed in to change notification settings - Fork 480
/
NetSimLogBrowserTable.jsx
213 lines (189 loc) · 5.26 KB
/
NetSimLogBrowserTable.jsx
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
/** @file Table of log rows displayed in the log browser */
import React, {PropTypes} from 'react';
import orderBy from 'lodash/orderBy';
import {Table, sort} from 'reactabular';
import moment from 'moment';
import color from "../util/color";
import wrappedSortable from '../templates/tables/wrapped_sortable';
import Packet from './Packet';
let style = {
nowrap: {
whiteSpace: 'nowrap'
},
prewrap: {
whiteSpace: 'pre-wrap'
},
table: {
marginBottom: 0
},
td: {
color: color.charcoal,
fontFamily: 'monospace',
// Make sure table text can be selected and copied
userSelect: 'text'
}
};
style.nowrapTd = Object.assign({}, style.td, style.nowrap);
style.prewrapTd = Object.assign({}, style.td, style.prewrap);
/**
* Table of log rows displayed in the Log Browser modal.
* Wraps configuration and sorting behavior around a Reactabular table.
* @see http://reactabular.js.org
*/
export default class NetSimLogBrowserTable extends React.Component {
static propTypes = {
logRows: PropTypes.arrayOf(PropTypes.object).isRequired,
headerFields: PropTypes.arrayOf(PropTypes.string).isRequired,
renderedRowLimit: PropTypes.number,
teacherView: PropTypes.bool,
currentSentByFilter: PropTypes.string.isRequired
};
state = {
sortingColumns: {
0: {direction: 'desc', position: 0}
}
};
getSortingColumns = () => this.state.sortingColumns || {};
// The user requested sorting, adjust the sorting state accordingly.
onSort = (selectedColumn) => {
this.setState({
sortingColumns: sort.byColumn({
sortingColumns: this.state.sortingColumns,
// Custom sortingOrder removes 'no-sort' from the cycle
sortingOrder: {
FIRST: 'asc',
asc: 'desc',
desc: 'asc'
},
selectedColumn
})
});
};
render() {
const headerFields = this.props.headerFields;
// Define a sorting transform that can be applied to each column
const sortable = wrappedSortable(
this.getSortingColumns,
this.onSort,
{
default: {color: 'rgba(255, 255, 255, 0.2 )'}
}
);
const showToAddress = headerFields.indexOf(Packet.HeaderType.TO_ADDRESS) > -1;
const showFromAddress = headerFields.indexOf(Packet.HeaderType.FROM_ADDRESS) > -1;
const showPacketInfo = headerFields.indexOf(Packet.HeaderType.PACKET_INDEX) > -1 &&
headerFields.indexOf(Packet.HeaderType.PACKET_COUNT) > -1;
let columns = [];
columns.push({
property: 'timestamp',
header: {
label: 'Time',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {
format: timeFormatter,
props: {style: style.nowrapTd}
}
});
if (this.props.teacherView) {
columns.push({
property: 'sent-by',
header: {
label: 'Sent By',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {props: {style: style.nowrapTd}}
});
}
columns.push({
property: 'logged-by',
header: {
label: 'Logged By',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {props: {style: style.nowrapTd}}
});
columns.push({
property: 'status',
header: {
label: 'Status',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {props: {style: style.nowrapTd}}
});
if (showFromAddress) {
columns.push({
property: 'from-address',
header: {
label: 'From',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {props: {style: style.nowrapTd}}
});
}
if (showToAddress) {
columns.push({
property: 'to-address',
header: {
label: 'To',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {props: {style: style.nowrapTd}}
});
}
if (showPacketInfo) {
columns.push({
property: 'packet-info',
header: {
label: 'Packet',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {props: {style: style.nowrapTd}}
});
}
columns.push({
property: 'message',
header: {
label: 'Message',
transforms: [sortable],
props: {style: style.nowrap}
},
cell: {props: {style: style.prewrapTd}}
});
const {logRows} = this.props;
const {sortingColumns} = this.state;
let sortedRows = sort.sorter({
columns,
sortingColumns,
sort: orderBy
})(logRows);
// Filter by "sent by"
const sentByMatch = this.props.currentSentByFilter.match(/^by (.*)$/);
if (sentByMatch) {
sortedRows = sortedRows.filter(row => row['sent-by'] === sentByMatch[1]);
}
// Limit number of rendered rows
if (this.props.renderedRowLimit !== undefined) {
sortedRows = sortedRows.slice(0, this.props.renderedRowLimit);
}
return (
<Table.Provider columns={columns} style={style.table}>
<Table.Header/>
<Table.Body
rows={sortedRows}
rowKey="uuid"
/>
</Table.Provider>
);
}
}
function timeFormatter(timestamp) {
return moment(timestamp).format('h:mm:ss.SSS A');
}