list.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. import React from 'react';
  7. import ListItem from './list_item';
  8. import update from 'react-addons-update';
  9. import './list.less';
  10. class List extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. current: this.props.current,
  15. list: this.props.hosts.list
  16. };
  17. this.last_content = this.props.hosts.sys.content;
  18. SH_event.on('imported', () => {
  19. this.setState({
  20. current: this.props.current,
  21. list: this.props.hosts.list
  22. }, () => {
  23. SH_event.emit('change');
  24. });
  25. });
  26. SH_event.on('change', () => {
  27. SH_event.emit('save_data', this.state.list);
  28. let content = this.getOnContent();
  29. if (content !== this.last_content) {
  30. SH_event.emit('apply', content, () => {
  31. this.last_content = content;
  32. });
  33. }
  34. });
  35. SH_event.on('host_added', (data) => {
  36. this.setState({
  37. list: update(this.state.list, {$push: [data]})
  38. }, () => {
  39. this.selectOne(data);
  40. setTimeout(() => {
  41. SH_event.emit('change', true);
  42. let el = this.refs.items;
  43. el.scrollTop = document.querySelector('.list-item.selected').offsetTop - el.offsetHeight + 50;
  44. this.checkUpdateHost(data);
  45. }, 100);
  46. });
  47. });
  48. SH_event.on('host_edited', (data, host) => {
  49. let idx = this.state.list.findIndex((item) => item == host);
  50. if (idx == -1) return;
  51. this.setState({
  52. list: update(this.state.list, {$splice: [[idx, 1, data]]})
  53. }, () => {
  54. this.selectOne(data);
  55. setTimeout(() => {
  56. SH_event.emit('change', true);
  57. this.checkUpdateHost(data, true);
  58. }, 100);
  59. });
  60. });
  61. SH_event.on('host_refreshed', (data, host) => {
  62. let idx = this.state.list.findIndex((item) => item == host);
  63. if (idx == -1) return;
  64. this.setState({
  65. list: update(this.state.list, {$splice: [[idx, 1, data]]})
  66. }, () => {
  67. setTimeout(() => {
  68. if (host === this.state.current) {
  69. this.selectOne(data);
  70. }
  71. SH_event.emit('change', true);
  72. }, 100);
  73. });
  74. });
  75. SH_event.on('del_host', (host) => {
  76. let list = this.state.list;
  77. let idx_to_del = list.findIndex((item) => {
  78. return host === item;
  79. });
  80. if (idx_to_del == -1) return;
  81. // list.splice(idx_to_del, 1);
  82. this.setState({
  83. list: update(this.state.list, {$splice: [[idx_to_del, 1]]})
  84. // list: this.state.list.filter((item, idx) => idx != idx_to_del)
  85. }, () => {
  86. setTimeout(() => {
  87. let list = this.state.list;
  88. let next_host = list[idx_to_del] || list[list.length - 1] || this.props.hosts.sys;
  89. if (next_host) {
  90. this.selectOne(next_host);
  91. }
  92. SH_event.emit('change');
  93. }, 100);
  94. });
  95. });
  96. SH_event.on('get_on_hosts', (callback) => {
  97. callback(this.getOnItems());
  98. });
  99. ipcRenderer.on('get_host_list', () => {
  100. ipcRenderer.send('send_host_list', this.state.list);
  101. });
  102. ipcRenderer.on('get_export_data', (e, fn) => {
  103. let data = Object.assign({}, {
  104. version: require('../../configs').version,
  105. list: this.state.list.map(item => {
  106. let new_item = Object.assign({}, item);
  107. new_item.on = false;
  108. return new_item;
  109. })
  110. });
  111. ipcRenderer.send('export_data', fn, JSON.stringify(data));
  112. });
  113. SH_event.on('top_toggle', (on, items) => {
  114. this.setState({
  115. list: this.state.list.map((item) => {
  116. if (items.findIndex((i) => i == item) > -1) {
  117. item.on = on;
  118. }
  119. return item;
  120. })
  121. }, () => {
  122. SH_event.emit('change');
  123. });
  124. });
  125. SH_event.on('loading_done', (host, data) => {
  126. SH_event.emit('host_refreshed', data, host);
  127. // if (host == this.state.current || host._ == this.state.current) {
  128. // setTimeout(() => {
  129. // this.selectOne(this.state.current);
  130. // }, 100);
  131. // }
  132. if (data.error) {
  133. console.log(data.error);
  134. }
  135. });
  136. // auto check refresh
  137. setTimeout(() => {
  138. this.autoCheckRefresh();
  139. }, 1000 * 5);
  140. }
  141. /**
  142. * 检查当前 host 是否需要从网络下载更新
  143. * @param host
  144. * @param force {Boolean} 如果为 true,则只要是 remote 且 refresh_interval != 0,则强制更新
  145. */
  146. checkUpdateHost(host, force = false) {
  147. SH_event.emit('check_host_refresh', host, force);
  148. }
  149. autoCheckRefresh() {
  150. let remote_idx = -1;
  151. this.state.list.map((host, idx) => {
  152. if (host.where === 'remote') {
  153. remote_idx++;
  154. }
  155. setTimeout(() => {
  156. SH_event.emit('check_host_refresh', host);
  157. }, 1000 * 5 * remote_idx + idx);
  158. });
  159. // let wait = 1000 * 60 * 10;
  160. let wait = 1000 * 30; // test only
  161. setTimeout(() => {
  162. this.autoCheckRefresh();
  163. }, wait);
  164. }
  165. apply(content, success) {
  166. SH_event.emit('apply', content, () => {
  167. this.last_content = content;
  168. success();
  169. SH_event.emit('save_data', this.state.list);
  170. SH_Agent.notify({
  171. message: 'host updated.'
  172. });
  173. });
  174. }
  175. selectOne(host) {
  176. this.setState({
  177. current: host
  178. });
  179. this.props.setCurrent(host);
  180. }
  181. toggleOne(idx, success) {
  182. let content = this.getOnContent(idx);
  183. this.apply(content, () => {
  184. let choice_mode = SH_Agent.pref.get('choice_mode');
  185. if (choice_mode === 'single') {
  186. // 单选模式
  187. this.setState({
  188. list: this.state.list.map((item, _idx) => {
  189. if (idx != _idx) {
  190. item.on = false;
  191. }
  192. return item;
  193. })
  194. });
  195. }
  196. if (typeof success === 'function') {
  197. success();
  198. }
  199. });
  200. }
  201. getOnItems(idx = -1) {
  202. let choice_mode = SH_Agent.pref.get('choice_mode');
  203. return this.state.list.filter((item, _idx) => {
  204. if (choice_mode === 'single') {
  205. return !item.on && _idx == idx;
  206. } else {
  207. return (item.on && _idx != idx) || (!item.on && _idx == idx);
  208. }
  209. });
  210. }
  211. getOnContent(idx = -1) {
  212. let contents = this.getOnItems(idx).map((item) => {
  213. return item.content || '';
  214. });
  215. contents.unshift('# SwitchHosts!');
  216. return contents.join(`\n\n`);
  217. }
  218. customItems() {
  219. return this.state.list.map((item, idx) => {
  220. return (
  221. <ListItem
  222. data={item}
  223. idx={idx}
  224. selectOne={this.selectOne.bind(this)}
  225. current={this.state.current}
  226. onToggle={(success)=> this.toggleOne(idx, success)}
  227. key={'host-' + idx}
  228. dragOrder={(sidx, tidx) => this.dragOrder(sidx, tidx)}
  229. />
  230. )
  231. });
  232. }
  233. dragOrder(source_idx, target_idx) {
  234. let source = this.state.list[source_idx];
  235. let target = this.state.list[target_idx];
  236. let list = this.state.list.filter((item, idx) => idx != source_idx);
  237. let new_target_idx = list.findIndex((item) => item == target);
  238. let to_idx;
  239. if (source_idx < target_idx) {
  240. // append
  241. to_idx = new_target_idx + 1;
  242. } else {
  243. // insert before
  244. to_idx = new_target_idx;
  245. }
  246. list.splice(to_idx, 0, source);
  247. this.setState({
  248. list: list
  249. });
  250. setTimeout(() => {
  251. SH_event.emit('change');
  252. }, 100);
  253. }
  254. componentDidMount() {
  255. }
  256. render() {
  257. return (
  258. <div id="sh-list">
  259. <ListItem
  260. data={this.props.hosts.sys}
  261. selectOne={this.selectOne.bind(this)}
  262. current={this.state.current}
  263. sys="1"/>
  264. <div ref="items" className="custom-items">
  265. {this.customItems()}
  266. </div>
  267. </div>
  268. );
  269. }
  270. }
  271. export default List;