-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.js
More file actions
133 lines (112 loc) · 4.44 KB
/
layout.js
File metadata and controls
133 lines (112 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let dashboardGrid;
let currentPw = "";
// 1. Grid initialisieren
function initGridstack() {
dashboardGrid = GridStack.init({
cellHeight: 50,
margin: 20,
animate: true,
staticGrid: true,
disableOneColumnMode: false,
oneColumnModeDomSort: true
});
dashboardGrid.on('change', function(event, items) {
saveLayout();
});
}
// 2. Layout speichern (Nur in DB und nur wenn PW da ist)
async function saveLayout() {
if (!dashboardGrid || !currentPw || currentPw === "") {
return;
}
const layoutData = dashboardGrid.save();
try {
const response = await fetch('/api/layout', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
layout: layoutData,
pw: currentPw
})
});
if (!response.ok) {
const errorText = await response.text();
console.error("Speichern fehlgeschlagen:", errorText);
}
} catch (e) {
console.error("Netzwerkfehler beim Speichern:", e);
}
}
// 3. Layout beim Starten laden
async function loadLayout() {
let savedLayout = null;
try {
const response = await fetch('/api/layout');
if (response.ok) {
const data = await response.json();
if (data && data.layout) savedLayout = data.layout;
}
} catch (e) { console.error("DB Load failed", e); }
// Wenn ein Layout auf dem Server existiert, anwenden
if (savedLayout && dashboardGrid) {
dashboardGrid.removeAll();
dashboardGrid.load(savedLayout);
console.log("Globales Layout erfolgreich geladen.");
}
}
// 4. Reset-Funktion für den Button im Admin-Bereich
async function resetDatabaseLayout() {
if (!confirm("Möchtest du das Layout für ALLE Nutzer auf den Standard zurücksetzen?")) return;
if (!currentPw) return alert("Bitte erst als Admin einloggen!");
const res = await fetch('/api/layout', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
layout: "RESET", // Signalwort für das Python-Backend
pw: currentPw
})
});
if (res.ok) {
location.reload(); // Seite neu laden, um Standard-HTML zu zeigen
}
}
document.addEventListener("DOMContentLoaded", async () => {
// --- PHASE 1: Das Gerüst aufbauen ---
initGridstack();
await loadLayout(); // Wartet, bis Boxen aus DB oder LocalStorage da sind
// --- PHASE 2: Startwerte für Datumsfelder setzen ---
const t = new Date().toISOString().split('T')[0];
const startInput = document.getElementById('start');
const endInput = document.getElementById('end');
if (startInput && endInput) {
startInput.value = t;
endInput.value = t;
startInput.addEventListener('change', updateQuickButtonsActiveState);
endInput.addEventListener('change', updateQuickButtonsActiveState);
}
// --- PHASE 3: Daten in die Boxen pumpen ---
// Wir prüfen bei jeder Funktion, ob sie existiert, um Fehler zu vermeiden
try {
if (typeof fetchData === "function") await fetchData();
if (typeof updateWeather === "function") updateWeather();
if (typeof updateLive === "function") updateLive();
if (typeof updatePeaks === "function") updatePeaks();
if (typeof updateQuickButtonsActiveState === "function") updateQuickButtonsActiveState();
// ML-Funktionen
if (typeof loadForecast === "function") loadForecast();
if (typeof loadGlobalShap === "function") loadGlobalShap();
if (typeof loadFeatureImportance === "function") loadFeatureImportance();
// Heatmaps
if (typeof initHeatmapYears === "function") initHeatmapYears();
if (typeof initHourlyHeatmap === "function") initHourlyHeatmap();
} catch (err) {
console.error("Fehler beim initialen Daten-Load:", err);
}
// --- PHASE 4: Intervalle für Updates starten ---
setInterval(() => { if (typeof updateLive === "function") updateLive(); }, 5000);
setInterval(() => { if (typeof fetchData === "function") fetchData(); }, 60000);
setInterval(() => { if (typeof updatePeaks === "function") updatePeaks(); }, 60000);
setInterval(() => { if (typeof checkLoadingStatus === "function") checkLoadingStatus(); }, 500);
});