Skip to content
Snippets Groups Projects
Grid.vue 3.73 KiB
Newer Older
<template>
Julian's avatar
Julian committed
  <div
    @dragover.prevent="handleDragOver"
    @drop.prevent="handleDrop"
    class="grid"
  >
    <slot name="highlight">
Julian's avatar
Julian committed
      <div id="highlight" ref="highlight"></div>
    </slot>
Julian's avatar
Julian committed
    <DragContainer
      v-for="item in items"
      :key="item.key"
Julian's avatar
Julian committed
      :drag-i-d="item.key"
Julian's avatar
Julian committed
      :x="item.x"
      :y="item.y"
      :w="item.w"
      :h="item.h"
    >
    </DragContainer>
  </div>
</template>

<script>
Julian's avatar
Julian committed
import DragContainer from "./DragContainer.vue";

export default {
Julian's avatar
Julian committed
  name: "Grid",
  components: {
    DragContainer,
  },
  props: {
    rows: {
      type: Number,
Julian's avatar
Julian committed
      required: true,
    },
    cols: {
      type: Number,
Julian's avatar
Julian committed
      required: true,
    },
    posValidation: {
      type: Function,
      required: false,
    },
  },
  methods: {
    positionAllowed(x, y, key) {
Julian's avatar
Julian committed
      if (x < 0 || y < 0) return false;
      if (x > this.cols) return false;
      if (y > this.rows) return false;

      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;
    },
Julian's avatar
Julian committed
    handleDragOver(event) {
      let data = event.dataTransfer.getData("vueDrag/gridItem");
      let element = JSON.parse(data);
      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;
      }

Julian's avatar
Julian committed
      this.$refs.highlight.style.display = "block";
      this.$refs.highlight.style.gridColumnStart = coords.x + "";
      this.$refs.highlight.style.gridRowStart = coords.y + "";
      this.$refs.highlight.style.gridColumnEnd = "span " + element.w;
      this.$refs.highlight.style.gridRowEnd = "span " + element.h;
    },
    handleDrop(event) {
Julian's avatar
Julian committed
      this.$refs.highlight.style.display = "none";
Julian's avatar
Julian committed
      let data = event.dataTransfer.getData("vueDrag/gridItem");
      let element = JSON.parse(data);
      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;

Julian's avatar
Julian committed
      this.items.splice(
        this.items.findIndex((i) => {
          return i.key === element.key;
        }),
        1
      );
Julian's avatar
Julian committed
      element.x = coords.x;
      element.y = coords.y;
Julian's avatar
Julian committed
      this.items.push(element);
Julian's avatar
Julian committed
    getCoords(x, y) {
      return {
        x: Math.ceil(x / (this.$el.offsetWidth / this.cols)),
        y: Math.ceil(y / (this.$el.offsetHeight / this.rows)),
      };
    },
Julian's avatar
Julian committed
  },
  data() {
    return {
      items: [
        { x: 3, y: 3, w: 1, h: 1, key: "obj1" },
        { x: 1, y: 1, w: 1, h: 1, key: "obj2" },
        { x: 4, y: 3, w: 2, h: 2, key: "obj3" },
      ],
    };
  },
};
</script>

<style scoped>
#highlight {
  background: darkgrey;
  border: grey dashed 2px;
Julian's avatar
Julian committed
  display: none;
  transition: all 2s ease-in-out;
  z-index: -1;
  pointer-events: none;
  user-select: none;
Julian's avatar
Julian committed

.grid {
  display: grid;
  grid-template-columns: repeat(v-bind(cols), 1fr);
  grid-template-rows: repeat(v-bind(rows), 1fr);
  width: 100%;
  height: 100%;
  min-width: 100px;
  min-height: 100px;
  gap: 1em;
}
Julian's avatar
Julian committed
</style>