edit.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import MyFrame from './frame'
  8. import classnames from 'classnames'
  9. import Group from './group'
  10. import Agent from '../Agent'
  11. import makeId from '../../app/libs/make-id'
  12. import './edit.less'
  13. export default class EditPrompt extends React.Component {
  14. constructor (props) {
  15. super(props)
  16. this.state = {
  17. show: false,
  18. is_add: true,
  19. where: 'local',
  20. title: '',
  21. url: '',
  22. last_refresh: null,
  23. refresh_interval: 0,
  24. is_loading: false
  25. }
  26. this.current_hosts = null
  27. }
  28. tryToFocus () {
  29. let el = this.refs.body && this.refs.body.querySelector('input[type=text]')
  30. el && el.focus()
  31. }
  32. clear () {
  33. this.setState({
  34. where: 'local',
  35. title: '',
  36. url: '',
  37. last_refresh: null,
  38. refresh_interval: 0
  39. })
  40. }
  41. componentDidMount () {
  42. Agent.on('add_hosts', () => {
  43. this.setState({
  44. show: true,
  45. is_add: true
  46. })
  47. setTimeout(() => {
  48. this.tryToFocus()
  49. }, 100)
  50. })
  51. Agent.on('edit_hosts', (hosts) => {
  52. this.current_hosts = hosts
  53. this.setState({
  54. show: true,
  55. is_add: false,
  56. where: hosts.where || 'local',
  57. title: hosts.title || '',
  58. url: hosts.url || '',
  59. last_refresh: hosts.last_refresh || null,
  60. refresh_interval: hosts.refresh_interval || 0
  61. })
  62. setTimeout(() => {
  63. this.tryToFocus()
  64. }, 100)
  65. })
  66. Agent.on('loading_done', (old_hosts, data) => {
  67. if (old_hosts === this.current_hosts) {
  68. this.setState({
  69. last_refresh: data.last_refresh,
  70. is_loading: false
  71. })
  72. Agent.emit('host_refreshed', data, this.current_hosts)
  73. }
  74. })
  75. }
  76. onOK () {
  77. this.setState({
  78. title: (this.state.title || '').replace(/^\s+|\s+$/g, ''),
  79. url: (this.state.url || '').replace(/^\s+|\s+$/g, '')
  80. })
  81. if (this.state.title === '') {
  82. this.refs.title.focus()
  83. return false
  84. }
  85. if (this.state.where === 'remote' && this.state.url === '') {
  86. this.refs.url.focus()
  87. return false
  88. }
  89. let data = Object.assign({}, this.current_hosts, this.state,
  90. this.state.is_add ? {
  91. id: makeId(),
  92. content: `# ${this.state.title}`,
  93. on: false
  94. } : {})
  95. if (!data.id) data.id = makeId()
  96. delete data['is_add']
  97. Agent.emit('update_hosts', data)
  98. this.setState({
  99. show: false
  100. })
  101. this.clear()
  102. }
  103. onCancel () {
  104. this.setState({
  105. show: false
  106. })
  107. this.clear()
  108. }
  109. confirmDel () {
  110. let {lang} = this.props
  111. if (!confirm(lang.confirm_del)) return
  112. Agent.emit('del_hosts', this.current_hosts)
  113. this.setState({
  114. show: false
  115. })
  116. this.clear()
  117. }
  118. getRefreshOptions () {
  119. let {lang} = this.props
  120. let k = [
  121. [0, `${lang.never}`],
  122. [1, `1 ${lang.hour}`],
  123. [24, `24 ${lang.hours}`],
  124. [168, `7 ${lang.days}`]
  125. ]
  126. if (Agent.IS_DEV) {
  127. k.splice(1, 0, [0.002778, `10s (for DEV)`]) // dev test only
  128. }
  129. return k.map(([v, n], idx) => {
  130. return (
  131. <option value={v} key={idx}>{n}</option>
  132. )
  133. })
  134. }
  135. getEditOperations () {
  136. if (this.state.is_add) return null
  137. let {lang} = this.props
  138. return (
  139. <div>
  140. <div className="ln">
  141. <a href="#" className="del"
  142. onClick={this.confirmDel.bind(this)}
  143. >
  144. <i className="iconfont icon-delete"/>
  145. <span>{lang.del_hosts}</span>
  146. </a>
  147. </div>
  148. </div>
  149. )
  150. }
  151. refresh () {
  152. if (this.state.is_loading) return
  153. Agent.emit('check_host_refresh', this.current_hosts, true)
  154. this.setState({
  155. is_loading: true
  156. }, () => {
  157. setTimeout(() => {
  158. this.setState({
  159. is_loading: false
  160. })
  161. }, 1000)
  162. })
  163. }
  164. renderGroup () {
  165. if (this.state.where !== 'group') return null
  166. return <Group list={this.props.list}/>
  167. }
  168. renderRemoteInputs () {
  169. if (this.state.where !== 'remote') return null
  170. let {lang} = this.props
  171. return (
  172. <div className="remote-ipts">
  173. <div className="ln">
  174. <div className="title">{lang.url}</div>
  175. <div className="cnt">
  176. <input
  177. type="text"
  178. ref="url"
  179. value={this.state.url}
  180. placeholder="http://"
  181. onChange={(e) => this.setState({url: e.target.value})}
  182. onKeyDown={(e) => (e.keyCode === 13 && this.onOK()) ||
  183. (e.keyCode === 27 && this.onCancel())}
  184. />
  185. </div>
  186. </div>
  187. <div className="ln">
  188. <div className="title">{lang.auto_refresh}</div>
  189. <div className="cnt">
  190. <select
  191. value={this.state.refresh_interval}
  192. onChange={(e) => this.setState(
  193. {refresh_interval: parseFloat(e.target.value) || 0})}
  194. >
  195. {this.getRefreshOptions()}
  196. </select>
  197. <i
  198. className={classnames({
  199. iconfont: 1,
  200. 'icon-refresh': 1,
  201. 'invisible': !this.current_hosts ||
  202. this.state.url !== this.current_hosts.url,
  203. 'loading': this.state.is_loading
  204. })}
  205. title={lang.refresh}
  206. onClick={() => this.refresh()}
  207. />
  208. <span className="last-refresh">
  209. {lang.last_refresh}
  210. {this.state.last_refresh || 'N/A'}
  211. </span>
  212. </div>
  213. </div>
  214. </div>
  215. )
  216. }
  217. body () {
  218. let {lang} = this.props
  219. return (
  220. <div ref="body">
  221. <div className="ln">
  222. <input id="ipt-local" type="radio" name="where" value="local"
  223. checked={this.state.where === 'local'}
  224. onChange={(e) => this.setState({where: e.target.value})}
  225. />
  226. <label htmlFor="ipt-local">{lang.where_local}</label>
  227. <input id="ipt-remote" type="radio" name="where" value="remote"
  228. checked={this.state.where === 'remote'}
  229. onChange={(e) => this.setState({where: e.target.value})}
  230. />
  231. <label htmlFor="ipt-remote">{lang.where_remote}</label>
  232. <input id="ipt-remote" type="radio" name="where" value="group"
  233. checked={this.state.where === 'group'}
  234. onChange={(e) => this.setState({where: e.target.value})}
  235. />
  236. <label htmlFor="ipt-remote">{lang.where_group}</label>
  237. </div>
  238. <div className="ln">
  239. <div className="title">{lang.host_title}</div>
  240. <div className="cnt">
  241. <input
  242. type="text"
  243. ref="title"
  244. name="text"
  245. value={this.state.title}
  246. onChange={(e) => this.setState({title: e.target.value})}
  247. onKeyDown={(e) => (e.keyCode === 13 && this.onOK() ||
  248. e.keyCode === 27 && this.onCancel())}
  249. />
  250. </div>
  251. </div>
  252. {this.renderRemoteInputs()}
  253. {this.renderGroup()}
  254. {this.getEditOperations()}
  255. </div>
  256. )
  257. }
  258. render () {
  259. let {lang} = this.props
  260. return (
  261. <MyFrame
  262. show={this.state.show}
  263. head={lang[this.state.is_add ? 'add_hosts' : 'edit_hosts']}
  264. body={this.body()}
  265. onOK={() => this.onOK()}
  266. onCancel={() => this.onCancel()}
  267. lang={this.props.lang}
  268. />
  269. )
  270. }
  271. }