list-item.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 isInViewport from 'wheel-js/src/browser/isInViewport'
  10. import './list-item.less'
  11. export default class ListItem extends React.Component {
  12. constructor (props) {
  13. super(props)
  14. this.is_sys = !!this.props.sys
  15. this.state = {}
  16. }
  17. getTitle () {
  18. let {lang} = this.props
  19. return this.is_sys ? lang.sys_host_title : this.props.data.title ||
  20. lang.untitled
  21. }
  22. beSelected () {
  23. this.props.setCurrent(this.props.data)
  24. }
  25. toggle () {
  26. Agent.emit('toggle_hosts', Object.assign({}, this.props.data))
  27. }
  28. toEdit () {
  29. Agent.emit('edit_hosts', Object.assign({}, this.props.data))
  30. }
  31. componentDidMount () {
  32. let {just_added_id, data} = this.props
  33. //Agent.on('select_hosts', id => {
  34. // if (id && id === this.props.data.id) {
  35. // this.beSelected()
  36. // this.el && this.el.scrollIntoView()
  37. // }
  38. //})
  39. if (!this.el) {
  40. return
  41. }
  42. let el = this.el
  43. if (just_added_id === data.id && !isInViewport(el)) {
  44. el.scrollIntoView()
  45. this.props.justAdd(null)
  46. }
  47. }
  48. render () {
  49. let {data, sys, current, show} = this.props
  50. if (!data) return null
  51. let is_selected = data.id === current.id || (data.is_sys && current.is_sys)
  52. let attrs = {
  53. 'data-id': data.id || ''
  54. }
  55. return (
  56. <div className={classnames({
  57. 'list-item': 1
  58. //, 'hidden': !this.isMatched()
  59. , 'sys-hosts': sys
  60. , 'selected': is_selected
  61. , 'hidden': show === false
  62. })}
  63. onClick={this.beSelected.bind(this)}
  64. ref={el => this.el = el}
  65. {...attrs}
  66. >
  67. {sys ? null : (
  68. <div className="item-buttons">
  69. <i
  70. className="iconfont icon-edit"
  71. onClick={this.toEdit.bind(this)}
  72. />
  73. <i className={classnames({
  74. 'switch': 1
  75. , 'iconfont': 1
  76. , 'icon-on': data.on
  77. , 'icon-off': !data.on
  78. })}
  79. onClick={this.toggle.bind(this)}
  80. />
  81. </div>
  82. )}
  83. <i className={classnames({
  84. 'iconfont': 1
  85. , 'item-icon': 1
  86. , 'icon-warn': !!data.error
  87. , 'icon-file': !sys && !data.error && data.where !== 'group'
  88. , 'icon-files': data.where === 'group'
  89. , 'icon-sysserver': sys && !data.error
  90. })}
  91. title={data.error || ''}
  92. />
  93. <span>{this.getTitle()}</span>
  94. </div>
  95. )
  96. }
  97. }