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