brendmanager/public/plan.html

101 lines
11 KiB
HTML

<!doctype html>
<html lang="ru"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title data-t="ui.plan.title"></title>
<style>
body{font:15px system-ui,sans-serif;margin:0;padding:16px;max-width:1100px;margin:auto;color:#222}
h1{font-size:20px}h2{font-size:16px;margin-top:24px}
table{border-collapse:collapse;width:100%}td,th{border:1px solid #ddd;padding:4px 8px;text-align:left}
.skew{background:#ffe3e0;font-weight:600}.grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(150px,1fr));gap:6px}
.day{border:1px solid #ddd;border-radius:6px;padding:6px;min-height:60px;font-size:13px}.day.empty{color:#aaa}
.idea{background:#eef3ff;border-radius:4px;padding:2px 4px;margin-top:3px;cursor:pointer}
.noa{background:#ffd9a8}.st{font-size:11px;border-radius:8px;padding:0 5px;margin-left:4px;background:#ddd}.st-review{background:#fff0c2}.st-approved{background:#d8f2d8}.st-published{background:#cfe3ff}
form{display:grid;gap:8px;max-width:420px}#err{color:#b00}
.smp{display:inline-block;border:1px dashed #9a7400;background:#fff6d6;color:#6b5000;border-radius:4px;padding:0 5px;font-size:12px}
label{display:grid;gap:2px}label>span{font-size:13px;color:#555}.hint{font-size:13px;color:#555;margin:2px 0}.none{color:#555;font-style:italic}
input,select,textarea,button{font:inherit}.card{border:1px solid #ccc;border-radius:8px;padding:8px 12px;max-width:700px}.st.ok{background:#d8f2d8}.st.off{background:#eee}button.off{opacity:.55;cursor:not-allowed}.real{border:1px solid #2a6;border-radius:4px;padding:0 5px;font-size:12px;color:#174}p.st{font-size:14px;border-radius:6px;padding:4px 8px;margin-left:0}#card label{display:inline-grid;margin-right:8px;vertical-align:bottom}
</style></head><body>
<p><a href="/" data-t="ui.common.home"></a></p>
<h1 data-t="ui.plan.h1"></h1>
<p data-t="ui.plan.intro"></p>
<label style="max-width:420px"><span data-t="ui.plan.start"></span><input type="date" id="start" data-tip="ui.plan.startTip"></label>
<p class="hint" data-t="ui.plan.startHint"></p><p id="err" role="alert"></p>
<h2 data-t="ui.plan.hSummary"></h2><p class="none" id="sumEmpty" data-t="ui.plan.sumEmpty" hidden></p><table id="summary"></table>
<h2 id="hForm" data-t="ui.plan.hNew"></h2>
<form id="f"><input type="hidden" id="id">
<label><span data-t="ui.plan.f.title"></span><input id="title" data-ph="ui.plan.f.titlePh" aria-required="true"></label>
<label><span data-t="ui.plan.f.desc"></span><textarea id="desc" data-ph="ui.plan.f.descPh"></textarea></label>
<label><span data-t="ui.plan.f.rubric"></span><select id="rubric" data-tip="ui.plan.f.rubricTip"></select></label>
<label><span data-t="ui.plan.f.date"></span><input type="date" id="date" data-tip="ui.plan.f.dateTip"></label>
<p class="hint" data-t="ui.plan.f.dateHint"></p>
<div><button data-t="ui.plan.save" data-tip="ui.plan.saveTip"></button> <button type="button" id="del" hidden data-t="ui.plan.del" data-tip="ui.plan.delTip"></button> <button type="button" id="new" data-t="ui.plan.clear" data-tip="ui.plan.clearTip"></button></div></form>
<h2 id="apcardh" data-t="plan.card.h"></h2><p class="hint" data-t="plan.card.lead"></p>
<!--plan-card-->
<h2 data-t="ap.pending"></h2><table id="pend"></table>
<h2 data-t="ap.history"></h2><p id="hint" data-t="ap.hint"></p><div id="card" hidden></div>
<label style="max-width:420px;margin-top:16px"><span data-t="ap.filter"></span><select id="filter" data-tip="ap.filterTip"></select></label>
<h2 data-t="ui.plan.hUnsched"></h2><div id="unsched"></div>
<h2 data-t="ui.plan.hCal"></h2><p class="none" id="calEmpty" data-t="ui.plan.calEmpty" hidden></p><div class="grid" id="cal"></div>
<script>
let S;const $=id=>document.getElementById(id);
const esc=s=>String(s).replace(/[&<>"]/g,c=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;"}[c]));
const rn=id=>S.settings.rubrics.find(r=>r.id===id).name;
const WANT=new URLSearchParams(location.search).get("lang")||"ru";
async function api(m,u,b){const r=await fetch(u,{method:m,headers:{"content-type":"application/json","x-lang":WANT},body:b&&JSON.stringify(b)});
const d=await r.json();$("err").textContent=r.ok?"":d.error;if(r.ok){S=d;render()}return r.ok}
let TX={};const tx=k=>TX[k]||k,when=v=>v?v.slice(0,16).replace("T"," "):"";
const stl=s=>tx("ap.status."+s);
const chip=i=>`<div class="idea" data-id="${i.id}" title="${esc(tx("ui.plan.chipTip"))}">${esc(i.title)} · ${esc(rn(i.rubric))}<span class="st st-${i.status}">${esc(stl(i.status))}</span>${i.noAssets?`<span class="st noa">${esc(tx("asset.none"))}</span>`:""}</div>`;
const f=()=>$("filter").value;const vis=l=>l.filter(i=>!f()||i.status===f());
const opts=l=>l.map(x=>`<option value="${esc(x)}">${esc(x)}</option>`).join("");
function renderAp(){
const cur=f();$("filter").innerHTML=`<option value="">${esc(tx("ap.all"))}</option>`+S.statuses.map(x=>`<option value="${x}">${esc(stl(x))}</option>`).join("");$("filter").value=cur;
$("pend").innerHTML=S.pending.length?`<tr><th>${esc(tx("ui.plan.col.item"))}</th><th>${esc(tx("ap.approver"))}</th><th>${esc(tx("ui.plan.col.sent"))}</th></tr>`+S.pending.map(p=>`<tr><td>${esc(p.title)}</td><td>${esc(p.approver)}</td><td>${esc(tx("ap.sentOn").replace("{when}",when(p.submittedAt)))}</td></tr>`).join(""):`<tr><td class="none">${esc(tx("ap.pendingEmpty"))}</td></tr>`;
const i=S.ideas.find(x=>x.id==sel);$("card").hidden=!i;if(i)card(i);
}
let sel=null;
const cmt=i=>i.comments.length?`<h3>${esc(tx("ap.comments"))}</h3><ul>`+[...i.comments].reverse().map(c=>`<li>${esc(c.by)}: ${esc(c.text)} (${when(c.at)})</li>`).join("")+"</ul>":"";
function card(i){
const hist=i.history.map(h=>`<li>${esc(tx("ap.historyRow").replace("{by}",h.by==="system"?tx("ap.system"):h.by).replace("{from}",stl(h.from)).replace("{to}",stl(h.to)).replace("{when}",when(h.at)))}</li>`).join("");
const pubs=S.publications.filter(p=>p.ideaId===i.id).map(p=>`<li>${p.test?`<b class="smp" title="${esc(tx("ap.testWhy"))}">${esc(tx("ap.test"))}</b> `:""}${esc(tx("ap.channel."+p.channel))}, ${p.date}${p.test&&p.link?` <a href="${esc(p.link)}" target="_blank" rel="noopener">${esc(tx("botp.test.card.open"))}</a>`:""}</li>`).join("");
let act="";
if(i.status==="draft"||i.status==="review")act+=`<p><label><span>${esc(tx("ap.approver"))}</span><select id="apr" title="${esc(tx("ap.approverTip"))}">${opts(S.approvers)}</select></label> <button id="bSub" title="${esc(tx("ui.plan.submitTip"))}">${esc(tx("ap.submit"))}</button></p>`;
if(i.status==="review")act+=`<p><label><span>${esc(tx("ap.by"))}</span><select id="by" title="${esc(tx("ap.byTip"))}">${opts(S.approvers)}</select></label> <label><span>${esc(tx("ap.comment"))}</span><input id="cm" placeholder="${esc(tx("ap.commentPh"))}"></label> <button id="bOk" title="${esc(tx("ui.plan.approveTip"))}">${esc(tx("ap.approve"))}</button> <button id="bRet" title="${esc(tx("ui.plan.returnTip"))}">${esc(tx("ap.return"))}</button></p>`;
if(i.status==="approved"||i.status==="published")act+=`<p><label><span>${esc(tx("ap.channel"))}</span><select id="ch" title="${esc(tx("ap.channelTip"))}">${S.channels.map(c=>`<option value="${c}">${esc(tx("ap.channel."+c))}</option>`).join("")}</select></label> <label><span>${esc(tx("ap.pubDate"))}</span><input type="date" id="pd" title="${esc(tx("ap.pubDateTip"))}"></label> <button id="bPub" title="${esc(tx("ui.plan.pubTip"))}">${esc(tx("ap.markPublished"))}</button></p><small>${esc(tx("ap.manualNote"))}</small>`;
$("card").innerHTML=`<b>${esc(i.title)}</b><span class="st st-${i.status}">${esc(stl(i.status))}</span>${i.approver?`<div>${esc(tx("ap.approver"))}: ${esc(i.approver)}</div>`:""}${cmt(i)}<h3>${esc(tx("asset.list"))} · <a href="/assets.html">${esc(tx("asset.library"))}</a></h3>${i.assets.length?"<ul>"+i.assets.map(a=>`<li>${esc(a.title)} (${esc(tx("asset.kind."+a.kind))})${a.preview?` <img src="${esc(a.preview)}" alt="" style="max-height:40px">`:a.url?` <a href="${esc(a.url)}" target="_blank" rel="noopener">${esc(tx("asset.open"))}</a>`:""}</li>`).join("")+"</ul>":`<span class="none">${esc(tx("asset.none"))}</span>`}${act}${pubs?`<h3>${esc(tx("ap.publications"))}</h3><ul>${pubs}</ul>`:""}<ul>${hist}</ul>`;
if(i.approver&&$("apr"))$("apr").value=i.approver;if(i.approver&&$("by"))$("by").value=i.approver;
const go=(a,b)=>api("POST",`/api/ideas/${i.id}/${a}`,b);
if($("bSub"))$("bSub").onclick=()=>go("submit",{approver:$("apr").value,by:$("apr").value});
if($("bOk"))$("bOk").onclick=()=>go("decision",{action:"approve",by:$("by").value});
if($("bRet"))$("bRet").onclick=()=>go("decision",{action:"return",by:$("by").value,comment:$("cm").value});
if($("bPub"))$("bPub").onclick=()=>go("publish",{channel:$("ch").value,date:$("pd").value,by:i.approver});
}
function render(){
$("start").value=S.start;
$("rubric").innerHTML=S.settings.rubrics.map(r=>`<option value="${r.id}">${esc(r.name)}</option>`).join("");
const H=k=>`<th>${esc(tx("ui.plan.col."+k))}</th>`,none=k=>`<span class="none">${esc(tx(k))}</span>`;
$("sumEmpty").hidden=S.ideas.length>0;
$("summary").innerHTML="<tr>"+["rubric","count","share","target","note"].map(H).join("")+"</tr>"+S.summary.rubrics.map(r=>
`<tr class="${r.skew?"skew":""}"><td>${esc(r.name)}</td><td>${r.count}</td><td>${r.share}%</td><td>${r.target}%</td><td>${r.skew?esc(tx("ui.plan.skew")):""}</td></tr>`).join("");
$("unsched").innerHTML=vis(S.unscheduled).map(chip).join("")||none(S.unscheduled.length?"ui.plan.filterEmpty":"ui.plan.unschedEmpty");
$("calEmpty").hidden=S.calendar.some(d=>d.ideas.length);
$("cal").innerHTML=S.calendar.map(d=>`<div class="day ${d.ideas.length?"":"empty"}"><b>${d.date}</b>${vis(d.ideas).map(chip).join("")}</div>`).join("");
renderAp();
}
function fill(i){$("id").value=i?i.id:"";$("title").value=i?i.title:"";$("desc").value=i?i.description:"";
$("rubric").value=i?i.rubric:1;$("date").value=i&&i.date||"";$("del").hidden=!i;$("hForm").textContent=tx(i?"ui.plan.hEdit":"ui.plan.hNew")}
const QS=new URLSearchParams(location.search);
document.addEventListener("click",e=>{const el=e.target.closest(".idea");if(el){const p=new URLSearchParams();if(QS.get("lang"))p.set("lang",QS.get("lang"));p.set("id",el.dataset.id);location.href="/plan.html?"+p+"#apcardh"}});
$("filter").onchange=render;
$("new").onclick=()=>fill(null);
$("del").onclick=async()=>{if(await api("DELETE","/api/ideas/"+$("id").value))fill(null)};
$("start").onchange=()=>api("PUT","/api/settings",{start:$("start").value});
$("f").onsubmit=async e=>{e.preventDefault();const b={title:$("title").value,description:$("desc").value,rubric:Number($("rubric").value),date:$("date").value||null};
const id=$("id").value;if(await(id?api("PUT","/api/ideas/"+id,b):api("POST","/api/ideas",b)))fill(null)};
const get=u=>fetch(u,{headers:{"x-lang":WANT}}).then(r=>r.json());
Promise.all([get("/api/texts"),get("/api/state")]).then(([t,d])=>{TX=t.texts;document.title=tx("ui.plan.title");document.documentElement.lang=t.lang;
for(const e of document.querySelectorAll("[data-t]"))e.textContent=tx(e.dataset.t);
for(const e of document.querySelectorAll("[data-ph]"))e.placeholder=tx(e.dataset.ph);
for(const e of document.querySelectorAll("[data-tip]"))e.title=tx(e.dataset.tip);
S=d;render();const pk=S.ideas.find(i=>String(i.id)===QS.get("id"));if(pk){sel=pk.id;fill(pk);renderAp()}else fill(null)});
</script></body></html>