From 193777ffb7d9470e9a0e1e3603af0d45c484eacc Mon Sep 17 00:00:00 2001
From: Julian Leucker <leuckerj@gmail.com>
Date: Fri, 10 Feb 2023 12:08:48 +0100
Subject: [PATCH] Allow data to be a function

---
 src/DragGrid.vue | 47 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 10 deletions(-)

diff --git a/src/DragGrid.vue b/src/DragGrid.vue
index 4c0874a..dc69c52 100644
--- a/src/DragGrid.vue
+++ b/src/DragGrid.vue
@@ -13,15 +13,15 @@
       v-for="item in value"
       :key="item.key"
       :drag-i-d="item.key"
-      :x="get('x', item)"
-      :y="get('y', item)"
-      :w="get('w', item)"
-      :h="get('h', item)"
-      :data="item.data"
+      :x="getInt('x', item)"
+      :y="getInt('y', item)"
+      :w="getInt('w', item)"
+      :h="getInt('h', item)"
+      :data="getObject('data', item)"
       :context="context"
       :grid-id="gridId"
     >
-      <slot v-bind="item" name="item">
+      <slot v-bind="transformItem(item)" name="item">
         <dl>
           <dt>Key</dt>
           <dd>{{ item.key }}</dd>
@@ -88,8 +88,14 @@ export default {
 
       for (let item of this.value) {
         if (key === item.key) continue;
-        if (x >= item.x && x < item.x + item.w) {
-          if (y >= item.y && y < item.y + item.h) {
+        if (
+          x >= this.getInt("x", item) &&
+          x < this.getInt("x", item) + this.getInt("w", item)
+        ) {
+          if (
+            y >= this.getInt("y", item) &&
+            y < this.getInt("y", item) + this.getInt("h", item)
+          ) {
             return false;
           }
         }
@@ -184,9 +190,30 @@ export default {
         y: Math.ceil(y / (this.$el.offsetHeight / this.rows)),
       };
     },
-    get(property, item) {
+    getInt(property, item) {
       let val = item[property] || 1;
-      return val instanceof Function ? val(this) : parseInt(val);
+      return val instanceof Function ? val(this.gridData) : parseInt(val);
+    },
+    getObject(property, item) {
+      let val = item[property] || {};
+      return val instanceof Function ? val(this.gridData) : val;
+    },
+    transformItem(item) {
+      let newItem = { key: item.key };
+      newItem.x = this.getInt("x", item);
+      newItem.y = this.getInt("y", item);
+      newItem.w = this.getInt("w", item);
+      newItem.h = this.getInt("h", item);
+      newItem.data = this.getObject("data", item);
+      return newItem;
+    },
+  },
+  computed: {
+    gridData() {
+      return {
+        gridId: this.gridId,
+        context: this.context,
+      };
     },
   },
 };
-- 
GitLab