app.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 './Agent'
  13. import './app.less'
  14. export default class App extends React.Component {
  15. constructor (props) {
  16. super(props)
  17. this.state = {
  18. list: [], // 用户的 hosts 列表
  19. sys_hosts: {}, // 系统 hosts
  20. current: {}, // 当前 hosts
  21. lang: {} // 语言
  22. }
  23. this.loadHosts()
  24. Agent.pact('getLang').then(lang => {
  25. this.setState({lang})
  26. })
  27. Agent.on('toggle_hosts', (hosts, on) => {
  28. Agent.pact('toggleHosts', hosts.id, on)
  29. .then(() => {
  30. hosts.on = on
  31. this.setState({
  32. list: this.state.list
  33. })
  34. })
  35. .catch(e => {
  36. console.log(e)
  37. })
  38. })
  39. }
  40. loadHosts () {
  41. Agent.pact('getHosts').then(data => {
  42. this.setState({
  43. list: data.list,
  44. sys_hosts: data.sys_hosts,
  45. current: data.sys_hosts
  46. })
  47. })
  48. }
  49. setCurrent (hosts) {
  50. if (hosts.is_sys) {
  51. Agent.act('getSysHosts', (e, _hosts) => {
  52. this.setState({
  53. sys_hosts: _hosts,
  54. current: _hosts
  55. })
  56. })
  57. } else {
  58. this.setState({
  59. current: hosts
  60. })
  61. }
  62. }
  63. static isReadOnly (host) {
  64. return !host || host.is_sys || host.where === 'remote'
  65. }
  66. toSave () {
  67. clearTimeout(this._t)
  68. this._t = setTimeout(() => {
  69. //Agent.emit('change')
  70. Agent.pact('saveHosts', this.state.list)
  71. }, 1000)
  72. }
  73. setHostsContent (v) {
  74. if (this.state.current.content === v) return // not changed
  75. this.state.current.content = v || ''
  76. this.toSave()
  77. }
  78. componentDidMount () {
  79. window.addEventListener('keydown', (e) => {
  80. if (e.keyCode === 27) {
  81. Agent.emit('esc')
  82. }
  83. }, false)
  84. }
  85. render () {
  86. let current = this.state.current
  87. return (
  88. <div id="app" className={'platform-' + Agent.platform}>
  89. <Panel
  90. list={this.state.list}
  91. sys_hosts={this.state.sys_hosts}
  92. current={current}
  93. setCurrent={this.setCurrent.bind(this)}
  94. lang={this.state.lang}
  95. />
  96. <Content
  97. current={current}
  98. readonly={App.isReadOnly(current)}
  99. setHostsContent={this.setHostsContent.bind(this)}
  100. lang={this.state.lang}
  101. />
  102. <div className="frames">
  103. {/*<SudoPrompt/>*/}
  104. <EditPrompt lang={this.state.lang} list={this.state.list}/>
  105. {/*<PreferencesPrompt/>*/}
  106. </div>
  107. </div>
  108. )
  109. }
  110. }