app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 util from '../libs/util'
  13. import './app.less'
  14. class App extends React.Component {
  15. constructor (props) {
  16. super(props)
  17. let _data = SH_Agent.getHosts()
  18. this.state = {
  19. hosts: _data,
  20. current: _data.sys
  21. }
  22. SH_event.on('after_apply', () => {
  23. if (this.state.current.is_sys) {
  24. // 重新读取
  25. this.setState({
  26. current: SH_Agent.getSysHosts()
  27. })
  28. }
  29. })
  30. ipcRenderer.on('to_import', (e, fn) => {
  31. if (!confirm(SH_Agent.lang.confirm_import)) return
  32. SH_Agent.readFile(fn, (err, cnt) => {
  33. if (err) {
  34. alert(err.message || 'Import Error!')
  35. return
  36. }
  37. let data
  38. try {
  39. data = JSON.parse(cnt)
  40. } catch (e) {
  41. console.log(e)
  42. alert(
  43. e.message || 'Bad format, the import file should be a JSON file.')
  44. return
  45. }
  46. if (!data.list || !Array.isArray(data.list)) {
  47. alert('Bad format, the data JSON should have a [list] field.')
  48. return
  49. }
  50. data.list.map(item => {
  51. if (!item.id) {
  52. item.id = util.makeId()
  53. }
  54. })
  55. this.setState({
  56. hosts: Object.assign({}, this.state.hosts, {list: data.list})
  57. }, () => {
  58. SH_event.emit('imported')
  59. })
  60. console.log('imported.')
  61. })
  62. })
  63. ipcRenderer.send('reg_renderer')
  64. }
  65. setCurrent (host) {
  66. this.setState({
  67. current: host.is_sys ? SH_Agent.getSysHosts() : host
  68. })
  69. }
  70. static isReadOnly (host) {
  71. return host.is_sys || host.where === 'remote'
  72. }
  73. toSave () {
  74. clearTimeout(this._t)
  75. this._t = setTimeout(() => {
  76. SH_event.emit('change')
  77. }, 1000)
  78. }
  79. setHostContent (v) {
  80. if (this.state.current.content === v) return // not changed
  81. this.state.current.content = v || ''
  82. this.toSave()
  83. }
  84. componentDidMount () {
  85. window.addEventListener('keydown', (e) => {
  86. if (e.keyCode === 27) {
  87. SH_event.emit('esc')
  88. }
  89. }, false)
  90. }
  91. render () {
  92. let current = this.state.current
  93. return (
  94. <div id="app" className={'platform-' + platform}>
  95. <Panel hosts={this.state.hosts} current={current}
  96. setCurrent={this.setCurrent.bind(this)}/>
  97. <Content current={current} readonly={App.isReadOnly(current)}
  98. setHostContent={this.setHostContent.bind(this)}/>
  99. <div className="frames">
  100. <SudoPrompt/>
  101. <EditPrompt hosts={this.state.hosts}/>
  102. <PreferencesPrompt/>
  103. </div>
  104. </div>
  105. )
  106. }
  107. }
  108. export default App