App.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. let {current, list} = this.state
  110. if (current.content === v) return // not changed
  111. current = Object.assign({}, current, {
  112. content: v || ''
  113. })
  114. list = list.slice(0)
  115. let idx = list.findIndex(i => i.id === current.id)
  116. if (idx !== -1) {
  117. list.splice(idx, 1, current)
  118. }
  119. this.setState({
  120. current,
  121. list
  122. }, () => {
  123. this.toSave()
  124. })
  125. }
  126. justAdd (id) {
  127. this.setState({
  128. just_added_id: id
  129. })
  130. }
  131. componentDidMount () {
  132. window.addEventListener('keydown', (e) => {
  133. if (e.keyCode === 27) {
  134. Agent.emit('esc')
  135. }
  136. }, false)
  137. window.addEventListener('mouseup', () => {
  138. Agent.emit('drag_end')
  139. })
  140. }
  141. render () {
  142. let current = this.state.current
  143. return (
  144. <div id="app" className={'platform-' + Agent.platform}>
  145. <SudoPrompt lang={this.state.lang}/>
  146. <EditPrompt
  147. lang={this.state.lang}
  148. list={this.state.list}
  149. justAdd={this.justAdd.bind(this)}
  150. />
  151. <PreferencesPrompt
  152. lang={this.state.lang}
  153. />
  154. <Panel
  155. list={this.state.list}
  156. sys_hosts={this.state.sys_hosts}
  157. current={current}
  158. setCurrent={this.setCurrent.bind(this)}
  159. lang={this.state.lang}
  160. just_added_id={this.state.just_added_id}
  161. justAdd={this.justAdd.bind(this)}
  162. />
  163. <Content
  164. current={current}
  165. readonly={App.isReadOnly(current)}
  166. setHostsContent={this.setHostsContent.bind(this)}
  167. lang={this.state.lang}
  168. />
  169. <About lang={this.state.lang}/>
  170. </div>
  171. )
  172. }
  173. }