|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>Graph Viewer</title> |
| 7 | + <link rel="stylesheet" href="{{ url_for('static', filename='css/graph.css') }}" /> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + <header> |
| 11 | + <h1>Graph Viewer</h1> |
| 12 | + <p class="status">Loading graph from the /extract endpoint...</p> |
| 13 | + </header> |
| 14 | + <main> |
| 15 | + <div id="graph"></div> |
| 16 | + <aside class="sidebar"> |
| 17 | + <h2 id="sidebar-title">No node selected</h2> |
| 18 | + <div id="node-details">Select a node to view details.</div> |
| 19 | + </aside> |
| 20 | + </main> |
| 21 | + |
| 22 | + <script src="https://unpkg.com/cytoscape@3.4.0/dist/cytoscape.min.js"></script> |
| 23 | + <script> |
| 24 | + const status = document.querySelector('.status'); |
| 25 | + const sidebarTitle = document.getElementById('sidebar-title'); |
| 26 | + const details = document.getElementById('node-details'); |
| 27 | + |
| 28 | + function escapeHtml(value) { |
| 29 | + return String(value || '') |
| 30 | + .replace(/&/g, '&') |
| 31 | + .replace(/</g, '<') |
| 32 | + .replace(/>/g, '>') |
| 33 | + .replace(/"/g, '"') |
| 34 | + .replace(/'/g, '''); |
| 35 | + } |
| 36 | + |
| 37 | + function renderNodeDetails(node) { |
| 38 | + const data = node.data(); |
| 39 | + sidebarTitle.textContent = data.label || 'Selected node'; |
| 40 | + |
| 41 | + const occurrences = Array.isArray(data.occurrences) ? data.occurrences : []; |
| 42 | + if (!occurrences.length) { |
| 43 | + details.innerHTML = '<p>No occurrences available.</p>'; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + details.innerHTML = occurrences.map(item => { |
| 48 | + const link = escapeHtml(item.link); |
| 49 | + const context = item.context; |
| 50 | + return ` |
| 51 | + <section> |
| 52 | + <p><a href="${link}" target="_blank" rel="noopener noreferrer">View on GOV.UK</a></p> |
| 53 | + <p>${context}</p> |
| 54 | + </section> |
| 55 | + `; |
| 56 | + }).join(''); |
| 57 | + } |
| 58 | + |
| 59 | + async function loadGraph() { |
| 60 | + try { |
| 61 | + const response = await fetch('/graph-viewmodel'); |
| 62 | + if (!response.ok) { |
| 63 | + throw new Error(`Failed to load graph: ${response.status} ${response.statusText}`); |
| 64 | + } |
| 65 | + |
| 66 | + const graphData = await response.json(); |
| 67 | + |
| 68 | + status.textContent = 'Graph loaded successfully.'; |
| 69 | + |
| 70 | + const cy = cytoscape({ |
| 71 | + container: document.getElementById('graph'), |
| 72 | + boxSelectionEnabled: false, |
| 73 | + autoungrabify: true, |
| 74 | + elements: graphData, |
| 75 | + style: [ |
| 76 | + { |
| 77 | + selector: 'node[type="entity"]', |
| 78 | + style: { |
| 79 | + 'background-color': '#2563eb', |
| 80 | + 'label': 'data(label)', |
| 81 | + 'color': '#ffffff', |
| 82 | + 'text-valign': 'center', |
| 83 | + 'text-halign': 'center', |
| 84 | + 'width': 'label', |
| 85 | + 'padding': '12px', |
| 86 | + 'shape': 'roundrectangle' |
| 87 | + } |
| 88 | + }, |
| 89 | + { |
| 90 | + selector: 'node[type="alias"]', |
| 91 | + style: { |
| 92 | + 'background-color': '#f59e0b', |
| 93 | + 'label': 'data(label)', |
| 94 | + 'color': '#111827', |
| 95 | + 'text-valign': 'center', |
| 96 | + 'text-halign': 'center', |
| 97 | + 'width': 'label', |
| 98 | + 'padding': '10px', |
| 99 | + 'shape': 'ellipse' |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + selector: 'edge', |
| 104 | + style: { |
| 105 | + 'width': 2, |
| 106 | + 'line-color': '#9ca3af', |
| 107 | + 'target-arrow-color': '#9ca3af', |
| 108 | + 'target-arrow-shape': 'triangle', |
| 109 | + 'curve-style': 'bezier', |
| 110 | + 'label': 'data(label)', |
| 111 | + 'font-size': 10, |
| 112 | + 'text-rotation': 'autorotate', |
| 113 | + 'text-margin-y': -8 |
| 114 | + } |
| 115 | + }, |
| 116 | + { |
| 117 | + selector: ':selected', |
| 118 | + style: { |
| 119 | + 'border-width': 3, |
| 120 | + 'border-color': '#10b981' |
| 121 | + } |
| 122 | + } |
| 123 | + ], |
| 124 | + layout: { |
| 125 | + name: 'cose', |
| 126 | + idealEdgeLength: 120, |
| 127 | + nodeOverlap: 20, |
| 128 | + refresh: 20, |
| 129 | + fit: true, |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + cy.on('tap', 'node', (event) => { |
| 134 | + renderNodeDetails(event.target); |
| 135 | + }); |
| 136 | + |
| 137 | + cy.on('tap', (event) => { |
| 138 | + if (event.target === cy) { |
| 139 | + sidebarTitle.textContent = 'No node selected'; |
| 140 | + details.innerHTML = '<p>Select a node to view details.</p>'; |
| 141 | + } |
| 142 | + }); |
| 143 | + } catch (error) { |
| 144 | + status.textContent = error.message; |
| 145 | + details.textContent = 'Unable to display graph.'; |
| 146 | + console.error(error); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + loadGraph(); |
| 151 | + </script> |
| 152 | + </body> |
| 153 | +</html> |
0 commit comments