list.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 Agent from '../../../renderer/Agent'
  9. import update from 'react-addons-update'
  10. import './list.less'
  11. class List extends React.Component {
  12. constructor (props) {
  13. super(props)
  14. this.state = {
  15. current: this.props.current,
  16. list: this.props.list,
  17. sys: this.props.sys
  18. }
  19. this.last_content = this.props.sys.content
  20. // auto check refresh
  21. setTimeout(() => {
  22. this.autoCheckRefresh()
  23. }, 1000 * 5)
  24. }
  25. /**
  26. * 检查当前 host 是否需要从网络下载更新
  27. * @param host
  28. * @param force {Boolean} 如果为 true,则只要是 remote 且 refresh_interval != 0,则强制更新
  29. */
  30. checkUpdateHost (host, force = false) {
  31. Agent.emit('check_host_refresh', host, force)
  32. }
  33. autoCheckRefresh () {
  34. let remote_idx = -1
  35. this.state.list.map((host, idx) => {
  36. if (host.where === 'remote') {
  37. remote_idx++
  38. }
  39. setTimeout(() => {
  40. Agent.emit('check_host_refresh', host)
  41. }, 1000 * 5 * remote_idx + idx)
  42. })
  43. // let wait = 1000 * 60 * 10;
  44. let wait = 1000 * 30 // test only
  45. setTimeout(() => {
  46. this.autoCheckRefresh()
  47. }, wait)
  48. }
  49. apply (content, success) {
  50. Agent.emit('apply', content, () => {
  51. this.last_content = content
  52. success()
  53. Agent.emit('save_data', this.state.list)
  54. Agent.notify({
  55. message: 'host updated.'
  56. })
  57. })
  58. }
  59. selectOne (host) {
  60. this.setState({
  61. current: host
  62. })
  63. this.props.setCurrent(host)
  64. }
  65. toggleOne (idx, success) {
  66. let content = this.getOnContent(idx)
  67. this.apply(content, () => {
  68. let choice_mode = Agent.pref.get('choice_mode')
  69. if (choice_mode === 'single') {
  70. // 单选模式
  71. this.setState({
  72. list: this.state.list.map((item, _idx) => {
  73. if (idx !== _idx) {
  74. item.on = false
  75. }
  76. return item
  77. })
  78. })
  79. }
  80. if (typeof success === 'function') {
  81. success()
  82. }
  83. })
  84. }
  85. getOnItems (idx = -1) {
  86. let choice_mode = Agent.pref.get('choice_mode')
  87. return this.state.list.filter((item, _idx) => {
  88. if (choice_mode === 'single') {
  89. return !item.on && _idx === idx
  90. } else {
  91. return (item.on && _idx !== idx) || (!item.on && _idx === idx)
  92. }
  93. })
  94. }
  95. getOnContent (idx = -1) {
  96. let contents = this.getOnItems(idx).map((item) => {
  97. return item.content || ''
  98. })
  99. contents.unshift('# SwitchHosts!')
  100. return contents.join(`\n\n`)
  101. }
  102. customItems () {
  103. return this.state.list.map((item, idx) => {
  104. return (
  105. <ListItem
  106. data={item}
  107. idx={idx}
  108. selectOne={this.selectOne.bind(this)}
  109. current={this.state.current}
  110. onToggle={(success) => this.toggleOne(idx, success)}
  111. key={'host-' + idx}
  112. dragOrder={(sidx, tidx) => this.dragOrder(sidx, tidx)}
  113. />
  114. )
  115. })
  116. }
  117. dragOrder (source_idx, target_idx) {
  118. let source = this.state.list[source_idx]
  119. let target = this.state.list[target_idx]
  120. let list = this.state.list.filter((item, idx) => idx !== source_idx)
  121. let new_target_idx = list.findIndex((item) => item === target)
  122. let to_idx
  123. if (source_idx < target_idx) {
  124. // append
  125. to_idx = new_target_idx + 1
  126. } else {
  127. // insert before
  128. to_idx = new_target_idx
  129. }
  130. list.splice(to_idx, 0, source)
  131. this.setState({
  132. list: list
  133. })
  134. setTimeout(() => {
  135. Agent.emit('change')
  136. }, 100)
  137. }
  138. componentDidMount () {
  139. }
  140. render () {
  141. return (
  142. <div id="sh-list">
  143. <ListItem
  144. data={this.props.sys}
  145. lang={this.props.lang}
  146. selectOne={this.selectOne.bind(this)}
  147. current={this.state.current}
  148. sys="1"/>
  149. <div ref="items" className="custom-items">
  150. {this.customItems()}
  151. </div>
  152. </div>
  153. )
  154. }
  155. }
  156. export default List