nci/static/js/app/components/terminal/terminal.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-05-07 21:55:40 +00:00
'use strict';
define([
2015-06-14 15:24:03 +00:00
'underscore',
2015-05-07 21:55:40 +00:00
'react',
'reflux',
2015-05-10 16:53:33 +00:00
'app/stores/terminal',
2015-06-14 15:24:03 +00:00
'ansi_up',
2015-05-10 16:53:33 +00:00
'templates/app/components/terminal/terminal'
2015-06-14 15:24:03 +00:00
], function(_, React, Reflux, terminalStore, ansiUp, template) {
2015-05-07 21:55:40 +00:00
var Component = React.createClass({
mixins: [Reflux.ListenerMixin],
2015-11-18 20:14:46 +00:00
shouldScrollBottom: true,
2015-06-14 15:24:03 +00:00
ignoreScrollEvent: false,
2015-05-07 21:55:40 +00:00
componentDidMount: function() {
2015-05-10 16:53:33 +00:00
this.listenTo(terminalStore, this.updateItems);
var node = this.refs.code.getDOMNode();
this.initialScrollPosition = node.getBoundingClientRect().top;
2015-05-07 21:55:40 +00:00
},
2015-11-18 20:14:46 +00:00
prepareOutput: function(output) {
return output.map(function(row) {
return ansiUp.ansi_to_html(row.replace('\r', ''));
2015-11-18 20:14:46 +00:00
});
2015-06-14 15:24:03 +00:00
},
2015-11-18 20:14:46 +00:00
componentWillUpdate: function() {
var node = this.refs.code.getDOMNode(),
body = document.getElementsByTagName('body')[0];
this.shouldScrollBottom = window.innerHeight + body.scrollTop >=
node.offsetHeight + this.initialScrollPosition;
2015-06-14 15:24:03 +00:00
},
2015-11-18 20:14:46 +00:00
componentDidUpdate: function() {
if (this.shouldScrollBottom) {
var node = this.refs.code.getDOMNode(),
body = document.getElementsByTagName('body')[0];
body.scrollTop = this.initialScrollPosition + node.offsetHeight;
2015-11-18 20:14:46 +00:00
}
2015-07-08 20:37:31 +00:00
},
2015-06-14 15:24:03 +00:00
updateItems: function(build) {
2015-05-10 16:53:33 +00:00
// listen just our console update
2015-06-14 15:24:03 +00:00
if (build.buildId === this.props.build) {
2015-07-08 20:37:31 +00:00
this.setState({data: this.prepareOutput(build.data)});
2015-05-10 16:53:33 +00:00
}
2015-05-07 21:55:40 +00:00
},
2015-05-17 13:48:16 +00:00
render: template,
2015-05-07 21:55:40 +00:00
getInitialState: function() {
return {
2015-11-18 20:14:46 +00:00
data: []
2015-05-07 21:55:40 +00:00
};
}
});
return Component;
});