group.jsx 2.5 KB

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