app.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 NotificationSystem from 'react-notification-system'
  13. import Agent from './Agent'
  14. import { reg as events_reg } from './events/index'
  15. import notificationStyle from './misc/notificationStyle'
  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._notificationSystem = null
  29. this.loadHosts()
  30. Agent.pact('getPref')
  31. .then(pref => {
  32. return pref.user_language || 'en'
  33. })
  34. .then(l => {
  35. Agent.pact('getLang', l).then(lang => {
  36. this.setState({lang})
  37. })
  38. })
  39. events_reg(this)
  40. Agent.on('drag_start', () => {
  41. this.is_dragging = true
  42. console.log('drag_start')
  43. })
  44. Agent.on('drag_end', () => {
  45. this.is_dragging = false
  46. console.log('drag_end')
  47. })
  48. Agent.on('err', e => {
  49. console.log(e)
  50. this._notificationSystem.addNotification({
  51. title: e.title,
  52. message: e.content,
  53. position: 'tr',
  54. autoDismiss: 10,
  55. level: 'error'
  56. })
  57. })
  58. setInterval(() => {
  59. let list = this.state.list
  60. if (this.is_dragging || !list || list.length === 0) return
  61. console.log('checkNeedRemoteRefresh')
  62. Agent.pact('checkNeedRemoteRefresh', list)
  63. .then(list => {
  64. Agent.emit('refresh_end')
  65. if (!list) return
  66. Agent.emit('list_updated', list)
  67. })
  68. .catch(e => {
  69. console.log(e)
  70. })
  71. }, 60 * 1000)
  72. }
  73. loadHosts () {
  74. Agent.pact('getHosts').then(data => {
  75. let state = {
  76. list: data.list,
  77. sys_hosts: data.sys_hosts
  78. }
  79. let current = this.state.current
  80. state.current = data.list.find(item => item.id === current.id) ||
  81. data.sys_hosts
  82. this.setState(state)
  83. })
  84. }
  85. setCurrent (hosts) {
  86. if (hosts.is_sys) {
  87. Agent.pact('getSysHosts')
  88. .then(_hosts => {
  89. this.setState({
  90. sys_hosts: _hosts,
  91. current: _hosts
  92. })
  93. })
  94. } else {
  95. this.setState({
  96. current: hosts
  97. })
  98. }
  99. }
  100. static isReadOnly (hosts) {
  101. return !hosts || hosts.is_sys || hosts.where === 'remote' ||
  102. hosts.where === 'group'
  103. }
  104. toSave () {
  105. clearTimeout(this._t)
  106. this._t = setTimeout(() => {
  107. Agent.emit('save', this.state.list)
  108. }, 1000)
  109. }
  110. setHostsContent (v) {
  111. if (this.state.current.content === v) return // not changed
  112. let current = Object.assign({}, this.state.current, {
  113. content: v || ''
  114. })
  115. let list = this.state.list.slice(0)
  116. let idx = list.findIndex(i => i.id === current.id)
  117. if (idx !== -1) {
  118. list.splice(idx, 1, current)
  119. }
  120. this.setState({
  121. current,
  122. list
  123. }, () => {
  124. this.toSave()
  125. })
  126. }
  127. justAdd (id) {
  128. this.setState({
  129. just_added_id: id
  130. })
  131. }
  132. componentDidMount () {
  133. this._notificationSystem = this.refs.notificationSystem
  134. window.addEventListener('keydown', (e) => {
  135. if (e.keyCode === 27) {
  136. Agent.emit('esc')
  137. }
  138. }, false)
  139. window.addEventListener('mouseup', () => {
  140. Agent.emit('drag_end')
  141. })
  142. }
  143. render () {
  144. let current = this.state.current
  145. return (
  146. <div id="app" className={'platform-' + Agent.platform}>
  147. <NotificationSystem ref="notificationSystem" style={notificationStyle}/>
  148. <Panel
  149. list={this.state.list}
  150. sys_hosts={this.state.sys_hosts}
  151. current={current}
  152. setCurrent={this.setCurrent.bind(this)}
  153. lang={this.state.lang}
  154. just_added_id={this.state.just_added_id}
  155. justAdd={this.justAdd.bind(this)}
  156. />
  157. <Content
  158. current={current}
  159. readonly={App.isReadOnly(current)}
  160. setHostsContent={this.setHostsContent.bind(this)}
  161. lang={this.state.lang}
  162. />
  163. <div className="frames">
  164. <SudoPrompt lang={this.state.lang}/>
  165. <EditPrompt
  166. lang={this.state.lang}
  167. list={this.state.list}
  168. justAdd={this.justAdd.bind(this)}
  169. />
  170. <PreferencesPrompt
  171. lang={this.state.lang}
  172. />
  173. </div>
  174. </div>
  175. )
  176. }
  177. }