aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/components/eventlog.js
blob: 6ada58ffe57001b2e6450c8ae4bd127560cc50ce (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
import React from "react"
import ReactDOM from "react-dom"
import {connect} from 'react-redux'
import shallowEqual from "shallowequal"
import {toggleEventLogFilter, toggleEventLogVisibility} from "../ducks/eventLog"
import AutoScroll from "./helpers/AutoScroll";
import {calcVScroll} from "./helpers/VirtualScroll"
import {ToggleButton} from "./common";

function LogIcon({event}) {
    let icon = {web: "html5", debug: "bug"}[event.level] || "info";
    return <i className={`fa fa-fw fa-${icon}`}></i>
}

function LogEntry({event, registerHeight}) {
    return <div ref={registerHeight}>
        <LogIcon event={event}/>
        {event.message}
    </div>;
}

class EventLogContents extends React.Component {

    static defaultProps = {
        rowHeight: 18,
    };

    constructor(props) {
        super(props);

        this.heights = {};
        this.state = {vScroll: calcVScroll()};

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

    componentDidMount() {
        window.addEventListener("resize", this.onViewportUpdate);
        this.onViewportUpdate();
    }

    componentWillUnmount() {
        window.removeEventListener("resize", this.onViewportUpdate);
    }

    componentDidUpdate() {
        this.onViewportUpdate();
    }

    onViewportUpdate() {
        const viewport = ReactDOM.findDOMNode(this);

        const vScroll = calcVScroll({
            itemCount: this.props.events.length,
            rowHeight: this.props.rowHeight,
            viewportTop: viewport.scrollTop,
            viewportHeight: viewport.offsetHeight,
            itemHeights: this.props.events.map(entry => this.heights[entry.id]),
        });

        if (!shallowEqual(this.state.vScroll, vScroll)) {
            this.setState({vScroll});
        }
    }

    setHeight(id, node) {
        if (node && !this.heights[id]) {
            const height = node.offsetHeight;
            if (this.heights[id] !== height) {
                this.heights[id] = height;
                this.onViewportUpdate();
            }
        }
    }

    render() {
        const vScroll = this.state.vScroll;
        const events = this.props.events
            .slice(vScroll.start, vScroll.end)
            .map(event =>
                <LogEntry
                    event={event}
                    key={event.id}
                    registerHeight={(node) => this.setHeight(event.id, node)}
                />
            );

        return (
            <pre onScroll={this.onViewportUpdate}>
                <div style={{ height: vScroll.paddingTop }}></div>
                {events}
                <div style={{ height: vScroll.paddingBottom }}></div>
            </pre>
        );
    }
}

EventLogContents = AutoScroll(EventLogContents);


const EventLogContentsContainer = connect(
    state => ({
        events: state.eventLog.filteredEvents
    })
)(EventLogContents);


export const ToggleEventLog = connect(
    state => ({
        checked: state.eventLog.visible
    }),
    dispatch => ({
        onToggle: () => dispatch(toggleEventLogVisibility())
    })
)(ToggleButton);


const ToggleFilter = connect(
    (state, ownProps) => ({
        checked: state.eventLog.filter[ownProps.text]
    }),
    (dispatch, ownProps) => ({
        onToggle: () => dispatch(toggleEventLogFilter(ownProps.text))
    })
)(ToggleButton);


const EventLog = ({close}) =>
    <div className="eventlog">
        <div>
            Eventlog
            <div className="pull-right">
                <ToggleFilter text="debug"/>
                <ToggleFilter text="info"/>
                <ToggleFilter text="web"/>
                <i onClick={close} className="fa fa-close"></i>
            </div>
        </div>
        <EventLogContentsContainer/>
    </div>;

EventLog.propTypes = {
    close: React.PropTypes.func.isRequired
};

const EventLogContainer = connect(
    undefined,
    dispatch => ({
        close: () => dispatch(toggleEventLogVisibility())
    })
)(EventLog);

export default EventLogContainer;