group.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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: this.props.hosts.list
  16. }
  17. this.current_host = null
  18. }
  19. makeItem (item) {
  20. let attrs = {
  21. 'data-id': '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. componentDidMount () {
  61. Sortable.create(this.refs.group_valid, {
  62. group: 'sorting'
  63. , sort: false
  64. })
  65. Sortable.create(this.refs.group_current, {
  66. group: 'sorting'
  67. , sort: true
  68. , onSort: evt => {
  69. this.getCurrentListFromDOM()
  70. }
  71. })
  72. }
  73. render () {
  74. return (
  75. <div id="hosts-group">
  76. {this.makeList()}
  77. <div className="arrow"/>
  78. {this.currentList()}
  79. </div>
  80. )
  81. }
  82. }