Skip to content

Commit

Permalink
fix: more tweaks to remove logic
Browse files Browse the repository at this point in the history
  • Loading branch information
filipgutica committed Feb 12, 2025
1 parent fec0c5d commit a02e33e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<div class="sandbox-container">
<DashboardRenderer
v-if="definition.type === ValidationResultType.Success"
can-edit
:config="definition.data"
:context="context"
draggable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
class="grid-stack"
>
<div
v-for="(tile, i) in tiles"
:key="i"
v-for="tile in tiles"
:id="`tile-${tile.id}`"
:key="tile.id"
class="grid-stack-item"
:data-id="tile.id"
:gs-h="tile.layout.size.rows"
Expand Down Expand Up @@ -48,7 +49,9 @@ let grid: GridStack | null = null
const makeTilesFromGridstackNodes = (items: GridStackNode[]) => {
return props.tiles.map((tile) => {
const item = items.find(item => Number(item.el?.getAttribute('data-id')) === tile.id)
const item = items.find(item => {
return item.el?.id === `tile-${tile.id}`
})
if (item) {
return {
...tile,
Expand All @@ -70,6 +73,7 @@ onMounted(() => {
resizable: { handles: 'se' },
lazyLoad: true,
handle: '.tile-header',
}, gridContainer.value)
grid.on('change', (_, items) => {
const updatedTiles = makeTilesFromGridstackNodes(items)
Expand All @@ -92,33 +96,32 @@ watch(() => props.tiles.length, async (newLen, oldLen) => {
if (newLen > oldLen && grid) {
await nextTick()
const tileToAdd = props.tiles.slice(oldLen)
const nodesToAdd = tileToAdd.map(e => {
return {
w: e.layout.size.cols,
h: e.layout.size.rows,
for (const tile of tileToAdd) {
grid.makeWidget(`#tile-${tile.id}`, {
autoPosition: true,
el: gridContainer.value?.querySelector(`[data-id="${e.id}"]`) as HTMLElement,
} as GridStackNode
})
grid.load(nodesToAdd)
w: tile.layout.size.cols,
h: tile.layout.size.rows,
lazyLoad: true,
})
}
}
})
watch(() => props.tiles, async (newTiles, oldTiles) => {
if (newTiles.length < oldTiles.length && grid) {
// We havn't actually added a concept of "unique ids" to the tiles yet, so the actual tile "id" is simply tied
// to the index by default. This works fine for modifying tiles, however, when it comes to identifying a tile
// that was removed, it gets a little tricky. Since the "layout" of a tile is unique, we can use that to uniquely
// identify tiles in order to detect what was removed.
const tileToRemove = oldTiles.find(oldTile => !newTiles.find(newTile => JSON.stringify(newTile.layout) === JSON.stringify(oldTile.layout)))
if (tileToRemove) {
const el = gridContainer.value?.querySelector(`[data-id="${tileToRemove.id}"]`) as HTMLElement
watch(() => props.tiles, async (tiles) => {
if (grid && gridContainer.value) {
for (const tile of tiles) {
const el = gridContainer.value.querySelector(`#tile-${tile.id}`) as HTMLElement
if (el) {
grid.removeWidget(el)
grid.update(el, {
x: tile.layout.position.col,
y: tile.layout.position.row,
w: tile.layout.size.cols,
h: tile.layout.size.rows,
})
}
}
await nextTick()
grid.compact('compact', false)
}
})
Expand Down

0 comments on commit a02e33e

Please sign in to comment.