Mercurial > hg > Members > nobuyasu > Consensus
view public/viewer/treeArrangement.js @ 107:42799d9c3753 draft
create parseJSONDate function
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 17 Mar 2013 19:53:59 +0900 |
parents | a76259e2ca9f |
children | 409e8fd79794 |
line wrap: on
line source
/**/(function(){/**/ //agreeType:agreeType //relation:relationType //position:displayPosition //newPosition:animationPositon //info:claimInfo //id:claimID //mentions:claimArray //view:viewObject if(!window.JSON)window.JSON={parse:function(json){return eval("("+json+")");}} var jsondata; window.LoadJSON=LoadJSON; function LoadJSON(url){ var http=new XMLHttpRequest(); http.open("GET",url,false); http.send(); return JSON.parse(jsondata=http.responseText); } window.LoadNewJSON=LoadNewJSON; function LoadNewJSON(params){ if(!params)params=[[],[0]] var json=JSON.parse(jsondata) for(var i=0;i<params.length;i++){ var param=params[i]; var arr=json.mentions; for(var j=0;j<param.length;j++){ arr=arr[param[j]].claim.mentions; } arr.push({type:"suggestion",id:10+i,claim:JSON.parse(jsondata)}); } return json; } window.getUserListFromJSONTree=getUserListFromJSONTree; function getUserListFromJSONTree(root){ var list=[],map={}; function add(u){ if(map['#'+u])return; map['#'+u]=true; list.push(u); } function collect(node){ add(node.info.author); var users=node.info.users; for(var i=0;i<users.length;i++)add(users[i].id); for(var i=0;i<node.mentions.length;i++){ collect(node.mentions[i]); } } collect(root); return list.sort(); } window.createJSONTreeInfo=createJSONTreeInfo; function createJSONTreeInfo(jsonobj){ var root=createTree(jsonobj); var layout=calcTreePosition(root,1.5); return {layout:layout,root:root}; } function createTree(node){ var obj={mentions:[]}; if(node.claim){ obj.id=node.id; obj.relation=node.type; node=node.claim; } obj.agreeType=node.type; obj.info=node; if(node.id)obj.id=node.id; for(var i=0;i<node.mentions.length;i++){ obj.mentions[i]=createTree(node.mentions[i]); } obj.mentions.sort(function(a,b){return a.id-b.id}) delete node.mentions; return obj; } window.updateJSONTreeDiff=updateJSONTreeDiff; function updateJSONTreeDiff(node,newnode,newflag){ if(newflag==1){ newnode.newPosition=newnode.position; newnode.position={x:node.position.x,y:node.position.y}; for(var i=0;i<newnode.mentions.length;i++){ updateJSONTreeDiff(node,newnode.mentions[i],1); } }else if(newflag==-1){ node.newPosition={x:newnode.newPosition.x,y:newnode.newPosition.y,remove:true}; for(var i=0;i<node.mentions.length;i++){ updateJSONTreeDiff(node.mentions[i],newnode,-1); } }else{ node.info=newnode.info; node.relation=newnode.relation; node.newPosition=newnode.position; var oldmentionmap={}; for(var i=0;i<node.mentions.length;i++){ var m=node.mentions[i]; oldmentionmap[m.id]=m; } for(var i=0;i<newnode.mentions.length;i++){ var nm=newnode.mentions[i]; var om=oldmentionmap[nm.id]; delete oldmentionmap[nm.id]; if(om)updateJSONTreeDiff(om,nm); else{ node.mentions.push(nm); updateJSONTreeDiff(node,nm,1); } } for(var i in oldmentionmap){ updateJSONTreeDiff(oldmentionmap[i],node,-1); } } } function calcTreePosition(root,interval){ if(!(interval>1))interval=1; function shiftPosition(node,x){ node.position.x+=x; node.position.y++; var childs=node.mentions; for(var i=0;i<childs.length;i++)shiftPosition(childs[i],x); } function recTree(node){ node.position={x:0,y:0}; var childs=node.mentions; if(childs.length==0)return [{l:0,r:0}]; var lrarr=[]; for(var i=0;i<childs.length;i++){ lrarr.push(recTree(childs[i])); } if(true){ var wmax=0; for(var i=0;i<lrarr.length;i++){ for(var j=i+1;j<lrarr.length;j++){ var max=0; for(var depth=0;lrarr[i][depth]&&lrarr[j][depth];depth++){ var dist=lrarr[i][depth].r-lrarr[j][depth].l+(depth?interval:1); if(dist>max)max=dist; } var w=max/(j-i); if(wmax<w)wmax=w; } } var LR=[{l:0,r:0}]; for(var i=0;i<childs.length;i++){ var dx=wmax*(i-(childs.length-1)/2); shiftPosition(childs[i],dx); var lr=lrarr[i]; for(var d=0;d<lr.length;d++){ var l=lr[d].l+dx,r=lr[d].r+dx; if(!LR[d+1])LR[d+1]={l:l,r:r}; else if(l<LR[d+1].l)LR[d+1].l=l; else if(LR[d+1].l<r)LR[d+1].r=r; } } return LR; }else{ var LR=lrarr[0]; var pos=[0]; for(var i=1;i<lrarr.length;i++){ var lr=lrarr[i]; var maxDist=0; console.log(lr.length) for(var depth=0;depth<lr.length;depth++){ if(!LR[depth])break; var dist=LR[depth].r-lr[depth].l+(depth?interval:1); if(dist>maxDist)maxDist=dist; } pos[i]=maxDist; for(var depth=0;depth<lr.length;depth++){ if(LR[depth])LR[depth].r=lr[depth].r+maxDist; else LR[depth]={l:lr[depth].l+maxDist,r:lr[depth].r+maxDist}; } } var center=(LR[0].l+LR[0].r)/2; for(var i=0;i<childs.length;i++){ console.log(pos[i],center); shiftPosition(childs[i],pos[i]-center); } for(var i=0;i<LR.length;i++){ LR[i].l-=center; LR[i].r-=center; } LR.unshift({l:0,r:0}); return LR; } } var LR=recTree(root); var left=0,right=0; for(var i=0;i<LR.length;i++){ if(LR[i].l<left)left=LR[i].l; if(LR[i].r>right)right=LR[i].r; } return {LR:LR,depth:LR.length,left:left,right:right}; } function checkTreeArangement(){ var canvas=document.createElement("canvas"); obj=LoadJSON("../mocksenario/consensus/browse/1"); var rndparam=[[2,3],[2,3],[2,3]]; for(var i=0;i<100;i++)rndparam.push([0,3]); obj=createRandom(rndparam); result=calcTreePosition(obj,2); var size=(result.right-result.left) canvas.width=size*20+40;canvas.height=result.depth*50+50; g=canvas.getContext('2d'); document.body.appendChild(canvas); canvas.style.border="1px dotted red"; function recDraw(node){ g.beginPath(); var text=(node.toulmin||(node.claim&&node.claim.toulmin)||{title:"none"}).title; var x=20-20*result.left+20*node.position.x,y=50*(1+node.position.y); g.fillText(text,x-g.measureText(text).width/2,y); var childs=node.mentions||node.claim.mentions; for(var i=0,o;o=childs[i];i++){ var x2=20-20*result.left+20*o.position.x,y2=50*(1+o.position.y); g.beginPath();g.moveTo(x,y);g.lineTo(x2,y2);g.stroke(); recDraw(o); } } recDraw(obj); } function rndStr(){ var s="",n=3+3*Math.random(); var c="abcdefghijklmnopqrstuvwxyz"; for(var i=0;i<n;i++)s+=c.charAt(c.length*Math.random()) return s; } window.createRandom=createRandom; function createRandom(childInfo,depth){ if(!depth)depth=0; var info=childInfo[depth]; var node={claim:{toulmin:{title:rndStr()},mentions:[]}}; if(!info)return node; var n=info[0]+Math.floor((info[1]-info[0])*Math.random()); for(var i=0;i<n;i++){ node.claim.mentions.push(createRandom(childInfo,depth+1)); } return node; } /**/})();/**/