group.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 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. let attrs = {
  23. 'data-id': item.id || ''
  24. }
  25. return (
  26. <div className="hosts-item" {...attrs}>
  27. <i className={classnames({
  28. 'iconfont': 1
  29. , 'item-icon': 1
  30. , 'icon-file': item.where !== 'group'
  31. , 'icon-files': item.where === 'group'
  32. })}
  33. />
  34. <span>{item.title}</span>
  35. </div>
  36. )
  37. }
  38. makeList () {
  39. let include = this.state.include
  40. let items = this.state.list
  41. .filter(item => item.where !== 'group' && !include.includes(item.id))
  42. .map(item => this.makeItem(item))
  43. return (
  44. <div id="hosts-group-valid">
  45. <div ref="group_valid" className="hosts-group-list">
  46. {items}
  47. </div>
  48. </div>
  49. )
  50. }
  51. currentList () {
  52. let list = this.state.list
  53. let items = this.state.include
  54. .map(id => list.find(item => item.id === id))
  55. .map(item => this.makeItem(item))
  56. return (
  57. <div id="hosts-group-current">
  58. <div ref="group_current" className="hosts-group-list">
  59. {items}
  60. </div>
  61. </div>
  62. )
  63. }
  64. getCurrentListFromDOM () {
  65. let {updateInclude} = this.props
  66. let nodes = this.refs.group_current.getElementsByClassName('hosts-item')
  67. nodes = listToArray(nodes)
  68. let ids = nodes.map(item => item.getAttribute('data-id'))
  69. this.ids = ids
  70. updateInclude(ids)
  71. }
  72. componentWillMount () {
  73. this.setState({
  74. list: this.props.list,
  75. include: this.props.include
  76. })
  77. }
  78. componentDidMount () {
  79. Sortable.create(this.refs.group_valid, {
  80. group: 'sorting'
  81. , sort: false
  82. })
  83. Sortable.create(this.refs.group_current, {
  84. group: 'sorting'
  85. , sort: true
  86. , onSort: () => {
  87. this.getCurrentListFromDOM()
  88. }
  89. })
  90. }
  91. render () {
  92. return (
  93. <div id="hosts-group">
  94. {this.makeList()}
  95. <div className="arrow"/>
  96. {this.currentList()}
  97. </div>
  98. )
  99. }
  100. }