makeSortable.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * sort
  3. * @author: oldj
  4. * @homepage: https://oldj.net
  5. */
  6. import Sortable from 'sortablejs'
  7. import lodash from 'lodash'
  8. import styles from './List.less'
  9. import Agent from '../Agent'
  10. export default function (el) {
  11. let source_id
  12. let target_id
  13. let where_to
  14. function getId (el, is_child = false) {
  15. if (!el || el.tagName !== 'DIV') {
  16. return [null, is_child]
  17. }
  18. if (!el.className.includes('list-item')) {
  19. return getId(el.parentNode, true)
  20. }
  21. return [el.getAttribute('data-id'), is_child]
  22. }
  23. Sortable.create(el, {
  24. group: 'list-sorting',
  25. sort: true,
  26. ghostClass: styles['sort-bg'],
  27. animation: 150,
  28. onStart: (e) => {
  29. //console.log(e)
  30. source_id = getId(e.item)[0]
  31. Agent.emit('drag_start')
  32. },
  33. onMove: lodash.debounce((e) => {
  34. //console.log(e.related, e.willInsertAfter)
  35. let is_child
  36. [target_id, is_child] = getId(e.related)
  37. if (is_child) {
  38. where_to = 'in'
  39. } else {
  40. where_to = e.willInsertAfter ? 'after' : 'before'
  41. }
  42. Agent.emit('drag_move', target_id, where_to)
  43. return false
  44. }, 200),
  45. onEnd: (e) => {
  46. //console.log(e.newIndex)
  47. console.log(source_id, target_id, where_to)
  48. //onSort: () => {
  49. // this.getCurrentListFromDOM()
  50. // Agent.emit('drag_end')
  51. Agent.emit('drag_done', {source_id, target_id, where_to})
  52. }
  53. })
  54. }