From 59010ab902e744f48984d848a70abe3fb3c6404f Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 13 May 2017 01:16:08 +0300 Subject: [PATCH] =?UTF-8?q?MCSM:WebConsole:=20=D0=BA=D0=BE=D0=BD=D1=81?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=20=D0=B0=D0=BA=D1=82=D0=B8=D0=B2=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=20=D0=BD=D0=B5=20=D0=B0=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D0=B2=D0=BD=D0=BE=D0=B9=20=D0=B2=D0=BA=D0=BB=D0=B0=D0=B4=D0=BA?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/components.js | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/mcserver-manager/src/main/resources/components.js b/mcserver-manager/src/main/resources/components.js index 26f38ef..1297a8e 100644 --- a/mcserver-manager/src/main/resources/components.js +++ b/mcserver-manager/src/main/resources/components.js @@ -55,7 +55,13 @@ var Tabs = React.createClass({ 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, ce('ul', {className: 'nav nav-tabs', id: 'tabs'}, tabsElm), @@ -66,35 +72,45 @@ var Tabs = React.createClass({ var WebConsole = React.createClass({ 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{ lines: [] }}, render: function(){ 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(){ - this.ws.close(); + this.disconnect(); } }); var ServerInfo = React.createClass({ + tabStateWebConsole: function(state) { + if (state === 1) { + this.refs.webconsole.connect(); + } + }, + /*--------------------*/ getInitialState: function(){return { title: null, data: [] @@ -106,14 +122,14 @@ var ServerInfo = React.createClass({ return( ce('div', null, ce('h2', {style: {'margin-top': '0px'}}, this.state.title), - ce(Tabs, {tabs: ['Онлайн', 'Консоль']}, + ce(Tabs, {tabs: ['Онлайн', 'Консоль'], stateCallback: this.tabStateWebConsole}, ce(NvLineChart, {datum: [{ key: 'Online players', color: '#37d668', area: true, values: this.state.data }]}), - ce(WebConsole) + ce(WebConsole, {ref: 'webconsole'}) ) ) )