Skip to content
Snippets Groups Projects
Commit 748edaf5 authored by Julian's avatar Julian
Browse files

(Dis)Allow dragging between grids

parent 5321953b
No related branches found
No related tags found
1 merge request!2Resolve "Implement basic functionality"
...@@ -13,11 +13,35 @@ import CircularCard from "./components/CircularCard.vue"; ...@@ -13,11 +13,35 @@ import CircularCard from "./components/CircularCard.vue";
<div class="container">{{ item }}</div> <div class="container">{{ item }}</div>
</template> </template>
</DragGrid> </DragGrid>
<DragGrid :rows="3" :cols="3" v-model="ticTacToe" class="tic-tac-toe"> <div class="ttt-container">
<template #item="item"> <DragGrid
<CircularCard> {{ item.key.startsWith("a") ? "X" : "O" }}</CircularCard> :rows="3"
</template> :cols="3"
</DragGrid> v-model="ticTacToe1"
class="tic-tac-toe"
context="ticTacToe"
>
<template #item="item">
<CircularCard>
{{ item.key.startsWith("a") ? "X" : "O" }}</CircularCard
>
</template>
</DragGrid>
<span> These two are two different grids but are interchangeable! </span>
<DragGrid
:rows="3"
:cols="3"
v-model="ticTacToe2"
class="tic-tac-toe"
context="ticTacToe"
>
<template #item="item">
<CircularCard>
{{ item.key.startsWith("a") ? "X" : "O" }}</CircularCard
>
</template>
</DragGrid>
</div>
</div> </div>
</template> </template>
...@@ -51,7 +75,7 @@ export default { ...@@ -51,7 +75,7 @@ export default {
}, },
{ x: 5, y: 3, w: 1, h: 1, key: "obj9", data: {} }, { x: 5, y: 3, w: 1, h: 1, key: "obj9", data: {} },
], ],
ticTacToe: [ ticTacToe1: [
{ x: 1, y: 1, w: 1, h: 1, key: "a1", data: {} }, { x: 1, y: 1, w: 1, h: 1, key: "a1", data: {} },
{ x: 1, y: 3, w: 1, h: 1, key: "a2", data: {} }, { x: 1, y: 3, w: 1, h: 1, key: "a2", data: {} },
{ x: 2, y: 2, w: 1, h: 1, key: "a3", data: {} }, { x: 2, y: 2, w: 1, h: 1, key: "a3", data: {} },
...@@ -60,6 +84,7 @@ export default { ...@@ -60,6 +84,7 @@ export default {
{ x: 1, y: 2, w: 1, h: 1, key: "b2", data: {} }, { x: 1, y: 2, w: 1, h: 1, key: "b2", data: {} },
{ x: 3, y: 1, w: 1, h: 1, key: "b3", data: {} }, { x: 3, y: 1, w: 1, h: 1, key: "b3", data: {} },
], ],
ticTacToe2: [{ x: 1, y: 1, w: 1, h: 1, key: "b4", data: {} }],
}; };
}, },
}; };
...@@ -95,4 +120,9 @@ export default { ...@@ -95,4 +120,9 @@ export default {
.tic-tac-toe { .tic-tac-toe {
max-width: 400px; max-width: 400px;
} }
.ttt-container {
display: flex;
justify-content: space-between;
}
</style> </style>
...@@ -23,10 +23,6 @@ ...@@ -23,10 +23,6 @@
"vue": "^2.7.14" "vue": "^2.7.14"
}, },
"devDependencies": { "devDependencies": {
"rollup": "^3.12.1",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-vue": "^6.0.0",
"vuepress": "^1.9.8",
"@rushstack/eslint-patch": "^1.1.0", "@rushstack/eslint-patch": "^1.1.0",
"@vitejs/plugin-legacy": "^2.0.0", "@vitejs/plugin-legacy": "^2.0.0",
"@vitejs/plugin-vue2": "^1.1.2", "@vitejs/plugin-vue2": "^1.1.2",
...@@ -34,7 +30,14 @@ ...@@ -34,7 +30,14 @@
"eslint": "^8.5.0", "eslint": "^8.5.0",
"eslint-plugin-vue": "^9.0.0", "eslint-plugin-vue": "^9.0.0",
"prettier": "^2.5.1", "prettier": "^2.5.1",
"rollup": "^3.12.1",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-vue": "^6.0.0",
"terser": "^5.14.2", "terser": "^5.14.2",
"vite": "^3.0.2" "vite": "^3.0.2",
"vuepress": "^1.9.8"
},
"dependencies": {
"uuid": "^9.0.0"
} }
} }
...@@ -18,6 +18,7 @@ export default { ...@@ -18,6 +18,7 @@ export default {
w: this.w, w: this.w,
h: this.h, h: this.h,
data: this.data, data: this.data,
context: this.context,
}) })
); );
}, },
...@@ -29,6 +30,7 @@ export default { ...@@ -29,6 +30,7 @@ export default {
w: Number, w: Number,
h: Number, h: Number,
data: Object, data: Object,
context: String,
}, },
}; };
</script> </script>
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
:w="item.w" :w="item.w"
:h="item.h" :h="item.h"
:data="item.data" :data="item.data"
:context="context"
> >
<slot v-bind="item" name="item"> <slot v-bind="item" name="item">
<dl> <dl>
...@@ -36,6 +37,7 @@ ...@@ -36,6 +37,7 @@
<script> <script>
import DragContainer from "./DragContainer.vue"; import DragContainer from "./DragContainer.vue";
import { v4 as uuidv4 } from "uuid";
export default { export default {
name: "DragGrid", name: "DragGrid",
...@@ -60,6 +62,11 @@ export default { ...@@ -60,6 +62,11 @@ export default {
type: Array, type: Array,
required: true, required: true,
}, },
context: {
type: String,
required: false,
default: uuidv4,
},
}, },
methods: { methods: {
positionAllowed(x, y, key) { positionAllowed(x, y, key) {
...@@ -83,6 +90,11 @@ export default { ...@@ -83,6 +90,11 @@ export default {
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);
if (element.context !== this.context) {
this.$refs.highlight.style.display = "none";
return;
}
let newPositionValid = true; let newPositionValid = true;
for (let x = coords.x; x < coords.x + element.w; x++) { for (let x = coords.x; x < coords.x + element.w; x++) {
...@@ -108,7 +120,11 @@ export default { ...@@ -108,7 +120,11 @@ export default {
this.$refs.highlight.style.display = "none"; this.$refs.highlight.style.display = "none";
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, this.context);
if (element.context !== this.context) {
return;
}
let coords = this.getCoords(event.layerX, event.layerY); let coords = this.getCoords(event.layerX, event.layerY);
...@@ -125,12 +141,11 @@ export default { ...@@ -125,12 +141,11 @@ export default {
if (!newPositionValid) return; if (!newPositionValid) return;
let valueCopy = structuredClone(this.value); let valueCopy = structuredClone(this.value);
valueCopy.splice(
valueCopy.findIndex((i) => { let index = valueCopy.findIndex((i) => {
return i.key === element.key; return i.key === element.key;
}), });
1 if (index >= 0) valueCopy.splice(index, 1);
);
element.x = coords.x; element.x = coords.x;
element.y = coords.y; element.y = coords.y;
......
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