Group.jsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import { Icon } from 'antd'
  8. import Sortable from 'sortablejs'
  9. import listToArray from 'wheel-js/src/common/listToArray'
  10. import './Group.less'
  11. export default class Group extends React.Component {
  12. constructor (props) {
  13. super(props)
  14. this.state = {
  15. list: [],
  16. include: []
  17. }
  18. this.current_hosts = null
  19. this.ids = []
  20. }
  21. makeItem (item) {
  22. if (!item) {
  23. return null
  24. }
  25. let attrs = {
  26. 'data-id': item.id || ''
  27. }
  28. let icon_type
  29. if (item.where === 'remote') {
  30. icon_type = 'global'
  31. } else if (item.where === 'group') {
  32. icon_type = 'copy'
  33. } else {
  34. icon_type = 'file-text'
  35. }
  36. return (
  37. <div className="hosts-item" {...attrs}>
  38. <Icon
  39. type={icon_type}
  40. className="iconfont"
  41. />
  42. <span>{item.title || 'untitled'}</span>
  43. </div>
  44. )
  45. }
  46. makeList () {
  47. let include = this.state.include
  48. let items = this.state.list
  49. .filter(item => item.where !== 'group' && !include.includes(item.id))
  50. .map(item => this.makeItem(item))
  51. return (
  52. <div id="hosts-group-valid">
  53. <div ref="group_valid" className="hosts-group-list">
  54. {items}
  55. </div>
  56. </div>
  57. )
  58. }
  59. currentList () {
  60. let list = this.state.list
  61. let items = this.state.include
  62. .map(id => list.find(item => item.id === id))
  63. .map(item => this.makeItem(item))
  64. return (
  65. <div id="hosts-group-current">
  66. <div ref="group_current" className="hosts-group-list">
  67. {items}
  68. </div>
  69. </div>
  70. )
  71. }
  72. getCurrentListFromDOM () {
  73. let {updateInclude} = this.props
  74. let nodes = this.refs.group_current.getElementsByClassName('hosts-item')
  75. nodes = listToArray(nodes)
  76. let ids = nodes.map(item => item.getAttribute('data-id'))
  77. this.ids = ids
  78. updateInclude(ids)
  79. }
  80. componentWillMount () {
  81. this.setState({
  82. list: this.props.list,
  83. include: this.props.include
  84. })
  85. }
  86. componentDidMount () {
  87. Sortable.create(this.refs.group_valid, {
  88. group: 'sorting'
  89. , animation: 150
  90. , sort: false
  91. })
  92. Sortable.create(this.refs.group_current, {
  93. group: 'sorting'
  94. , animation: 150
  95. , sort: true
  96. , onSort: () => {
  97. this.getCurrentListFromDOM()
  98. }
  99. })
  100. }
  101. render () {
  102. return (
  103. <div id="hosts-group">
  104. {this.makeList()}
  105. <Icon className="arrow" type="arrow-right" />
  106. {this.currentList()}
  107. </div>
  108. )
  109. }
  110. }