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