group.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/list-to-array'
  10. import './group.less'
  11. export default class Group extends React.Component {
  12. constructor (props) {
  13. super(props)
  14. this.state = {
  15. list: []
  16. }
  17. this.current_hosts = null
  18. }
  19. makeItem (item) {
  20. let attrs = {
  21. 'data-id': item.id || ''
  22. }
  23. return (
  24. <div className="hosts-item" {...attrs}>
  25. <i className={classnames({
  26. 'iconfont': 1
  27. , 'item-icon': 1
  28. , 'icon-file': item.where !== 'group'
  29. , 'icon-files': item.where === 'group'
  30. })}
  31. />
  32. <span>{item.title}</span>
  33. </div>
  34. )
  35. }
  36. makeList () {
  37. let items = this.state.list
  38. .filter(item => item.where !== 'group')
  39. .map(item => this.makeItem(item))
  40. return (
  41. <div id="hosts-group-valid">
  42. <div ref="group_valid" className="hosts-group-list">
  43. {items}
  44. </div>
  45. </div>
  46. )
  47. }
  48. currentList () {
  49. return (
  50. <div id="hosts-group-current">
  51. <div ref="group_current" className="hosts-group-list"></div>
  52. </div>
  53. )
  54. }
  55. getCurrentListFromDOM () {
  56. let nodes = this.refs.group_current.getElementsByClassName('hosts-item')
  57. nodes = listToArray(nodes)
  58. console.log(nodes)
  59. }
  60. componentWillMount () {
  61. this.setState({
  62. list: this.props.list
  63. })
  64. }
  65. componentDidMount () {
  66. Sortable.create(this.refs.group_valid, {
  67. group: 'sorting'
  68. , sort: false
  69. })
  70. Sortable.create(this.refs.group_current, {
  71. group: 'sorting'
  72. , sort: true
  73. , onSort: evt => {
  74. this.getCurrentListFromDOM()
  75. }
  76. })
  77. }
  78. render () {
  79. return (
  80. <div id="hosts-group">
  81. {this.makeList()}
  82. <div className="arrow"/>
  83. {this.currentList()}
  84. </div>
  85. )
  86. }
  87. }