Skip to content
Lunyu.html 8.49 KiB
Newer Older
Koichi Yasuoka's avatar
Koichi Yasuoka committed
<!DOCTYPE html><head><meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Universal Dependencies on 論語</title>
<script src="conllusvgview.js"></script>
<script src="conllusvgload.js"></script>
<script>
var Lunyu=new Object();
Lunyu.url="/gitlab/api/v4/projects/Kanbun%2Fud-kanbun/repository/";
Lunyu.init=function(){
  var t=document.getElementById("top").cloneNode(true);
  t.style.position="static";
  t.style.visibility="hidden";
  document.body.appendChild(t);
  Lunyu.svg=new Array();
  Lunyu.pre=new Array();
  Lunyu.timer=setTimeout("clearTimeout(Lunyu.timer);Lunyu.checkview()",200);
  Lunyu.volchange();
}
Lunyu.checkview=function(){
  var t,r,b,i,x;
  Lunyu.timer=setTimeout("clearTimeout(Lunyu.timer);Lunyu.checkview()",200);
  if(Lunyu.curr==null)
    return;
  if(Lunyu.svg[Lunyu.curr]==null||Lunyu.pre[Lunyu.curr]==null){
    Lunyu.buildup(Lunyu.curr);
    Lunyu.lastTop=null;
    return;
  }
  if(Lunyu.svg[Lunyu.curr].style.position=="absolute"){
    Lunyu.svg[Lunyu.curr].style.position="static";
    Lunyu.svg[Lunyu.curr].style.visibility="visible";
  }
  conllusvg.rewrite("pre"+Lunyu.curr);
  r=Lunyu.svg[Lunyu.curr].getBoundingClientRect();
  t=r.top;
  b=r.bottom;
  r=document.getElementById("top").getBoundingClientRect().bottom;
  x=Lunyu.curr;
  if(t-r>20){
    while(t-r>0){
      i=Lunyu.svg[Lunyu.curr-1];
      if(i==null)
	break;
      if(i.style.position=="absolute")
	break;
      Lunyu.curr--;
      if(Lunyu.curr==0)
	break;
      t=i.getBoundingClientRect().top;
    }
  }
  else if(b<r){
    while(b<r){
      i=Lunyu.svg[Lunyu.curr+1];
      if(i==null)
	break;
      if(i.style.position=="absolute")
        break;
      Lunyu.curr++;
      if(Lunyu.curr==Lunyu.currmax)
	break;
      b=i.getBoundingClientRect().bottom;
    }
  }
  Lunyu.lastTop=Lunyu.svg[Lunyu.curr].getBoundingClientRect().top;
  if(Lunyu.curr!=x)
    document.getElementById("count").value=Lunyu.curr;
  for(i=Lunyu.curr+1;i<=Lunyu.currmax;i++){
    if(Lunyu.svg[i]==null||Lunyu.pre[i]==null){
      Lunyu.buildup(i);
      break;
    }
    if(Lunyu.svg[i].style.position=="absolute"){
      Lunyu.svg[i].style.position="static";
      Lunyu.svg[i].style.visibility="visible";
    }
    conllusvg.rewrite("pre"+i);
    r=Lunyu.svg[i].getBoundingClientRect();
    if(r.top-window.innerHeight-1000>0)
      break;
  }
  for(i=Lunyu.curr-1;i>=0;i--){
    if(Lunyu.svg[i]==null||Lunyu.pre[i]==null){
      Lunyu.buildup(i);
      break;
    }
    if(Lunyu.svg[i].style.position=="absolute"){
      if(Lunyu.pre[i].textContent>""){
	Lunyu.svg[i].style.position="static";
	Lunyu.svg[i].style.visibility="visible";
      }
      else
	break;
    }
    conllusvg.rewrite("pre"+i);
    r=Lunyu.svg[Lunyu.curr].getBoundingClientRect().top;
    if(Math.abs(r-Lunyu.lastTop)>5){
      if(document.scrollingElement==null)
        document.documentElement.scrollTop+=r-Lunyu.lastTop;
      else
        document.scrollingElement.scrollTop+=r-Lunyu.lastTop;
      Lunyu.lastTop=r;
    }
    r=Lunyu.svg[i].getBoundingClientRect();
    if(parseInt(r.bottom,10)<-300)
      break;
  }
}
Lunyu.buildup=function(x){
  var t,i;
  if(Lunyu.svg[x]==null){
    t=document.createElementNS("http://www.w3.org/2000/svg","svg");
    for(i=parseInt(x,10)+1;i<=Lunyu.currmax;i++){
      if(Lunyu.svg[i]!=null)
	break;
    }
    t.style.position="absolute";
    t.style.visibility="hidden";
    if(i>Lunyu.currmax)
      document.body.appendChild(t);
    else
      document.body.insertBefore(t,Lunyu.svg[i]);
    Lunyu.svg[x]=t;
    t.addEventListener("mousedown",Lunyu.onmousedown);
    t.addEventListener("touchstart",Lunyu.onmousedown);
  }
  if(Lunyu.pre[x]==null){
    t=document.createElement("pre");
    t.style.display="none";
    t.setAttribute("id","pre"+x);
    document.body.appendChild(t);
    Lunyu.pre[x]=t;
    if(conllusvg.main["pre"+x]==null)
      conllusvg.view(Lunyu.svg[x],"pre"+x);
    i=Lunyu.volpath+"/"+("000"+x).slice(-3)+".txt";
    conllusvg.loadRemote("pre"+x,Lunyu.url+"files/"+encodeURIComponent(i)+"/raw?ref=master");
  }
}
Lunyu.volchange=function(){
  var s,x;
  Lunyu.clearSVG();
  for(s in Lunyu.pre){
    if(Lunyu.pre[s]!=null){
      document.body.removeChild(Lunyu.pre[s]);
    }
    Lunyu.pre[s]=null;
  }
  s=document.getElementById("vol");
  Lunyu.volpath=s.options[s.selectedIndex].value;
  if(Lunyu.tail==null)
    Lunyu.tail=new Array();
  if(Lunyu.tail[Lunyu.volpath]==null)
    Lunyu.gettree();
  else
    Lunyu.writeSVG();
}
Lunyu.gettree=function(){
  var p;
  if(Lunyu.request==null){
    Lunyu.request=new XMLHttpRequest();
    Lunyu.request.onload=Lunyu.ongettree;
  }
  if(Lunyu.tail[Lunyu.volpath]==null)
    p=1;
  else
    p=Math.floor(parseInt(Lunyu.tail[Lunyu.volpath],10)/100)+2;
  Lunyu.request.open("GET",Lunyu.url+"tree?per_page=100&path="+Lunyu.volpath+"&page="+p);
  Lunyu.request.send();
}
Lunyu.ongettree=function(e){
  var i,s;
  if(Lunyu.request.status==200){
    s=Lunyu.request.responseText;
    i=s.lastIndexOf('.txt"');
    if(i<0)
      Lunyu.writeSVG();
    else{
      Lunyu.tail[Lunyu.volpath]=s.substring(i-3,i);
      Lunyu.gettree();
    }
  }
}
Lunyu.writeSVG=function(){
  var c=document.getElementById("count");
  Lunyu.clearSVG();
  Lunyu.currmax=parseInt(Lunyu.tail[Lunyu.volpath],10);
  Lunyu.curr=parseInt(c.value,10);
  if(Lunyu.curr<0)
    Lunyu.curr=0;
  if(Lunyu.curr>Lunyu.currmax)
    Lunyu.curr=Lunyu.currmax;
  c.value=Lunyu.curr;
  Lunyu.lastTop=null;
}
Lunyu.clearSVG=function(){
  var x;
  Lunyu.curr=Lunyu.lastTop=null;
  if(Lunyu.svg!=null){
    for(x in Lunyu.svg){
      if(Lunyu.svg[x]!=null){
	Lunyu.svg[x].style.position="absolute";
	Lunyu.svg[x].style.visibility="hidden";
      }
    }
  }
  if(document.scrollingElement==null)
    document.documentElement.scrollTop=0;
  else
    document.scrollingElement.scrollTop=0;
}
Lunyu.onmousedown=function(e){
  var x,y;
  if(Lunyu.lastX==null){
    Lunyu.lastX=e.clientX;
    Lunyu.lastY=e.clientY;
    return;
  }
  x=e.clientX-Lunyu.lastX;
  y=e.clientY-Lunyu.lastY;
  if(x*x+y*y>9){
    Lunyu.lastX=e.clientX;
    Lunyu.lastY=e.clientY;
    return;
  }
  Lunyu.lastX=null;
  x=e.currentTarget;
  for(y in Lunyu.svg){
    if(Lunyu.svg[y]==x){
      x=Lunyu.volpath+"/"+("000"+y).slice(-3)+".txt";
      window.open("editor-kanbun.html#"+Lunyu.url+"files/"+encodeURIComponent(x)+"/raw?ref=master");
      break;
    }
  }
}
Lunyu.reload=function(){
  var i,r;
  for(i=Lunyu.curr;i<=Lunyu.currmax;i++){
    if(Lunyu.svg[i]==null||Lunyu.pre[i]==null)
      return;
    r=Lunyu.svg[i].getBoundingClientRect();
    if(r.top>window.innerHeight||r.bottom<0)
      return;
    r=Lunyu.volpath+"/"+("000"+i).slice(-3)+".txt";
    conllusvg.loadRemote("pre"+i,Lunyu.url+"files/"+encodeURIComponent(r)+"/raw?ref=master");
  }
}
Lunyu.gitlab=function(){
  var r=Lunyu.volpath+"/"+("000"+Lunyu.curr).slice(-3)+".txt";
  window.open("/gitlab/Kanbun/ud-kanbun/blob/master/"+r);
}
</script>
<body onload="Lunyu.init()">
<div id="top" style="position:fixed; top:0px; left:0px; padding:10px; width:100%; background:white">
<font size="+3"><b>論語</b></font>
<select id="vol" onchange="Lunyu.volchange()">
<option value="kanripo/kR1h0004/001">學而篇第一</option>
<option value="kanripo/kR1h0004/002">為政篇第二</option>
<option value="kanripo/kR1h0004/003">八佾篇第三</option>
<option value="kanripo/kR1h0004/004">里仁篇第四</option>
<option value="kanripo/kR1h0004/005">公冶長篇第五</option>
<option value="kanripo/kR1h0004/006">雍也篇第六</option>
<option value="kanripo/kR1h0004/007">述而篇第七</option>
<option value="kanripo/kR1h0004/008">泰伯篇第八</option>
<option value="kanripo/kR1h0004/009">子罕篇第九</option>
<option value="kanripo/kR1h0004/010">鄉黨篇第十</option>
<option value="kanripo/kR1h0004/011">先進篇第十一</option>
<option value="kanripo/kR1h0004/012">顏淵篇第十二</option>
<option value="kanripo/kR1h0004/013">子路篇第十三</option>
<option value="kanripo/kR1h0004/014">憲問篇第十四</option>
<option value="kanripo/kR1h0004/015">衛靈公篇第十五</option>
<option value="kanripo/kR1h0004/016">季氏篇第十六</option>
<option value="kanripo/kR1h0004/017">陽貨篇第十七</option>
<option value="kanripo/kR1h0004/018">微子篇第十八</option>
<option value="kanripo/kR1h0004/019">子張篇第十九</option>
<option value="kanripo/kR1h0004/020">堯曰篇第二十</option>
</select>
<input type="text" id="count" size=3 value=0 style="text-align:right" onchange="Lunyu.writeSVG()" />
<input type="button" value="GitLab" onclick="Lunyu.gitlab()" />
<input type="button" value="Reload" onclick="Lunyu.reload()" />
</div>
</body>