list-item.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import classnames from 'classnames'
  8. import Agent from '../Agent'
  9. import './list-item.less'
  10. export default class ListItem extends React.Component {
  11. constructor (props) {
  12. super(props)
  13. this.is_sys = !!this.props.sys
  14. this.state = {}
  15. }
  16. getTitle () {
  17. let {lang} = this.props
  18. return this.is_sys ? lang.sys_host_title : this.props.data.title ||
  19. lang.untitled
  20. }
  21. beSelected () {
  22. this.props.setCurrent(this.props.data)
  23. }
  24. toggle () {
  25. let on = !this.props.data.on
  26. Agent.emit('toggle_hosts', this.props.data, on)
  27. }
  28. toEdit () {
  29. Agent.emit('edit_hosts', this.props.data)
  30. }
  31. componentDidMount () {
  32. Agent.on('select', id => {
  33. if (id && id === this.props.data.id) {
  34. this.beSelected()
  35. this.el && this.el.scrollIntoView()
  36. }
  37. })
  38. }
  39. render () {
  40. let {data, sys, current} = this.props
  41. let is_selected = data === current
  42. if (!data) return null
  43. return (
  44. <div className={classnames({
  45. 'list-item': 1
  46. //, 'hidden': !this.isMatched()
  47. , 'sys-hosts': sys
  48. , 'selected': is_selected
  49. })}
  50. onClick={this.beSelected.bind(this)}
  51. ref={el => this.el = el}
  52. >
  53. {sys ? null : (
  54. <div>
  55. <i className={classnames({
  56. 'switch': 1
  57. , 'iconfont': 1
  58. , 'icon-on': data.on
  59. , 'icon-off': !data.on
  60. })}
  61. onClick={this.toggle.bind(this)}
  62. />
  63. <i
  64. className="iconfont icon-edit"
  65. onClick={this.toEdit.bind(this)}
  66. />
  67. </div>
  68. )}
  69. <i className={classnames({
  70. 'iconfont': 1
  71. , 'item-icon': 1
  72. , 'icon-warn': !!data.error
  73. , 'icon-file': !sys && !data.error && data.where !== 'group'
  74. , 'icon-files': data.where === 'group'
  75. , 'icon-sysserver': sys && !data.error
  76. })}
  77. title={data.error || ''}
  78. />
  79. <span>{this.getTitle()}</span>
  80. </div>
  81. )
  82. }
  83. }