content.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. import React from 'react';
  7. import Editor from './editor';
  8. import classnames from 'classnames';
  9. import './content.less';
  10. export default class Content extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.codemirror = null;
  14. this.state = {
  15. is_loading: this.props.current.is_loading,
  16. code: this.props.current.content || ''
  17. };
  18. this._t = null;
  19. SH_event.on('loading', (host) => {
  20. if (host === this.props.current) {
  21. this.setState({
  22. is_loading: true
  23. });
  24. }
  25. });
  26. SH_event.on('loading_done', (host, data) => {
  27. if (host === this.props.current) {
  28. this.setState({
  29. is_loading: false,
  30. code: data.content || ''
  31. });
  32. }
  33. });
  34. }
  35. setValue(v) {
  36. this.props.setHostsContent(v);
  37. }
  38. componentWillReceiveProps(next_props) {
  39. this.setState({
  40. is_loading: next_props.current.is_loading,
  41. code: next_props.current.content || ''
  42. });
  43. }
  44. render() {
  45. let {current} = this.props;
  46. return (
  47. <div id="sh-content">
  48. <div className="inform">
  49. <span
  50. className={classnames({
  51. loading: 1,
  52. show: this.state.is_loading
  53. })}
  54. >loading...</span>
  55. <i
  56. className={classnames({
  57. show: current.where === 'remote',
  58. iconfont: 1,
  59. 'icon-earth': 1
  60. })}
  61. title={SH_Agent.lang.remote_hosts}
  62. />
  63. <i
  64. className={classnames({
  65. show: this.props.readonly,
  66. iconfont: 1,
  67. 'icon-lock2': 1
  68. })}
  69. title={SH_Agent.lang.readonly}
  70. />
  71. </div>
  72. <div className={classnames({
  73. errorMessage: 1,
  74. show: !!this.props.current.error
  75. })}>{this.props.current.error}</div>
  76. <Editor
  77. code={this.state.code}
  78. readonly={this.props.readonly}
  79. setValue={this.setValue.bind(this)}/>
  80. </div>
  81. );
  82. }
  83. }