ListItem.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 { Icon } from 'antd'
  9. import Agent from '../Agent'
  10. import isInViewport from 'wheel-js/src/browser/isInViewport'
  11. import styles from './ListItem.less'
  12. export default class ListItem extends React.Component {
  13. constructor (props) {
  14. super(props)
  15. this.is_sys = !!this.props.sys
  16. this.state = {}
  17. }
  18. getTitle () {
  19. let {lang} = this.props
  20. return this.is_sys ? lang.sys_hosts_title : this.props.data.title ||
  21. lang.untitled
  22. }
  23. beSelected () {
  24. this.props.setCurrent(this.props.data)
  25. }
  26. toggle () {
  27. Agent.emit('toggle_hosts', Object.assign({}, this.props.data))
  28. }
  29. toEdit () {
  30. Agent.emit('edit_hosts', Object.assign({}, this.props.data))
  31. }
  32. componentDidMount () {
  33. let {just_added_id, data} = this.props
  34. //Agent.on('select_hosts', id => {
  35. // if (id && id === this.props.data.id) {
  36. // this.beSelected()
  37. // this.el && this.el.scrollIntoView()
  38. // }
  39. //})
  40. if (!this.el) {
  41. return
  42. }
  43. let el = this.el
  44. if (just_added_id === data.id && !isInViewport(el)) {
  45. el.scrollIntoView()
  46. this.props.justAdd(null)
  47. }
  48. }
  49. render () {
  50. let {data, sys, current, show} = this.props
  51. if (!data) return null
  52. let is_selected = data.id === current.id || (data.is_sys && current.is_sys)
  53. let attrs = {
  54. 'data-id': data.id || ''
  55. }
  56. let icon_type
  57. if (sys) {
  58. icon_type = 'desktop'
  59. } else if (data.where === 'remote') {
  60. icon_type = 'global'
  61. } else if (data.where === 'group') {
  62. icon_type = 'copy'
  63. } else {
  64. icon_type = 'file-text'
  65. }
  66. return (
  67. <div className={classnames({
  68. 'list-item': 1, // 用于排序选择
  69. [styles['list-item']]: 1,
  70. //, 'hidden': !this.isMatched()
  71. [styles['sys-hosts']]: sys,
  72. [styles['selected']]: is_selected,
  73. 'hidden': show === false
  74. })}
  75. onClick={this.beSelected.bind(this)}
  76. ref={el => this.el = el}
  77. {...attrs}
  78. >
  79. {sys ? null : (
  80. <div className={styles['item-buttons']}>
  81. <i
  82. className={classnames({
  83. iconfont: 1,
  84. 'icon-edit': 1
  85. })}
  86. onClick={this.toEdit.bind(this)}
  87. />
  88. <i className={classnames({
  89. iconfont: 1,
  90. switch: 1,
  91. 'icon-on': data.on,
  92. 'icon-off': !data.on
  93. })}
  94. onClick={this.toggle.bind(this)}
  95. />
  96. </div>
  97. )}
  98. {/*<i className={classnames({*/}
  99. {/*'iconfont': 1*/}
  100. {/*, 'item-icon': 1*/}
  101. {/*, 'icon-warn': !!data.error*/}
  102. {/*, 'icon-file': !sys && !data.error && data.where !== 'group'*/}
  103. {/*, 'icon-files': data.where === 'group'*/}
  104. {/*, 'icon-sysserver': sys && !data.error*/}
  105. {/*})}*/}
  106. {/*title={data.error || ''}*/}
  107. {/*/>*/}
  108. <Icon
  109. type={icon_type}
  110. className={classnames({
  111. iconfont: 1,
  112. 'item-icon': 1
  113. })}
  114. title={data.error || ''}
  115. />
  116. <span>{this.getTitle()}</span>
  117. </div>
  118. )
  119. }
  120. }