AP1.13a: Read-only Gate-Graph aus Abhaengigkeiten.
Test Suite / lint-backend (push) Waiting to run
Test Suite / compose-smoke (push) Waiting to run
Test Suite / k6 /api/health Baseline (push) Blocked by required conditions
Test Suite / playwright-smoke (push) Blocked by required conditions
Deploy Development / deploy (push) Successful in 46s
Test Suite / pytest-backend (push) Has been cancelled

Initiative-API fuer Roadmap-Dependencies, SVG-Layout mit Liste/Graph-Umschalter auf der Zielzustaende-Seite.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-07-10 15:50:20 +02:00
1 parent 83fdb9a2a2
commit 7f03504c2d
10 files changed
+712 -2

No files matched your search

+194
View File
@@ -0,0 +1,194 @@
/** Layout-Konstanten für read-only Gate-Graph (AP1.13a). */
export const GATE_NODE_WIDTH = 200
export const GATE_NODE_HEIGHT = 72
export const GATE_LAYER_GAP_Y = 56
export const GATE_NODE_GAP_X = 48
export const GATE_GRAPH_PADDING = 32
/**
* @typedef {{ id: string, title?: string, status?: string, sort_order?: number }} GateNode
* @typedef {{ from_item_id: string, to_item_id: string, dependency_type: string }} GateDependency
* @typedef {{ id: string, x: number, y: number, width: number, height: number, item: GateNode }} PositionedGateNode
* @typedef {{ id: string, fromId: string, toId: string, dependency_type: string, x1: number, y1: number, x2: number, y2: number }} GateGraphEdge
*/
/**
* Kanten für Schichtung: requires = Voraussetzung → abhängiges Gate;
* blocks = Blocker → geblocktes Gate.
*
* @param {GateDependency[]} dependencies
* @returns {{ from: string, to: string, dependency_type: string }[]}
*/
export function buildLayoutEdges(dependencies) {
const edges = []
for (const dep of dependencies) {
if (dep.dependency_type === 'requires') {
edges.push({
from: dep.to_item_id,
to: dep.from_item_id,
dependency_type: dep.dependency_type,
})
} else if (dep.dependency_type === 'blocks') {
edges.push({
from: dep.from_item_id,
to: dep.to_item_id,
dependency_type: dep.dependency_type,
})
}
}
return edges
}
/**
* @param {GateNode[]} items
* @param {GateDependency[]} dependencies
* @returns {{ nodes: PositionedGateNode[], edges: GateGraphEdge[], width: number, height: number }}
*/
export function computeGateGraphLayout(items, dependencies) {
if (!items.length) {
return { nodes: [], edges: [], width: 0, height: 0 }
}
const sortedItems = [...items].sort(
(a, b) =>
(a.sort_order ?? 0) - (b.sort_order ?? 0) ||
(a.title || '').localeCompare(b.title || '') ||
a.id.localeCompare(b.id),
)
const layoutEdges = buildLayoutEdges(dependencies)
const layerById = new Map()
for (const item of sortedItems) {
layerById.set(item.id, 0)
}
const maxIterations = sortedItems.length + 1
for (let i = 0; i < maxIterations; i += 1) {
let changed = false
for (const edge of layoutEdges) {
const fromLayer = layerById.get(edge.from) ?? 0
const toLayer = layerById.get(edge.to) ?? 0
const nextLayer = fromLayer + 1
if (nextLayer > toLayer) {
layerById.set(edge.to, nextLayer)
changed = true
}
}
if (!changed) break
}
const layers = new Map()
for (const item of sortedItems) {
const layer = layerById.get(item.id) ?? 0
if (!layers.has(layer)) layers.set(layer, [])
layers.get(layer).push(item)
}
const layerIndices = [...layers.keys()].sort((a, b) => a - b)
const maxLayerWidth = Math.max(
...layerIndices.map((layer) => layers.get(layer).length),
1,
)
const nodes = []
for (const layer of layerIndices) {
const row = layers.get(layer)
const rowWidth =
row.length * GATE_NODE_WIDTH + Math.max(0, row.length - 1) * GATE_NODE_GAP_X
const canvasWidth =
maxLayerWidth * GATE_NODE_WIDTH +
Math.max(0, maxLayerWidth - 1) * GATE_NODE_GAP_X +
GATE_GRAPH_PADDING * 2
const startX = GATE_GRAPH_PADDING + Math.max(0, (canvasWidth - rowWidth) / 2 - GATE_GRAPH_PADDING)
row.forEach((item, index) => {
nodes.push({
id: item.id,
item,
width: GATE_NODE_WIDTH,
height: GATE_NODE_HEIGHT,
x: startX + index * (GATE_NODE_WIDTH + GATE_NODE_GAP_X),
y: GATE_GRAPH_PADDING + layer * (GATE_NODE_HEIGHT + GATE_LAYER_GAP_Y),
})
})
}
const nodeById = new Map(nodes.map((node) => [node.id, node]))
const edges = dependencies
.map((dep) => {
const sourceNode =
dep.dependency_type === 'requires'
? nodeById.get(dep.to_item_id)
: nodeById.get(dep.from_item_id)
const targetNode =
dep.dependency_type === 'requires'
? nodeById.get(dep.from_item_id)
: nodeById.get(dep.to_item_id)
if (!sourceNode || !targetNode) return null
const center = (node) => ({
x: node.x + node.width / 2,
y: node.y + node.height / 2,
})
if (dep.dependency_type === 'related') {
const from = center(sourceNode)
const to = center(targetNode)
return {
id: dep.id || `${dep.from_item_id}-${dep.to_item_id}-${dep.dependency_type}`,
fromId: dep.from_item_id,
toId: dep.to_item_id,
dependency_type: dep.dependency_type,
x1: from.x,
y1: from.y,
x2: to.x,
y2: to.y,
}
}
return {
id: dep.id || `${dep.from_item_id}-${dep.to_item_id}-${dep.dependency_type}`,
fromId: dep.from_item_id,
toId: dep.to_item_id,
dependency_type: dep.dependency_type,
x1: sourceNode.x + sourceNode.width / 2,
y1: sourceNode.y + sourceNode.height,
x2: targetNode.x + targetNode.width / 2,
y2: targetNode.y,
}
})
.filter(Boolean)
const width =
Math.max(...nodes.map((node) => node.x + node.width), GATE_NODE_WIDTH) + GATE_GRAPH_PADDING
const height =
Math.max(...nodes.map((node) => node.y + node.height), GATE_NODE_HEIGHT) + GATE_GRAPH_PADDING
return { nodes, edges, width, height }
}
/**
* @param {string} initiativeId
* @param {number} itemCount
* @returns {'list' | 'graph'}
*/
export function resolveGatesViewMode(initiativeId, itemCount) {
if (typeof window !== 'undefined' && initiativeId) {
const stored = window.localStorage.getItem(`kairo.plan.gates.view:${initiativeId}`)
if (stored === 'list' || stored === 'graph') {
return stored
}
}
return itemCount <= 5 ? 'list' : 'graph'
}
/**
* @param {string} initiativeId
* @param {'list' | 'graph'} mode
*/
export function storeGatesViewMode(initiativeId, mode) {
if (typeof window !== 'undefined' && initiativeId) {
window.localStorage.setItem(`kairo.plan.gates.view:${initiativeId}`, mode)
}
}
+34
View File
@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest'
import { buildLayoutEdges, computeGateGraphLayout } from './gateGraphLayout.js'
describe('gateGraphLayout', () => {
it('layers requires dependencies with prerequisite above dependent', () => {
const items = [
{ id: 'a', title: 'A', sort_order: 0 },
{ id: 'b', title: 'B', sort_order: 1 },
]
const dependencies = [
{ id: 'd1', from_item_id: 'a', to_item_id: 'b', dependency_type: 'requires' },
]
expect(buildLayoutEdges(dependencies)).toEqual([
{ from: 'b', to: 'a', dependency_type: 'requires' },
])
const layout = computeGateGraphLayout(items, dependencies)
const nodeA = layout.nodes.find((node) => node.id === 'a')
const nodeB = layout.nodes.find((node) => node.id === 'b')
expect(nodeB.y).toBeLessThan(nodeA.y)
})
it('places items without dependencies on one row by sort_order', () => {
const items = [
{ id: 'b', title: 'B', sort_order: 10 },
{ id: 'a', title: 'A', sort_order: 0 },
]
const layout = computeGateGraphLayout(items, [])
expect(layout.nodes.map((node) => node.id)).toEqual(['a', 'b'])
expect(layout.nodes[0].y).toBe(layout.nodes[1].y)
expect(layout.nodes[0].x).toBeLessThan(layout.nodes[1].x)
})
})