10-drag-from-outside.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. var testLayout = [
  2. {"x":0,"y":0,"w":2,"h":2,"i":"0"},
  3. {"x":2,"y":0,"w":2,"h":4,"i":"1"},
  4. {"x":4,"y":0,"w":2,"h":5,"i":"2"},
  5. {"x":6,"y":0,"w":2,"h":3,"i":"3"},
  6. {"x":8,"y":0,"w":2,"h":3,"i":"4"},
  7. {"x":10,"y":0,"w":2,"h":3,"i":"5"},
  8. {"x":0,"y":5,"w":2,"h":5,"i":"6"},
  9. {"x":2,"y":5,"w":2,"h":5,"i":"7"},
  10. {"x":4,"y":5,"w":2,"h":5,"i":"8"},
  11. {"x":5,"y":10,"w":4,"h":3,"i":"9"},
  12. ];
  13. var mouseXY={"x":null,"y":null};
  14. var DragPos = {"x":null,"y":null,"w":1,"h":1,"i":null};
  15. document.addEventListener("dragover", function(e) {
  16. mouseXY.x=e.clientX;
  17. mouseXY.y=e.clientY;
  18. }, false);
  19. new Vue({
  20. el: '#app',
  21. data: {
  22. layout: testLayout,
  23. },
  24. methods: {
  25. drag:function(e){
  26. let parentRect = document.getElementById('content').getBoundingClientRect();
  27. let mouseInGrid=false;
  28. if (((mouseXY.x>parentRect.left) && (mouseXY.x<parentRect.right)) && ((mouseXY.y>parentRect.top) && (mouseXY.y<parentRect.bottom))){
  29. mouseInGrid=true;
  30. }
  31. if (mouseInGrid==true && (this.layout.findIndex(item => item.i === 'drop')) == -1){
  32. this.layout.push({
  33. x: (this.layout.length * 2) % (this.colNum || 12),
  34. y: this.layout.length + (this.colNum || 12), // puts it at the bottom
  35. w: 1,
  36. h: 1,
  37. i: 'drop',
  38. });
  39. }
  40. let index=this.layout.findIndex(item => item.i === 'drop');
  41. if (index!=-1){
  42. try {
  43. this.$refs.gridLayout.$children[this.layout.length].$refs.item.style.display="none";
  44. } catch {
  45. }
  46. let el=this.$refs.gridLayout.$children[index];
  47. el.dragging={"top":mouseXY.y-parentRect.top,"left":mouseXY.x-parentRect.left};
  48. let new_pos=el.calcXY(mouseXY.y-parentRect.top, mouseXY.x-parentRect.left);
  49. if (mouseInGrid==true){
  50. this.$refs.gridLayout.dragEvent('dragstart', 'drop', new_pos.x,new_pos.y,1,1);
  51. DragPos.i=String(index); DragPos.x=this.layout[index].x; DragPos.y=this.layout[index].y;
  52. }
  53. if (mouseInGrid==false){
  54. this.$refs.gridLayout.dragEvent('dragend', 'drop', new_pos.x,new_pos.y,1,1);
  55. this.layout = this.layout.filter(obj => obj.i !=='drop');
  56. }
  57. }
  58. },
  59. dragend:function(e){
  60. let parentRect = document.getElementById('content').getBoundingClientRect();
  61. let mouseInGrid=false;
  62. if (((mouseXY.x>parentRect.left) && (mouseXY.x<parentRect.right)) && ((mouseXY.y>parentRect.top) && (mouseXY.y<parentRect.bottom))){
  63. mouseInGrid=true;
  64. }
  65. if (mouseInGrid==true){
  66. alert(`Dropped element props:\n${JSON.stringify(DragPos, ['x', 'y', 'w', 'h'], 2)}`);
  67. this.$refs.gridLayout.dragEvent('dragend', 'drop', DragPos.x,DragPos.y,1,1);
  68. this.layout = this.layout.filter(obj => obj.i !=='drop');
  69. // UNCOMMENT below if you want to add a grid-item
  70. /*
  71. this.layout.push({
  72. x: DragPos.x,
  73. y: DragPos.y,
  74. w: 1,
  75. h: 1,
  76. i: DragPos.i,
  77. });
  78. this.$refs.gridLayout.dragEvent('dragend', DragPos.i, DragPos.x,DragPos.y,1,1);
  79. try {
  80. this.$refs.gridLayout.$children[this.layout.length].$refs.item.style.display="block";
  81. } catch {
  82. }
  83. */
  84. }
  85. },
  86. },
  87. });