context-mapper 0.20.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,622 @@
1
+ import mermaid from "https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs";
2
+
3
+ const MODEL = /*__MODEL__*/null;
4
+
5
+ // Lookups. A "single" domain is one bounded context with the same id.
6
+ const domainById = Object.fromEntries(MODEL.domains.map(d => [d.id, d]));
7
+ const contextById = Object.fromEntries(MODEL.contexts.map(c => [c.id, c]));
8
+ const clusterById = Object.fromEntries(MODEL.clusters.map(c => [c.id, c]));
9
+ const owner = {};
10
+ MODEL.contexts.forEach(c => owner[c.id] = c.domain);
11
+ MODEL.domains.forEach(d => owner[d.id] = d.id);
12
+ const nameOf = id => (contextById[id] || domainById[id]).name;
13
+ const isMulti = id => domainById[id] && !domainById[id].single;
14
+
15
+ // Same graphs as graph.py: context level as written, domain level lifted and merged.
16
+ // childOf says which container a node renders inside: a context inside its domain, a member inside its cluster.
17
+ function levelGraph(level) {
18
+ const childOf = {};
19
+ MODEL.domains.filter(d => d.cluster).forEach(d => childOf[d.id] = d.cluster);
20
+ if (level === "context") {
21
+ const nodes = new Set([...MODEL.contexts.map(c => c.id), ...MODEL.domains.filter(d => !d.single).map(d => d.id)]);
22
+ MODEL.contexts.filter(c => !domainById[c.domain].single).forEach(c => childOf[c.id] = c.domain);
23
+ return { nodes, childOf, edges: MODEL.edges.map(e => ({ from: e.from, to: e.to, styles: new Set([e.style]), via: [e] })) };
24
+ }
25
+ const merged = new Map();
26
+ MODEL.edges.forEach(e => {
27
+ const a = owner[e.from], b = owner[e.to];
28
+ if (a === b) return;
29
+ const key = a + "|" + b;
30
+ if (!merged.has(key)) merged.set(key, { from: a, to: b, styles: new Set(), via: [] });
31
+ merged.get(key).styles.add(e.style);
32
+ merged.get(key).via.push(e);
33
+ });
34
+ return { nodes: new Set(MODEL.domains.map(d => d.id)), childOf,
35
+ edges: [...merged.values()] };
36
+ }
37
+ const GRAPHS = { context: levelGraph("context"), domain: levelGraph("domain") };
38
+
39
+ // A container and everything rendered inside it.
40
+ function descendants(g, ids) {
41
+ const kids = {};
42
+ Object.entries(g.childOf).forEach(([c, p]) => (kids[p] = kids[p] || []).push(c));
43
+ const out = new Set();
44
+ const stack = [...ids];
45
+ while (stack.length) {
46
+ const n = stack.pop();
47
+ if (out.has(n)) continue;
48
+ out.add(n);
49
+ stack.push(...(kids[n] || []));
50
+ }
51
+ return out;
52
+ }
53
+
54
+ function focusSet(level, focus) {
55
+ if (!focus) return new Set();
56
+ if (clusterById[focus]) {
57
+ const starts = new Set([focus, ...clusterById[focus].members]);
58
+ if (level === "context") clusterById[focus].members.forEach(m => domainById[m].contexts.forEach(c => starts.add(c)));
59
+ return starts;
60
+ }
61
+ if (level === "domain") return new Set([owner[focus]]);
62
+ if (isMulti(focus)) return new Set([focus, ...domainById[focus].contexts]);
63
+ return new Set([focus]);
64
+ }
65
+
66
+ function walk(g, starts, depth, dir, hidden) {
67
+ const allowed = n => starts.has(n) || !hidden.has(n);
68
+ if (!starts.size) return new Set([...g.nodes].filter(allowed));
69
+ const up = {}, down = {};
70
+ g.nodes.forEach(n => { up[n] = []; down[n] = []; });
71
+ g.edges.forEach(e => { up[e.from].push(e.to); down[e.to].push(e.from); });
72
+ const seen = new Set(starts);
73
+ const go = next => {
74
+ let frontier = [...starts];
75
+ for (let i = 0; i < depth && frontier.length; i++) {
76
+ const nf = [];
77
+ // Reaching a container reaches what it holds, at the same hop.
78
+ frontier.forEach(n => (next[n] || []).forEach(m => descendants(g, [m]).forEach(r => {
79
+ if (!seen.has(r) && allowed(r)) { seen.add(r); nf.push(r); }
80
+ })));
81
+ frontier = nf;
82
+ }
83
+ };
84
+ if (dir !== "down") go(up);
85
+ if (dir !== "up") go(down);
86
+ return seen;
87
+ }
88
+
89
+ function relations(id) {
90
+ const g = isMulti(id) ? GRAPHS.domain : GRAPHS.context;
91
+ const byName = (a, b) => nameOf(a).localeCompare(nameOf(b));
92
+ return {
93
+ ups: g.edges.filter(e => e.from === id).map(e => e.to).sort(byName),
94
+ downs: g.edges.filter(e => e.to === id).map(e => e.from).sort(byName),
95
+ };
96
+ }
97
+
98
+ const $ = id => document.getElementById(id);
99
+ const levelSel = $("level"), focusSel = $("focus"), depthSel = $("depth"), dirSel = $("dir");
100
+
101
+ (function fillFocus() {
102
+ const byName = (a, b) => a.name.localeCompare(b.name);
103
+ const group = (label, items) => {
104
+ if (!items.length) return;
105
+ const og = document.createElement("optgroup");
106
+ og.label = label;
107
+ items.sort(byName).forEach(i => og.appendChild(new Option(i.name, i.id)));
108
+ focusSel.appendChild(og);
109
+ };
110
+ group("Domains", [...MODEL.domains]);
111
+ group("Clusters", [...MODEL.clusters]);
112
+ // One group per domain, so a context sits next to the others of its domain.
113
+ [...MODEL.domains].sort(byName).forEach(d =>
114
+ group(`Bounded contexts: ${d.name}`, MODEL.contexts.filter(c => c.domain === d.id && !domainById[c.id])));
115
+ })();
116
+
117
+ const DIAGRAM = MODEL.mermaid;
118
+
119
+ // A container first renders as the view says, else as its own `defaultDisplay` says, else expanded.
120
+ const defaultDisplayOf = id => (clusterById[id] || domainById[id]).defaultDisplay || "expanded";
121
+ const STYLE_HIDE = DIAGRAM.hide || [];
122
+ const defaultMode = id => {
123
+ const d = DIAGRAM.display;
124
+ if (d === null || d === undefined) return defaultDisplayOf(id);
125
+ return typeof d === "object" ? (d[id] || defaultDisplayOf(id)) : d;
126
+ };
127
+ const MODES = [["expanded", "Expanded"], ["collapsed", "Collapsed"], ["contents", "Contents"], ["hidden", "Hidden"]];
128
+ const displaySels = {};
129
+ const toggles = {}; // a single domain — one plain box — as a hide/show toggle
130
+ // The panel's "Show & hide" form, built once so it survives the re-renders of
131
+ // the focus info above it: one row per domain and cluster, grouped by kind.
132
+ // A multi-context domain or a cluster gets a display select; a single domain
133
+ // gets a hide/show toggle instead, driven by `hiddenContexts` like the list
134
+ // toggles below.
135
+ function buildControls() {
136
+ const nameSpan = name => {
137
+ const span = document.createElement("span");
138
+ span.className = "name";
139
+ span.textContent = name;
140
+ return span;
141
+ };
142
+ const addSelect = (id, name, cluster) => {
143
+ const label = document.createElement("label");
144
+ label.className = "row";
145
+ const span = nameSpan(name);
146
+ if (cluster) span.classList.add("cluster"); // a dashed underline, like the box it controls
147
+ label.appendChild(span);
148
+ const sel = document.createElement("select");
149
+ MODES.forEach(([value, text]) => sel.appendChild(new Option(text, value)));
150
+ label.appendChild(sel);
151
+ $("display").appendChild(label);
152
+ displaySels[id] = sel;
153
+ };
154
+ const addToggle = (id, name) => {
155
+ const label = document.createElement("label");
156
+ label.className = "row";
157
+ label.appendChild(nameSpan(name));
158
+ const hint = document.createElement("span");
159
+ hint.className = "hint";
160
+ const link = document.createElement("a");
161
+ link.className = "toggle";
162
+ label.append(hint, link);
163
+ $("display").appendChild(label);
164
+ toggles[id] = { row: label, hint, link };
165
+ };
166
+ const sub = text => {
167
+ const h = document.createElement("h4");
168
+ h.textContent = text;
169
+ $("display").appendChild(h);
170
+ };
171
+ sub("Domains");
172
+ MODEL.domains.forEach(d => (d.single ? addToggle : addSelect)(d.id, d.name));
173
+ sub("Clusters");
174
+ MODEL.clusters.forEach(c => addSelect(c.id, c.name, true));
175
+ }
176
+ buildControls();
177
+ const displayModes = () => Object.fromEntries(Object.entries(displaySels).map(([id, sel]) => [id, sel.value]));
178
+ // What `hide` in the style file keeps out, of any kind: a bounded context, a
179
+ // domain, a cluster member — and what `defaultDisplay: hidden` starts with,
180
+ // which only a bounded context or a single domain can be: one box with nothing
181
+ // inside. The toggles below hide the same way, so a hidden item stays hidden
182
+ // even where focus would open it. A container's select mirrors the hide while
183
+ // it holds, and takes over the moment it is touched.
184
+ const DEFAULT_HIDE = new Set([...STYLE_HIDE,
185
+ ...MODEL.contexts.filter(c => c.defaultDisplay === "hidden").map(c => c.id)]);
186
+ const hiddenContexts = new Set(DEFAULT_HIDE);
187
+
188
+ // The form's toggle links are static DOM: on every render, refresh what each
189
+ // one says and does — a hidden item's link shows it, a visible one's hides it.
190
+ const refreshToggles = () => Object.entries(toggles).forEach(([id, t]) => {
191
+ const hidden = hiddenContexts.has(id);
192
+ t.link.removeAttribute("data-hide");
193
+ t.link.removeAttribute("data-show");
194
+ t.link.setAttribute(hidden ? "data-show" : "data-hide", id);
195
+ t.link.textContent = hidden ? "show" : "hide";
196
+ t.hint.textContent = hidden ? "(hidden)" : "";
197
+ t.row.classList.toggle("off", hidden);
198
+ });
199
+ if (DIAGRAM.elk) {
200
+ const { default: elkLayouts } = await import("https://cdn.jsdelivr.net/npm/@mermaid-js/layout-elk@0/dist/mermaid-layout-elk.esm.min.mjs");
201
+ mermaid.registerLayoutLoaders(elkLayouts);
202
+ }
203
+ mermaid.initialize({ startOnLoad: false, securityLevel: "loose" });
204
+
205
+ function state() {
206
+ return { level: levelSel.value, focus: focusSel.value, depth: Number(depthSel.value), dir: dirSel.value,
207
+ display: displayModes(), hide: new Set(hiddenContexts) };
208
+ }
209
+
210
+ function readHash() {
211
+ const p = new URLSearchParams(location.hash.slice(1));
212
+ levelSel.value = p.get("level") === "domain" ? "domain" : "context";
213
+ focusSel.value = p.has("focus") && (owner[p.get("focus")] || clusterById[p.get("focus")]) ? p.get("focus") : "";
214
+ depthSel.value = p.get("depth") || "1";
215
+ dirSel.value = p.get("dir") || "both";
216
+ Object.entries(displaySels).forEach(([id, sel]) => {
217
+ const m = p.get("display." + id);
218
+ sel.value = MODES.some(([value]) => value === m) ? m : defaultMode(id);
219
+ });
220
+ hiddenContexts.clear();
221
+ DEFAULT_HIDE.forEach(id => hiddenContexts.add(id));
222
+ for (const [key, value] of p) {
223
+ const m = /^hide\.(.+)$/.exec(key);
224
+ if (!m) continue;
225
+ if (value === "1") hiddenContexts.add(m[1]);
226
+ else if (value === "0") hiddenContexts.delete(m[1]);
227
+ }
228
+ Object.entries(displaySels).forEach(([id, sel]) => { if (hiddenContexts.has(id)) sel.value = "hidden"; });
229
+ }
230
+
231
+ function hashFor(s) {
232
+ const p = new URLSearchParams();
233
+ if (s.level !== "context") p.set("level", s.level);
234
+ if (s.focus) p.set("focus", s.focus);
235
+ if (s.depth !== 1) p.set("depth", s.depth);
236
+ if (s.dir !== "both") p.set("dir", s.dir);
237
+ Object.entries(s.display).forEach(([id, mode]) => { if (mode !== defaultMode(id) && !s.hide.has(id)) p.set("display." + id, mode); });
238
+ s.hide.forEach(id => { if (!DEFAULT_HIDE.has(id)) p.set("hide." + id, "1"); });
239
+ DEFAULT_HIDE.forEach(id => { if (!s.hide.has(id)) p.set("hide." + id, "0"); });
240
+ const h = p.toString();
241
+ return h ? "#" + h : "";
242
+ }
243
+
244
+ // Same diagram as mermaid.py's render(): keep the two in step.
245
+ const nodeId = id => id.replace(/\W/g, "_");
246
+ const quote = value => value.replaceAll('"', "#quot;");
247
+ // A node id in the page is an id with every non-word character turned into an
248
+ // underscore, so `audit-log` reaches the DOM as `audit_log`. This maps back to the
249
+ // item, for the places that read ids out of the SVG.
250
+ const itemOfNode = {};
251
+ [...MODEL.contexts, ...MODEL.domains, ...MODEL.clusters].forEach(item => itemOfNode[nodeId(item.id)] = item);
252
+
253
+ function mermaidSource(s) {
254
+ const g = GRAPHS[s.level];
255
+ let starts = focusSet(s.level, s.focus);
256
+ const containers = [...MODEL.domains.filter(d => !d.single), ...MODEL.clusters];
257
+ const state = {};
258
+ containers.forEach(d => state[d.id] = starts.has(d.id) ? "expanded" : (s.display[d.id] || defaultMode(d.id)));
259
+ // What isn't shown: containers set to hidden, with everything inside them,
260
+ // plus the bounded contexts hidden here. The focus itself is never hidden.
261
+ const hidden = descendants(g, [...containers.filter(d => state[d.id] === "hidden").map(d => d.id), ...s.hide]);
262
+ if (s.focus) {
263
+ starts = new Set([...starts].filter(id => !hidden.has(id)));
264
+ starts.add(s.focus);
265
+ if (s.level === "domain" && owner[s.focus]) starts.add(owner[s.focus]);
266
+ }
267
+ const visible = walk(g, starts, s.depth, s.dir, hidden);
268
+
269
+ const kidsOf = {};
270
+ Object.entries(g.childOf).forEach(([c, p]) => (kidsOf[p] = kidsOf[p] || []).push(c));
271
+ const held = d => (s.level === "context" ? (d.contexts || []).filter(c => c !== d.id) : [])
272
+ .concat((d.members || []).filter(m => m !== d.id));
273
+ const folded = {}; // node -> the collapsed container it folded into
274
+ const fold = (cid, into) => (kidsOf[cid] || []).forEach(child => {
275
+ if (starts.has(child)) return;
276
+ if (visible.has(child)) folded[child] = into;
277
+ fold(child, into);
278
+ });
279
+ const collapsedAncestor = cid => {
280
+ let p = g.childOf[cid];
281
+ while (p) { if (state[p] === "collapsed") return true; p = g.childOf[p]; }
282
+ return false;
283
+ };
284
+ containers.filter(d => state[d.id] === "collapsed" && !collapsedAncestor(d.id)).forEach(d => fold(d.id, d.id));
285
+
286
+ const lines = ["---", JSON.stringify({ config: DIAGRAM.config }), "---", `flowchart ${DIAGRAM.direction}`];
287
+ Object.entries(MODEL.types).forEach(([id, t]) =>
288
+ lines.push(` classDef ${id} ${t.mermaid_style}`));
289
+ lines.push(` classDef focus ${DIAGRAM.focus_style}`);
290
+
291
+ const plainNodes = [];
292
+ const drawn = new Set();
293
+ const box = (id, label, type, indent) => {
294
+ plainNodes.push(id);
295
+ drawn.add(id);
296
+ return `${indent}${nodeId(id)}["${label}"]${type ? ":::" + type : ""}`;
297
+ };
298
+
299
+ const drawChild = (c, indent) => {
300
+ if (contextById[c] && !domainById[c]) {
301
+ const out = [box(c, quote(contextById[c].name), contextById[c].type, indent)];
302
+ if (contextById[c].mermaid_style) out.push(`${indent}style ${nodeId(c)} ${contextById[c].mermaid_style}`);
303
+ return out;
304
+ }
305
+ return drawEntry(domainById[c], indent);
306
+ };
307
+
308
+ function drawEntry(d, indent) {
309
+ if (folded[d.id] || (hidden.has(d.id) && d.id !== s.focus)) return [];
310
+ const st = state[d.id] || "expanded";
311
+ if (st === "hidden") return [];
312
+ const isCluster = !domainById[d.id];
313
+ const kids = held(d);
314
+ const foldedKids = kids.filter(c => folded[c] === d.id);
315
+ const inside = kids.filter(c => visible.has(c) && !folded[c]);
316
+ if (isCluster) {
317
+ // A cluster isn't a node in the walk: it draws only through what it holds.
318
+ // The focus is the exception: with everything it holds hidden, it is
319
+ // still one box of its own.
320
+ if (st === "collapsed" && !foldedKids.length) return [];
321
+ if (st === "expanded" && !inside.length && d.id !== s.focus) return [];
322
+ } else if (!visible.has(d.id) && !foldedKids.length && !inside.length && !kids.some(c => starts.has(c))) {
323
+ // Not reached itself, holds nothing that was reached, and no start
324
+ // either. A domain that holds one of those — the focus of a
325
+ // bounded-context view, or a context reached on its own — still draws,
326
+ // so it has a box to appear in.
327
+ return [];
328
+ }
329
+ if (st === "contents") return inside.flatMap(c => drawChild(c, indent));
330
+ if (st === "expanded" && kids.length) {
331
+ let show = inside;
332
+ if (!show.length) {
333
+ // Nothing inside was reached: show what is there that isn't hidden.
334
+ // A start is never hidden, not even by `hide`.
335
+ show = kids.filter(c => !folded[c] && (!hidden.has(c) || starts.has(c)));
336
+ }
337
+ if (show.length) {
338
+ drawn.add(d.id);
339
+ const out = [`${indent}subgraph ${nodeId(d.id)}["${quote(d.name)}"]`];
340
+ show.forEach(c => out.push(...drawChild(c, indent + " ")));
341
+ out.push(`${indent}end`);
342
+ let style = d.mermaid_style || DIAGRAM.domain_style;
343
+ if (d.id === s.focus) style += "," + DIAGRAM.focus_style;
344
+ out.push(`${indent}style ${nodeId(d.id)} ${style}`);
345
+ return out;
346
+ }
347
+ }
348
+ let label = quote(d.name);
349
+ if (DIAGRAM.child_labels && !d.single) {
350
+ const named = (d.contexts || []).filter(c => c !== d.id && !hidden.has(c)).map(nameOf)
351
+ .concat((d.members || []).filter(m => folded[m] === d.id).map(nameOf)).sort();
352
+ if (named.length) label = `<b>${label}</b><br/>${quote(named.join(", "))}`;
353
+ }
354
+ const out = [box(d.id, label, d.type, indent)];
355
+ if (d.mermaid_style) out.push(`${indent}style ${nodeId(d.id)} ${d.mermaid_style}`);
356
+ return out;
357
+ }
358
+
359
+ // A domain drawn inside another container comes from that container's own drawing.
360
+ const opens = new Set(Object.entries(state).filter(([, st]) => st === "expanded" || st === "contents")
361
+ .map(([id]) => id).filter(id => (domainById[id] ? visible.has(id) : true) && !folded[id] && !hidden.has(id)));
362
+ const atTop = d => !opens.has(g.childOf[d.id]);
363
+
364
+ if (DIAGRAM.group_by_type) {
365
+ Object.entries(MODEL.types).forEach(([typeId, t]) => {
366
+ const block = MODEL.domains.filter(d => d.type === typeId && atTop(d)).flatMap(d => drawEntry(d, " "));
367
+ if (block.length) lines.push(` subgraph type_${typeId}["${quote(t.name)}"]`, ...block, " end");
368
+ });
369
+ } else {
370
+ MODEL.domains.filter(atTop).forEach(d => lines.push(...drawEntry(d, " ")));
371
+ MODEL.clusters.forEach(c => lines.push(...drawEntry(c, " ")));
372
+ }
373
+
374
+ // Merge edges that land on the same pair once containers are collapsed.
375
+ const target = n => folded[n] || n;
376
+ const foldedBoxes = new Set(Object.values(folded));
377
+ const merged = new Map();
378
+ g.edges.forEach(e => {
379
+ if (!visible.has(e.from) || !visible.has(e.to)) return;
380
+ const a = target(e.from), b = target(e.to);
381
+ if (a === b || !drawn.has(a) || !drawn.has(b)) return;
382
+ const key = a + "|" + b;
383
+ if (!merged.has(key)) merged.set(key, { a, b, styles: new Set(), names: new Set(), via: [] });
384
+ const entry = merged.get(key);
385
+ e.styles.forEach(x => entry.styles.add(x));
386
+ entry.via.push(...e.via);
387
+ if (foldedBoxes.has(b)) entry.names.add(nameOf(e.to));
388
+ else if (foldedBoxes.has(a)) entry.names.add(nameOf(e.from));
389
+ });
390
+ const byStyle = {};
391
+ // What the tooltip of each arrow says, in the order the arrows are drawn.
392
+ const tips = [...merged.values()].map(entry => entry.via.filter(e => e.comment).map(tipEntry));
393
+ [...merged.values()].forEach((entry, i) => {
394
+ const style = entry.styles.has("solid") ? "solid" : [...entry.styles].sort()[0];
395
+ const label = entry.names.size && DIAGRAM.child_labels ? `|"${quote([...entry.names].sort().join(", "))}"|` : "";
396
+ lines.push(` ${nodeId(entry.a)} ${style === "dotted" ? "-.->" : "-->"}${label} ${nodeId(entry.b)}`);
397
+ (byStyle[style] = byStyle[style] || []).push(i);
398
+ });
399
+ Object.entries(byStyle).forEach(([style, indexes]) =>
400
+ lines.push(` linkStyle ${indexes.join(",")} interpolate ${DIAGRAM.curve} ${DIAGRAM.edge_styles[style] || ""}`.trimEnd()));
401
+
402
+ const highlight = s.level === "domain" && owner[s.focus] ? owner[s.focus] : s.focus;
403
+ plainNodes.forEach(n => { if (n === highlight) lines.push(` class ${nodeId(n)} focus`); });
404
+ // Clicking a box focuses on it, a cluster included. A plain link keeps back and forward working.
405
+ plainNodes.forEach(n => { if (owner[n] || clusterById[n]) lines.push(` click ${nodeId(n)} href "${hashFor({ ...s, focus: n })}"`); });
406
+ return { source: lines.join("\n") + "\n", visible, tips };
407
+ }
408
+
409
+ let renderCount = 0;
410
+ async function render() {
411
+ const s = state();
412
+ history.replaceState(null, "", hashFor(s) || location.pathname);
413
+ const { source, visible, tips } = mermaidSource(s);
414
+ renderInfo(s, visible);
415
+ refreshToggles();
416
+ const run = ++renderCount;
417
+ try {
418
+ const { svg, bindFunctions } = await mermaid.render("diagram-" + run, source);
419
+ if (run !== renderCount) return; // a newer render took over
420
+ $("graph").innerHTML = svg;
421
+ bindFunctions?.($("graph"));
422
+ const svgEl = $("graph").querySelector("svg");
423
+ bindEdgeHover(svgEl, tips);
424
+ bindBoxTips(svgEl);
425
+ } catch (err) {
426
+ if (run === renderCount) $("graph").textContent = "Could not render this diagram: " + err.message;
427
+ }
428
+ }
429
+
430
+ // Boxes are g elements whose id holds our node id: "<svg>-flowchart-<id>-<n>", or "<svg>-<id>" for a domain.
431
+ function boxMapOf(svg) {
432
+ const boxes = {};
433
+ svg.querySelectorAll("g[id]").forEach(g => {
434
+ const m = /-flowchart-(.+)-\d+$/.exec(g.id);
435
+ if (m) boxes[m[1]] = g;
436
+ else if (g.classList.contains("cluster")) boxes[g.id.slice(svg.id.length + 1)] = g;
437
+ });
438
+ Object.values(boxes).forEach(g => g.classList.add("box"));
439
+ return boxes;
440
+ }
441
+
442
+ // Hovering an arrow highlights it, its arrowhead and the two boxes it joins, and shows its comments.
443
+ function bindEdgeHover(svg, tips) {
444
+ if (!svg) return;
445
+ const paths = [...svg.querySelectorAll("path.flowchart-link")];
446
+ const labels = [...svg.querySelectorAll(".edgeLabel")];
447
+ const boxes = boxMapOf(svg);
448
+ // An edge id reads "<svg>-L_<from>_<to>_<n>". Ids may hold underscores, so match against the boxes we know.
449
+ const endsOf = path => {
450
+ const rest = path.id.slice(svg.id.length + 3).replace(/_\d+$/, "");
451
+ for (const from of Object.keys(boxes)) {
452
+ if (rest.startsWith(from + "_") && boxes[rest.slice(from.length + 1)]) return [boxes[from], boxes[rest.slice(from.length + 1)]];
453
+ }
454
+ return [];
455
+ };
456
+ const markers = {}; // an accent copy of each arrowhead, made on demand
457
+ const accentMarker = ref => {
458
+ const m = /url\(["']?#([^"')]+)["']?\)/.exec(ref || "");
459
+ const src = m && svg.getElementById(m[1]);
460
+ if (!src) return null;
461
+ if (!markers[m[1]]) {
462
+ const copy = src.cloneNode(true);
463
+ copy.id = m[1] + "-on";
464
+ copy.querySelectorAll("*").forEach(n => { n.style.fill = "var(--accent)"; n.style.stroke = "var(--accent)"; });
465
+ src.parentNode.appendChild(copy);
466
+ markers[m[1]] = copy.id;
467
+ }
468
+ return `url(#${markers[m[1]]})`;
469
+ };
470
+
471
+ paths.forEach((path, i) => {
472
+ const hit = path.cloneNode(false);
473
+ hit.removeAttribute("id");
474
+ hit.removeAttribute("style");
475
+ hit.removeAttribute("marker-end");
476
+ hit.removeAttribute("marker-start");
477
+ hit.setAttribute("class", "edge-hit");
478
+ path.after(hit);
479
+
480
+ const ends = endsOf(path);
481
+ const label = labels[i]; // mermaid draws labels in edge order
482
+ const marker = path.getAttribute("marker-end");
483
+
484
+ const note = tips[i] || [];
485
+ hit.addEventListener("mousemove", ev => showTip(note, ev));
486
+ hit.addEventListener("mouseenter", ev => {
487
+ showTip(note, ev);
488
+ svg.classList.add("hovering");
489
+ path.classList.add("edge-on");
490
+ label?.classList.add("edge-on");
491
+ ends.forEach(n => n.classList.add("node-on"));
492
+ const on = accentMarker(marker);
493
+ if (on) path.setAttribute("marker-end", on);
494
+ });
495
+ hit.addEventListener("mouseleave", () => {
496
+ hideTip();
497
+ svg.classList.remove("hovering");
498
+ path.classList.remove("edge-on");
499
+ label?.classList.remove("edge-on");
500
+ ends.forEach(n => n.classList.remove("node-on"));
501
+ if (marker) path.setAttribute("marker-end", marker);
502
+ });
503
+ });
504
+ }
505
+
506
+ // Hovering a box shows its description, when it has one. The domain box that
507
+ // wraps several bounded contexts is left out: there it is only background, and
508
+ // its description has its own place on the domain view.
509
+ function bindBoxTips(svg) {
510
+ const boxes = Object.entries(boxMapOf(svg)).filter(([, g]) => !g.classList.contains("cluster"));
511
+ const innermost = target => {
512
+ let best = null;
513
+ boxes.forEach(([id, g]) => {
514
+ if (g.contains(target) && (!best || best.g.contains(g))) best = { id, g };
515
+ });
516
+ return best ? best.id : null;
517
+ };
518
+ svg.addEventListener("mousemove", ev => {
519
+ if (ev.target.closest(".edge-hit")) return; // the arrow's own tooltip owns the pointer here
520
+ const item = itemOfNode[innermost(ev.target) || ""];
521
+ if (item?.description) showTip([{ title: item.name, text: item.description }], ev);
522
+ else hideTip();
523
+ });
524
+ svg.addEventListener("pointerleave", hideTip);
525
+ }
526
+
527
+ // One entry of a tooltip: always a title (who depends on whom) and a description (the comment).
528
+ // An arrow can stand for several dependencies, so every entry names its own.
529
+ const tipEntry = dep => ({ title: `${nameOf(dep.from)} → ${nameOf(dep.to)}`, text: dep.comment });
530
+
531
+ // A small box that follows the cursor. Comments come from context-map.yml, so they go in as text, never as HTML.
532
+ const tip = document.createElement("div");
533
+ tip.id = "tip";
534
+ document.body.appendChild(tip);
535
+ function showTip(note, ev) {
536
+ if (!note.length) return hideTip();
537
+ tip.replaceChildren(...note.map(n => {
538
+ const p = document.createElement("p");
539
+ const title = document.createElement("b");
540
+ title.textContent = n.title;
541
+ p.append(title, document.createElement("br"), n.text);
542
+ return p;
543
+ }));
544
+ tip.style.display = "block";
545
+ const x = Math.min(ev.clientX + 14, window.innerWidth - tip.offsetWidth - 8);
546
+ const y = ev.clientY + 18 + tip.offsetHeight > window.innerHeight ? ev.clientY - tip.offsetHeight - 12 : ev.clientY + 18;
547
+ tip.style.left = Math.max(8, x) + "px";
548
+ tip.style.top = Math.max(8, y) + "px";
549
+ }
550
+ const hideTip = () => { tip.style.display = "none"; };
551
+
552
+ const escapeHtml = t => t.replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[c]));
553
+ const link = id => `<a data-go="${id}">${escapeHtml(nameOf(id))}</a>`;
554
+ const list = ids => ids.length ? "<ul>" + ids.map(id => `<li>${link(id)}</li>`).join("") + "</ul>" : '<p class="hint">None</p>';
555
+ const chip = type => { const t = MODEL.types[type]; return `<span class="chip" style="background:${t.fill};border-color:${t.stroke}">${t.name}</span>`; };
556
+
557
+ function renderInfo(s, visible) {
558
+ const legend = '<div class="legend">' + Object.keys(MODEL.types).map(chip).join("") + "</div>";
559
+ const howTo = `<h3>How to read</h3><p class="hint">An arrow from A to B means A depends on B. Upstream is at the top. The outer boxes are domains, the coloured boxes inside them are bounded contexts. The Show &amp; hide form in the right panel has a control for every domain and cluster: a multi-context domain or a cluster can be expanded (the boxes inside it), collapsed (one box), or hidden — a dashed underline marks a cluster's — and a one-box domain hides with a toggle. Focusing on a domain or a cluster lists what it holds, and each item there can be hidden too. Click a box to focus on it.</p>`;
560
+ if (!s.focus) {
561
+ $("info").innerHTML = `<h2>Whole system</h2><p class="hint">${visible.size} items shown. Pick a domain, cluster or bounded context to see only what it touches.</p>
562
+ <p><a href="mermaid/index.html">Static diagrams</a></p>${legend}${howTo}`;
563
+ return;
564
+ }
565
+ const id = s.focus;
566
+ const item = contextById[id] || domainById[id] || clusterById[id];
567
+ const itemWith = item_id => {
568
+ const hiddenHere = s.hide.has(item_id);
569
+ const toggle = `<a data-${hiddenHere ? "show" : "hide"}="${item_id}" class="toggle">${hiddenHere ? "show" : "hide"}</a>`;
570
+ return `<li>${link(item_id)} ${hiddenHere ? `<span class="hint">(hidden)</span> ` : ""}${toggle}</li>`;
571
+ };
572
+ let html;
573
+ if (clusterById[id]) {
574
+ html = `<h2>${escapeHtml(item.name)}</h2><span class="hint">Cluster</span>
575
+ <p><a href="mermaid/index.html">All diagrams</a></p>
576
+ <h3>Members</h3><ul>${item.members.map(itemWith).join("")}</ul>`;
577
+ } else {
578
+ html = `<h2>${escapeHtml(item.name)}</h2>${chip(item.type)} <span class="hint">${domainById[id] ? "Domain" : "Bounded context"}</span>
579
+ <p><a href="mermaid/${id}.html">Static diagram</a> · <a href="mermaid/index.html">All diagrams</a></p>`;
580
+ if (item.description) html += `<p class="hint">${escapeHtml(item.description)}</p>`;
581
+ if (isMulti(id)) {
582
+ html += `<h3>Bounded contexts</h3><ul>${domainById[id].contexts.map(itemWith).join("")}</ul>`;
583
+ } else if (isMulti(owner[id])) {
584
+ html += `<h3>Domain</h3>${list([owner[id]])}`;
585
+ }
586
+ const r = relations(id);
587
+ html += `<h3>Depends on (${r.ups.length})</h3>${list(r.ups)}<h3>Used by (${r.downs.length})</h3>${list(r.downs)}`;
588
+ if (!isMulti(id) && isMulti(owner[id])) {
589
+ const own = GRAPHS.context.edges;
590
+ const dUps = own.filter(e => e.from === owner[id]).map(e => e.to);
591
+ const dDowns = own.filter(e => e.to === owner[id]).map(e => e.from);
592
+ const pname = escapeHtml(nameOf(owner[id]));
593
+ if (dUps.length) html += `<h3>${pname} as a whole depends on</h3>${list(dUps)}`;
594
+ if (dDowns.length) html += `<h3>${pname} as a whole is used by</h3>${list(dDowns)}`;
595
+ }
596
+ }
597
+ $("info").innerHTML = html + `<h3>Legend</h3>${legend}${howTo}`;
598
+ }
599
+
600
+ const setHidden = (id, hide) => {
601
+ hiddenContexts[hide ? "add" : "delete"](id);
602
+ if (displaySels[id]) displaySels[id].value = hide ? "hidden" : defaultMode(id);
603
+ };
604
+ $("panel").addEventListener("click", ev => {
605
+ const hideToggle = ev.target.closest("[data-hide]");
606
+ if (hideToggle) { setHidden(hideToggle.dataset.hide, true); render(); return; }
607
+ const showToggle = ev.target.closest("[data-show]");
608
+ if (showToggle) { setHidden(showToggle.dataset.show, false); render(); return; }
609
+ const go = ev.target.closest("[data-go]");
610
+ if (go) { focusSel.value = go.dataset.go; render(); }
611
+ });
612
+ [levelSel, focusSel, depthSel, dirSel].forEach(el => el.addEventListener("change", render));
613
+ // A container's select takes over from a hide the moment it is touched.
614
+ Object.entries(displaySels).forEach(([id, sel]) => sel.addEventListener("change", () => {
615
+ hiddenContexts.delete(id);
616
+ render();
617
+ }));
618
+ $("reset").addEventListener("click", () => { history.replaceState(null, "", location.pathname); readHash(); render(); });
619
+ window.addEventListener("hashchange", () => { readHash(); render(); });
620
+
621
+ readHash();
622
+ render();
@@ -0,0 +1,50 @@
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">
6
+ <title>Context Map</title>
7
+ <style>
8
+ /*__STYLE__*/
9
+ </style>
10
+ </head>
11
+ <body>
12
+ <header>
13
+ <h1>Context Map</h1>
14
+ <label>Show
15
+ <select id="level">
16
+ <option value="context">Bounded contexts</option>
17
+ <option value="domain">Domains</option>
18
+ </select>
19
+ </label>
20
+ <label>Focus
21
+ <select id="focus"><option value="">Whole system</option></select>
22
+ </label>
23
+ <label>Depth
24
+ <select id="depth">
25
+ <option value="1">1 hop</option>
26
+ <option value="2">2 hops</option>
27
+ <option value="99">All</option>
28
+ </select>
29
+ </label>
30
+ <label>Direction
31
+ <select id="dir">
32
+ <option value="both">Both</option>
33
+ <option value="up">Depends on (upstream)</option>
34
+ <option value="down">Used by (downstream)</option>
35
+ </select>
36
+ </label>
37
+ <button id="reset">Reset</button>
38
+ </header>
39
+ <main>
40
+ <div id="graph"></div>
41
+ <aside id="panel">
42
+ <div id="info"></div>
43
+ <div id="display"><h3>Show &amp; hide</h3></div>
44
+ </aside>
45
+ </main>
46
+ <script type="module">
47
+ /*__SCRIPT__*/
48
+ </script>
49
+ </body>
50
+ </html>