|
1 | 1 | import React from 'react';
|
| 2 | +import PropTypes from 'prop-types'; |
2 | 3 |
|
3 |
| -const propTypes = {}; |
4 |
| -const defaultProps = {}; |
| 4 | +import { EmptyState } from 'patternfly-react'; |
| 5 | +import { Button } from 'patternfly-react'; |
| 6 | +import { noop } from 'patternfly-react'; |
| 7 | +import { CONNECTED, DISCONNECTED, LOADING } from './constants'; |
5 | 8 |
|
6 |
| -const SerialConsole = () => <div>Serial Console</div>; |
| 9 | +import XTerm from './XTerm'; |
| 10 | +import SerialConsoleActions from './SerialConsoleActions'; |
7 | 11 |
|
8 |
| -SerialConsole.propTypes = propTypes; |
9 |
| -SerialConsole.defaultProps = defaultProps; |
| 12 | +class SerialConsole extends React.Component { |
| 13 | + componentDidMount() { |
| 14 | + this.props.onConnect(); |
| 15 | + } |
| 16 | + |
| 17 | + componentWillUnmount() { |
| 18 | + this.props.onDisconnect(); |
| 19 | + } |
| 20 | + |
| 21 | + onResetClick = (event) => { |
| 22 | + if (event.button !== 0) return; |
| 23 | + |
| 24 | + this.props.onDisconnect(); |
| 25 | + this.props.onConnect(); |
| 26 | + event.target.blur(); |
| 27 | + this.focusTerminal(); |
| 28 | + }; |
| 29 | + |
| 30 | + onDisconnectClick = (event) => { |
| 31 | + if (event.button !== 0) return; |
| 32 | + |
| 33 | + this.props.onDisconnect(); |
| 34 | + event.target.blur(); |
| 35 | + this.focusTerminal(); |
| 36 | + }; |
| 37 | + |
| 38 | + /** |
| 39 | + * Backend sent data. |
| 40 | + */ |
| 41 | + onDataReceived(data) { |
| 42 | + if (this.childTerminal && this.props.status === CONNECTED) { |
| 43 | + this.childTerminal.onDataReceived(data); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Backend closed connection. |
| 49 | + */ |
| 50 | + onConnectionClosed(reason) { |
| 51 | + if (this.childTerminal) { |
| 52 | + this.childTerminal.onConnectionClosed(reason); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + focusTerminal = () => { |
| 57 | + this.childTerminal && this.childTerminal.focus(); |
| 58 | + }; |
| 59 | + |
| 60 | + render() { |
| 61 | + const { id, status, topClassName } = this.props; |
| 62 | + const idPrefix = `${id || 'id'}-serialconsole`; |
| 63 | + |
| 64 | + let terminal; |
| 65 | + let isDisconnectEnabled = false; |
| 66 | + switch (status) { |
| 67 | + case CONNECTED: |
| 68 | + terminal = ( |
| 69 | + <XTerm |
| 70 | + ref={c => { |
| 71 | + this.childTerminal = c; |
| 72 | + }} |
| 73 | + cols={this.props.cols} |
| 74 | + rows={this.props.rows} |
| 75 | + onConnect={this.props.onConnect} |
| 76 | + onDisconnect={this.props.onDisconnect} |
| 77 | + onData={this.props.onData} |
| 78 | + onTitleChanged={this.props.onTitleChanged} |
| 79 | + onResize={this.props.onResize} |
| 80 | + /> |
| 81 | + ); |
| 82 | + isDisconnectEnabled = true; |
| 83 | + break; |
| 84 | + case DISCONNECTED: |
| 85 | + terminal = ( |
| 86 | + <EmptyState> |
| 87 | + <EmptyState.Title> |
| 88 | + {this.props.textDisconnectedTitle} |
| 89 | + </EmptyState.Title> |
| 90 | + <EmptyState.Info>{this.props.textDisconnected}</EmptyState.Info> |
| 91 | + <EmptyState.Action> |
| 92 | + <Button |
| 93 | + bsStyle="primary" |
| 94 | + bsSize="large" |
| 95 | + onClick={this.props.onConnect} |
| 96 | + > |
| 97 | + {this.props.textConnect} |
| 98 | + </Button> |
| 99 | + </EmptyState.Action> |
| 100 | + </EmptyState> |
| 101 | + ); |
| 102 | + break; |
| 103 | + case LOADING: |
| 104 | + default: |
| 105 | + terminal = <span>{this.props.textLoading}</span>; |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + return ( |
| 110 | + <div className={topClassName} id={id}> |
| 111 | + <SerialConsoleActions |
| 112 | + idPrefix={idPrefix} |
| 113 | + isDisconnectEnabled={isDisconnectEnabled} |
| 114 | + onDisconnect={this.onDisconnectClick} |
| 115 | + onReset={this.onResetClick} |
| 116 | + textDisconnect={this.props.textDisconnect} |
| 117 | + textReconnect={this.props.textReconnect} |
| 118 | + /> |
| 119 | + <div className="panel-body console-terminal-pf">{terminal}</div> |
| 120 | + </div> |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +SerialConsole.propTypes = { |
| 126 | + /** Initiate connection to backend. In other words, the calling components manages connection state. */ |
| 127 | + onConnect: PropTypes.func.isRequired, |
| 128 | + /** Close connection to backend */ |
| 129 | + onDisconnect: PropTypes.func.isRequired, |
| 130 | + /** Terminal has been resized, backend shall be informed. (rows, cols) => {} */ |
| 131 | + onResize: PropTypes.func, |
| 132 | + /** Terminal produced data, like key-press */ |
| 133 | + onData: PropTypes.func, |
| 134 | + /** Terminal title has been changed. */ |
| 135 | + onTitleChanged: PropTypes.func, |
| 136 | + |
| 137 | + /** Connection status, a value from [''connected', 'disconnected', 'loading']. Default is 'loading' for a not matching value. */ |
| 138 | + status: PropTypes.string.isRequired, |
| 139 | + id: PropTypes.string, |
| 140 | + |
| 141 | + /** Size of the terminal component */ |
| 142 | + rows: PropTypes.number, |
| 143 | + cols: PropTypes.number, |
| 144 | + |
| 145 | + /** Enable customization */ |
| 146 | + topClassName: PropTypes.string, |
| 147 | + |
| 148 | + /** Localization */ |
| 149 | + textDisconnect: PropTypes.string, |
| 150 | + textDisconnectedTitle: PropTypes.string, |
| 151 | + textDisconnected: PropTypes.string, |
| 152 | + textLoading: PropTypes.string, |
| 153 | + textReconnect: PropTypes.string, |
| 154 | + textConnect: PropTypes.string |
| 155 | +}; |
| 156 | + |
| 157 | +SerialConsole.defaultProps = { |
| 158 | + topClassName: '', |
| 159 | + |
| 160 | + id: '', |
| 161 | + rows: 25, |
| 162 | + cols: 80, |
| 163 | + |
| 164 | + onTitleChanged: noop, |
| 165 | + onData: noop, |
| 166 | + onResize: noop, |
| 167 | + |
| 168 | + textDisconnectedTitle: 'Disconnected from serial console', |
| 169 | + textDisconnected: 'Click Connect to open serial console.', |
| 170 | + textLoading: 'Loading ...', |
| 171 | + textConnect: 'Connect', |
| 172 | + textDisconnect: undefined /** Default is set in SerialConsoleActions */, |
| 173 | + textReconnect: undefined /** Default is set in SerialConsoleActions */ |
| 174 | +}; |
10 | 175 |
|
11 | 176 | export default SerialConsole;
|
0 commit comments