Skip to content
Snippets Groups Projects
Commit 6e943df7 authored by Julian's avatar Julian
Browse files

Don't allow items inside each other

parent a733304f
No related branches found
No related tags found
1 merge request!2Resolve "Implement basic functionality"
...@@ -37,13 +37,43 @@ export default { ...@@ -37,13 +37,43 @@ export default {
type: Number, type: Number,
required: true, required: true,
}, },
posValidation: {
type: Function,
required: false,
},
}, },
methods: { methods: {
positionAllowed(x, y, key) {
for (let item of this.items) {
if (key === item.key) continue;
if (x >= item.x && x < item.x + item.w) {
if (y >= item.y && y < item.y + item.h) {
return false;
}
}
}
if (this.posValidation) return this.posValidation(x, y);
return true;
},
handleDragOver(event) { handleDragOver(event) {
let data = event.dataTransfer.getData("vueDrag/gridItem"); let data = event.dataTransfer.getData("vueDrag/gridItem");
let element = JSON.parse(data); let element = JSON.parse(data);
let coords = this.getCoords(event.layerX, event.layerY); let coords = this.getCoords(event.layerX, event.layerY);
let newPositionValid = true;
for (let x = coords.x; x < coords.x + element.w; x++) {
for (let y = coords.y; y < coords.y + element.h; y++) {
newPositionValid = this.positionAllowed(x, y, element.key);
if (!newPositionValid) break;
}
}
if (!newPositionValid) {
this.$refs.highlight.style.display = "none";
return;
}
this.$refs.highlight.style.display = "block"; this.$refs.highlight.style.display = "block";
this.$refs.highlight.style.gridColumnStart = coords.x + ""; this.$refs.highlight.style.gridColumnStart = coords.x + "";
this.$refs.highlight.style.gridRowStart = coords.y + ""; this.$refs.highlight.style.gridRowStart = coords.y + "";
...@@ -55,13 +85,27 @@ export default { ...@@ -55,13 +85,27 @@ export default {
let data = event.dataTransfer.getData("vueDrag/gridItem"); let data = event.dataTransfer.getData("vueDrag/gridItem");
let element = JSON.parse(data); let element = JSON.parse(data);
console.log(element); console.log(element);
let coords = this.getCoords(event.layerX, event.layerY);
let newPositionValid = true;
for (let x = coords.x; x < coords.x + element.w; x++) {
for (let y = coords.y; y < coords.y + element.h; y++) {
newPositionValid = this.positionAllowed(x, y, element.key);
if (!newPositionValid) break;
}
}
if (!newPositionValid) return;
this.items.splice( this.items.splice(
this.items.findIndex((i) => { this.items.findIndex((i) => {
return i.key === element.key; return i.key === element.key;
}), }),
1 1
); );
let coords = this.getCoords(event.layerX, event.layerY);
element.x = coords.x; element.x = coords.x;
element.y = coords.y; element.y = coords.y;
this.items.push(element); this.items.push(element);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment