/
eventlog.js
198 lines (164 loc) · 5.3 KB
/
eventlog.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
import React from "react"
import ReactDOM from "react-dom"
import { connect } from 'react-redux'
import shallowEqual from "shallowequal"
import {Query} from "../actions.js"
import {toggleEventLogFilter} from "../reduxActions"
import AutoScroll from "./helpers/AutoScroll";
import {calcVScroll} from "./helpers/VirtualScroll"
import {StoreView} from "../store/view.js"
class EventLogContents extends React.Component {
static contextTypes = {
eventStore: React.PropTypes.object.isRequired,
};
static defaultProps = {
rowHeight: 18,
};
constructor(props, context) {
super(props, context);
this.view = new StoreView(
this.context.eventStore,
entry => this.props.filter[entry.level]
);
this.heights = {};
this.state = { entries: this.view.list, vScroll: calcVScroll() };
this.onChange = this.onChange.bind(this);
this.onViewportUpdate = this.onViewportUpdate.bind(this);
}
componentDidMount() {
window.addEventListener("resize", this.onViewportUpdate);
this.view.addListener("add", this.onChange);
this.view.addListener("recalculate", this.onChange);
this.onViewportUpdate();
}
componentWillUnmount() {
window.removeEventListener("resize", this.onViewportUpdate);
this.view.removeListener("add", this.onChange);
this.view.removeListener("recalculate", this.onChange);
this.view.close();
}
componentDidUpdate() {
this.onViewportUpdate();
}
componentWillReceiveProps(nextProps) {
if (nextProps.filter !== this.props.filter) {
this.view.recalculate(
entry => nextProps.filter[entry.level]
);
}
}
onViewportUpdate() {
const viewport = ReactDOM.findDOMNode(this);
const vScroll = calcVScroll({
itemCount: this.state.entries.length,
rowHeight: this.props.rowHeight,
viewportTop: viewport.scrollTop,
viewportHeight: viewport.offsetHeight,
itemHeights: this.state.entries.map(entry => this.heights[entry.id]),
});
if (!shallowEqual(this.state.vScroll, vScroll)) {
this.setState({ vScroll });
}
}
onChange() {
this.setState({ entries: this.view.list });
}
setHeight(id, ref) {
if (ref && !this.heights[id]) {
const height = ReactDOM.findDOMNode(ref).offsetHeight;
if (this.heights[id] !== height) {
this.heights[id] = height;
this.onViewportUpdate();
}
}
}
getIcon(level) {
return { web: "html5", debug: "bug" }[level] || "info";
}
render() {
const vScroll = this.state.vScroll;
const entries = this.state.entries.slice(vScroll.start, vScroll.end);
return (
<pre onScroll={this.onViewportUpdate}>
<div style={{ height: vScroll.paddingTop }}></div>
{entries.map((entry, index) => (
<div key={entry.id} ref={this.setHeight.bind(this, entry.id)}>
<i className={`fa fa-fw fa-${this.getIcon(entry.level)}`}></i>
{entry.message}
</div>
))}
<div style={{ height: vScroll.paddingBottom }}></div>
</pre>
);
}
}
ToggleFilter.propTypes = {
name: React.PropTypes.string.isRequired,
toggleLevel: React.PropTypes.func.isRequired,
active: React.PropTypes.bool,
};
function ToggleFilter ({ name, active, toggleLevel }) {
let className = "label ";
if (active) {
className += "label-primary";
} else {
className += "label-default";
}
function onClick(event) {
event.preventDefault();
toggleLevel();
}
return (
<a
href="#"
className={className}
onClick={onClick}>
{name}
</a>
);
}
const mapStateToProps = (state, ownProps) => {
return {
active: state.eventLog.visibilityFilter[ownProps.name]
}
};
const mapDispatchToProps = (dispatch, ownProps) => {
return {
toggleLevel: () => {
dispatch(toggleEventLogFilter(ownProps.name))
}
}
};
const ToggleEventLogFilter = connect(
mapStateToProps,
mapDispatchToProps
)(ToggleFilter);
const AutoScrollEventLog = AutoScroll(EventLogContents);
const VisibleAutoScrollEventLog = connect(
function mapStateToProps(state, ownProps) {
return {filter: state.eventLog.visibilityFilter}
})(AutoScrollEventLog);
var EventLog = React.createClass({
close() {
var d = {};
d[Query.SHOW_EVENTLOG] = undefined;
this.props.updateLocation(undefined, d);
},
render() {
return (
<div className="eventlog">
<div>
Eventlog
<div className="pull-right">
<ToggleEventLogFilter name="debug"/>
<ToggleEventLogFilter name="info"/>
<ToggleEventLogFilter name="web"/>
<i onClick={this.close} className="fa fa-close"></i>
</div>
</div>
<VisibleAutoScrollEventLog/>
</div>
);
}
});
export default EventLog;