1
0

App.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import { notification } from 'antd'
  8. import Panel from './panel/Panel'
  9. import Content from './content/Content'
  10. import SudoPrompt from './frame/SudoPrompt'
  11. import EditPrompt from './frame/EditPrompt'
  12. import About from './about/About'
  13. import PreferencesPrompt from './frame/PreferencesPrompt'
  14. import Agent from './Agent'
  15. import { reg as events_reg } from './events/index'
  16. import './App.less'
  17. export default class App extends React.Component {
  18. constructor (props) {
  19. super(props)
  20. this.state = {
  21. list: [], // 用户的 hosts 列表
  22. sys_hosts: {}, // 系统 hosts
  23. current: {}, // 当前 hosts
  24. lang: {}, // 语言
  25. just_added_id: null
  26. }
  27. this.is_dragging = false
  28. this.loadHosts()
  29. Agent.pact('getPref')
  30. .then(pref => {
  31. return pref.user_language || 'en'
  32. })
  33. .then(l => {
  34. Agent.pact('getLang', l).then(lang => {
  35. this.setState({lang})
  36. })
  37. })
  38. events_reg(this)
  39. Agent.on('drag_start', () => {
  40. this.is_dragging = true
  41. console.log('drag_start')
  42. })
  43. Agent.on('drag_end', () => {
  44. this.is_dragging = false
  45. console.log('drag_end')
  46. })
  47. Agent.on('err', e => {
  48. console.log(e)
  49. notification.error({
  50. message: e.title,
  51. description: e.content,
  52. duration: 10,
  53. style: {backgroundColor: '#fff0f0'}
  54. })
  55. })
  56. setInterval(() => {
  57. let list = this.state.list
  58. if (this.is_dragging || !list || list.length === 0) return
  59. console.log('checkNeedRemoteRefresh')
  60. Agent.pact('checkNeedRemoteRefresh', list)
  61. .then(list => {
  62. Agent.emit('refresh_end')
  63. if (!list) return
  64. Agent.emit('list_updated', list)
  65. })
  66. .catch(e => {
  67. console.log(e)
  68. })
  69. }, 60 * 1000)
  70. }
  71. loadHosts () {
  72. Agent.pact('getHosts').then(data => {
  73. let state = {
  74. list: data.list,
  75. sys_hosts: data.sys_hosts
  76. }
  77. let current = this.state.current
  78. state.current = data.list.find(item => item.id === current.id) ||
  79. data.sys_hosts
  80. this.setState(state)
  81. })
  82. }
  83. setCurrent (hosts) {
  84. if (hosts.is_sys) {
  85. Agent.pact('getSysHosts')
  86. .then(_hosts => {
  87. this.setState({
  88. sys_hosts: _hosts,
  89. current: _hosts
  90. })
  91. })
  92. } else {
  93. this.setState({
  94. current: hosts
  95. })
  96. }
  97. }
  98. static isReadOnly (hosts) {
  99. return !hosts || hosts.is_sys || hosts.where === 'remote' ||
  100. hosts.where === 'group'
  101. }
  102. toSave () {
  103. clearTimeout(this._t)
  104. this._t = setTimeout(() => {
  105. Agent.emit('save', this.state.list)
  106. }, 1000)
  107. }
  108. setHostsContent (v) {
  109. if (this.state.current.content === v) return // not changed
  110. let current = Object.assign({}, this.state.current, {
  111. content: v || ''
  112. })
  113. let list = this.state.list.slice(0)
  114. let idx = list.findIndex(i => i.id === current.id)
  115. if (idx !== -1) {
  116. list.splice(idx, 1, current)
  117. }
  118. this.setState({
  119. current,
  120. list
  121. }, () => {
  122. this.toSave()
  123. })
  124. }
  125. justAdd (id) {
  126. this.setState({
  127. just_added_id: id
  128. })
  129. }
  130. componentDidMount () {
  131. window.addEventListener('keydown', (e) => {
  132. if (e.keyCode === 27) {
  133. Agent.emit('esc')
  134. }
  135. }, false)
  136. window.addEventListener('mouseup', () => {
  137. Agent.emit('drag_end')
  138. })
  139. }
  140. render () {
  141. let current = this.state.current
  142. return (
  143. <div id="app" className={'platform-' + Agent.platform}>
  144. <SudoPrompt lang={this.state.lang}/>
  145. <EditPrompt
  146. lang={this.state.lang}
  147. list={this.state.list}
  148. justAdd={this.justAdd.bind(this)}
  149. />
  150. <PreferencesPrompt
  151. lang={this.state.lang}
  152. />
  153. <Panel
  154. list={this.state.list}
  155. sys_hosts={this.state.sys_hosts}
  156. current={current}
  157. setCurrent={this.setCurrent.bind(this)}
  158. lang={this.state.lang}
  159. just_added_id={this.state.just_added_id}
  160. justAdd={this.justAdd.bind(this)}
  161. />
  162. <Content
  163. current={current}
  164. readonly={App.isReadOnly(current)}
  165. setHostsContent={this.setHostsContent.bind(this)}
  166. lang={this.state.lang}
  167. />
  168. <About lang={this.state.lang}/>
  169. </div>
  170. )
  171. }
  172. }