-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (115 loc) · 4.72 KB
/
script.js
File metadata and controls
140 lines (115 loc) · 4.72 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
134
135
136
137
138
139
async function loadYAML(url) {
const response = await fetch(url);
const text = await response.text();
return jsyaml.load(text);
}
async function checkServiceStatus(service, cardElement) {
if (!service.statusCheck) return;
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 3000);
const response = await fetch(service.statusCheck, { signal: controller.signal });
clearTimeout(timeoutId);
const isOnline = response.ok;
const color = isOnline ? 'green' : 'red';
const dotHTML = `
<span class="absolute top-2 right-2 flex h-3 w-3">
<span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-${color}-400 opacity-75"></span>
<span class="relative inline-flex rounded-full h-3 w-3 bg-${color}-500"></span>
</span>
`;
cardElement.insertAdjacentHTML('beforeend', dotHTML);
} catch (error) {
const dotHTML = `
<span class="absolute top-2 right-2 flex h-3 w-3">
<span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-red-400 opacity-75"></span>
<span class="relative inline-flex rounded-full h-3 w-3 bg-red-500"></span>
</span>
`;
cardElement.insertAdjacentHTML('beforeend', dotHTML);
}
}
function createCard(service) {
const ext = service.icon.endsWith('.svg') ? 'svg' : 'png';
const iconUrl = `https://raw.githubusercontent.com/homarr-labs/dashboard-icons/main/${ext}/${service.icon}`;
const id = `service-${Math.random().toString(36).substr(2, 9)}`;
return `
<a id="${id}" href="${service.url}" target="_blank"
class="relative bg-gray-500/50 hover:bg-white/70 transition rounded-md p-2 shadow-lg flex flex-col">
<div class="flex items-center justify-start gap-2">
<img src="${iconUrl}" alt="${service.name}" class="w-12 h-12" />
<span class="bg-gray-600 bg-opacity-80 text-white text-sm font-semibold px-2 py-1 rounded-md">
${service.name}
</span>
</div>
</a>
`;
}
async function renderDashboard(data) {
const container = document.getElementById('dashboard');
const layout = data.layout?.trim() || 'rows';
const services = data.services;
// Group services by category
const categories = {};
services.forEach(service => {
const category = service.category?.trim() || 'Uncategorized';
if (!categories[category]) {
categories[category] = [];
}
categories[category].push(service);
});
// Set layout class for the whole page (categories container)
const layoutClass = layout === 'columns'
? 'grid gap-4 grid-cols-[repeat(auto-fit,minmax(250px,1fr))]'
: 'flex flex-col gap-2';
const layoutContainer = document.createElement('div');
layoutContainer.className = layoutClass;
for (const [category, items] of Object.entries(categories)) {
const section = document.createElement('section');
section.className = 'gap-2';
const categoryTitle = document.createElement('button');
categoryTitle.className = 'text-2xl font-bold mb-2 flex items-center gap-2 cursor-default';
categoryTitle.innerHTML = `
<span class="transform transition-transform duration-200">▸</span>
${category}
`;
categoryTitle.addEventListener('click', () => {
itemsContainer.classList.toggle('hidden');
const arrow = categoryTitle.querySelector('span');
arrow.style.transform = itemsContainer.classList.contains('hidden') ? 'rotate(90deg)' : 'rotate(0deg)';
});
const itemsContainer = document.createElement('div');
itemsContainer.className = 'flex flex-col gap-2';
for (const service of items) {
const cardHTML = createCard(service);
const cardWrapper = document.createElement('div');
cardWrapper.innerHTML = cardHTML;
const cardElement = cardWrapper.firstElementChild;
await checkServiceStatus(service, cardElement);
itemsContainer.appendChild(cardElement);
}
section.appendChild(categoryTitle);
section.appendChild(itemsContainer);
layoutContainer.appendChild(section);
}
container.appendChild(layoutContainer);
}
function applyBackground(background) {
const body = document.getElementById('dashboard-body');
if (!background || !background.type) return;
if (background.type === 'color' && background.color) {
body.style.backgroundColor = background.color;
body.style.backgroundImage = 'none';
}
if (background.type === 'image' && background.image) {
body.style.backgroundImage = `url("${background.image}")`;
body.style.backgroundSize = 'cover';
body.style.backgroundPosition = 'center';
body.style.backgroundRepeat = 'no-repeat';
body.style.backgroundColor = '';
}
}
loadYAML('data.yaml').then(data => {
applyBackground(data.background);
renderDashboard(data);
});