Commit e5d9a633 authored by Koichi Yasuoka's avatar Koichi Yasuoka
Browse files

日本樂府

parent f07f1236
Loading
Loading
Loading
Loading
+304 −0
Original line number Diff line number Diff line
<!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 Nihongafu=new Object();
Nihongafu.url="/gitlab/api/v4/projects/Kanbun%2Fud-ja-kanbun/repository/";
Nihongafu.init=function(){
  var t=document.getElementById("top").cloneNode(true);
  t.style.position="static";
  t.style.visibility="hidden";
  document.body.appendChild(t);
  Nihongafu.checkURL();
  Nihongafu.svg=new Array();
  Nihongafu.pre=new Array();
  Nihongafu.timer=setTimeout("clearTimeout(Nihongafu.timer);Nihongafu.checkview()",200);
  Nihongafu.volchange();
}
Nihongafu.checkURL=function(){
  var h=location.href;
  var i=h.indexOf("#");
  var s;
  if(0>i)
    return;
  if(1>h.length-i)
    return;
  h=decodeURIComponent(h.slice(i+1));
  i=h.lastIndexOf("/");
  if(1<i){
    document.getElementById("count").value=h.slice(i+1);
    h=h.substring(0,i);
  }
  s=document.getElementById("vol");
  for(i=0;i<s.length;i++){
    if(s.options[i].value.indexOf(h)>=0){
      s.selectedIndex=i;
      return;
    }
  }
}
Nihongafu.checkview=function(){
  var t,r,b,i,x;
  Nihongafu.timer=setTimeout("clearTimeout(Nihongafu.timer);Nihongafu.checkview()",200);
  if(Nihongafu.curr==null)
    return;
  if(Nihongafu.svg[Nihongafu.curr]==null||Nihongafu.pre[Nihongafu.curr]==null){
    Nihongafu.buildup(Nihongafu.curr);
    Nihongafu.lastTop=null;
    return;
  }
  if(Nihongafu.svg[Nihongafu.curr].style.position=="absolute"){
    Nihongafu.svg[Nihongafu.curr].style.position="static";
    Nihongafu.svg[Nihongafu.curr].style.visibility="visible";
  }
  conllusvg.rewrite("pre"+Nihongafu.curr);
  r=Nihongafu.svg[Nihongafu.curr].getBoundingClientRect();
  t=r.top;
  b=r.bottom;
  r=document.getElementById("top").getBoundingClientRect().bottom;
  x=Nihongafu.curr;
  if(t-r>20){
    while(t-r>0){
      i=Nihongafu.svg[Nihongafu.curr-1];
      if(i==null)
	break;
      if(i.style.position=="absolute")
	break;
      Nihongafu.curr--;
      if(Nihongafu.curr==0)
	break;
      t=i.getBoundingClientRect().top;
    }
  }
  else if(b<r){
    while(b<r){
      i=Nihongafu.svg[Nihongafu.curr+1];
      if(i==null)
	break;
      if(i.style.position=="absolute")
        break;
      Nihongafu.curr++;
      if(Nihongafu.curr==Nihongafu.currmax)
	break;
      b=i.getBoundingClientRect().bottom;
    }
  }
  if(Nihongafu.curr!=x){
    if(Nihongafu.retrytimes>0){
      Nihongafu.retrytimes--;
      Nihongafu.curr=parseInt(document.getElementById("count").value,10);
      t=Nihongafu.svg[Nihongafu.curr].getBoundingClientRect().top;
      if(Math.abs(t-r)>5){
        if(document.scrollingElement==null)
          document.documentElement.scrollTop+=t-r;
        else
          document.scrollingElement.scrollTop+=t-r;
      }
    }
    else
      document.getElementById("count").value=Nihongafu.curr;
  }
  else if(Nihongafu.retrytimes>0)
    Nihongafu.retrytimes--;
  Nihongafu.lastTop=Nihongafu.svg[Nihongafu.curr].getBoundingClientRect().top;
  for(i=Nihongafu.curr+1;i<=Nihongafu.currmax;i++){
    if(Nihongafu.svg[i]==null||Nihongafu.pre[i]==null){
      Nihongafu.buildup(i);
      break;
    }
    if(Nihongafu.svg[i].style.position=="absolute"){
      Nihongafu.svg[i].style.position="static";
      Nihongafu.svg[i].style.visibility="visible";
    }
    conllusvg.rewrite("pre"+i);
    r=Nihongafu.svg[i].getBoundingClientRect();
    if(r.top-window.innerHeight-1000>0)
      break;
  }
  for(i=Nihongafu.curr-1;i>=0;i--){
    if(Nihongafu.svg[i]==null||Nihongafu.pre[i]==null){
      Nihongafu.buildup(i);
      break;
    }
    if(Nihongafu.svg[i].style.position=="absolute"){
      if(Nihongafu.pre[i].textContent>""){
	Nihongafu.svg[i].style.position="static";
	Nihongafu.svg[i].style.visibility="visible";
      }
      else
	break;
    }
    conllusvg.rewrite("pre"+i);
    r=Nihongafu.svg[Nihongafu.curr].getBoundingClientRect().top;
    if(Math.abs(r-Nihongafu.lastTop)>5){
      if(document.scrollingElement==null)
        document.documentElement.scrollTop+=r-Nihongafu.lastTop;
      else
        document.scrollingElement.scrollTop+=r-Nihongafu.lastTop;
      Nihongafu.lastTop=r;
    }
    r=Nihongafu.svg[i].getBoundingClientRect();
    if(parseInt(r.bottom,10)<-300)
      break;
  }
}
Nihongafu.buildup=function(x){
  var t,i;
  if(Nihongafu.svg[x]==null){
    t=document.createElementNS("http://www.w3.org/2000/svg","svg");
    for(i=parseInt(x,10)+1;i<=Nihongafu.currmax;i++){
      if(Nihongafu.svg[i]!=null)
	break;
    }
    t.style.position="absolute";
    t.style.visibility="hidden";
    if(i>Nihongafu.currmax)
      document.body.appendChild(t);
    else
      document.body.insertBefore(t,Nihongafu.svg[i]);
    Nihongafu.svg[x]=t;
    t.addEventListener("mousedown",Nihongafu.onmousedown);
    t.addEventListener("touchstart",Nihongafu.onmousedown);
  }
  if(Nihongafu.pre[x]==null){
    t=document.createElement("pre");
    t.style.display="none";
    t.setAttribute("id","pre"+x);
    document.body.appendChild(t);
    Nihongafu.pre[x]=t;
    if(conllusvg.main["pre"+x]==null)
      conllusvg.view(Nihongafu.svg[x],"pre"+x);
    i=Nihongafu.volpath+"/"+("000"+x).slice(-3)+".txt";
    conllusvg.loadRemote("pre"+x,Nihongafu.url+"files/"+encodeURIComponent(i)+"/raw?ref=master");
  }
}
Nihongafu.volchange=function(){
  var s,x;
  Nihongafu.clearSVG();
  for(s in Nihongafu.pre){
    if(Nihongafu.pre[s]!=null){
      document.body.removeChild(Nihongafu.pre[s]);
    }
    Nihongafu.pre[s]=null;
  }
  s=document.getElementById("vol");
  Nihongafu.volpath=s.options[s.selectedIndex].value;
  if(Nihongafu.tail==null)
    Nihongafu.tail=new Array();
  if(Nihongafu.tail[Nihongafu.volpath]==null)
    Nihongafu.gettree();
  else
    Nihongafu.writeSVG();
}
Nihongafu.gettree=function(){
  var p;
  if(Nihongafu.request==null){
    Nihongafu.request=new XMLHttpRequest();
    Nihongafu.request.onload=Nihongafu.ongettree;
  }
  if(Nihongafu.tail[Nihongafu.volpath]==null)
    p=1;
  else
    p=Math.floor(parseInt(Nihongafu.tail[Nihongafu.volpath],10)/100)+2;
  Nihongafu.request.open("GET",Nihongafu.url+"tree?per_page=100&path="+Nihongafu.volpath+"&page="+p);
  Nihongafu.request.send();
}
Nihongafu.ongettree=function(e){
  var i,s;
  if(Nihongafu.request.status==200){
    s=Nihongafu.request.responseText;
    i=s.lastIndexOf('.txt"');
    if(i<0)
      Nihongafu.writeSVG();
    else{
      Nihongafu.tail[Nihongafu.volpath]=s.substring(i-3,i);
      Nihongafu.gettree();
    }
  }
}
Nihongafu.goto=function(){
  Nihongafu.retrytimes=3;
  Nihongafu.writeSVG();
}
Nihongafu.writeSVG=function(){
  var c=document.getElementById("count");
  Nihongafu.clearSVG();
  Nihongafu.currmax=parseInt(Nihongafu.tail[Nihongafu.volpath],10);
  Nihongafu.curr=parseInt(c.value,10);
  if(Nihongafu.curr<0)
    Nihongafu.curr=0;
  if(Nihongafu.curr>Nihongafu.currmax)
    Nihongafu.curr=Nihongafu.currmax;
  c.value=Nihongafu.curr;
  Nihongafu.lastTop=null;
}
Nihongafu.clearSVG=function(){
  var x;
  Nihongafu.curr=Nihongafu.lastTop=null;
  if(Nihongafu.svg!=null){
    for(x in Nihongafu.svg){
      if(Nihongafu.svg[x]!=null){
	Nihongafu.svg[x].style.position="absolute";
	Nihongafu.svg[x].style.visibility="hidden";
      }
    }
  }
  if(document.scrollingElement==null)
    document.documentElement.scrollTop=0;
  else
    document.scrollingElement.scrollTop=0;
}
Nihongafu.onmousedown=function(e){
  var x,y;
  if(Nihongafu.lastX==null){
    Nihongafu.lastX=e.clientX;
    Nihongafu.lastY=e.clientY;
    return;
  }
  x=e.clientX-Nihongafu.lastX;
  y=e.clientY-Nihongafu.lastY;
  if(x*x+y*y>9){
    Nihongafu.lastX=e.clientX;
    Nihongafu.lastY=e.clientY;
    return;
  }
  Nihongafu.lastX=null;
  x=e.currentTarget;
  for(y in Nihongafu.svg){
    if(Nihongafu.svg[y]==x){
      x=Nihongafu.volpath+"/"+("000"+y).slice(-3)+".txt";
      window.open("editor-kanbun.html#"+Nihongafu.url+"files/"+encodeURIComponent(x)+"/raw?ref=master");
      break;
    }
  }
}
Nihongafu.reload=function(){
  var i,r;
  for(i=Nihongafu.curr;i<=Nihongafu.currmax;i++){
    if(Nihongafu.svg[i]==null||Nihongafu.pre[i]==null)
      return;
    r=Nihongafu.svg[i].getBoundingClientRect();
    if(r.top>window.innerHeight||r.bottom<0)
      return;
    r=Nihongafu.volpath+"/"+("000"+i).slice(-3)+".txt";
    conllusvg.loadRemote("pre"+i,Nihongafu.url+"files/"+encodeURIComponent(r)+"/raw?ref=master");
  }
}
Nihongafu.gitlab=function(){
  var r=Nihongafu.volpath+"/"+("000"+Nihongafu.curr).slice(-3)+".txt";
  window.open("/gitlab/Kanbun/ud-ja-kanbun/blob/master/"+r);
}
</script>
<body onload="Nihongafu.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="Nihongafu.volchange()">
<option value="iwanami/N066F/001" selected>001 日出處</option>
</select>
<input type="text" id="count" size=3 value=0 style="text-align:right" onchange="Nihongafu.goto()" />
<input type="button" value="GitLab" onclick="Nihongafu.gitlab()" />
<input type="button" value="Reload" onclick="Nihongafu.reload()" />
</div>
</body>