//License: Expat(MIT)
//Chris Xiong 2017
var pp=5,pn=0,animating,adir=1;
var t,c,psw,curp,cbuf,cmdtl,flt;
function request(url,func)
{
var h=new XMLHttpRequest();
h.open("GET",url);
h.onload=()=>{func(h.response,h.status)};
h.send();
}
function parsetags(tgs)
{
var r="";
var a=tgs.split(",");
r="#"+a[0];
for(var i=1;i<a.length;++i)r+=" #"+a[i];
return r;
}
function rmblk(i)
{
var l=c.querySelectorAll("div.pendingrm");
if(i>=l.length)
{
setTimeout(function(){for(var i of l)c.removeChild(i);},500);
return;
}
var e=l.item(i);
var w=e.getBoundingClientRect().width;
e.style.left=adir*-1.3*w+"px";
setTimeout(rmblk,100,i+1);
}
function etrblk(i)
{
var l=c.querySelectorAll("div.block:not(.pendingrm)");
if(!i)
{
for(var e of l)
if(e.id!="ptemplate")e.style.left=(adir*1.2*e.getBoundingClientRect().width)+"px";
}
if(i>=l.length)
{
setTimeout(()=>{animating=false;},500);
return;
}
var e=l.item(i);
if(e.id!="ptemplate")
e.style.left="0";
setTimeout(etrblk,100,i+1);
}
function setfilter(f,nlp)
{
if(animating)return;
if(f[0]=='#')f=f.substr(1);
var ch=document.getElementById('tagslist').children;
var ff=false;
for(var i=0;i<ch.length;++i)
if(ch[i].children[0].innerHTML=="#"+f)
if(ch[i].children[0].classList.contains('active'))ch[i].children[0].classList.remove('active'),ff=true;
else ch[i].children[0].classList.add('active');else ch[i].children[0].classList.remove('active');
flt=f;if(ff)flt="";
pn=-1;if(!nlp)loadpage(0);
}
function modloc()
{
var base=window.location.toString().substr(0,window.location.toString().search('/blog/list')+10);
var ret=base+(flt.length?'/'+flt:'')+'/'+pn;
window.history.replaceState("","Chrisoft::Blog",ret);
}
function loadpage(_pn)
{
if(animating||_pn==pn)return;
if(_pn>pn)adir=1;else adir=-1;
pn=_pn;animating=true;modloc();
var l=c.querySelectorAll("div.block");
for(var i of l)
if(i.id!="ptemplate")
{
i.classList.add("pendingrm");
r=i.getBoundingClientRect();
i.style.top=(r.top-16)+"px";
i.style.left=r.left+"px";
i.style.width=r.width+"px";
}
for(var i of l)if(i.id!="ptemplate")i.style.position="fixed";
setTimeout(rmblk,10,0);
curp.innerHTML=(pn+1)+"/";
request("/blog/cgi-bin/get-archive-list.cgi?pp="+pp+"&pc"+(flt.length?"&f="+flt:""),
function(r){curp.innerHTML+=Number(r).toString();});
request("/blog/cgi-bin/get-archive-list.cgi?pp="+pp+"&pn="+pn+(flt.length?"&f="+flt:""),
function(r,s)
{
if(s!=200){animating=false;return;}
o=JSON.parse(r);
for(var i=0;i<o.postsOnPage;++i)
{
var e=t.cloneNode(true);
e.style.display="block";e.id="";
e.querySelector("h3").innerHTML='<a href=/blog/post/'+o.posts[i].filename+'>'+o.posts[i].title+'</a>';
e.querySelector("span.left").innerHTML=parsetags(o.posts[i].tags);
e.querySelector("span.right").innerHTML=o.posts[i].date;
c.insertBefore(e,psw);
}
setTimeout(etrblk,10,0);
}
);
}
function lastpage(){loadpage(pn-1<0?0:pn-1);}
function nextpage(){request("/blog/cgi-bin/get-archive-list.cgi?pp="+pp+"&pc"+(flt.length?"&f="+flt:""),function(r){var pc=Number(r);if(pn+1<pc)loadpage(pn+1);});}
function blinit()
{
var parr=window.location.pathname.substr(10).split('/');
var pflt="";ppn=0;
if(parr.length>1&&parr[1].length)
{
if(!isNaN(parr[1]))ppn=Number(parr[1]);
else if(parr.length>2&&parr[2].length&&!isNaN(parr[2])){pflt=parr[1];ppn=Number(parr[2]);}
else if(parr.length>=2)pflt=parr[1];
}
t=document.getElementById("ptemplate");
c=document.getElementById("content");
psw=document.getElementById("insanch");
curp=document.getElementById("curp");
cbuf=document.getElementById("cmdbuf");
document.onkeypress=keypress;
document.onkeydown=(e)=>{switch(e.key){case "ArrowLeft":lastpage();break;case "ArrowRight":nextpage();break;}};
request("/blog/cgi-bin/get-archive-list.cgi?gt",
(r)=>
{
o=JSON.parse(r);
for(var i=0;i<o.length;++i)
{
var l=document.createElement('li');
l.innerHTML='<a href="javascript:void(0);" onclick="setfilter(\''+o[i]+'\');">'+o[i]+"</a>";
document.getElementById('tagslist').appendChild(l);
}
pn=-1;setfilter("#"+pflt,true);loadpage(ppn);
}
);
}
function showcmdbuf()
{
cbuf.style.opacity="1";
if(cmdtl){clearTimeout(cmdtl);cmdtl=0;}
cmdtl=setTimeout(execcmd,1000);
}
function execcmd()
{
var v=false;
switch(cbuf.innerHTML)
{
case "h":case "k":
lastpage();v=true;
break;
case "j":case "l":
nextpage();v=true;
break;
case "gg":
loadpage(0);v=true;
break;
case "G":
v=true;
request("/blog/cgi-bin/get-archive-list.cgi?pp="+pp+"&pc",
function(r){var pc=Number(r);loadpage(pc-1);}
);
break;
case "xyzzy":
cbuf.innerHTML="Going minesweeping!";
v=true;setTimeout(()=>{window.location.href="/minesweeper";},500);
break;
}
if(parseInt(cbuf.innerHTML).toString()+"G"==cbuf.innerHTML.trim())
{
v=true;
request("/blog/cgi-bin/get-archive-list.cgi?pp="+pp+"&pc",
function(r)
{var pc=Number(r),tpn=parseInt(cbuf.innerHTML)-1;if(tpn>=pc)tpn=pc-1;if(tpn<0)tpn=0;loadpage(tpn);}
);
}
if(cbuf.innerHTML[0]==':')
{
var sparr=cbuf.innerHTML.split(' ');
switch(sparr[0])
{
case ':setfilter':
case ':filter':
case ':flt':
v=true;
if(sparr.length<2)
{
cbuf.innerHTML='require 1 parameter';
}
else setfilter(sparr[1]);
break;
case ':nofilter':
case ':noflt':
v=true;
setfilter('');
break;
}
}
cbuf.style.opacity="0";
if(!v)cbuf.innerHTML="Unknown command.";
}
function keypress(e)
{
if(cbuf.style.opacity=="0")cbuf.innerHTML="";
if(e.key.length==1)
{
cbuf.innerHTML+=e.key;showcmdbuf();
if(cbuf.innerHTML.length==1&&'hjkl'.search(cbuf.innerHTML)!=-1)
execcmd();
}
if(e.key=="Enter")execcmd();
}