2016-07-17 23:06:43 +00:00
|
|
|
import React, { PropTypes } from 'react';
|
|
|
|
|
2016-07-18 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* How many console messages to store
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2016-07-17 23:06:43 +00:00
|
|
|
const consoleMax = 5;
|
|
|
|
|
|
|
|
class Console extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-07-18 00:05:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of React Elements that include previous console messages
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
2016-07-17 23:06:43 +00:00
|
|
|
this.children = [];
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:49:57 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.isPlaying && !this.props.isPlaying) {
|
2016-07-17 23:06:43 +00:00
|
|
|
this.children = [];
|
2016-07-18 00:49:57 +00:00
|
|
|
} else if (nextProps.consoleEvent !== this.props.consoleEvent) {
|
|
|
|
const args = nextProps.consoleEvent.arguments;
|
|
|
|
const method = nextProps.consoleEvent.method;
|
|
|
|
const nextChild = (
|
|
|
|
<div key={this.children.length} className={method}>
|
|
|
|
{Object.keys(args).map((key) => <span key={`${this.children.length}-${key}`}>{args[key]}</span>)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
this.children.push(nextChild);
|
2016-07-17 23:06:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:49:57 +00:00
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
return (nextProps.consoleEvent !== this.props.consoleEvent) || (nextProps.isPlaying && !this.props.isPlaying);
|
|
|
|
}
|
2016-07-17 23:06:43 +00:00
|
|
|
|
2016-07-18 00:49:57 +00:00
|
|
|
render() {
|
|
|
|
const childrenToDisplay = this.children.slice(-consoleMax);
|
2016-07-17 23:06:43 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref="console" className="preview-console">
|
2016-07-18 00:49:57 +00:00
|
|
|
{childrenToDisplay}
|
2016-07-17 23:06:43 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.propTypes = {
|
|
|
|
consoleEvent: PropTypes.object,
|
|
|
|
isPlaying: PropTypes.bool.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Console;
|