app.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import Panel from './panel/panel'
  8. import Content from './content/content'
  9. import SudoPrompt from './frame/sudo'
  10. import EditPrompt from './frame/edit'
  11. import PreferencesPrompt from './frame/preferences'
  12. import Agent from '../../renderer/Agent'
  13. import './app.less'
  14. class App extends React.Component {
  15. constructor (props) {
  16. super(props)
  17. this.state = {
  18. list: [],
  19. sys: {},
  20. current: {},
  21. lang: {}
  22. }
  23. Agent.act('getUserHosts', (e, data) => {
  24. this.setState({
  25. list: data.list,
  26. sys: data.sys
  27. })
  28. })
  29. Agent.act('getLang', (e, lang) => {
  30. this.setState({lang})
  31. })
  32. }
  33. setCurrent (hosts) {
  34. if (hosts.is_sys) {
  35. Agent.act('getSysHosts', (e, _hosts) => {
  36. this.setState({
  37. current: _hosts
  38. })
  39. })
  40. } else {
  41. this.setState({
  42. current: hosts
  43. })
  44. }
  45. }
  46. static isReadOnly (hosts) {
  47. return !hosts || hosts.is_sys || hosts.where === 'remote'
  48. }
  49. toSave () {
  50. clearTimeout(this._t)
  51. this._t = setTimeout(() => {
  52. Agent.emit('change')
  53. }, 1000)
  54. }
  55. setHostContent (v) {
  56. if (this.state.current.content === v) return // not changed
  57. this.state.current.content = v || ''
  58. this.toSave()
  59. }
  60. componentDidMount () {
  61. window.addEventListener('keydown', (e) => {
  62. if (e.keyCode === 27) {
  63. Agent.emit('esc')
  64. }
  65. }, false)
  66. }
  67. render () {
  68. let current = this.state.current
  69. return (
  70. <div id="app" className={'platform-' + Agent.platform}>
  71. <Panel
  72. list={this.state.list}
  73. sys={this.state.sys}
  74. current={current}
  75. setCurrent={this.setCurrent.bind(this)}
  76. lang={this.state.lang}
  77. />
  78. <Content
  79. current={current}
  80. readonly={App.isReadOnly(current)}
  81. setHostContent={this.setHostContent.bind(this)}
  82. lang={this.state.lang}
  83. />
  84. <div className="frames">
  85. <SudoPrompt/>
  86. <EditPrompt hosts={this.state.hosts}/>
  87. <PreferencesPrompt/>
  88. </div>
  89. </div>
  90. )
  91. }
  92. }
  93. export default App