edit.js 7.4 KB

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