2022-06-06 18:48:16 +03:00
|
|
|
async function drawGraph(baseUrl, isHome, pathColors, graphConfig) {
|
2022-06-02 10:16:02 +03:00
|
|
|
|
|
|
|
let {
|
2022-06-01 15:49:27 +03:00
|
|
|
depth,
|
|
|
|
enableDrag,
|
|
|
|
enableLegend,
|
|
|
|
enableZoom,
|
|
|
|
opacityScale,
|
|
|
|
scale,
|
|
|
|
repelForce,
|
2022-06-02 10:16:02 +03:00
|
|
|
fontSize} = graphConfig;
|
|
|
|
|
2022-05-27 18:40:00 +03:00
|
|
|
const container = document.getElementById("graph-container")
|
2022-03-07 21:25:02 +03:00
|
|
|
const { index, links, content } = await fetchData
|
2022-05-03 20:54:39 +03:00
|
|
|
|
2022-05-03 20:38:41 +03:00
|
|
|
// Use .pathname to remove hashes / searchParams / text fragments
|
2022-05-03 20:55:45 +03:00
|
|
|
const cleanUrl = window.location.origin + window.location.pathname
|
2022-05-03 20:54:39 +03:00
|
|
|
|
|
|
|
const curPage = cleanUrl.replace(/\/$/g, "").replace(baseUrl, "")
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-02 08:06:33 +03:00
|
|
|
const parseIdsFromLinks = (links) => [
|
|
|
|
...new Set(links.flatMap((link) => [link.source, link.target])),
|
2022-05-02 19:56:44 +03:00
|
|
|
]
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-02 19:04:36 +03:00
|
|
|
// Links is mutated by d3. We want to use links later on, so we make a copy and pass that one to d3
|
|
|
|
// Note: shallow cloning does not work because it copies over references from the original array
|
2022-05-02 19:56:44 +03:00
|
|
|
const copyLinks = JSON.parse(JSON.stringify(links))
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
const neighbours = new Set()
|
2022-05-27 18:40:00 +03:00
|
|
|
const wl = [curPage || "/", "__SENTINEL"]
|
2022-03-07 21:25:02 +03:00
|
|
|
if (depth >= 0) {
|
|
|
|
while (depth >= 0 && wl.length > 0) {
|
|
|
|
// compute neighbours
|
|
|
|
const cur = wl.shift()
|
2022-05-27 18:40:00 +03:00
|
|
|
if (cur === "__SENTINEL") {
|
2022-03-07 21:25:02 +03:00
|
|
|
depth--
|
2022-05-27 18:40:00 +03:00
|
|
|
wl.push("__SENTINEL")
|
2022-03-07 21:25:02 +03:00
|
|
|
} else {
|
|
|
|
neighbours.add(cur)
|
|
|
|
const outgoing = index.links[cur] || []
|
|
|
|
const incoming = index.backlinks[cur] || []
|
2022-05-27 18:40:00 +03:00
|
|
|
wl.push(...outgoing.map((l) => l.target), ...incoming.map((l) => l.source))
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-07 21:25:02 +03:00
|
|
|
} else {
|
2022-05-02 19:56:44 +03:00
|
|
|
parseIdsFromLinks(copyLinks).forEach((id) => neighbours.add(id))
|
2022-03-07 21:25:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const data = {
|
2022-05-02 08:06:33 +03:00
|
|
|
nodes: [...neighbours].map((id) => ({ id })),
|
2022-05-27 18:40:00 +03:00
|
|
|
links: copyLinks.filter((l) => neighbours.has(l.source) && neighbours.has(l.target)),
|
2022-03-07 21:25:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const color = (d) => {
|
2022-05-27 18:40:00 +03:00
|
|
|
if (d.id === curPage || (d.id === "/" && curPage === "")) {
|
|
|
|
return "var(--g-node-active)"
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
for (const pathColor of pathColors) {
|
|
|
|
const path = Object.keys(pathColor)[0]
|
|
|
|
const colour = pathColor[path]
|
|
|
|
if (d.id.startsWith(path)) {
|
|
|
|
return colour
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-27 18:40:00 +03:00
|
|
|
return "var(--g-node)"
|
2022-03-07 21:25:02 +03:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:06:33 +03:00
|
|
|
const drag = (simulation) => {
|
2022-03-07 21:25:02 +03:00
|
|
|
function dragstarted(event, d) {
|
2022-05-02 19:14:51 +03:00
|
|
|
if (!event.active) simulation.alphaTarget(1).restart()
|
|
|
|
d.fx = d.x
|
|
|
|
d.fy = d.y
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
function dragged(event, d) {
|
2022-05-02 19:14:51 +03:00
|
|
|
d.fx = event.x
|
|
|
|
d.fy = event.y
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
function dragended(event, d) {
|
2022-05-02 19:14:51 +03:00
|
|
|
if (!event.active) simulation.alphaTarget(0)
|
|
|
|
d.fx = null
|
|
|
|
d.fy = null
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-27 18:40:00 +03:00
|
|
|
const noop = () => {}
|
2022-05-02 08:06:33 +03:00
|
|
|
return d3
|
|
|
|
.drag()
|
2022-05-27 18:40:00 +03:00
|
|
|
.on("start", enableDrag ? dragstarted : noop)
|
|
|
|
.on("drag", enableDrag ? dragged : noop)
|
|
|
|
.on("end", enableDrag ? dragended : noop)
|
2022-03-07 21:25:02 +03:00
|
|
|
}
|
|
|
|
|
2022-06-01 15:49:27 +03:00
|
|
|
const height = Math.max(container.offsetHeight, isHome ? 500 : 250)
|
2022-05-02 19:56:44 +03:00
|
|
|
const width = container.offsetWidth
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-02 08:06:33 +03:00
|
|
|
const simulation = d3
|
|
|
|
.forceSimulation(data.nodes)
|
2022-06-01 15:49:27 +03:00
|
|
|
.force("charge", d3.forceManyBody().strength(-100 * repelForce))
|
2022-05-02 08:06:33 +03:00
|
|
|
.force(
|
2022-05-27 18:40:00 +03:00
|
|
|
"link",
|
2022-05-02 08:06:33 +03:00
|
|
|
d3
|
|
|
|
.forceLink(data.links)
|
|
|
|
.id((d) => d.id)
|
2022-05-27 18:40:00 +03:00
|
|
|
.distance(40),
|
2022-05-02 08:06:33 +03:00
|
|
|
)
|
2022-05-27 18:40:00 +03:00
|
|
|
.force("center", d3.forceCenter())
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-02 08:06:33 +03:00
|
|
|
const svg = d3
|
2022-05-27 18:40:00 +03:00
|
|
|
.select("#graph-container")
|
|
|
|
.append("svg")
|
|
|
|
.attr("width", width)
|
|
|
|
.attr("height", height)
|
2022-06-01 15:49:27 +03:00
|
|
|
.attr('viewBox', [-width / 2 * 1 / scale, -height / 2 * 1 / scale, width * 1 / scale, height * 1 / scale])
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
if (enableLegend) {
|
2022-05-27 18:40:00 +03:00
|
|
|
const legend = [{ Current: "var(--g-node-active)" }, { Note: "var(--g-node)" }, ...pathColors]
|
2022-03-07 21:25:02 +03:00
|
|
|
legend.forEach((legendEntry, i) => {
|
|
|
|
const key = Object.keys(legendEntry)[0]
|
|
|
|
const colour = legendEntry[key]
|
2022-05-02 08:06:33 +03:00
|
|
|
svg
|
2022-05-27 18:40:00 +03:00
|
|
|
.append("circle")
|
|
|
|
.attr("cx", -width / 2 + 20)
|
|
|
|
.attr("cy", height / 2 - 30 * (i + 1))
|
|
|
|
.attr("r", 6)
|
|
|
|
.style("fill", colour)
|
2022-05-02 08:06:33 +03:00
|
|
|
svg
|
2022-05-27 18:40:00 +03:00
|
|
|
.append("text")
|
|
|
|
.attr("x", -width / 2 + 40)
|
|
|
|
.attr("y", height / 2 - 30 * (i + 1))
|
2022-05-02 08:06:33 +03:00
|
|
|
.text(key)
|
2022-05-27 18:40:00 +03:00
|
|
|
.style("font-size", "15px")
|
|
|
|
.attr("alignment-baseline", "middle")
|
2022-03-07 21:25:02 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw links between nodes
|
2022-05-02 08:06:33 +03:00
|
|
|
const link = svg
|
2022-05-27 18:40:00 +03:00
|
|
|
.append("g")
|
|
|
|
.selectAll("line")
|
2022-03-07 21:25:02 +03:00
|
|
|
.data(data.links)
|
2022-05-27 18:40:00 +03:00
|
|
|
.join("line")
|
|
|
|
.attr("class", "link")
|
|
|
|
.attr("stroke", "var(--g-link)")
|
|
|
|
.attr("stroke-width", 2)
|
|
|
|
.attr("data-source", (d) => d.source.id)
|
|
|
|
.attr("data-target", (d) => d.target.id)
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
// svg groups
|
2022-05-27 18:40:00 +03:00
|
|
|
const graphNode = svg.append("g").selectAll("g").data(data.nodes).enter().append("g")
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-04-29 01:49:16 +03:00
|
|
|
// calculate radius
|
|
|
|
const nodeRadius = (d) => {
|
|
|
|
const numOut = index.links[d.id]?.length || 0
|
|
|
|
const numIn = index.backlinks[d.id]?.length || 0
|
2022-08-05 21:04:01 +03:00
|
|
|
return 2 + Math.sqrt(numOut + numIn)
|
2022-04-29 01:49:16 +03:00
|
|
|
}
|
|
|
|
|
2022-03-07 21:25:02 +03:00
|
|
|
// draw individual nodes
|
2022-05-02 08:06:33 +03:00
|
|
|
const node = graphNode
|
2022-05-27 18:40:00 +03:00
|
|
|
.append("circle")
|
|
|
|
.attr("class", "node")
|
|
|
|
.attr("id", (d) => d.id)
|
|
|
|
.attr("r", nodeRadius)
|
|
|
|
.attr("fill", color)
|
|
|
|
.style("cursor", "pointer")
|
|
|
|
.on("click", (_, d) => {
|
2022-05-02 19:04:36 +03:00
|
|
|
// SPA navigation
|
2022-05-27 18:40:00 +03:00
|
|
|
window.Million.navigate(new URL(`${baseUrl}${decodeURI(d.id).replace(/\s+/g, "-")}/`), ".singlePage")
|
2022-03-07 21:25:02 +03:00
|
|
|
})
|
2022-05-27 18:40:00 +03:00
|
|
|
.on("mouseover", function (_, d) {
|
|
|
|
d3.selectAll(".node").transition().duration(100).attr("fill", "var(--g-node-inactive)")
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-02 08:06:33 +03:00
|
|
|
const neighbours = parseIdsFromLinks([
|
|
|
|
...(index.links[d.id] || []),
|
|
|
|
...(index.backlinks[d.id] || []),
|
2022-05-02 19:56:44 +03:00
|
|
|
])
|
2022-05-27 18:40:00 +03:00
|
|
|
const neighbourNodes = d3.selectAll(".node").filter((d) => neighbours.includes(d.id))
|
2022-03-07 21:25:02 +03:00
|
|
|
const currentId = d.id
|
2022-06-01 23:30:40 +03:00
|
|
|
window.Million.prefetch(new URL(`${baseUrl}${decodeURI(d.id).replace(/\s+/g, "-")}/`))
|
2022-05-02 08:06:33 +03:00
|
|
|
const linkNodes = d3
|
2022-05-27 18:40:00 +03:00
|
|
|
.selectAll(".link")
|
2022-05-02 19:56:44 +03:00
|
|
|
.filter((d) => d.source.id === currentId || d.target.id === currentId)
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
// highlight neighbour nodes
|
2022-05-27 18:40:00 +03:00
|
|
|
neighbourNodes.transition().duration(200).attr("fill", color)
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
// highlight links
|
2022-05-27 18:40:00 +03:00
|
|
|
linkNodes.transition().duration(200).attr("stroke", "var(--g-link-active)")
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-06-02 10:35:28 +03:00
|
|
|
const bigFont = fontSize*1.5
|
2022-06-01 15:49:27 +03:00
|
|
|
|
2022-03-07 21:25:02 +03:00
|
|
|
// show text for self
|
|
|
|
d3.select(this.parentNode)
|
|
|
|
.raise()
|
2022-05-27 18:40:00 +03:00
|
|
|
.select("text")
|
2022-03-07 21:25:02 +03:00
|
|
|
.transition()
|
|
|
|
.duration(200)
|
2022-06-01 15:49:27 +03:00
|
|
|
.attr('opacityOld', d3.select(this.parentNode).select('text').style("opacity"))
|
|
|
|
.style('opacity', 1)
|
|
|
|
.style('font-size', bigFont+'em')
|
|
|
|
.attr('dy', d => nodeRadius(d) + 20 + 'px') // radius is in px
|
2022-05-02 08:06:33 +03:00
|
|
|
})
|
2022-05-27 18:40:00 +03:00
|
|
|
.on("mouseleave", function (_, d) {
|
|
|
|
d3.selectAll(".node").transition().duration(200).attr("fill", color)
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
const currentId = d.id
|
2022-05-02 08:06:33 +03:00
|
|
|
const linkNodes = d3
|
2022-05-27 18:40:00 +03:00
|
|
|
.selectAll(".link")
|
2022-05-02 19:56:44 +03:00
|
|
|
.filter((d) => d.source.id === currentId || d.target.id === currentId)
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-05-27 18:40:00 +03:00
|
|
|
linkNodes.transition().duration(200).attr("stroke", "var(--g-link)")
|
2022-03-07 21:25:02 +03:00
|
|
|
|
2022-06-01 15:49:27 +03:00
|
|
|
d3.select(this.parentNode)
|
|
|
|
.select("text")
|
|
|
|
.transition()
|
|
|
|
.duration(200)
|
|
|
|
.style('opacity', d3.select(this.parentNode).select('text').attr("opacityOld"))
|
|
|
|
.style('font-size', fontSize+'em')
|
|
|
|
.attr('dy', d => nodeRadius(d) + 8 + 'px') // radius is in px
|
2022-03-07 21:25:02 +03:00
|
|
|
})
|
2022-05-02 19:14:51 +03:00
|
|
|
.call(drag(simulation))
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
// draw labels
|
2022-05-02 08:06:33 +03:00
|
|
|
const labels = graphNode
|
2022-05-27 18:40:00 +03:00
|
|
|
.append("text")
|
|
|
|
.attr("dx", 0)
|
|
|
|
.attr("dy", (d) => nodeRadius(d) + 8 + "px")
|
|
|
|
.attr("text-anchor", "middle")
|
|
|
|
.text((d) => content[d.id]?.title || d.id.replace("-", " "))
|
2022-06-01 15:49:27 +03:00
|
|
|
.style('opacity', (opacityScale - 1) / 3.75)
|
2022-05-27 18:40:00 +03:00
|
|
|
.style("pointer-events", "none")
|
2022-06-01 15:49:27 +03:00
|
|
|
.style('font-size', fontSize+'em')
|
2022-04-29 01:49:16 +03:00
|
|
|
.raise()
|
2022-05-02 19:14:51 +03:00
|
|
|
.call(drag(simulation))
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
// set panning
|
|
|
|
|
|
|
|
if (enableZoom) {
|
2022-05-02 08:06:33 +03:00
|
|
|
svg.call(
|
|
|
|
d3
|
|
|
|
.zoom()
|
|
|
|
.extent([
|
|
|
|
[0, 0],
|
|
|
|
[width, height],
|
|
|
|
])
|
|
|
|
.scaleExtent([0.25, 4])
|
2022-05-27 18:40:00 +03:00
|
|
|
.on("zoom", ({ transform }) => {
|
|
|
|
link.attr("transform", transform)
|
|
|
|
node.attr("transform", transform)
|
2022-06-01 15:49:27 +03:00
|
|
|
const scale = transform.k * opacityScale;
|
2022-05-02 19:56:44 +03:00
|
|
|
const scaledOpacity = Math.max((scale - 1) / 3.75, 0)
|
2022-05-27 18:40:00 +03:00
|
|
|
labels.attr("transform", transform).style("opacity", scaledOpacity)
|
|
|
|
}),
|
2022-05-02 19:56:44 +03:00
|
|
|
)
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
2022-03-07 21:25:02 +03:00
|
|
|
|
|
|
|
// progress the simulation
|
2022-05-27 18:40:00 +03:00
|
|
|
simulation.on("tick", () => {
|
2022-03-07 21:25:02 +03:00
|
|
|
link
|
2022-05-27 18:40:00 +03:00
|
|
|
.attr("x1", (d) => d.source.x)
|
|
|
|
.attr("y1", (d) => d.source.y)
|
|
|
|
.attr("x2", (d) => d.target.x)
|
|
|
|
.attr("y2", (d) => d.target.y)
|
|
|
|
node.attr("cx", (d) => d.x).attr("cy", (d) => d.y)
|
|
|
|
labels.attr("x", (d) => d.x).attr("y", (d) => d.y)
|
2022-05-02 19:14:51 +03:00
|
|
|
})
|
2022-03-07 21:25:02 +03:00
|
|
|
}
|