MediaWiki:CommonDashboard.js: differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| Riga 1: | Riga 1: | ||
/ | /* dashboard.js — Masticationpedia (colla UI) */ | ||
// ---------- Utilità ---------- | |||
// | const $ = sel => document.querySelector(sel); | ||
const $$ = sel => Array.from(document.querySelectorAll(sel)); | |||
function logActivity(msg) { | |||
const box = $('#activityLogContent'); | |||
if (!box) return; | |||
const now = new Date(); | |||
const box = | const hh = now.toTimeString().slice(0,8); | ||
const line = `[${hh}] ${msg}`; | |||
const pre = document.createElement('div'); | |||
pre.textContent = line; | |||
box.prepend(pre); | |||
} | |||
if (! | |||
const | |||
const | |||
const | |||
} | |||
async function postJSON(url, body) { | |||
function | const r = await fetch(url, { | ||
const | |||
method: 'POST', | method: 'POST', | ||
headers: { 'Content-Type': 'application/ | headers: {'Content-Type':'application/json'}, | ||
body | body: JSON.stringify(body || {}) | ||
}); | |||
}). | // alcuni endpoint tornano text; gestisco entrambe | ||
const txt = await r.text(); | |||
try { return JSON.parse(txt); } catch { return {ok:false, raw:txt}; } | |||
} | } | ||
// | // ---------- Toggle sezioni ---------- | ||
window.toggleDashboardBox = function(id){ | |||
// | // chiudi tutte | ||
['api-settings','project-status','chatgpt-plus','test-tools','activity-log'].forEach(i=>{ | |||
const el = document.getElementById(i); | |||
if (el) el.style.display = 'none'; | |||
const | }); | ||
// apri quella scelta | |||
if ( | const box = document.getElementById(id); | ||
if (box) box.style.display = 'block'; | |||
}; | |||
// ---------- Clear log server ---------- | |||
window.clearServerLog = async function(){ | |||
try { | try { | ||
const | const r = await fetch('/dashboard/api/log_clear.php', {cache:'no-store'}); | ||
const j = await r.json(); | |||
if (j.ok) { | |||
alert('Log svuotato ✅'); | |||
logActivity('🧹 Log server svuotato'); | |||
const | |||
if ( | |||
logActivity( | |||
} else { | } else { | ||
alert('Errore svuota log: ' + (j.error||'')); | |||
logActivity( | logActivity('❌ Errore svuota log'); | ||
} | } | ||
} catch (e) { | } catch(e) { | ||
alert('Errore rete: ' + e.message); | |||
} | } | ||
}; | }; | ||
// | // ---------- Test API OpenAI ---------- | ||
window.testAPIConnection = async function(){ | |||
const model = ($('#model-select')?.value || 'gpt-4o-2024-05-13').trim(); | |||
const prompt = ($('#test-prompt')?.value || 'Dimmi una curiosità sulla mandibola').trim(); | |||
logActivity(`🚀 Test API via proxy — Modello: ${model}`); | |||
const res = await postJSON('/dashboard/api/openai_project_gpt.php', {model, prompt}); | |||
const | if (res.status === 'ok' && res.result) { | ||
if ( | logActivity(`✅ Proxy OK — Modello: ${model}`); | ||
const out = $('#gptResponse'); | |||
if (out) out.textContent = res.result; | |||
} else { | |||
logActivity(`❌ Proxy errore: ${(res.error||res.raw||'sconosciuto')}`); | |||
alert('Errore API: ' + (res.error||res.raw||'')); | |||
} | |||
}; | |||
// ---------- Analizza progetto (Stato Progetti) ---------- | |||
const | function buildPromptForGPT(){ | ||
const goal = ($('#p_goal')?.value||'').trim(); | |||
const audience = ($('#p_audience')?.value||'').trim(); | |||
const deliverable = ($('#p_deliverable')?.value||'').trim(); | |||
const constraints = ($('#p_constraints')?.value||'').trim(); | |||
const parts = []; | const parts = []; | ||
parts.push( | parts.push('Sei un project manager ed editor senior. Rispondi in ITALIANO. Fornisci un piano chiaro e operativo.'); | ||
parts.push( | if (goal) parts.push('\n# Obiettivo\n' + goal); | ||
parts.push( | if (audience) parts.push('\n# Pubblico\n' + audience); | ||
parts.push( | if (deliverable) parts.push('\n# Output atteso\n' + deliverable); | ||
parts.push( | if (constraints) parts.push('\n# Vincoli\n' + constraints); | ||
parts.push(` | |||
# Formato output richiesto | |||
- Sommario introduttivo | |||
- Punti di forza e rischi | |||
- Piano step-by-step (milestones) | |||
- File/Config necessari dal server (ELENCO puntuale dei file da leggere) | |||
- Risorse & Budget hint | |||
- Metriche di successo`); | |||
return parts.join('\n'); | |||
return parts.join( | |||
} | } | ||
window.runProjectAnalysis = async function(){ | |||
const btn = $('#btnAnalyze'); | |||
window. | if (!btn) return; | ||
const btn | btn.disabled = true; btn.textContent = 'Analizzo…'; | ||
try { | try { | ||
const res = await | const prompt = buildPromptForGPT(); | ||
const model = ($('#model-select')?.value || 'gpt-4o-2024-05-13').trim(); | |||
const res = await postJSON('/dashboard/api/openai_project_gpt.php', {model, prompt}); | |||
if (res.status === 'ok' && res.result) { | |||
const out = $('#gptResponse'); | |||
if (out) out.textContent = res.result; | |||
logActivity('🧠 Analisi GPT completata'); | |||
if ( | |||
} else { | } else { | ||
const msg = res.error || res.raw || 'Errore sconosciuto'; | |||
logActivity('❌ Analisi GPT fallita: ' + msg); | |||
alert('Errore analisi: ' + msg); | |||
} | } | ||
} catch ( | } catch(e){ | ||
alert('Errore rete: ' + e.message); | |||
} finally { | } finally { | ||
btn.disabled = false; btn.textContent = 'Analizza con GPT'; | |||
} | } | ||
}; | }; | ||
// | // ---------- Progetti (crea + lista) ---------- | ||
async function refreshProjects(){ | |||
const list = $('#projectsList'); | |||
if (!list) return; | |||
list.innerHTML = '<em>Carico…</em>'; | |||
const | |||
if (! | |||
try { | try { | ||
const r = await fetch( | const r = await fetch('/dashboard/api/project_list.php', {cache:'no-store'}); | ||
const | const j = await r.json(); | ||
if (!j.ok || !Array.isArray(j.projects)) { list.textContent = 'Nessun progetto.'; return; } | |||
if (!j.projects.length) { list.textContent = 'Nessun progetto.'; return; } | |||
list.innerHTML = j.projects.map(name => ` | |||
<li style="display:flex;gap:8px;align-items:center;margin:4px 0;"> | |||
<code>${name}</code> | |||
<button data-open="${name}">Apri</button> | |||
<button data-zip="${name}">ZIP</button> | |||
<button data-del="${name}" title="Sposta nel cestino">🗑️</button> | |||
</li> | |||
`).join(''); | |||
list.querySelectorAll('[data-open]').forEach(b=>{ | |||
b.addEventListener('click', ()=>selectProject(b.getAttribute('data-open'))); | |||
}); | |||
list.querySelectorAll('[data-zip]').forEach(b=>{ | |||
b.addEventListener('click', ()=>zipProject(b.getAttribute('data-zip'))); | |||
}); | |||
list.querySelectorAll('[data-del]').forEach(b=>{ | |||
b.addEventListener('click', ()=>deleteProject(b.getAttribute('data-del'))); | |||
}); | |||
} catch(e) { | |||
list.textContent = 'Errore lista: ' + e.message; | |||
} | } | ||
} | } | ||
async function selectProject(name){ | |||
function | // eventuale endpoint select (se esiste). Per ora solo log. | ||
logActivity('📂 Project selezionato: ' + name); | |||
} | } | ||
async function zipProject(name){ | |||
async function | |||
try { | try { | ||
const | const j = await postJSON('/dashboard/api/project_backup.php', { name }); | ||
if (j.ok) { alert('ZIP creato: ' + (j.zip||'')); logActivity('🗜️ Backup creato: ' + (j.zip||'')); } | |||
else { alert('Errore ZIP: ' + (j.error||'')); } | |||
} catch(e){ alert('Errore: ' + e.message); } | |||
} catch (e) { | |||
} | } | ||
async function deleteProject(name){ | |||
if (!confirm('Spostare nel cestino "'+name+'"?')) return; | |||
async function deleteProject(name) { | |||
if (!confirm( | |||
try { | try { | ||
const | const j = await postJSON('/dashboard/api/project_delete.php', { name }); | ||
if (j.ok) { logActivity('🗑️ Progetto spostato nel cestino'); refreshProjects(); } | |||
else { alert('Errore delete: ' + (j.error||'')); } | |||
} catch(e){ alert('Errore: ' + e.message); } | |||
if ( | |||
} catch (e) { | |||
} | } | ||
async function createProject(){ | |||
async function | const inp = $('#newProjectName'); | ||
const name = (inp?.value||'').trim(); | |||
if (!name) { alert('Inserisci un nome progetto'); return; } | |||
try { | try { | ||
const | const j = await postJSON('/dashboard/api/project_create.php', { name }); | ||
if (j.ok) { | |||
logActivity('✅ Progetto creato: ' + name); | |||
inp.value = ''; | |||
refreshProjects(); | |||
} else { | |||
alert('Errore creazione: ' + (j.error||'')); | |||
if ( | } | ||
} catch(e) { | |||
alert('Errore rete: ' + e.message); | |||
} catch (e) { | |||
alert('Errore | |||
} | } | ||
} | } | ||
// | // ---------- Carica in OpenAI (stub clic pulsante in alto) ---------- | ||
window.uploadToOpenAI = function(){ | |||
alert('Funzione "Carica in OpenAI" in preparazione.'); | |||
}; | |||
// ---------- Bind all ---------- | |||
document.addEventListener('DOMContentLoaded', ()=>{ | |||
// bottoni/azioni principali già definiti in HTML | |||
$('#btnAnalyze') && $('#btnAnalyze').addEventListener('click', window.runProjectAnalysis); | |||
$('#btnCreateProject') && $('#btnCreateProject').addEventListener('click', createProject); | |||
refreshProjects(); | |||
}); | }); | ||
Versione delle 18:03, 23 set 2025
/* dashboard.js — Masticationpedia (colla UI) */
// ---------- Utilità ----------
const $ = sel => document.querySelector(sel);
const $$ = sel => Array.from(document.querySelectorAll(sel));
function logActivity(msg) {
const box = $('#activityLogContent');
if (!box) return;
const now = new Date();
const hh = now.toTimeString().slice(0,8);
const line = `[${hh}] ${msg}`;
const pre = document.createElement('div');
pre.textContent = line;
box.prepend(pre);
}
async function postJSON(url, body) {
const r = await fetch(url, {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: JSON.stringify(body || {})
});
// alcuni endpoint tornano text; gestisco entrambe
const txt = await r.text();
try { return JSON.parse(txt); } catch { return {ok:false, raw:txt}; }
}
// ---------- Toggle sezioni ----------
window.toggleDashboardBox = function(id){
// chiudi tutte
['api-settings','project-status','chatgpt-plus','test-tools','activity-log'].forEach(i=>{
const el = document.getElementById(i);
if (el) el.style.display = 'none';
});
// apri quella scelta
const box = document.getElementById(id);
if (box) box.style.display = 'block';
};
// ---------- Clear log server ----------
window.clearServerLog = async function(){
try {
const r = await fetch('/dashboard/api/log_clear.php', {cache:'no-store'});
const j = await r.json();
if (j.ok) {
alert('Log svuotato ✅');
logActivity('🧹 Log server svuotato');
} else {
alert('Errore svuota log: ' + (j.error||''));
logActivity('❌ Errore svuota log');
}
} catch(e) {
alert('Errore rete: ' + e.message);
}
};
// ---------- Test API OpenAI ----------
window.testAPIConnection = async function(){
const model = ($('#model-select')?.value || 'gpt-4o-2024-05-13').trim();
const prompt = ($('#test-prompt')?.value || 'Dimmi una curiosità sulla mandibola').trim();
logActivity(`🚀 Test API via proxy — Modello: ${model}`);
const res = await postJSON('/dashboard/api/openai_project_gpt.php', {model, prompt});
if (res.status === 'ok' && res.result) {
logActivity(`✅ Proxy OK — Modello: ${model}`);
const out = $('#gptResponse');
if (out) out.textContent = res.result;
} else {
logActivity(`❌ Proxy errore: ${(res.error||res.raw||'sconosciuto')}`);
alert('Errore API: ' + (res.error||res.raw||''));
}
};
// ---------- Analizza progetto (Stato Progetti) ----------
function buildPromptForGPT(){
const goal = ($('#p_goal')?.value||'').trim();
const audience = ($('#p_audience')?.value||'').trim();
const deliverable = ($('#p_deliverable')?.value||'').trim();
const constraints = ($('#p_constraints')?.value||'').trim();
const parts = [];
parts.push('Sei un project manager ed editor senior. Rispondi in ITALIANO. Fornisci un piano chiaro e operativo.');
if (goal) parts.push('\n# Obiettivo\n' + goal);
if (audience) parts.push('\n# Pubblico\n' + audience);
if (deliverable) parts.push('\n# Output atteso\n' + deliverable);
if (constraints) parts.push('\n# Vincoli\n' + constraints);
parts.push(`
# Formato output richiesto
- Sommario introduttivo
- Punti di forza e rischi
- Piano step-by-step (milestones)
- File/Config necessari dal server (ELENCO puntuale dei file da leggere)
- Risorse & Budget hint
- Metriche di successo`);
return parts.join('\n');
}
window.runProjectAnalysis = async function(){
const btn = $('#btnAnalyze');
if (!btn) return;
btn.disabled = true; btn.textContent = 'Analizzo…';
try {
const prompt = buildPromptForGPT();
const model = ($('#model-select')?.value || 'gpt-4o-2024-05-13').trim();
const res = await postJSON('/dashboard/api/openai_project_gpt.php', {model, prompt});
if (res.status === 'ok' && res.result) {
const out = $('#gptResponse');
if (out) out.textContent = res.result;
logActivity('🧠 Analisi GPT completata');
} else {
const msg = res.error || res.raw || 'Errore sconosciuto';
logActivity('❌ Analisi GPT fallita: ' + msg);
alert('Errore analisi: ' + msg);
}
} catch(e){
alert('Errore rete: ' + e.message);
} finally {
btn.disabled = false; btn.textContent = 'Analizza con GPT';
}
};
// ---------- Progetti (crea + lista) ----------
async function refreshProjects(){
const list = $('#projectsList');
if (!list) return;
list.innerHTML = '<em>Carico…</em>';
try {
const r = await fetch('/dashboard/api/project_list.php', {cache:'no-store'});
const j = await r.json();
if (!j.ok || !Array.isArray(j.projects)) { list.textContent = 'Nessun progetto.'; return; }
if (!j.projects.length) { list.textContent = 'Nessun progetto.'; return; }
list.innerHTML = j.projects.map(name => `
<li style="display:flex;gap:8px;align-items:center;margin:4px 0;">
<code>${name}</code>
<button data-open="${name}">Apri</button>
<button data-zip="${name}">ZIP</button>
<button data-del="${name}" title="Sposta nel cestino">🗑️</button>
</li>
`).join('');
list.querySelectorAll('[data-open]').forEach(b=>{
b.addEventListener('click', ()=>selectProject(b.getAttribute('data-open')));
});
list.querySelectorAll('[data-zip]').forEach(b=>{
b.addEventListener('click', ()=>zipProject(b.getAttribute('data-zip')));
});
list.querySelectorAll('[data-del]').forEach(b=>{
b.addEventListener('click', ()=>deleteProject(b.getAttribute('data-del')));
});
} catch(e) {
list.textContent = 'Errore lista: ' + e.message;
}
}
async function selectProject(name){
// eventuale endpoint select (se esiste). Per ora solo log.
logActivity('📂 Project selezionato: ' + name);
}
async function zipProject(name){
try {
const j = await postJSON('/dashboard/api/project_backup.php', { name });
if (j.ok) { alert('ZIP creato: ' + (j.zip||'')); logActivity('🗜️ Backup creato: ' + (j.zip||'')); }
else { alert('Errore ZIP: ' + (j.error||'')); }
} catch(e){ alert('Errore: ' + e.message); }
}
async function deleteProject(name){
if (!confirm('Spostare nel cestino "'+name+'"?')) return;
try {
const j = await postJSON('/dashboard/api/project_delete.php', { name });
if (j.ok) { logActivity('🗑️ Progetto spostato nel cestino'); refreshProjects(); }
else { alert('Errore delete: ' + (j.error||'')); }
} catch(e){ alert('Errore: ' + e.message); }
}
async function createProject(){
const inp = $('#newProjectName');
const name = (inp?.value||'').trim();
if (!name) { alert('Inserisci un nome progetto'); return; }
try {
const j = await postJSON('/dashboard/api/project_create.php', { name });
if (j.ok) {
logActivity('✅ Progetto creato: ' + name);
inp.value = '';
refreshProjects();
} else {
alert('Errore creazione: ' + (j.error||''));
}
} catch(e) {
alert('Errore rete: ' + e.message);
}
}
// ---------- Carica in OpenAI (stub clic pulsante in alto) ----------
window.uploadToOpenAI = function(){
alert('Funzione "Carica in OpenAI" in preparazione.');
};
// ---------- Bind all ----------
document.addEventListener('DOMContentLoaded', ()=>{
// bottoni/azioni principali già definiti in HTML
$('#btnAnalyze') && $('#btnAnalyze').addEventListener('click', window.runProjectAnalysis);
$('#btnCreateProject') && $('#btnCreateProject').addEventListener('click', createProject);
refreshProjects();
});