app.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 { reg as events_reg } from './events/index'
  14. import './app.less'
  15. export default class App extends React.Component {
  16. constructor (props) {
  17. super(props)
  18. this.state = {
  19. list: [], // 用户的 hosts 列表
  20. sys_hosts: {}, // 系统 hosts
  21. current: {}, // 当前 hosts
  22. lang: {} // 语言
  23. }
  24. this.loadHosts()
  25. Agent.pact('getLang').then(lang => {
  26. this.setState({lang})
  27. })
  28. events_reg(this)
  29. setInterval(() => {
  30. let list = this.state.list
  31. if (!list || list.length === 0) return
  32. Agent.pact('checkNeedRemoteRefresh', list)
  33. .then(list => {
  34. if (!list) return
  35. Agent.emit('list_updated', list)
  36. })
  37. }, 10000)
  38. }
  39. loadHosts () {
  40. Agent.pact('getHosts').then(data => {
  41. this.setState({
  42. list: data.list,
  43. sys_hosts: data.sys_hosts,
  44. current: data.sys_hosts
  45. })
  46. })
  47. }
  48. setCurrent (hosts) {
  49. if (hosts.is_sys) {
  50. Agent.pact('getSysHosts')
  51. .then(_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 (hosts) {
  64. return !hosts || hosts.is_sys || hosts.where === 'remote' ||
  65. hosts.where === 'group'
  66. }
  67. toSave () {
  68. clearTimeout(this._t)
  69. this._t = setTimeout(() => {
  70. Agent.emit('save', 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 lang={this.state.lang}/>
  104. <EditPrompt lang={this.state.lang} list={this.state.list}/>
  105. {/*<PreferencesPrompt/>*/}
  106. </div>
  107. </div>
  108. )
  109. }
  110. }