Code générique de la page d'accueil personnalisée YunoHost (Makefile, scripts, éditeur visuel)
This commit is contained in:
1 parent
44d01ea7cd
commit
455988e49c
16 files changed
+2025
No files matched your search
+435
@@ -0,0 +1,435 @@
|
||||
let state = null;
|
||||
let dirty = false;
|
||||
let dragSource = null; // { appId } or { groupId } currently being dragged
|
||||
|
||||
const PALETTE = [
|
||||
"#1db9c3", "#ff5a1f", "#2094f3", "#ffc230", "#00a651", "#e6721a",
|
||||
"#b71c1c", "#f39c12", "#0d3b66", "#3b8a8c", "#568d34", "#609926",
|
||||
"#d24939", "#0066cc", "#8e44ad", "#16a085", "#c0392b", "#2c3e50",
|
||||
];
|
||||
|
||||
function uid(prefix) {
|
||||
return `${prefix}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
function initialsOf(name) {
|
||||
const words = name.trim().split(/\s+/);
|
||||
if (words.length === 1) return words[0].slice(0, 2).toUpperCase();
|
||||
return (words[0][0] + words[1][0]).toUpperCase();
|
||||
}
|
||||
|
||||
function colorOf(name) {
|
||||
let hash = 0;
|
||||
for (let i = 0; i < name.length; i++) hash = (hash * 31 + name.charCodeAt(i)) >>> 0;
|
||||
return PALETTE[hash % PALETTE.length];
|
||||
}
|
||||
|
||||
function setStatus(text, kind) {
|
||||
const el = document.getElementById("status");
|
||||
el.textContent = text;
|
||||
el.className = "status" + (kind ? " " + kind : "");
|
||||
}
|
||||
|
||||
function markDirty() {
|
||||
dirty = true;
|
||||
setStatus("Modifications non enregistrées");
|
||||
}
|
||||
|
||||
function findAppLocation(appId) {
|
||||
const pinnedIdx = (state.pinned || []).findIndex((a) => a.id === appId);
|
||||
if (pinnedIdx !== -1) return { list: state.pinned, index: pinnedIdx };
|
||||
for (const group of state.groups) {
|
||||
const idx = group.apps.findIndex((a) => a.id === appId);
|
||||
if (idx !== -1) return { list: group.apps, index: idx };
|
||||
}
|
||||
const idx = state.ungrouped.findIndex((a) => a.id === appId);
|
||||
if (idx !== -1) return { list: state.ungrouped, index: idx };
|
||||
return null;
|
||||
}
|
||||
|
||||
function computeDropIndexGrid(containerEl, clientX, clientY) {
|
||||
const items = Array.from(containerEl.querySelectorAll(":scope > .app-tile"));
|
||||
if (items.length === 0) return 0;
|
||||
let closestIndex = items.length;
|
||||
let closestDist = Infinity;
|
||||
items.forEach((item, i) => {
|
||||
const rect = item.getBoundingClientRect();
|
||||
const cx = rect.left + rect.width / 2;
|
||||
const cy = rect.top + rect.height / 2;
|
||||
const dist = Math.hypot(clientX - cx, clientY - cy);
|
||||
if (dist < closestDist) {
|
||||
closestDist = dist;
|
||||
closestIndex = clientX < cx ? i : i + 1;
|
||||
}
|
||||
});
|
||||
return closestIndex;
|
||||
}
|
||||
|
||||
function computeDropIndexList(containerEl, selector, clientY) {
|
||||
const items = Array.from(containerEl.querySelectorAll(selector));
|
||||
if (items.length === 0) return 0;
|
||||
let closestIndex = items.length;
|
||||
let closestDist = Infinity;
|
||||
items.forEach((item, i) => {
|
||||
const rect = item.getBoundingClientRect();
|
||||
const cy = rect.top + rect.height / 2;
|
||||
const dist = Math.abs(clientY - cy);
|
||||
if (dist < closestDist) {
|
||||
closestDist = dist;
|
||||
closestIndex = clientY < cy ? i : i + 1;
|
||||
}
|
||||
});
|
||||
return closestIndex;
|
||||
}
|
||||
|
||||
function renderTile(app) {
|
||||
const tile = document.createElement("div");
|
||||
tile.className = "app-tile";
|
||||
tile.draggable = true;
|
||||
tile.dataset.appId = app.id;
|
||||
|
||||
tile.addEventListener("dragstart", (e) => {
|
||||
e.stopPropagation();
|
||||
dragSource = { appId: app.id };
|
||||
e.dataTransfer.effectAllowed = "move";
|
||||
e.dataTransfer.setData("text/plain", app.id);
|
||||
tile.classList.add("dragging");
|
||||
});
|
||||
tile.addEventListener("dragend", (e) => {
|
||||
e.stopPropagation();
|
||||
tile.classList.remove("dragging");
|
||||
dragSource = null;
|
||||
});
|
||||
|
||||
const removeBtn = document.createElement("button");
|
||||
removeBtn.className = "remove-btn";
|
||||
removeBtn.textContent = "✕";
|
||||
removeBtn.title = "Supprimer cette application";
|
||||
removeBtn.addEventListener("click", () => {
|
||||
if (!confirm(`Supprimer "${app.name}" définitivement ?`)) return;
|
||||
const loc = findAppLocation(app.id);
|
||||
if (loc) loc.list.splice(loc.index, 1);
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
|
||||
const icon = document.createElement("div");
|
||||
icon.className = "tile-icon";
|
||||
if (app.icon) {
|
||||
const img = document.createElement("img");
|
||||
img.src = app.icon;
|
||||
img.alt = "";
|
||||
icon.appendChild(img);
|
||||
} else {
|
||||
icon.style.background = app.color || "#8e8e8e";
|
||||
icon.textContent = app.initials || initialsOf(app.name);
|
||||
}
|
||||
|
||||
const name = document.createElement("div");
|
||||
name.className = "tile-name";
|
||||
name.textContent = app.name;
|
||||
|
||||
tile.appendChild(removeBtn);
|
||||
tile.appendChild(icon);
|
||||
tile.appendChild(name);
|
||||
return tile;
|
||||
}
|
||||
|
||||
function renderAddTile(onAdd) {
|
||||
const add = document.createElement("button");
|
||||
add.className = "add-tile";
|
||||
add.type = "button";
|
||||
add.textContent = "+";
|
||||
add.title = "Ajouter une application";
|
||||
add.addEventListener("click", onAdd);
|
||||
return add;
|
||||
}
|
||||
|
||||
function attachGridDropzone(gridEl, targetListGetter) {
|
||||
gridEl.addEventListener("dragover", (e) => {
|
||||
if (!dragSource || !dragSource.appId) return;
|
||||
e.preventDefault();
|
||||
gridEl.classList.add("drag-over");
|
||||
});
|
||||
gridEl.addEventListener("dragleave", () => {
|
||||
gridEl.classList.remove("drag-over");
|
||||
});
|
||||
gridEl.addEventListener("drop", (e) => {
|
||||
e.preventDefault();
|
||||
gridEl.classList.remove("drag-over");
|
||||
if (!dragSource || !dragSource.appId) return;
|
||||
const appId = dragSource.appId;
|
||||
const loc = findAppLocation(appId);
|
||||
if (!loc) return;
|
||||
const [app] = loc.list.splice(loc.index, 1);
|
||||
const targetList = targetListGetter();
|
||||
const dropIndex = computeDropIndexGrid(gridEl, e.clientX, e.clientY);
|
||||
targetList.splice(Math.min(dropIndex, targetList.length), 0, app);
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
}
|
||||
|
||||
function openAppForm(onSubmit) {
|
||||
const backdrop = document.createElement("div");
|
||||
backdrop.className = "app-form-backdrop";
|
||||
|
||||
const form = document.createElement("form");
|
||||
form.className = "app-form";
|
||||
form.innerHTML = `
|
||||
<h3>Nouvelle application</h3>
|
||||
<label>Nom
|
||||
<input type="text" name="name" required placeholder="Ex: Navidrome" />
|
||||
</label>
|
||||
<label>URL
|
||||
<input type="url" name="url" required placeholder="https://sous-domaine.tondomaine.exemple/" />
|
||||
</label>
|
||||
<label>Icône (optionnel, sinon détection auto)
|
||||
<input type="url" name="icon" placeholder="https://..." />
|
||||
</label>
|
||||
<div class="app-form-actions">
|
||||
<button type="button" class="btn btn-outline" data-cancel>Annuler</button>
|
||||
<button type="submit" class="btn btn-primary">Ajouter</button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
backdrop.appendChild(form);
|
||||
document.body.appendChild(backdrop);
|
||||
form.querySelector('input[name="name"]').focus();
|
||||
|
||||
backdrop.addEventListener("click", (e) => {
|
||||
if (e.target === backdrop) backdrop.remove();
|
||||
});
|
||||
form.querySelector("[data-cancel]").addEventListener("click", () => backdrop.remove());
|
||||
|
||||
form.addEventListener("submit", (e) => {
|
||||
e.preventDefault();
|
||||
const data = new FormData(form);
|
||||
const name = data.get("name").trim();
|
||||
const url = data.get("url").trim();
|
||||
const icon = data.get("icon").trim();
|
||||
if (!name || !url) return;
|
||||
const app = {
|
||||
id: uid("app"),
|
||||
name,
|
||||
url,
|
||||
color: colorOf(name),
|
||||
initials: initialsOf(name),
|
||||
};
|
||||
if (icon) app.icon = icon;
|
||||
onSubmit(app);
|
||||
backdrop.remove();
|
||||
});
|
||||
}
|
||||
|
||||
function renderGroup(group, index) {
|
||||
const card = document.createElement("section");
|
||||
card.className = "group-card";
|
||||
card.draggable = true;
|
||||
card.dataset.groupId = group.id;
|
||||
|
||||
card.addEventListener("dragstart", (e) => {
|
||||
dragSource = { groupId: group.id };
|
||||
e.dataTransfer.effectAllowed = "move";
|
||||
e.dataTransfer.setData("text/plain", group.id);
|
||||
card.classList.add("dragging");
|
||||
});
|
||||
card.addEventListener("dragend", () => {
|
||||
card.classList.remove("dragging");
|
||||
dragSource = null;
|
||||
});
|
||||
|
||||
const header = document.createElement("div");
|
||||
header.className = "group-header";
|
||||
|
||||
const handle = document.createElement("span");
|
||||
handle.className = "drag-handle";
|
||||
handle.textContent = "⠿";
|
||||
handle.title = "Glisser pour réordonner les groupes";
|
||||
|
||||
const nameInput = document.createElement("input");
|
||||
nameInput.className = "group-name-input";
|
||||
nameInput.value = group.name;
|
||||
nameInput.draggable = false;
|
||||
nameInput.addEventListener("input", () => {
|
||||
group.name = nameInput.value;
|
||||
markDirty();
|
||||
});
|
||||
nameInput.addEventListener("mousedown", (e) => e.stopPropagation());
|
||||
|
||||
const collapsedLabel = document.createElement("label");
|
||||
collapsedLabel.className = "group-collapsed-label";
|
||||
const collapsedCheckbox = document.createElement("input");
|
||||
collapsedCheckbox.type = "checkbox";
|
||||
collapsedCheckbox.checked = !!group.collapsed;
|
||||
collapsedCheckbox.addEventListener("change", () => {
|
||||
group.collapsed = collapsedCheckbox.checked;
|
||||
markDirty();
|
||||
});
|
||||
collapsedLabel.appendChild(collapsedCheckbox);
|
||||
collapsedLabel.appendChild(document.createTextNode("replié par défaut"));
|
||||
|
||||
const deleteBtn = document.createElement("button");
|
||||
deleteBtn.className = "icon-btn";
|
||||
deleteBtn.textContent = "✕";
|
||||
deleteBtn.title = "Supprimer le groupe (les apps repassent en \"Sans groupe\")";
|
||||
deleteBtn.addEventListener("click", () => {
|
||||
if (!confirm(`Supprimer le groupe "${group.name}" ? Les apps repasseront en "Sans groupe".`)) return;
|
||||
state.ungrouped.push(...group.apps);
|
||||
state.groups.splice(index, 1);
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
|
||||
header.appendChild(handle);
|
||||
header.appendChild(nameInput);
|
||||
header.appendChild(collapsedLabel);
|
||||
header.appendChild(deleteBtn);
|
||||
|
||||
const grid = document.createElement("div");
|
||||
grid.className = "tile-grid";
|
||||
group.apps.forEach((app) => grid.appendChild(renderTile(app)));
|
||||
grid.appendChild(renderAddTile(() => {
|
||||
openAppForm((app) => {
|
||||
group.apps.push(app);
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
}));
|
||||
attachGridDropzone(grid, () => group.apps);
|
||||
|
||||
card.appendChild(header);
|
||||
card.appendChild(grid);
|
||||
return card;
|
||||
}
|
||||
|
||||
function attachGroupsDropzone(containerEl) {
|
||||
containerEl.addEventListener("dragover", (e) => {
|
||||
if (!dragSource || !dragSource.groupId) return;
|
||||
e.preventDefault();
|
||||
});
|
||||
containerEl.addEventListener("drop", (e) => {
|
||||
if (!dragSource || !dragSource.groupId) return;
|
||||
e.preventDefault();
|
||||
const groupId = dragSource.groupId;
|
||||
const fromIndex = state.groups.findIndex((g) => g.id === groupId);
|
||||
if (fromIndex === -1) return;
|
||||
const [group] = state.groups.splice(fromIndex, 1);
|
||||
const dropIndex = computeDropIndexList(containerEl, ".group-card", e.clientY);
|
||||
state.groups.splice(Math.min(dropIndex, state.groups.length), 0, group);
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
}
|
||||
|
||||
function render() {
|
||||
const root = document.getElementById("editor-root");
|
||||
root.innerHTML = "";
|
||||
|
||||
const pinnedSection = document.createElement("section");
|
||||
pinnedSection.className = "pinned-section";
|
||||
const pinnedHeading = document.createElement("h2");
|
||||
pinnedHeading.textContent = "Épinglées (en haut de la page)";
|
||||
pinnedSection.appendChild(pinnedHeading);
|
||||
|
||||
const pinnedGrid = document.createElement("div");
|
||||
pinnedGrid.className = "tile-grid";
|
||||
(state.pinned || []).forEach((app) => pinnedGrid.appendChild(renderTile(app)));
|
||||
pinnedGrid.appendChild(renderAddTile(() => {
|
||||
openAppForm((app) => {
|
||||
state.pinned = state.pinned || [];
|
||||
state.pinned.push(app);
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
}));
|
||||
attachGridDropzone(pinnedGrid, () => (state.pinned = state.pinned || []));
|
||||
pinnedSection.appendChild(pinnedGrid);
|
||||
root.appendChild(pinnedSection);
|
||||
|
||||
const groupsList = document.createElement("div");
|
||||
groupsList.id = "groups-list";
|
||||
state.groups.forEach((group, i) => groupsList.appendChild(renderGroup(group, i)));
|
||||
attachGroupsDropzone(groupsList);
|
||||
root.appendChild(groupsList);
|
||||
|
||||
const addGroupBtn = document.createElement("button");
|
||||
addGroupBtn.className = "add-group-btn";
|
||||
addGroupBtn.type = "button";
|
||||
addGroupBtn.textContent = "+ Nouveau groupe";
|
||||
addGroupBtn.addEventListener("click", () => {
|
||||
state.groups.push({ id: uid("group"), name: "Nouveau groupe", collapsed: false, apps: [] });
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
root.appendChild(addGroupBtn);
|
||||
|
||||
const ungroupedSection = document.createElement("section");
|
||||
ungroupedSection.className = "ungrouped-section";
|
||||
const heading = document.createElement("h2");
|
||||
heading.textContent = "Sans groupe";
|
||||
ungroupedSection.appendChild(heading);
|
||||
|
||||
const ungroupedGrid = document.createElement("div");
|
||||
ungroupedGrid.className = "tile-grid";
|
||||
state.ungrouped.forEach((app) => ungroupedGrid.appendChild(renderTile(app)));
|
||||
ungroupedGrid.appendChild(renderAddTile(() => {
|
||||
openAppForm((app) => {
|
||||
state.ungrouped.push(app);
|
||||
markDirty();
|
||||
render();
|
||||
});
|
||||
}));
|
||||
attachGridDropzone(ungroupedGrid, () => state.ungrouped);
|
||||
ungroupedSection.appendChild(ungroupedGrid);
|
||||
|
||||
root.appendChild(ungroupedSection);
|
||||
}
|
||||
|
||||
async function load() {
|
||||
const res = await fetch("config.json", { cache: "no-store" });
|
||||
state = await res.json();
|
||||
render();
|
||||
}
|
||||
|
||||
async function save() {
|
||||
try {
|
||||
const res = await fetch("/__save", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(state, null, 2),
|
||||
});
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
dirty = false;
|
||||
setStatus("Enregistré ✓", "saved");
|
||||
setTimeout(() => setStatus(""), 2000);
|
||||
} catch (err) {
|
||||
setStatus("Erreur d'enregistrement", "error");
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
async function exportToOutput() {
|
||||
await save();
|
||||
try {
|
||||
const res = await fetch("/__export", { method: "POST" });
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
setStatus("Exporté vers output/ ✓", "saved");
|
||||
setTimeout(() => setStatus(""), 2500);
|
||||
} catch (err) {
|
||||
setStatus("Erreur d'export", "error");
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById("save-btn").addEventListener("click", save);
|
||||
document.getElementById("export-btn").addEventListener("click", exportToOutput);
|
||||
|
||||
window.addEventListener("beforeunload", (e) => {
|
||||
if (dirty) {
|
||||
e.preventDefault();
|
||||
e.returnValue = "";
|
||||
}
|
||||
});
|
||||
|
||||
load();
|
||||
Reference in new issue
Block a user