OnSpill.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { getChild } from '../../src/utils.js';
  2. const drop = function({
  3. originalEvent,
  4. putSortable,
  5. dragEl,
  6. activeSortable,
  7. dispatchSortableEvent,
  8. hideGhostForTarget,
  9. unhideGhostForTarget
  10. }) {
  11. if (!originalEvent) return;
  12. let toSortable = putSortable || activeSortable;
  13. hideGhostForTarget();
  14. let touch = originalEvent.changedTouches && originalEvent.changedTouches.length ? originalEvent.changedTouches[0] : originalEvent;
  15. let target = document.elementFromPoint(touch.clientX, touch.clientY);
  16. unhideGhostForTarget();
  17. if (toSortable && !toSortable.el.contains(target)) {
  18. dispatchSortableEvent('spill');
  19. this.onSpill({ dragEl, putSortable });
  20. }
  21. };
  22. function Revert() {}
  23. Revert.prototype = {
  24. startIndex: null,
  25. dragStart({ oldDraggableIndex }) {
  26. this.startIndex = oldDraggableIndex;
  27. },
  28. onSpill({ dragEl, putSortable }) {
  29. this.sortable.captureAnimationState();
  30. if (putSortable) {
  31. putSortable.captureAnimationState();
  32. }
  33. let nextSibling = getChild(this.sortable.el, this.startIndex, this.options);
  34. if (nextSibling) {
  35. this.sortable.el.insertBefore(dragEl, nextSibling);
  36. } else {
  37. this.sortable.el.appendChild(dragEl);
  38. }
  39. this.sortable.animateAll();
  40. if (putSortable) {
  41. putSortable.animateAll();
  42. }
  43. },
  44. drop
  45. };
  46. Object.assign(Revert, {
  47. pluginName: 'revertOnSpill'
  48. });
  49. function Remove() {}
  50. Remove.prototype = {
  51. onSpill({ dragEl, putSortable }) {
  52. const parentSortable = putSortable || this.sortable;
  53. parentSortable.captureAnimationState();
  54. dragEl.parentNode && dragEl.parentNode.removeChild(dragEl);
  55. parentSortable.animateAll();
  56. },
  57. drop
  58. };
  59. Object.assign(Remove, {
  60. pluginName: 'removeOnSpill'
  61. });
  62. export default [Remove, Revert];
  63. export {
  64. Remove as RemoveOnSpill,
  65. Revert as RevertOnSpill
  66. };