list.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import ListItem from './list-item'
  8. import Sortable from 'sortablejs'
  9. import listToArray from 'wheel-js/src/common/list-to-array'
  10. import Agent from '../Agent'
  11. import './list.less'
  12. export default class List extends React.Component {
  13. constructor (props) {
  14. super(props)
  15. this.state = {}
  16. }
  17. customItems () {
  18. return this.props.list.map((item, idx) => {
  19. return (
  20. <ListItem
  21. data={item}
  22. idx={idx}
  23. current={this.props.current}
  24. setCurrent={this.props.setCurrent}
  25. //onToggle={(success) => this.toggleOne(idx, success)}
  26. key={'hosts-' + idx}
  27. //dragOrder={(sidx, tidx) => this.dragOrder(sidx, tidx)}
  28. />
  29. )
  30. })
  31. }
  32. getCurrentListFromDOM () {
  33. let nodes = this.refs.items.getElementsByClassName('list-item')
  34. nodes = listToArray(nodes)
  35. let ids = nodes.map(el => el.getAttribute('data-id'))
  36. Agent.emit('order', ids)
  37. }
  38. componentDidMount () {
  39. Sortable.create(this.refs.items, {
  40. group: 'list-sorting'
  41. , sort: true
  42. , onSort: evt => {
  43. this.getCurrentListFromDOM()
  44. //console.log(evt)
  45. }
  46. })
  47. }
  48. render () {
  49. return (
  50. <div id="sh-list">
  51. <ListItem
  52. data={this.props.sys_hosts}
  53. lang={this.props.lang}
  54. current={this.props.current}
  55. setCurrent={this.props.setCurrent}
  56. sys="1"/>
  57. <div ref="items" className="custom-items">
  58. {this.customItems()}
  59. </div>
  60. </div>
  61. )
  62. }
  63. }