Archived
0

MCSM:WebConsole: консоль активна при не активной вкладке

This commit is contained in:
2017-05-13 01:16:08 +03:00
parent 8bec28b655
commit 59010ab902

View File

@@ -55,7 +55,13 @@ var Tabs = React.createClass({
ce('a', {href: '#tab'+(i+1), onClick: _this.onTabClick.bind(_this, i)}, title))); ce('a', {href: '#tab'+(i+1), onClick: _this.onTabClick.bind(_this, i)}, title)));
}); });
var showElement = this.props.children[this.state.activeTab]; var showElement = this.props.children.map(function(child, i){
return ce('div', (i !== _this.state.activeTab ? {style: {display: 'none'}} : null), child);
});
if (this.props.stateCallback !== null) {
this.props.stateCallback(this.state.activeTab);
}
return(ce('div', null, return(ce('div', null,
ce('ul', {className: 'nav nav-tabs', id: 'tabs'}, tabsElm), ce('ul', {className: 'nav nav-tabs', id: 'tabs'}, tabsElm),
@@ -66,35 +72,45 @@ var Tabs = React.createClass({
var WebConsole = React.createClass({ var WebConsole = React.createClass({
ws: null, ws: null,
connect: function(){
if (this.ws !== null) return;
var _this = this;
this.ws = new WebSocket("ws://127.0.0.1:8770"); //FIXME указывать ip:port из настроек
this.ws.onopen = function(){ console.debug('WS: open...'); };
this.ws.onclose = function(){ console.debug('WS: close...'); };
this.ws.onerror = function(e){ console.debug('WS: error'); console.error(e); };
this.ws.onmessage = function(event){
_this.setState({ lines: _this.state.lines.concat([event.data]) });
};
},
disconnect: function() {
if (this.ws === null) return;
this.ws.close();
this.ws = null;
},
/*--------------------*/ /*--------------------*/
getInitialState: function(){return{ getInitialState: function(){return{
lines: [] lines: []
}}, }},
render: function(){ render: function(){
return( return(
ce('div', {id: 'webconsole', ref: 'webconsole'}, this.state.lines.map(function(line){ return ce('p', null, line); })) ce('div', {id: 'webconsole'},
this.state.lines.map(function(line){ return ce('p', null, line); }))
) )
}, },
componentDidMount: function(){
var _this = this;
this.ws = new WebSocket("ws://127.0.0.1:8770"); //FIXME указывать ip:port из настроек
this.ws.onopen = function(){ console.debug('WS: open...'); };
this.ws.onclose = function(){ console.debug('WS: close...'); };
this.ws.onmessage = function(event){
console.debug('WS: message: '+event.data);
_this.setState({ lines: _this.state.lines.concat([event.data]) });
};
},
componentDidUpdate: function() {
// this.refs.webconsole.scrollTop = this.refs.webconsole.scrollHeight;
},
componentWillUnmount: function(){ componentWillUnmount: function(){
this.ws.close(); this.disconnect();
} }
}); });
var ServerInfo = React.createClass({ var ServerInfo = React.createClass({
tabStateWebConsole: function(state) {
if (state === 1) {
this.refs.webconsole.connect();
}
},
/*--------------------*/
getInitialState: function(){return { getInitialState: function(){return {
title: null, title: null,
data: [] data: []
@@ -106,14 +122,14 @@ var ServerInfo = React.createClass({
return( return(
ce('div', null, ce('div', null,
ce('h2', {style: {'margin-top': '0px'}}, this.state.title), ce('h2', {style: {'margin-top': '0px'}}, this.state.title),
ce(Tabs, {tabs: ['Онлайн', 'Консоль']}, ce(Tabs, {tabs: ['Онлайн', 'Консоль'], stateCallback: this.tabStateWebConsole},
ce(NvLineChart, {datum: [{ ce(NvLineChart, {datum: [{
key: 'Online players', key: 'Online players',
color: '#37d668', color: '#37d668',
area: true, area: true,
values: this.state.data values: this.state.data
}]}), }]}),
ce(WebConsole) ce(WebConsole, {ref: 'webconsole'})
) )
) )
) )