/* Offline reader. The public artifact is loaded by graph-data.js; no network calls. */ (() => { 'use strict'; const graph = window.RESEARCH_GRAPH; const $ = (id) => document.getElementById(id); if (!graph || !Array.isArray(graph.nodes) || !Array.isArray(graph.edges)) { $('results').replaceChildren(make('p', 'load-error', 'The public graph could not be loaded. Keep graph-data.js beside this reader, or inspect graph.json.')); return; } const nodes = graph.nodes, edges = graph.edges, regions = graph.regions || [], paths = graph.paths || [], evidence = graph.evidence || []; const documents = graph.documents || [], documentById = new Map(documents.map(d=>[d.id,d])); const nodeById = new Map(nodes.map(x => [x.id, x])); const regionById = new Map(regions.map(x => [x.id, x])); const pathById = new Map(paths.map(x => [x.id, x])); const evidenceById = new Map(evidence.map(x => [x.id, x])); const state = {mode:'nodes', query:'', regions:new Set(), match:'any', selected:null, path:null}; const welcome = $('detail').firstElementChild.cloneNode(true); const kinds = {corpus_evidence:'Evidence paraphrase', reconstruction:'Reconstruction', retrospective_connection:'Retrospective connection', retrospective:'Retrospective connection', model_proposed:'Model proposal'}; let noticeTimer; function make(tag, className, text) { const element = document.createElement(tag); if (className) element.className = className; if (text !== undefined && text !== null) element.textContent = text; return element; } function words(value) { if (value === undefined || value === null) return ''; if (typeof value === 'string') return value; if (Array.isArray(value)) return value.map(words).filter(Boolean).join('\n'); return Object.entries(value).map(([key, item]) => `${key.replaceAll('_', ' ')}: ${words(item)}`).join('\n'); } function label(kind) { return kinds[kind] || String(kind || 'Reconstruction').replaceAll('_', ' '); } function badge(kind) { const e = make('span', 'status', label(kind)); e.dataset.kind = kind || 'reconstruction'; return e; } function hash(type, id, path) { const params = new URLSearchParams({[type]:id}); if (path) params.set('path', path); return '#' + params.toString(); } function link(text, type, id, className, path) { const e = make('a', className, text); e.href = hash(type, id, path); return e; } function section(title, content) { const e = make('section', 'detail-section'); e.append(make('h3', '', title)); if (content) e.append(make('p', 'prose', words(content))); return e; } function evidenceChips(ids) { const e = make('div', 'inline-evidence'); for (const id of ids || []) if (evidenceById.has(id)) e.append(link(id, 'evidence', id, 'evidence-chip')); return e; } function nodeRegions(node) { const ids = new Set(node.regions || []); for (const region of regions) if ((region.nodes || []).includes(node.id)) ids.add(region.id); return [...ids]; } function matchesRegions(node) { if (!state.regions.size) return true; const available = nodeRegions(node); const checks = [...state.regions].map(id => available.includes(id)); return state.match === 'all' ? checks.every(Boolean) : checks.some(Boolean); } function matchesQuery(value) { const haystack = words(value).toLocaleLowerCase(); return state.query.toLocaleLowerCase().trim().split(/\s+/).every(term => haystack.includes(term)); } function relevantNodes(item, type) { if (type === 'nodes') return [item]; if (type === 'documents') return item.linked_nodes.map(id=>nodeById.get(id)).filter(Boolean); if (type === 'paths') return (item.steps || []).map(x => nodeById.get(x.node)).filter(Boolean); const ids = new Set(nodes.filter(n => (n.evidence || []).includes(item.id)).map(n => n.id)); for (const edge of edges) if ((edge.evidence || []).includes(item.id)) { ids.add(edge.from); ids.add(edge.to); } return [...ids].map(id => nodeById.get(id)).filter(Boolean); } function listItems() { const items = state.mode === 'nodes' ? nodes : state.mode === 'paths' ? paths : state.mode === 'documents' ? documents : evidence; return items.filter(item => { const related = relevantNodes(item, state.mode); if (state.regions.size && !related.some(matchesRegions)) return false; if (matchesQuery(item)) return true; if (state.mode === 'nodes') return edges.some(e => (e.from === item.id || e.to === item.id) && matchesQuery(e)); return false; }); } function renderResults() { const items = listItems(); document.querySelectorAll('[data-view]').forEach(b => b.setAttribute('aria-pressed', String(b.dataset.view === state.mode))); const noun = state.mode === 'nodes' ? 'problem' : state.mode === 'paths' ? 'path' : state.mode === 'documents' ? 'document' : 'evidence statement'; $('results-count').textContent = `${items.length} ${noun}${items.length === 1 ? '' : 's'}`; $('filter-state').textContent = state.regions.size ? `${state.regions.size} region${state.regions.size === 1 ? '' : 's'} · ${state.match === 'all' ? 'intersection' : 'union'}` : ''; const fragment = document.createDocumentFragment(); for (const item of items) { const card = make('button', 'entry-card'); card.type = 'button'; card.setAttribute('aria-current', String(state.selected === item.id)); const meta = make('div', 'card-meta'); meta.append(badge(item.epistemic_kind), make('span', 'stable-id', item.id)); card.append(meta); if (state.mode === 'evidence') { card.append(make('h3', '', 'Editorial paraphrase'), make('p', '', words(item.statement))); } else { card.append(make('h3', '', item.title), make('p', '', words(item.question || item.purpose))); if (state.mode === 'nodes') card.append(make('div', 'card-regions', nodeRegions(item).map(id => regionById.get(id)?.title || id).join(' / '))); else card.append(make('div', 'card-regions', state.mode === 'documents' ? `${item.section_ids.length} sections · English` : `${(item.steps || []).length} steps`)); } card.addEventListener('click', () => navigate(state.mode === 'nodes' ? 'node' : state.mode === 'paths' ? 'path' : state.mode === 'documents' ? 'document' : 'evidence', item.id)); fragment.append(card); } if (!items.length) fragment.append(make('p', 'empty-state', 'No entries match this combination. Try fewer regions or a different search term.')); $('results').replaceChildren(fragment); } function renderRegions() { const fragment = document.createDocumentFragment(); for (const region of regions) { const row = make('label', 'region-label'); const input = make('input'); input.type = 'checkbox'; input.checked = state.regions.has(region.id); input.addEventListener('change', () => { input.checked ? state.regions.add(region.id) : state.regions.delete(region.id); renderResults(); }); row.append(input, make('span', '', region.title), make('small', '', nodes.filter(n => nodeRegions(n).includes(region.id)).length)); if (region.description) row.title = words(region.description); fragment.append(row); } $('regions').replaceChildren(fragment); } function notice(text) { clearTimeout(noticeTimer); $('notice').textContent = text; $('notice').classList.add('visible'); noticeTimer = setTimeout(() => $('notice').classList.remove('visible'), 2600); } async function copyLink(type, id) { const url = location.href.split('#')[0] + hash(type, id); try { if (navigator.clipboard && window.isSecureContext) await navigator.clipboard.writeText(url); else { const textarea = make('textarea'); textarea.value = url; textarea.setAttribute('aria-label', 'Stable entry link'); textarea.style.cssText = 'position:fixed;left:-9999px;top:0'; document.body.append(textarea); textarea.select(); const copied = document.execCommand('copy'); textarea.remove(); if (!copied) throw new Error('Clipboard unavailable'); } notice('Stable link copied'); } catch { notice('Copy the current address from your browser to share this entry.'); } } function detailTop(item, type) { const top = make('div', 'detail-top'), meta = make('div'); meta.append(badge(item.epistemic_kind), make('span', 'stable-id', item.id)); const button = make('button', 'copy-link', 'Copy link ↗'); button.type = 'button'; button.addEventListener('click', () => copyLink(type, item.id)); top.append(meta, button); return top; } function renderEvidence(ids, title = 'Evidence paraphrases') { const s = section(title); s.append(make('p', 'evidence-note', 'These statements are editorial paraphrases of corpus evidence. They are not verbatim quotations or independent proof of a formulation.')); for (const id of ids || []) { const item = evidenceById.get(id); if (!item) continue; const card = make('div', 'evidence-item'); card.append(link(id, 'evidence', id), make('p', '', words(item.statement))); s.append(card); } if (!(ids || []).some(id => evidenceById.has(id))) s.append(make('p', 'evidence-note', 'No evidence statement is attached to this entry.')); return s; } function renderPathContext(node) { const path = pathById.get(state.path); if (!path) return null; const steps = path.steps || [], at = steps.findIndex(x => x.node === node.id); if (at < 0) return null; const box = make('div', 'path-context'); const p = make('p', '', 'Following: '); p.append(link(path.title, 'path', path.id)); box.append(p); const controls = make('div', 'path-controls'); if (at > 0) controls.append(link('← Previous', 'node', steps[at - 1].node, '', path.id)); else controls.append(make('span', '', 'Beginning')); controls.append(make('span', '', `Step ${at + 1} of ${steps.length}`)); if (at + 1 < steps.length) controls.append(link('Next →', 'node', steps[at + 1].node, '', path.id)); else controls.append(make('span', '', 'Path complete')); box.append(controls); return box; } function renderNode(node) { const fragment = document.createDocumentFragment(); const context = renderPathContext(node); if (context) fragment.append(context); fragment.append(detailTop(node, 'node'), make('h2', '', node.title)); const tags = make('div', 'region-tags'); for (const id of nodeRegions(node)) { const tag = make('button', 'region-tag', regionById.get(id)?.title || id); tag.type = 'button'; tag.title = 'Show problems in this region'; tag.addEventListener('click', () => { state.regions = new Set([id]); state.mode = 'nodes'; state.query = ''; $('search').value = ''; renderRegions(); renderResults(); }); tags.append(tag); } fragment.append(tags); if (node.question) fragment.append(make('p', 'question', words(node.question))); if (node.formulation) fragment.append(section('Working formulation', node.formulation)); if (node.development) fragment.append(section(words(node.development).length < 40 ? 'Development state' : 'Development & revisions', words(node.development).replaceAll('_', ' '))); if (node.tension) fragment.append(section('Unresolved tension', node.tension)); const additionalQuestions = [...new Set(node.open_questions || [])].filter(q => q !== node.question && q !== node.challenge); if (additionalQuestions.length) { const s = section('Open questions'), list = make('ul', 'prose'); additionalQuestions.forEach(q => list.append(make('li', '', words(q)))); s.append(list); fragment.append(s); } if ((node.representations || []).length) { const s = section('Alternative representations'); for (const representation of node.representations) { const block = make('div', 'representation'); block.append(make('h4', '', representation.name)); if (representation.epistemic_kind && representation.epistemic_kind !== node.epistemic_kind) block.append(badge(representation.epistemic_kind)); const facets = make('div', 'rep-facets'); for (const [name, value] of [['What it makes available', representation.affords], ['What it leaves out', representation.loses]]) { const dl = make('dl'); dl.append(make('dt', '', name), make('dd', '', words(value))); facets.append(dl); } block.append(facets); s.append(block); } fragment.append(s); } if (node.challenge) { const s = section('Challenge this formulation'); const box = make('div', 'challenge-box'); box.append(make('p', 'prose', words(node.challenge))); s.append(box); fragment.append(s); } if (node.next_move) { const s = section('A possible next move'), box = make('div', 'proposal-box'); box.append(badge(node.next_move.epistemic_kind || 'model_proposed'), make('p', '', words(node.next_move.text || node.next_move))); s.append(box); fragment.append(s); } fragment.append(renderEvidence(node.evidence)); const related = edges.filter(e => e.from === node.id || e.to === node.id); const s = section(`Connections · ${related.length}`); for (const edge of related) { const outgoing = edge.from === node.id, otherId = outgoing ? edge.to : edge.from, other = nodeById.get(otherId); if (!other) continue; const card = make('div', 'relation-card'); card.append(make('div', 'relation-direction', outgoing ? `From this problem → ${words(edge.relation).replaceAll('_', ' ')}` : `${words(edge.relation).replaceAll('_', ' ')} → into this problem`)); card.append(link(other.title, 'node', other.id, 'relation-title'), badge(edge.epistemic_kind)); if (edge.warrant) card.append(make('p', '', words(edge.warrant))); card.append(evidenceChips(edge.evidence)); s.append(card); } if (!related.length) s.append(make('p', 'evidence-note', 'No connection has yet been formulated.')); fragment.append(s); const editions = documents.filter(d=>d.linked_nodes.includes(node.id)); if(editions.length){const ds=section('Research documents'),dl=make('div','backlinks');for(const d of editions)dl.append(link(d.title,'document',d.id));ds.append(dl);fragment.append(ds);} const containing = paths.filter(p => (p.steps || []).some(step => step.node === node.id)); if (containing.length) { const s = section('Paths through this problem'), links = make('div', 'backlinks'); containing.forEach(p => links.append(link(p.title, 'path', p.id))); s.append(links); fragment.append(s); } $('detail').replaceChildren(fragment); } function inlineMarkdown(parent, text) { const re = /(\[([^\]]+)\]\(([^)]+)\)|\*\*([^*]+)\*\*|\x60([^\x60]+)\x60)/g; let at=0; for(const m of text.matchAll(re)){ parent.append(document.createTextNode(text.slice(at,m.index))); if(m[2]){ const target=documents.find(d=>d.path==='documents/'+m[3]||d.path===m[3]); if(target) parent.append(link(m[2],'document',target.id)); else parent.append(document.createTextNode(m[2])); } else parent.append(make(m[4]?'strong':'code','',m[4]||m[5])); at=m.index+m[0].length; } parent.append(document.createTextNode(text.slice(at))); } function markdownBody(markdown) { const body=make('div','document-body'),lines=markdown.split('\n'); let paragraph=[],list=null,pre=[],table=null,headingIndex=0; function flush(){if(paragraph.length){const p=make('p');inlineMarkdown(p,paragraph.join(' '));body.append(p);paragraph=[];}list=null;table=null;} function flushPre(){if(pre.length){body.append(make('pre','',pre.join('\n')));pre=[];}} for(const line of lines){ if(line.startsWith(' ')){flush();pre.push(line.slice(4));continue;} flushPre(); if(/^# /.test(line)){flush();continue;} const h=line.match(/^(#{2,4}) (.+)$/); if(h){flush();const e=make('h'+Math.min(h[1].length+1,5),'',h[2]);if(h[1].length===2)e.id='document-section-'+(++headingIndex);body.append(e);continue;} if(!line.trim()){flush();continue;} if(line.startsWith('|')){ if(/^\|[\s:|-]+\|$/.test(line))continue; if(!table){flush();const wrap=make('div','document-table');table=make('table');wrap.append(table);body.append(wrap);} const tr=make('tr');for(const cell of line.split('|').slice(1,-1)){const td=make(table.children.length?'td':'th');inlineMarkdown(td,cell.trim());tr.append(td);}table.append(tr);continue; } const li=line.match(/^(?:- |(\d+)\. )(.+)$/); if(li){if(!list){flush();list=make(li[1]?'ol':'ul');body.append(list);}const el=make('li');inlineMarkdown(el,li[2]);list.append(el);continue;} if(line.startsWith('> ')){flush();const q=make('blockquote');inlineMarkdown(q,line.slice(2));body.append(q);continue;} paragraph.push(line); } flushPre();flush();return body; } function renderDocument(item){ const f=document.createDocumentFragment(); f.append(detailTop(item,'document'),make('h2','',item.title),make('p','evidence-note',item.editorial_scope)); const download=make('button','copy-link','Download English Markdown ↓');download.type='button'; download.addEventListener('click',()=>{const url=URL.createObjectURL(new Blob([item.markdown],{type:'text/markdown;charset=utf-8'}));const a=make('a');a.href=url;a.download=item.id+'.md';a.click();setTimeout(()=>URL.revokeObjectURL(url),1000);}); f.append(download); const contents=make('details','document-contents');contents.append(make('summary','',item.section_ids.length+' sections · jump within the text')); const ol=make('ol'); for(const s of graph.document_sections.filter(s=>s.document===item.id)){ const li=make('li'),a=link(s.title,'document',item.id);a.href+='§ion='+s.order;li.append(a);ol.append(li); } contents.append(ol);f.append(contents); f.append(markdownBody(item.markdown)); const related=section('Related problems'); for(const l of graph.document_links.filter(l=>l.document===item.id)){ const p=make('p');p.append(link(nodeById.get(l.node).title,'node',l.node),document.createTextNode(' · '+label(l.epistemic_kind)));related.append(p); } f.append(related,renderEvidence(item.context_evidence,'Contextual evidence anchors')); f.append(make('p','evidence-note','Context links locate related formulations. The edition is not a new independent witness for each anchor.')); $('detail').replaceChildren(f); } function renderPath(path) { const fragment = document.createDocumentFragment(); fragment.append(detailTop(path, 'path'), make('h2', '', path.title), make('p', 'prose', words(path.purpose))); if (path.caution) fragment.append(make('p', 'evidence-note', words(path.caution))); const list = make('ol', 'path-steps'); for (const step of path.steps || []) { const node = nodeById.get(step.node); if (!node) continue; const li = make('li'); li.append(link(node.title, 'node', node.id, '', path.id), make('p', '', words(step.reason))); list.append(li); } fragment.append(section('A route, open to revision')); fragment.lastChild.append(list); $('detail').replaceChildren(fragment); } function renderEvidenceDetail(item) { const fragment = document.createDocumentFragment(); fragment.append(detailTop(item, 'evidence'), make('h2', '', 'Evidence statement')); fragment.append(make('p', 'evidence-note', 'Editorial paraphrase of corpus evidence. This is not a verbatim quotation or an independently verified result.')); fragment.append(make('p', 'question', words(item.statement))); const used = nodes.filter(n => (n.evidence || []).includes(item.id)); const s = section('Formulations drawing on this statement'), links = make('div', 'backlinks'); used.forEach(n => links.append(link(n.title, 'node', n.id))); s.append(links); if (!used.length) s.append(make('p', 'evidence-note', 'No problem directly cites this statement. It may support a connection or research direction.')); fragment.append(s); const relations = edges.filter(e => (e.evidence || []).includes(item.id)); if (relations.length) { const s = section('Connections drawing on this statement'); for (const edge of relations) { const card = make('div', 'relation-card'); if (nodeById.has(edge.from)) card.append(link(nodeById.get(edge.from).title, 'node', edge.from)); card.append(make('p', '', `→ ${words(edge.relation).replaceAll('_', ' ')} →`)); if (nodeById.has(edge.to)) card.append(link(nodeById.get(edge.to).title, 'node', edge.to)); card.append(make('p', '', words(edge.warrant)), badge(edge.epistemic_kind)); s.append(card); } fragment.append(s); } $('detail').replaceChildren(fragment); } function navigate(type, id) { const target = hash(type, id); if (location.hash === target) readRoute(true); else location.hash = target; } function readRoute(focus = false) { const params = new URLSearchParams(location.hash.slice(1)); const type = params.has('node') ? 'node' : params.has('path') ? 'path' : params.has('evidence') ? 'evidence' : params.has('document') ? 'document' : null; if (!type) { state.selected = null; state.path = null; $('detail').replaceChildren(welcome.cloneNode(true)); document.title = graph.title || 'Research problem-space'; renderResults(); return; } const id = params.get(type); const item = (type === 'node' ? nodeById : type === 'path' ? pathById : type === 'document' ? documentById : evidenceById).get(id); if (!item) { $('detail').replaceChildren(make('h2', '', 'Entry not found'), make('p', 'prose', 'This identifier is not present in this version of the research space. Use the problem list to find a current formulation.')); state.selected = null; renderResults(); return; } state.selected = id; state.mode = type === 'node' ? 'nodes' : type === 'path' ? 'paths' : type === 'document' ? 'documents' : 'evidence'; state.path = type === 'node' ? params.get('path') : null; if (type === 'node') renderNode(item); else if (type === 'path') renderPath(item); else if(type === 'document') renderDocument(item); else renderEvidenceDetail(item); renderResults(); $('detail').scrollTop = 0; if (focus) { $('detail').focus({preventScroll:true}); if (matchMedia('(max-width:620px)').matches) $('detail').scrollIntoView({block:'start',behavior:'instant'}); } if(type==='document' && params.has('section')){const sectionId='document-section-'+Number(params.get('section'));requestAnimationFrame(()=>{const target=document.getElementById(sectionId);if(target)target.scrollIntoView({block:'start'});});} document.title = `${item.title || 'Evidence statement'} · ${graph.title || 'Research problem-space'}`; } $('artifact-title').textContent = graph.title || 'Research problem-space'; document.title = graph.title || 'Research problem-space'; $('artifact-version').textContent = `EVOLVING RESEARCH PROBLEM-SPACE${graph.version ? ` / ${graph.version}` : ''}`; if (graph.intention?.statement) { $('intention-text').textContent = words(graph.intention.statement); $('intention-kind').textContent = label(graph.intention.epistemic_kind); $('intention-evidence').append(evidenceChips(graph.intention.evidence)); } else $('intention-section').hidden = true; $('document-count').textContent = documents.length; $('node-count').textContent = nodes.length; $('path-count').textContent = paths.length; $('evidence-count').textContent = evidence.length; $('search').addEventListener('input', event => { state.query = event.target.value; renderResults(); }); $('region-match').addEventListener('change', event => { state.match = event.target.value; renderResults(); }); $('clear-filters').addEventListener('click', () => { state.regions.clear(); state.query = ''; $('search').value = ''; renderRegions(); renderResults(); }); document.querySelectorAll('[data-view]').forEach(button => button.addEventListener('click', () => { state.mode = button.dataset.view; renderResults(); })); window.addEventListener('hashchange', () => readRoute(true)); if (matchMedia('(max-width:860px)').matches) document.querySelector('.region-filter').open = false; renderRegions(); renderResults(); readRoute(false); })();