edit.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * @author oldj
  3. * @blog http://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 util from '../../libs/util'
  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. 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. SH_event.on('add_host', () => {
  42. this.setState({
  43. show: true,
  44. add: true,
  45. })
  46. setTimeout(() => {
  47. this.tryToFocus()
  48. }, 100)
  49. })
  50. SH_event.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. SH_event.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. SH_event.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. SH_event.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. if (!confirm(SH_Agent.lang.confirm_del)) return
  110. SH_event.emit('del_host', this.current_host)
  111. this.setState({
  112. show: false,
  113. })
  114. this.clear()
  115. }
  116. static getRefreshOptions () {
  117. let k = [
  118. [0, `${SH_Agent.lang.never}`],
  119. [1, `1 ${SH_Agent.lang.hour}`],
  120. [24, `24 ${SH_Agent.lang.hours}`],
  121. [168, `7 ${SH_Agent.lang.days}`],
  122. ]
  123. if (IS_DEV) {
  124. k.splice(1, 0, [0.002778, `10s (for DEV)`]) // dev test only
  125. }
  126. return k.map(([v, n], idx) => {
  127. return (
  128. <option value={v} key={idx}>{n}</option>
  129. )
  130. })
  131. }
  132. getEditOperations () {
  133. if (this.state.add) return null
  134. return (
  135. <div>
  136. <div className="ln">
  137. <a href="#" className="del"
  138. onClick={this.confirmDel.bind(this)}
  139. >
  140. <i className="iconfont icon-delete"/>
  141. <span>{SH_Agent.lang.del_host}</span>
  142. </a>
  143. </div>
  144. </div>
  145. )
  146. }
  147. refresh () {
  148. if (this.state.is_loading) return
  149. SH_event.emit('check_host_refresh', this.current_host, true)
  150. this.setState({
  151. is_loading: true,
  152. }, () => {
  153. setTimeout(() => {
  154. this.setState({
  155. is_loading: false,
  156. })
  157. }, 1000)
  158. })
  159. }
  160. renderGroup () {
  161. if (this.state.where !== 'group') return null
  162. return <Group hosts={this.props.hosts}/>
  163. }
  164. renderRemoteInputs () {
  165. if (this.state.where !== 'remote') return null
  166. return (
  167. <div className="remote-ipts">
  168. <div className="ln">
  169. <div className="title">{SH_Agent.lang.url}</div>
  170. <div className="cnt">
  171. <input
  172. type="text"
  173. ref="url"
  174. value={this.state.url}
  175. placeholder="http://"
  176. onChange={(e) => this.setState({url: e.target.value})}
  177. onKeyDown={(e) => (e.keyCode === 13 && this.onOK()) ||
  178. (e.keyCode === 27 && this.onCancel())}
  179. />
  180. </div>
  181. </div>
  182. <div className="ln">
  183. <div className="title">{SH_Agent.lang.auto_refresh}</div>
  184. <div className="cnt">
  185. <select
  186. value={this.state.refresh_interval}
  187. onChange={(e) => this.setState(
  188. {refresh_interval: parseFloat(e.target.value) || 0})}
  189. >
  190. {EditPrompt.getRefreshOptions()}
  191. </select>
  192. <i
  193. className={classnames({
  194. iconfont: 1,
  195. 'icon-refresh': 1,
  196. 'invisible': !this.current_host ||
  197. this.state.url != this.current_host.url,
  198. 'loading': this.state.is_loading,
  199. })}
  200. title={SH_Agent.lang.refresh}
  201. onClick={() => this.refresh()}
  202. />
  203. <span className="last-refresh">
  204. {SH_Agent.lang.last_refresh}
  205. {this.state.last_refresh || 'N/A'}
  206. </span>
  207. </div>
  208. </div>
  209. </div>
  210. )
  211. }
  212. body () {
  213. return (
  214. <div ref="body">
  215. <div className="ln">
  216. <input id="ipt-local" type="radio" name="where" value="local"
  217. checked={this.state.where === 'local'}
  218. onChange={(e) => this.setState({where: e.target.value})}
  219. />
  220. <label htmlFor="ipt-local">{SH_Agent.lang.where_local}</label>
  221. <input id="ipt-remote" type="radio" name="where" value="remote"
  222. checked={this.state.where === 'remote'}
  223. onChange={(e) => this.setState({where: e.target.value})}
  224. />
  225. <label htmlFor="ipt-remote">{SH_Agent.lang.where_remote}</label>
  226. <input id="ipt-remote" type="radio" name="where" value="group"
  227. checked={this.state.where === 'group'}
  228. onChange={(e) => this.setState({where: e.target.value})}
  229. />
  230. <label htmlFor="ipt-remote">{SH_Agent.lang.where_group}</label>
  231. </div>
  232. <div className="ln">
  233. <div className="title">{SH_Agent.lang.host_title}</div>
  234. <div className="cnt">
  235. <input
  236. type="text"
  237. ref="title"
  238. name="text"
  239. value={this.state.title}
  240. onChange={(e) => this.setState({title: e.target.value})}
  241. onKeyDown={(e) => (e.keyCode === 13 && this.onOK() ||
  242. e.keyCode === 27 && this.onCancel())}
  243. />
  244. </div>
  245. </div>
  246. {this.renderRemoteInputs()}
  247. {this.renderGroup()}
  248. {this.getEditOperations()}
  249. </div>
  250. )
  251. }
  252. render () {
  253. return (
  254. <MyFrame
  255. show={this.state.show}
  256. head={SH_Agent.lang[this.state.add ? 'add_host' : 'edit_host']}
  257. body={this.body()}
  258. onOK={() => this.onOK()}
  259. onCancel={() => this.onCancel()}
  260. />
  261. )
  262. }
  263. }