app.js 3.2 KB

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