Add Rulesets page with HTML structure and CSS styles

- Created a new HTML file for the Rulesets page, including a header, toolbar, rules grid, and rule detail popup.
- Implemented JavaScript functionality for loading, filtering, sorting, and managing rules.
- Added CSS styles for the layout, components, and responsive design of the Rulesets page.
This commit is contained in:
Giuseppe Raffa
2026-04-15 08:06:29 +02:00
parent c9402de2e4
commit 3094c06467
13 changed files with 2010 additions and 27 deletions

View File

@@ -0,0 +1,591 @@
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="/static/styles/style.css">
<link rel="stylesheet" href="/static/styles/rulesets.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="rs-page">
<!-- Header -->
<div class="rs-header">
<div class="rs-header-left">
<a href="/dashboard" class="rs-back">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M10 12L6 8L10 4" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
</a>
<h1>Rulesets</h1>
<div class="rs-type-picker" id="typePicker">
<button class="active" data-type="weather">Weather</button>
<button data-type="data">Data</button>
<button data-type="logs">Logs</button>
</div>
</div>
<div class="rs-header-right">
<span class="rs-saving" id="savingIndicator">Salvato</span>
</div>
</div>
<!-- Toolbar -->
<div class="rs-toolbar">
<div class="rs-toolbar-left">
<button class="rs-filter-btn active" data-filter="all">Tutte</button>
<button class="rs-filter-btn" data-filter="active">Attive</button>
<button class="rs-filter-btn" data-filter="archived">Archiviate</button>
<select class="rs-sort-select" id="sortSelect">
<option value="created_at">Data creazione</option>
<option value="version">Versione</option>
</select>
</div>
<div class="rs-toolbar-right">
<button class="rs-new-btn" id="newRuleBtn">+ Nuova Rule</button>
</div>
</div>
<!-- Rules Grid -->
<div class="rs-grid" id="rulesGrid">
<div class="rs-empty">Caricamento...</div>
</div>
</div>
<!-- Rule Detail Popup -->
<div class="rs-overlay" id="ruleOverlay" style="display:none">
<div class="rs-popup">
<div class="rs-popup-header">
<div class="rs-popup-header-left">
<span class="rs-card-id" id="popupId"></span>
<span class="rs-saving" id="popupSaving">Salvato</span>
</div>
<button class="rs-popup-close" id="popupClose">&times;</button>
</div>
<div class="rs-popup-body">
<!-- Version + Description -->
<div class="rs-section">
<div class="rs-field-row">
<span class="rs-field-label">Versione</span>
<input class="rs-field-input" id="popupVersion" placeholder="1.0.0" />
</div>
<div class="rs-field-row" id="popupDescRow" style="display:none">
<span class="rs-field-label">Descrizione</span>
<textarea class="rs-field-textarea" id="popupDesc" placeholder="Descrizione opzionale..."></textarea>
</div>
</div>
<!-- Tags -->
<div class="rs-section">
<div class="rs-section-title">Tags</div>
<div class="rs-tags-wrap" id="popupTags">
<input class="rs-tag-input" id="tagInput" placeholder="Aggiungi tag..." />
</div>
</div>
<!-- Actions -->
<div class="rs-section">
<div class="rs-section-title">Azioni</div>
<div class="rs-actions">
<button class="rs-action-btn active-toggle" id="toggleActiveBtn">Attiva</button>
<button class="rs-action-btn archive-toggle" id="toggleArchiveBtn">Archivia</button>
<button class="rs-action-btn danger" id="deleteRuleBtn">Elimina</button>
</div>
</div>
<!-- Items -->
<div class="rs-section">
<div class="rs-items-header">
<div class="rs-section-title">Items</div>
<button class="rs-add-item-btn" id="addItemBtn">+ Aggiungi</button>
</div>
<div class="rs-item-labels" id="itemLabelsRow"></div>
<div id="itemsList"></div>
</div>
</div>
</div>
</div>
<!-- Confirm Dialog -->
<div class="rs-confirm-overlay" id="confirmOverlay" style="display:none">
<div class="rs-confirm-box">
<h3 id="confirmTitle">Conferma</h3>
<p id="confirmText">Sei sicuro?</p>
<div class="rs-confirm-actions">
<button id="confirmCancel">Annulla</button>
<button class="confirm-danger" id="confirmOk">Conferma</button>
</div>
</div>
</div>
<script>
const API_URL = '{{ apiUrl }}';
// --- State ---
let currentType = 'weather';
let currentFilter = 'all';
let currentSort = 'created_at';
let allRules = [];
let openRule = null; // rule attualmente aperta nel popup
let saveTimers = {};
// --- Item field definitions per tipo ---
const ITEM_SCHEMA = {
weather: [
{ key: 'group_name', label: 'Gruppo', cls: 'medium' },
{ key: 'ref', label: 'Ref', cls: 'medium' },
{ key: 'name', label: 'Nome', cls: 'wide' },
{ key: 'unit', label: 'Unita', cls: 'narrow' },
],
data: [
{ key: 'category', label: 'Categoria', cls: 'medium' },
{ key: 'path', label: 'Path', cls: 'wide' },
{ key: 'unit', label: 'Unita', cls: 'narrow' },
],
logs: [
{ key: 'path', label: 'Path', cls: 'wide' },
{ key: 'ref', label: 'Ref', cls: 'narrow' },
{ key: 'unit', label: 'Unita', cls: 'narrow' },
{ key: 'measurement', label: 'Measurement', cls: 'medium' },
],
};
const HAS_DESC = { weather: true, data: false, logs: true };
// ========== API helpers ==========
async function api(method, path, body) {
const opts = { method, headers: {} };
if (body) {
opts.headers['Content-Type'] = 'application/json';
opts.body = JSON.stringify(body);
}
const res = await fetch(`${API_URL}${path}`, opts);
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.error || `HTTP ${res.status}`);
}
return res.json();
}
// ========== Load & Render Rules ==========
async function loadRules() {
try {
const data = await api('GET', '/rules');
allRules = data[currentType] || [];
renderGrid();
} catch (err) {
console.error('Error loading rules:', err);
document.getElementById('rulesGrid').innerHTML = '<div class="rs-empty">Errore nel caricamento</div>';
}
}
function filterAndSort(rules) {
let filtered = rules;
if (currentFilter === 'active') filtered = rules.filter(r => r.active && !r.archived);
else if (currentFilter === 'archived') filtered = rules.filter(r => r.archived);
filtered.sort((a, b) => {
if (currentSort === 'version') return (b.version || '').localeCompare(a.version || '', undefined, { numeric: true });
return new Date(b.created_at) - new Date(a.created_at);
});
return filtered;
}
function renderGrid() {
const grid = document.getElementById('rulesGrid');
const rules = filterAndSort(allRules);
if (rules.length === 0) {
grid.innerHTML = '<div class="rs-empty">Nessuna rule trovata</div>';
return;
}
grid.innerHTML = rules.map(r => {
const badges = [];
if (r.active) badges.push('<span class="rs-badge active">Attiva</span>');
else badges.push('<span class="rs-badge inactive">Inattiva</span>');
if (r.archived) badges.push('<span class="rs-badge archived">Archiviata</span>');
const tags = (r.tags || []).map(t => `<span class="rs-tag">${esc(t)}</span>`).join('');
const desc = r.description ? `<div class="rs-card-desc">${esc(r.description)}</div>` : '';
const date = r.created_at ? new Date(r.created_at).toLocaleDateString('it-IT', { day: '2-digit', month: 'short', year: 'numeric' }) : '';
return `
<div class="rs-card" data-id="${r.id}" onclick="openRuleDetail('${r.id}')">
<div class="rs-card-header">
<div>
<div class="rs-card-version">v${esc(r.version)}</div>
<span class="rs-card-id">${esc(r.id)}</span>
</div>
<div class="rs-card-badges">${badges.join('')}</div>
</div>
${desc}
${tags ? `<div class="rs-card-tags">${tags}</div>` : ''}
<div class="rs-card-footer">
<span class="rs-card-date">${date}</span>
</div>
</div>`;
}).join('');
}
function esc(str) {
if (!str) return '';
const d = document.createElement('div');
d.textContent = str;
return d.innerHTML;
}
// ========== Type Picker ==========
document.querySelectorAll('#typePicker button').forEach(btn => {
btn.onclick = () => {
document.querySelectorAll('#typePicker button').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentType = btn.dataset.type;
loadRules();
};
});
// ========== Filters ==========
document.querySelectorAll('.rs-filter-btn').forEach(btn => {
btn.onclick = () => {
document.querySelectorAll('.rs-filter-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentFilter = btn.dataset.filter;
renderGrid();
};
});
document.getElementById('sortSelect').onchange = (e) => {
currentSort = e.target.value;
renderGrid();
};
// ========== New Rule ==========
document.getElementById('newRuleBtn').onclick = async () => {
try {
const rule = await api('POST', `/rules/${currentType}`, {
version: '1.0.0',
tags: [],
description: HAS_DESC[currentType] ? '' : undefined
});
allRules.unshift(rule);
renderGrid();
openRuleDetail(rule.id);
flash('Salvato');
} catch (err) {
console.error('Error creating rule:', err);
}
};
// ========== Rule Detail Popup ==========
async function openRuleDetail(ruleId) {
try {
const data = await api('GET', `/rules/${currentType}/${ruleId}`);
openRule = data;
renderPopup();
document.getElementById('ruleOverlay').style.display = 'flex';
} catch (err) {
console.error('Error loading rule detail:', err);
}
}
function closePopup() {
document.getElementById('ruleOverlay').style.display = 'none';
openRule = null;
loadRules(); // refresh grid
}
document.getElementById('popupClose').onclick = closePopup;
document.getElementById('ruleOverlay').onclick = (e) => {
if (e.target === document.getElementById('ruleOverlay')) closePopup();
};
function renderPopup() {
const r = openRule;
document.getElementById('popupId').textContent = r.id;
document.getElementById('popupVersion').value = r.version || '';
// Description
const descRow = document.getElementById('popupDescRow');
if (HAS_DESC[currentType]) {
descRow.style.display = 'flex';
document.getElementById('popupDesc').value = r.description || '';
} else {
descRow.style.display = 'none';
}
// Tags
renderTags();
// Action buttons state
updateActionButtons();
// Items
renderItems();
}
// --- Auto-save fields ---
document.getElementById('popupVersion').oninput = () => debounceFieldSave('version');
document.getElementById('popupDesc').oninput = () => debounceFieldSave('description');
function debounceFieldSave(field) {
clearTimeout(saveTimers[field]);
saveTimers[field] = setTimeout(() => saveRuleField(field), 500);
}
async function saveRuleField(field) {
if (!openRule) return;
const body = {};
if (field === 'version') body.version = document.getElementById('popupVersion').value.trim();
if (field === 'description') body.description = document.getElementById('popupDesc').value.trim();
try {
const updated = await api('PUT', `/rules/${currentType}/${openRule.id}`, body);
Object.assign(openRule, updated);
// Update in allRules too
const idx = allRules.findIndex(r => r.id === openRule.id);
if (idx >= 0) Object.assign(allRules[idx], updated);
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error saving field:', err);
}
}
// --- Tags ---
function renderTags() {
const wrap = document.getElementById('popupTags');
const input = document.getElementById('tagInput');
// Remove old chips
wrap.querySelectorAll('.rs-tag-chip').forEach(c => c.remove());
// Re-add chips before input
(openRule.tags || []).forEach((tag, i) => {
const chip = document.createElement('span');
chip.className = 'rs-tag-chip';
chip.innerHTML = `${esc(tag)}<button data-idx="${i}">&times;</button>`;
chip.querySelector('button').onclick = () => removeTag(i);
wrap.insertBefore(chip, input);
});
}
document.getElementById('tagInput').onkeydown = async (e) => {
if (e.key === 'Enter' || e.key === ',') {
e.preventDefault();
const val = e.target.value.trim().replace(/,$/, '');
if (!val || !openRule) return;
const tags = [...(openRule.tags || []), val];
e.target.value = '';
try {
const updated = await api('PUT', `/rules/${currentType}/${openRule.id}`, { tags });
openRule.tags = updated.tags;
renderTags();
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error adding tag:', err);
}
}
};
async function removeTag(idx) {
if (!openRule) return;
const tags = [...(openRule.tags || [])];
tags.splice(idx, 1);
try {
const updated = await api('PUT', `/rules/${currentType}/${openRule.id}`, { tags });
openRule.tags = updated.tags;
renderTags();
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error removing tag:', err);
}
}
// --- Action Buttons ---
function updateActionButtons() {
const r = openRule;
const activeBtn = document.getElementById('toggleActiveBtn');
const archiveBtn = document.getElementById('toggleArchiveBtn');
activeBtn.textContent = r.active ? 'Disattiva' : 'Attiva';
activeBtn.className = `rs-action-btn active-toggle${r.active ? '' : ' off'}`;
archiveBtn.textContent = r.archived ? 'Dearchivia' : 'Archivia';
archiveBtn.className = `rs-action-btn archive-toggle${r.archived ? ' on' : ''}`;
}
document.getElementById('toggleActiveBtn').onclick = async () => {
if (!openRule) return;
try {
const res = await api('PATCH', `/rules/${currentType}/${openRule.id}/active`);
openRule.active = res.active;
updateActionButtons();
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error toggling active:', err);
}
};
document.getElementById('toggleArchiveBtn').onclick = async () => {
if (!openRule) return;
try {
const res = await api('PATCH', `/rules/${currentType}/${openRule.id}/archive`);
openRule.archived = res.archived;
updateActionButtons();
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error toggling archive:', err);
}
};
document.getElementById('deleteRuleBtn').onclick = () => {
if (!openRule) return;
showConfirm('Elimina Rule', `Vuoi eliminare la rule ${openRule.id}? Questa azione e irreversibile.`, async () => {
try {
await api('DELETE', `/rules/${currentType}/${openRule.id}`);
allRules = allRules.filter(r => r.id !== openRule.id);
closePopup();
} catch (err) {
console.error('Error deleting rule:', err);
}
});
};
// --- Items ---
function renderItems() {
const schema = ITEM_SCHEMA[currentType];
const items = openRule.items || [];
// Labels row
const labelsRow = document.getElementById('itemLabelsRow');
labelsRow.innerHTML = schema.map(f => `<span class="${f.cls}">${f.label}</span>`).join('') +
'<span class="toggle-space">On</span><span class="delete-space"></span>';
// Items list
const list = document.getElementById('itemsList');
if (items.length === 0) {
list.innerHTML = '<div style="padding:12px;font-size:0.8rem;color:var(--text-tertiary);">Nessun item. Clicca "+ Aggiungi" per crearne uno.</div>';
return;
}
list.innerHTML = items.map(item => {
const fields = schema.map(f =>
`<input class="rs-item-field ${f.cls}" value="${esc(String(item[f.key] || ''))}" data-field="${f.key}" data-item-id="${item.id}" onchange="saveItemField(this)" />`
).join('');
const toggleCls = item.enabled ? 'on' : '';
return `<div class="rs-item" data-item-id="${item.id}">
${fields}
<div class="rs-toggle ${toggleCls}" onclick="toggleItem(${item.id})"></div>
<button class="rs-item-delete" onclick="deleteItem(${item.id})">&times;</button>
</div>`;
}).join('');
}
document.getElementById('addItemBtn').onclick = async () => {
if (!openRule) return;
const schema = ITEM_SCHEMA[currentType];
const body = {};
// Fill with empty/default values
schema.forEach(f => { body[f.key] = ''; });
// Need at least non-empty values — open with placeholders
// For now, create with placeholder values
schema.forEach(f => { body[f.key] = f.key === 'enabled' ? true : '-'; });
try {
const item = await api('POST', `/rules/${currentType}/${openRule.id}/items`, body);
if (!openRule.items) openRule.items = [];
openRule.items.push(item);
renderItems();
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error adding item:', err);
}
};
async function saveItemField(input) {
if (!openRule) return;
const itemId = input.dataset.itemId;
const field = input.dataset.field;
const value = input.value.trim();
try {
await api('PUT', `/rules/${currentType}/${openRule.id}/items/${itemId}`, { [field]: value });
// Update local
const item = openRule.items.find(i => String(i.id) === String(itemId));
if (item) item[field] = value;
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error saving item field:', err);
}
}
async function toggleItem(itemId) {
if (!openRule) return;
try {
const res = await api('PATCH', `/rules/${currentType}/${openRule.id}/items/${itemId}/toggle`);
const item = openRule.items.find(i => i.id === itemId);
if (item) item.enabled = res.enabled;
renderItems();
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error toggling item:', err);
}
}
async function deleteItem(itemId) {
if (!openRule) return;
try {
await api('DELETE', `/rules/${currentType}/${openRule.id}/items/${itemId}`);
openRule.items = openRule.items.filter(i => i.id !== itemId);
renderItems();
flash('Salvato', 'popupSaving');
} catch (err) {
console.error('Error deleting item:', err);
}
}
// ========== Confirm Dialog ==========
let confirmCallback = null;
function showConfirm(title, text, onConfirm) {
document.getElementById('confirmTitle').textContent = title;
document.getElementById('confirmText').textContent = text;
confirmCallback = onConfirm;
document.getElementById('confirmOverlay').style.display = 'flex';
}
document.getElementById('confirmCancel').onclick = () => {
document.getElementById('confirmOverlay').style.display = 'none';
confirmCallback = null;
};
document.getElementById('confirmOk').onclick = async () => {
document.getElementById('confirmOverlay').style.display = 'none';
if (confirmCallback) await confirmCallback();
confirmCallback = null;
};
// ========== Flash "Salvato" indicator ==========
function flash(text, elId = 'savingIndicator') {
const el = document.getElementById(elId);
el.textContent = text;
el.classList.add('visible');
setTimeout(() => el.classList.remove('visible'), 1500);
}
// ========== Init ==========
document.addEventListener('DOMContentLoaded', () => loadRules());
</script>
</body>