list-item.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. let attrs = {
  44. 'data-id': data.id || ''
  45. }
  46. return (
  47. <div className={classnames({
  48. 'list-item': 1
  49. //, 'hidden': !this.isMatched()
  50. , 'sys-hosts': sys
  51. , 'selected': is_selected
  52. })}
  53. onClick={this.beSelected.bind(this)}
  54. ref={el => this.el = el}
  55. {...attrs}
  56. >
  57. {sys ? null : (
  58. <div>
  59. <i className={classnames({
  60. 'switch': 1
  61. , 'iconfont': 1
  62. , 'icon-on': data.on
  63. , 'icon-off': !data.on
  64. })}
  65. onClick={this.toggle.bind(this)}
  66. />
  67. <i
  68. className="iconfont icon-edit"
  69. onClick={this.toEdit.bind(this)}
  70. />
  71. </div>
  72. )}
  73. <i className={classnames({
  74. 'iconfont': 1
  75. , 'item-icon': 1
  76. , 'icon-warn': !!data.error
  77. , 'icon-file': !sys && !data.error && data.where !== 'group'
  78. , 'icon-files': data.where === 'group'
  79. , 'icon-sysserver': sys && !data.error
  80. })}
  81. title={data.error || ''}
  82. />
  83. <span>{this.getTitle()}</span>
  84. </div>
  85. )
  86. }
  87. }