Mercurial > hg > Members > nobuyasu > Consensus
changeset 110:409e8fd79794 draft
add showAllEdge function
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 18 Mar 2013 01:15:27 +0900 |
parents | 2633ac31c233 |
children | 5ad0ba30c785 |
files | public/viewer/treeArrangement.js public/viewer/viewer_white.html |
diffstat | 2 files changed, 321 insertions(+), 212 deletions(-) [+] |
line wrap: on
line diff
--- a/public/viewer/treeArrangement.js Sun Mar 17 22:39:58 2013 +0900 +++ b/public/viewer/treeArrangement.js Mon Mar 18 01:15:27 2013 +0900 @@ -1,4 +1,4 @@ -/**/(function(){/**/ +(function(){/**/ //agreeType:agreeType //relation:relationType //position:displayPosition @@ -12,241 +12,241 @@ 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); + 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)}); + 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; } - return json; + 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); + 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]); } - 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(); + } + 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}; + 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; + 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); - } + 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); + 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])); } - 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; } - - - 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; } - 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}; } - 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 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}; + } + 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); - } + 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); + } + 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; + 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; + 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; } /**/})();/**/ \ No newline at end of file
--- a/public/viewer/viewer_white.html Sun Mar 17 22:39:58 2013 +0900 +++ b/public/viewer/viewer_white.html Mon Mar 18 01:15:27 2013 +0900 @@ -242,7 +242,7 @@ function createNodeDiv(node) { node.view = $("<div class='graphNode'/>"); - node.position = calcNewNodePosition(); + if(!node.position) node.position = calcNewNodePosition(); node.view.hover(); var div = $("<div class='graphNodeContent'/>")[0]; div.innerHTML = node.property.title; @@ -301,7 +301,7 @@ return obj; } -var stateMachineConnector = { +var arrowConnector = { connector:"StateMachine", // paintStyle:{lineWidth:3,strokeStyle:"#056"}, paintStyle:{lineWidth:2,strokeStyle:"#aaf"}, @@ -311,6 +311,13 @@ overlays:[ ["PlainArrow", {location:1, width:20, length:12} ]] }; +var arrowBothConnector = { + connector:"StateMachine", + paintStyle:{lineWidth:2,strokeStyle:"#aaf"}, + hoverPaintStyle:{strokeStyle:"#dbe300"}, + endpoint:"Blank", +}; + function paintEdge() { var array = edgeArray; for (var i=0; i<array.length; i++) { @@ -327,7 +334,7 @@ var fromNode = graphObj.from; var toNode = graphObj.to; var rel = graphObj.rel; - var c = jsPlumb.connect({source:fromNode.view[0], target:toNode.view[0]}, stateMachineConnector); + var c = jsPlumb.connect({source:fromNode.view[0], target:toNode.view[0]}, arrowConnector); c.setLabel({label:"<span style='color:black;'>"+rel+"</span>",cssStyle:"color:black"}); return c; } @@ -386,32 +393,79 @@ } -function createNodeFromEdge(graphData, edge) { +function showEdge(graphData, edge) { + function connectEdge(fromNode, toNode, rel, type) { + var c; + if (type == 'both') { + /* TODO */ + c = jsPlumb.connect({source:fromNode.view[0], target:toNode.view[0]}, arrowBothConnector); + } else { + c = jsPlumb.connect({source:fromNode.view[0], target:toNode.view[0]}, arrowConnector); + } + c.setLabel({label:"<span style='color:black;'>"+rel+"</span>",cssStyle:"color:black"}); + return c; + } + if (edge.visible) return; var fromNode = graphData.nodes[edge.property.from]; - if (!fromNode.view) updateNode(fromNode); var toNode = graphData.nodes[edge.property.to]; - if (!toNode.view) updateNode(toNode); var rel = edge.property.rel; var type = edge.property.type; - var c; - if (type == 'both') { - /* TODO */ - c = jsPlumb.connect({source:fromNode.view[0], target:toNode.view[0]}, stateMachineConnector); - } else { - c = jsPlumb.connect({source:fromNode.view[0], target:toNode.view[0]}, stateMachineConnector); + connectEdge(fromNode, toNode, rel, type); + edge.visible = true; +} + +function createNodeFromEdge(graphData, edge, baseNode) { + if(edge.visible) return; + var fromNode = graphData.nodes[edge.property.from]; + if (!fromNode.view) { + updateNode(fromNode); + } + var toNode = graphData.nodes[edge.property.to]; + if (!toNode.view) { + updateNode(toNode); } - c.setLabel({label:"<span style='color:black;'>"+rel+"</span>",cssStyle:"color:black"}); + showEdge(graphData, edge); + +} + +function getRelationEdgeIds(node) { + var edgeIds = []; + for (var i=0; i<node.in.length; i++) edgeIds.push(node.in[i]); + for (var i=0; i<node.out.length; i++) edgeIds.push(node.out[i]); + for (var i=0; i<node.both.length; i++) edgeIds.push(node.both[i]); + return edgeIds; +} +function getRelationNodeIds(graphData, node) { + var nodes = graphData.nodes; + var edges = graphData.edges; + var nodeIds = []; + for (var i=0; i<node.in.length; i++) { + var edgeId = node.in[i]; + nodeIds.push(edges[edgeId].property.from); + } + for (var i=0; i<node.out.length; i++) { + var edgeId = node.out[i]; + nodeIds.push(edges[edgeId].property.to); + } + for (var i=0; i<node.both.length; i++) { + var edgeId = node.both[i]; + var fromId = edges[edgeId].property.from; + var toId = edges[edgeId].property.to; + if (fromId == node.property.id) { + nodeIds.push(toId); + } else { + nodeIds.push(fromId); + } + } + return nodeIds; } function updateRelationNodes(graphData, node) { var id = node.property.id; var nodes = graphData.nodes; var edges = graphData.edges; - var edgeIds = []; - for (var i=0; i<node.in.length; i++) edgeIds.push(node.in[i]); - for (var i=0; i<node.out.length; i++) edgeIds.push(node.out[i]); - for (var i=0; i<node.both.length; i++) edgeIds.push(node.both[i]); + var edgeIds = getRelationEdgeIds(node); for (var i=0; i<edgeIds.length; i++ ) { createNodeFromEdge(graphData, edges[edgeIds[i]]); } @@ -419,14 +473,56 @@ jsPlumb.draggable($('.graphNode')); } +var BASE_THETA = -30; +var BASE_ANGLE = 180; +var BASE_DISTANCE = 150; +function calcChildNodePosition(parent, num) { + var pX = parent.position.x; + var pY = parent.position.y; + var deg = BASE_ANGLE - num * BASE_THETA; + var theta = deg * (Math.PI / 180); + var cX = pX - (BASE_DISTANCE * Math.cos(theta)); + var cY = pY - (BASE_DISTANCE * Math.sin(theta)); + return {x:cX, y:cY}; +} + +function showAllEdge(graphData) { + var edges = graphData.edges; + for (var id in edges) { + showEdge(graphData, edges[id]); + } + jsPlumb.removeAllEndpoints(); + jsPlumb.draggable($('.graphNode')); +} + +function recursiveShowRelationNodes(graphData, node) { + if (!node.view) updateNode(node); + childNodeIds = getRelationNodeIds(graphData, node); + for (var i=0; i<childNodeIds.length; i++) { + var id = childNodeIds[i]; + var childNode = graphData.nodes[id]; + if (!childNode.view) { + childNode.position = calcChildNodePosition(node, i); + updateNode(childNode); + recursiveShowRelationNodes(graphData, childNode); + } + } + showAllEdge(graphData); +} + function testStart() { init(); var graphData = new GraphData(); parseJSONGraphData(graphData, sampleJSONData); var rootNodeId = "1"; var rootNode = graphData.nodes[rootNodeId]; + recursiveShowRelationNodes(graphData, rootNode); + +/* updateNode(rootNode); updateRelationNodes(graphData,rootNode); + updateRelationNodes(graphData,graphData.nodes["2"]); +*/ } @@ -464,7 +560,13 @@ title:"task3", kind:"agree", createdUser:"tamayose" - } + }, + { + id:"4", + title:"task4", + kind:"agree", + createdUser:"oshiro" + } ], edges: [ { @@ -487,7 +589,14 @@ to:"1", rel:"dependency", type:"both" - } + }, + { + id:"14", + from:"4", + to:"2", + rel:"dependency", + type:"" + } ] };