Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | 16x 73x 73x 36x 60x 37x 34x 2x 32x 28x 28x 28x 28x 35x 27x 27x 243x 225x 99x 99x 99x 99x 99x 99x 126x 126x 126x 113x 16x 6x 117x 11x 4x 8x 3x 3x 2x 9x 6x 4x 4x 2x 3x 34x 79x 72x 72x 9x 16x 85x 16x 81x 14x | // Editable model behind ConditionBuilder.vue: a condition tree (as stored in
// `transition.auto.when`) as nested groups of rows, each with a stable key for
// rendering and drag and drop.
// @implements UC-WORKFLOW-007.1
import type { WorkflowCondition, WorkflowConditionOperator } from '@/schemas/entity-schema'
/** "All of" (AND), "Any of" (OR), "None of" (NOT any) */
export type GroupMode = 'all' | 'any' | 'none'
export interface RowModel {
key: string
kind: 'row'
/** set for a row on the linked item (UC-WORKFLOW-008.1) */
via?: string
field: string
op: WorkflowConditionOperator
value?: unknown
}
export interface FormulaModel {
key: string
kind: 'formula'
formula: string
}
export interface GroupModel {
key: string
kind: 'group'
mode: GroupMode
children: NodeModel[]
}
export type NodeModel = RowModel | FormulaModel | GroupModel
let nextKey = 0
export function newKey(): string {
nextKey++
return `c${nextKey}`
}
function groupOf(mode: GroupMode, children: WorkflowCondition[]): GroupModel {
return { key: newKey(), kind: 'group', mode, children: children.map(toNode) }
}
function toNode(node: WorkflowCondition): NodeModel {
if ('all' in node) return groupOf('all', node.all)
if ('any' in node) return groupOf('any', node.any)
if ('not' in node) {
return 'any' in node.not ? groupOf('none', node.not.any) : groupOf('none', [node.not])
}
if ('formula' in node) return { key: newKey(), kind: 'formula', formula: node.formula }
const row: RowModel = { key: newKey(), kind: 'row', field: node.field, op: node.op }
if ('via' in node) row.via = node.via
if (node.value !== undefined) row.value = node.value
return row
}
/** The root group for a stored condition (none = an empty "All of" group) */
export function toModel(condition: WorkflowCondition | undefined): GroupModel {
if (!condition) return groupOf('all', [])
const node = toNode(condition)
return node.kind === 'group'
? node
: { key: newKey(), kind: 'group', mode: 'all', children: [node] }
}
function fromNode(node: NodeModel): WorkflowCondition | undefined {
if (node.kind === 'formula') return { formula: node.formula }
if (node.kind === 'row') {
const row: Record<string, unknown> = {}
if (node.via !== undefined) row.via = node.via
row.field = node.field
row.op = node.op
if (node.value !== undefined) row.value = node.value
return row as WorkflowCondition
}
const children = node.children
.map(fromNode)
.filter((child): child is WorkflowCondition => child !== undefined)
if (children.length === 0) return undefined
if (node.mode === 'all') return { all: children }
if (node.mode === 'any') return { any: children }
return { not: { any: children } }
}
/** The stored condition for a root group (undefined = no condition, always true) */
export function fromModel(root: GroupModel): WorkflowCondition | undefined {
return fromNode(root)
}
/** Finds a group by key, anywhere under `root` */
export function findGroup(root: GroupModel, key: string): GroupModel | undefined {
if (root.key === key) return root
for (const child of root.children) {
if (child.kind !== 'group') continue
const found = findGroup(child, key)
if (found) return found
}
return undefined
}
/** Removes a node by key, anywhere under `root` (the root itself stays) */
export function removeNode(root: GroupModel, key: string): void {
const index = root.children.findIndex((child) => child.key === key)
if (index !== -1) {
root.children.splice(index, 1)
return
}
root.children.forEach((child) => {
if (child.kind === 'group') removeNode(child, key)
})
}
/** Every row and formula row under `root` */
export function leaves(root: GroupModel): Array<RowModel | FormulaModel> {
return root.children.flatMap((child) => (child.kind === 'group' ? leaves(child) : [child]))
}
/**
* Formula text as the user reads it — `{fieldId}` shown as `{Field label}` —
* and back. A label matching no field (or several) is kept as typed, so the
* API reports it as an unknown field.
*/
export function formulaForDisplay(
formula: string,
labelOf: (fieldId: string) => string | undefined
): string {
return formula.replace(/\{([^}]*)\}/g, (match, id: string) => {
const label = labelOf(id.trim())
return label ? `{${label}}` : match
})
}
export function formulaForStorage(
text: string,
fields: Array<{ fieldId?: string; label: string }>
): string {
return text.replace(/\{([^}]*)\}/g, (match, label: string) => {
const wanted = label.trim().toLowerCase()
const byId = fields.find((f) => f.fieldId === label.trim())
if (byId) return `{${byId.fieldId}}`
const matches = fields.filter((f) => f.label.trim().toLowerCase() === wanted)
return matches.length === 1 ? `{${matches[0]!.fieldId}}` : match
})
}
|