Skip to content

Commit 52dbd71

Browse files
author
jacquesbach
committed
Global Card Resize
1 parent cad9403 commit 52dbd71

3 files changed

Lines changed: 35 additions & 29 deletions

File tree

static/js/layout.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,54 +85,60 @@ async function resetDatabaseLayout() {
8585
}
8686
}
8787

88-
function initAutoResizeForCard(cardId) {
88+
function initAutoHeightGrid() {
8989

90-
const gridItem = document.getElementById(cardId);
91-
const content = gridItem.querySelector(".card");
90+
const items = document.querySelectorAll(".grid-stack-item.auto-height");
9291

93-
if (!gridItem || !content) return;
92+
items.forEach(item => {
9493

95-
let resizeTimeout;
94+
const content = item.querySelector(".grid-stack-item-content");
95+
const card = content?.querySelector(".card");
9696

97-
const observer = new ResizeObserver(() => {
97+
if (!content || !card) return;
9898

99-
// kleines Debounce (wichtig wegen Chart.js Render-Zyklen)
100-
clearTimeout(resizeTimeout);
99+
let resizeTimeout;
101100

102-
resizeTimeout = setTimeout(() => {
101+
const resize = () => {
103102

104-
const newHeightPx = content.scrollHeight;
103+
const rect = card.getBoundingClientRect();
104+
const heightPx = rect.height;
105105

106106
const cellHeight = dashboardGrid.getCellHeight();
107-
const newGridHeight = Math.ceil(newHeightPx / cellHeight);
107+
const newH = Math.ceil(heightPx / cellHeight);
108108

109-
dashboardGrid.update(gridItem, { h: newGridHeight });
109+
// 🔥 Nur updaten wenn nötig (sehr wichtig!)
110+
if (item.gridstackNode.h !== newH) {
111+
dashboardGrid.update(item, { h: newH });
112+
}
113+
};
110114

111-
}, 80); // Sweet Spot
115+
const observer = new ResizeObserver(() => {
112116

113-
});
117+
clearTimeout(resizeTimeout);
114118

115-
observer.observe(content);
116-
}
119+
resizeTimeout = setTimeout(() => {
120+
resize();
121+
}, 60); // debounce für Chart.js
117122

118-
function forceGridResize(cardId) {
119-
const gridItem = document.getElementById(cardId);
120-
const content = gridItem.querySelector(".card");
123+
});
121124

122-
if (!gridItem || !content) return;
125+
observer.observe(card);
123126

124-
const newHeightPx = content.scrollHeight;
125-
const cellHeight = dashboardGrid.getCellHeight();
126-
const newGridHeight = Math.ceil(newHeightPx / cellHeight);
127+
// 👉 initial nach Render
128+
setTimeout(resize, 300);
127129

128-
dashboardGrid.update(gridItem, { h: newGridHeight });
130+
// 👉 fallback (Fonts / async Layout)
131+
requestAnimationFrame(() => {
132+
requestAnimationFrame(resize);
133+
});
134+
});
129135
}
130136

131137
document.addEventListener("DOMContentLoaded", async () => {
132138
// --- PHASE 1: Das Gerüst aufbauen ---
133139
initGridstack();
134140
await loadLayout(); // Wartet, bis Boxen aus DB oder LocalStorage da sind
135-
initAutoResizeForCard("card-forecast");
141+
initAutoHeightGrid();
136142

137143
// --- PHASE 2: Startwerte für Datumsfelder setzen ---
138144
const t = new Date().toISOString().split('T')[0];

static/js/script.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ async function loadForecast() {
11421142
},
11431143
animation: {
11441144
onComplete: () => {
1145-
forceGridResize("card-forecast");
1145+
window.dispatchEvent(new Event('resize'));
11461146
}
11471147
}
11481148
}
@@ -1178,7 +1178,7 @@ async function loadFeatureImportance() {
11781178
},
11791179
animation: {
11801180
onComplete: () => {
1181-
forceGridResize("card-forecast");
1181+
window.dispatchEvent(new Event('resize'));
11821182
}
11831183
}
11841184
}
@@ -1221,7 +1221,7 @@ async function loadGlobalShap() {
12211221
},
12221222
animation: {
12231223
onComplete: () => {
1224-
forceGridResize("card-forecast");
1224+
window.dispatchEvent(new Event('resize'));
12251225
}
12261226
}
12271227
}

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ <h1>
243243
</div>
244244
</div>
245245

246-
<div class="grid-stack-item" id="card-forecast" gs-w="12" gs-h="8">
246+
<div class="grid-stack-item auto-height" id="card-forecast" gs-w="12" gs-h="8">
247247
<div class="grid-stack-item-content">
248248
<div class="card card-light" style="height: 100%; box-sizing: border-box;">
249249
<div class="label">Prognose</div>

0 commit comments

Comments
 (0)