65 lines
11 KiB
JavaScript
65 lines
11 KiB
JavaScript
// Small deterministic layouts over canonical IDs. No inferred people, parentage, meetings or knowledge.
|
|
(() => {
|
|
const W=180,H=66,GAP=42,STEP=145;
|
|
const clean=s=>(s||'').toLowerCase().replace(/[-_]/g,' ').replace(/\s+/g,' ').trim();
|
|
function role(link){const t=clean(link.type),reverse=['child','son','daughter','biological child','adopted child','adoptive child','step child','stepchild','grandchild','grandson','granddaughter','great grandchild'].includes(t),parent=['parent','mother','father','biological parent','adoptive parent','step parent','grandparent','grandmother','grandfather','great grandparent'].includes(t);
|
|
if(parent||reverse){if(link.reciprocal)return {kind:'other',ambiguous:true};return {kind:'parent',from:reverse?link.b:link.a,to:reverse?link.a:link.b,delta:t.startsWith('great ')?3:t.startsWith('grand')?2:1,style:(t.includes('adoptive')||t.includes('adopted'))?'adoptive':t.startsWith('step')?'step':t.includes('biological')?'biological':'parent'};}
|
|
if(['spouse','partner','ex spouse','former spouse','romantic partner'].includes(t))return {kind:'partner',style:'partner'};
|
|
if(['sibling','half sibling','sister','brother'].includes(t))return {kind:'sibling',style:t==='half sibling'?'half':'sibling'};
|
|
return {kind:'other',style:'other'};
|
|
}
|
|
function signature(nodes,links){return JSON.stringify([nodes.map(n=>[n.id,n.birthDate||'']).sort((a,b)=>a[0]-b[0]),links.map(l=>[l.id,l.a,l.b,l.type,l.reciprocal]).sort((a,b)=>a[0]-b[0])]);}
|
|
function family(nodes,links){const byId=new Map(nodes.map(n=>[n.id,n])),valid=links.filter(l=>byId.has(l.a)&&byId.has(l.b)),roles=new Map(valid.map(l=>[l.id,role(l)])),warnings=[];if(valid.length!==links.length)warnings.push('A connection has an unavailable identity; no replacement person was invented.');if([...roles.values()].some(r=>r.ambiguous))warnings.push('A reciprocal parent/child record has ambiguous direction. It is shown without inferred parentage; correct it in the editor.');
|
|
if([...roles.values()].some(r=>r.kind==='other'&&!r.ambiguous))warnings.push('Some recorded kinship has no explicit parentage; its connector does not establish missing ancestors or exact generations.');
|
|
const children=new Map(nodes.map(n=>[n.id,[]]));for(const l of valid){const r=roles.get(l.id);if(r.kind==='parent')children.get(r.from).push(r.to);}
|
|
const reachCache=new Map();function reaches(a,b){const key=a+':'+b;if(reachCache.has(key))return reachCache.get(key);const todo=[a],seen=new Set();let found=false;while(todo.length){const n=todo.pop();if(seen.has(n))continue;seen.add(n);for(const c of children.get(n)||[]){if(c===b){found=true;break;}todo.push(c);}if(found)break;}reachCache.set(key,found);return found;}
|
|
const uf=new Map(nodes.map(n=>[n.id,n.id]));function find(id){if(uf.get(id)!==id)uf.set(id,find(uf.get(id)));return uf.get(id);}function merge(a,b){const aa=nodes.filter(n=>find(n.id)===find(a)),bb=nodes.filter(n=>find(n.id)===find(b));if(aa.some(x=>bb.some(y=>reaches(x.id,y.id)||reaches(y.id,x.id))))return false;uf.set(find(b),find(a));return true;}
|
|
const immediate=new Map(nodes.map(n=>[n.id,[]]));for(const l of valid){const r=roles.get(l.id);if(r.kind==='parent'&&r.delta===1)immediate.get(r.from).push(r.to);}
|
|
for(const family of immediate.values()){for(let i=1;i<family.length;i++)merge(family[0],family[i]);}
|
|
for(const l of valid){const r=roles.get(l.id);if(r.kind==='partner'||r.kind==='sibling')merge(l.a,l.b);}
|
|
const groups=new Map();for(const n of nodes){const id=find(n.id);if(!groups.has(id))groups.set(id,{id,people:[],rank:0,indegree:0,parents:[],children:[]});groups.get(id).people.push(n);}
|
|
for(const l of valid){const r=roles.get(l.id);if(r.kind!=='parent')continue;const a=groups.get(find(r.from)),b=groups.get(find(r.to));if(a===b)continue;a.children.push({id:b.id,delta:r.delta});b.parents.push(a.id);b.indegree++;}
|
|
const todo=[...groups.values()].filter(g=>g.indegree===0).sort((a,b)=>a.id-b.id),done=new Set(),topology=[];while(todo.length){const a=todo.shift();done.add(a.id);topology.push(a);for(const e of a.children){const b=groups.get(e.id);b.rank=Math.max(b.rank,a.rank+e.delta);if(--b.indegree===0)todo.push(b);}}
|
|
if(done.size!==groups.size){warnings.push('An ancestry cycle prevents a generational interpretation for some records. Those connections remain visible without fabricated ancestors.');for(const g of groups.values())if(!done.has(g.id))g.rank=0;}
|
|
for(const g of [...topology].reverse()){if(!g.children.length)continue;const bounds=g.children.map(e=>groups.get(e.id).rank-e.delta),limit=Math.min(...bounds);if(limit>g.rank)g.rank=limit;}
|
|
const parentsOf=new Map(nodes.map(n=>[n.id,[]]));for(const l of valid){const r=roles.get(l.id);if(r.kind==='parent'&&r.delta===1)parentsOf.get(r.to).push(r.from);}
|
|
const positions={},rowWidths=new Map(),orderedGroups=[...groups.values()],rows=[...new Set(orderedGroups.map(g=>g.rank))].sort((a,b)=>a-b);
|
|
function birth(a,b){if(a.birthDate&&b.birthDate&&a.birthDate!==b.birthDate)return a.birthDate.localeCompare(b.birthDate);if(a.birthDate&&!b.birthDate)return -1;if(!a.birthDate&&b.birthDate)return 1;return (a.name||'').localeCompare(b.name||'')||a.id-b.id;}
|
|
for(const rank of rows){const row=orderedGroups.filter(g=>g.rank===rank);const score=g=>{const p=g.parents.map(id=>groups.get(id)).flatMap(p=>p.people).map(p=>positions[p.id]?.x).filter(Number.isFinite);return p.length?p.reduce((a,b)=>a+b,0)/p.length:Number.MAX_SAFE_INTEGER;};const key=g=>g.people.flatMap(n=>parentsOf.get(n.id)).sort((a,b)=>a-b).join(',');row.sort((a,b)=>score(a)-score(b)||key(a).localeCompare(key(b))||birth(a.people[0],b.people[0]));let x=0;
|
|
for(const g of row){g.people.sort(birth);const families=new Map();for(const n of g.people)for(const p of parentsOf.get(n.id)){if(!families.has(p))families.set(p,[]);families.get(p).push(n);}const siblings=[...families.entries()].filter(([_,children])=>children.length>1).sort((a,b)=>b[1].length-a[1].length||a[0]-b[0])[0]?.[1];
|
|
if(siblings){const cores=[...siblings].sort(birth),coreIDs=new Set(cores.map(n=>n.id)),used=new Set(),ordered=[];for(const core of cores){if(used.has(core.id))continue;const partners=g.people.filter(n=>!used.has(n.id)&&!coreIDs.has(n.id)&&valid.some(l=>roles.get(l.id).kind==='partner'&&((l.a===core.id&&l.b===n.id)||(l.b===core.id&&l.a===n.id))));const block=[...partners];block.splice(Math.floor(block.length/2),0,core);for(const n of block){ordered.push(n);used.add(n.id);}}g.people=[...ordered,...g.people.filter(n=>!used.has(n.id))];}
|
|
else if(g.people.length>2){const degrees=new Map(g.people.map(n=>[n.id,valid.filter(l=>roles.get(l.id).kind==='partner'&&(l.a===n.id||l.b===n.id)).length]));const central=[...g.people].sort((a,b)=>degrees.get(b.id)-degrees.get(a.id)||a.id-b.id)[0];if(degrees.get(central.id)>1){g.people=g.people.filter(n=>n!==central);g.people.splice(Math.floor(g.people.length/2),0,central);}}
|
|
for(const n of g.people){positions[n.id]={x,y:rank*STEP};x+=W+GAP;}x+=24;
|
|
}rowWidths.set(rank,Math.max(W,x-GAP));}
|
|
const width=Math.max(W,...rowWidths.values())+200;for(const n of nodes){const p=positions[n.id],rank=groups.get(find(n.id)).rank;p.x+=100+(width-200-rowWidths.get(rank))/2;p.y+=100;}
|
|
// Explicit partnership rails form parental unions only when both recorded parents
|
|
// have compatible parentage. Co-parentage alone never creates a union.
|
|
const unions=new Map(),junctions=[];
|
|
for(const l of valid){if(roles.get(l.id).kind!=='partner')continue;const a=positions[l.a],b=positions[l.b];if(a.y!==b.y)continue;const key=[l.a,l.b].sort((a,b)=>a-b).join(':'),y=a.y+H+14+(l.id%3)*8;unions.set(key,{x:(a.x+b.x+W)/2,y,a:l.a,b:l.b});}
|
|
const routes=valid.map((l,i)=>{const a=positions[l.a],b=positions[l.b],r=roles.get(l.id);let d,kind=r.style||'other';
|
|
if(r.kind==='parent'){
|
|
const from=positions[r.from],to=positions[r.to];
|
|
if(to.y>from.y){
|
|
const peer=valid.find(other=>{const rr=roles.get(other.id);return other.id!==l.id&&rr.kind==='parent'&&rr.to===r.to&&rr.delta===1&&r.delta===1&&rr.style===r.style&&unions.has([r.from,rr.from].sort((a,b)=>a-b).join(':'));});
|
|
const union=peer&&to.y-from.y===STEP?unions.get([r.from,roles.get(peer.id).from].sort((a,b)=>a-b).join(':')):null;
|
|
const bus=to.y-22-(r.to%3)*10;
|
|
if(union){d=`M ${from.x+W/2} ${from.y+H} V ${union.y} H ${union.x} V ${bus} H ${to.x+W/2} V ${to.y}`;if(!junctions.some(j=>j.x===union.x&&j.y===union.y))junctions.push({x:union.x,y:union.y});}
|
|
else if(to.y-from.y>STEP){const corridor=Math.min(...Object.values(positions).map(p=>p.x))-24-(l.id%5)*8;d=`M ${from.x+W/2} ${from.y+H} V ${from.y+H+14} H ${corridor} V ${bus} H ${to.x+W/2} V ${to.y}`;}
|
|
else d=`M ${from.x+W/2} ${from.y+H} V ${bus} H ${to.x+W/2} V ${to.y}`;
|
|
}else{kind='other';const lane=Math.min(a.y,b.y)-24-(i%5)*8;d=`M ${a.x+W/2} ${a.y} V ${lane} H ${b.x+W/2} V ${b.y}`;}
|
|
}else if(r.kind==='partner'&&a.y===b.y){const union=unions.get([l.a,l.b].sort((a,b)=>a-b).join(':'));d=`M ${a.x+W/2} ${a.y+H} V ${union.y} H ${b.x+W/2} V ${b.y+H}`;}
|
|
else if(a.y===b.y){const lane=a.y-22-(i%4)*9;d=`M ${a.x+W/2} ${a.y} V ${lane} H ${b.x+W/2} V ${b.y}`;}
|
|
else{const x=Math.min(...Object.values(positions).map(p=>p.x))-24-(i%4)*8;d=`M ${a.x+W/2} ${a.y+H} V ${a.y+H+14} H ${x} V ${b.y-14} H ${b.x+W/2} V ${b.y}`;}
|
|
return {id:l.id,d,kind};});
|
|
|
|
return {positions,routes,junctions,width,height:Math.max(H,...Object.values(positions).map(p=>p.y+H))+100,warnings,nodeWidth:W,nodeHeight:H};
|
|
}
|
|
function story(nodes,links,focusID){const focus=nodes.find(n=>n.id===focusID)||nodes[0],positions={};if(!focus)return {positions,routes:[],warnings:[],width:W,height:H,nodeWidth:W,nodeHeight:H};positions[focus.id]={x:0,y:0};const other=nodes.filter(n=>n!==focus).sort((a,b)=>Number(b.physical)-Number(a.physical)||(a.name||'').localeCompare(b.name||''));const rings=[other.slice(0,10),other.slice(10)];let radiusX=260,radiusY=140;
|
|
rings.forEach((ring,index)=>{if(!ring.length)return;radiusX=Math.max(radiusX+index*(W+60),ring.length*(W+60)/(2*Math.PI));radiusY=Math.max(radiusY+index*(H+GAP),ring.length*(H+GAP)/(2*Math.PI));ring.forEach((n,i)=>{const angle=-Math.PI/2+2*Math.PI*i/ring.length;positions[n.id]={x:Math.cos(angle)*radiusX,y:Math.sin(angle)*radiusY};});});
|
|
const values=Object.values(positions),minX=Math.min(...values.map(p=>p.x))-100,minY=Math.min(...values.map(p=>p.y))-100;for(const p of values){p.x-=minX;p.y-=minY;}
|
|
const routes=links.filter(l=>positions[l.a]&&positions[l.b]).map((l,i)=>{const a=positions[l.a],b=positions[l.b];return {id:l.id,kind:'story',d:`M ${a.x+W/2} ${a.y+H/2} Q ${(a.x+b.x)/2+W/2+(i%3-1)*18} ${(a.y+b.y)/2+H/2-(i%2)*20}, ${b.x+W/2} ${b.y+H/2}`};});
|
|
return {positions,routes,width:Math.max(...values.map(p=>p.x+W))+100,height:Math.max(...values.map(p=>p.y+H))+100,warnings:[],nodeWidth:W,nodeHeight:H};
|
|
}
|
|
window.PlotDirectorContextRelationshipLayout={family,story,role,signature};
|
|
})();
|