MediaWiki:CommonDashboard.js
Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.
- Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (β-R su Mac)
- Google Chrome: premi Ctrl-Shift-R (β-Shift-R su un Mac)
- Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5.
// ============================================
// π§ CommonDashboard.js β versione GPT-4o + lettura file reale
// ============================================
// π Apri/chiudi box
window.toggleDashboardBox = function (id) {
const box = document.getElementById(id);
if (box) box.style.display = box.style.display === "block" ? "none" : "block";
};
// βΆοΈ Test connessione API OpenAI
window.testAPIConnection = async function () {
const key = document.getElementById('api-key').value.trim();
const prompt = document.getElementById('test-prompt').value;
const output = document.getElementById('api-result');
const model = "gpt-4o-2024-05-13";
if (!key || !prompt) {
output.innerText = 'β οΈ Inserisci una chiave valida e un prompt.';
return;
}
output.innerText = 'β³ Attendere risposta da OpenAI...';
logActivity(`π Test API avviato β Modello: <strong>${model}</strong>`);
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + key
},
body: JSON.stringify({
model: model,
messages: [{ role: "user", content: prompt }],
temperature: 0.7
})
});
const data = await response.json();
if (data.choices && data.choices.length > 0) {
output.innerText = "β
Risposta:\n" + data.choices[0].message.content;
logActivity(`β
Risposta ricevuta β Modello: <strong>${model}</strong>`);
} else if (data.error) {
output.innerText = "β Errore: " + data.error.message;
logActivity(`β Errore API: ${data.error.message}`);
} else {
output.innerText = "β Nessuna risposta ricevuta.";
}
} catch (e) {
output.innerText = "π« Errore di rete o sintassi: " + e.message;
logActivity(`β Errore di connessione: ${e.message}`);
}
};
// π Accesso File Server β Leggi file reale dal server via API PHP
document.querySelector('#readFileBtn')?.addEventListener('click', async () => {
const filenameInput = document.querySelector('#readFileName');
const fileContentDiv = document.querySelector('#readFileResult');
const filename = filenameInput.value.trim();
if (!filename) {
fileContentDiv.innerHTML = '<span style="color:red;">β οΈ Inserisci un nome di file.</span>';
return;
}
try {
const response = await fetch('/wiki/dashboard/api/read_file.php?filename=' + encodeURIComponent(filename));
const data = await response.json();
if (data.status === 'success') {
fileContentDiv.innerHTML = `<pre style="background:#f4f4f4;border:1px solid #ccc;padding:8px;max-height:400px;overflow:auto;font-size:14px;">${data.content}</pre>`;
logActivity(`π File letto: <strong>${filename}</strong>`);
} else {
fileContentDiv.innerHTML = `<span style="color:red;">β Errore: ${data.message}</span>`;
logActivity(`β Errore lettura file: ${data.message}`);
}
} catch (error) {
fileContentDiv.innerHTML = `<span style="color:red;">β Errore di connessione.</span>`;
console.error('Errore JS:', error);
}
});
// π¦ Box progetti β apertura dialog
window.openProjectDialog = function () {
document.getElementById("newProjectDialog").style.display = "block";
};
window.closeProjectDialog = function () {
document.getElementById("newProjectDialog").style.display = "none";
};
// π Aggiungi nuovo progetto alla tabella
window.addNewProject = function () {
const title = document.getElementById("newProjectTitle").value.trim();
const notes = document.getElementById("newProjectNotes").value.trim();
if (!title) return;
const table = document.getElementById("projectRows");
const newRow = document.createElement("tr");
newRow.innerHTML = `
<td style="padding:0.5rem; border:1px solid #ccc;">${title}</td>
<td style="padding:0.5rem; border:1px solid #ccc; color:green; font-weight:bold;">β
Completato</td>
<td style="padding:0.5rem; border:1px solid #ccc;">${new Date().toLocaleDateString("it-IT")}</td>
<td style="padding:0.5rem; border:1px solid #ccc;">${notes || 'β'}</td>
`;
table.appendChild(newRow);
window.closeProjectDialog();
logActivity(`π Progetto aggiunto: <strong>${title}</strong> β ${notes}`);
};
// π§ͺ Testa codice JS in tempo reale
window.runTestCode = function () {
const code = document.getElementById('codeArea').value;
const output = document.getElementById('consoleOutput');
try {
const result = eval(code);
output.textContent = "β
Output:\n" + result;
} catch (err) {
output.textContent = "β Errore:\n" + err;
}
};
// π§Ύ Registro attivitΓ β aggiungi evento
window.logActivity = function (messaggio) {
const contenitore = document.getElementById("activityLogContent");
if (!contenitore) return;
const ora = new Date().toLocaleTimeString("it-IT");
const paragrafo = document.createElement("p");
paragrafo.innerHTML = `<strong>[${ora}]</strong> ${messaggio}`;
contenitore.appendChild(paragrafo);
contenitore.scrollTop = contenitore.scrollHeight;
};
// π§Ή Svuota log
window.clearActivityLog = function () {
const contenitore = document.getElementById("activityLogContent");
contenitore.innerHTML = "<em>Registro svuotato.</em><br>";
logActivity("π§Ή Log svuotato manualmente.");
};
// πͺ Inizializza selettore file (box simulato)
document.addEventListener("DOMContentLoaded", function () {
const fileSelect = document.getElementById("fileSelect");
if (fileSelect) {
fileSelect.addEventListener("change", function () {
const selected = this.value;
const preview = document.getElementById("file-preview");
const mock = {
"common.css": "/* Contenuto CSS di esempio */",
"common.js": "// JS Demo",
"LocalSettings.php": "<?php\n# Configurazione MediaWiki\n$wgSitename = 'Masticationpedia';"
};
preview.value = mock[selected] || "Contenuto non disponibile.";
});
}
});