@@ -127,11 +127,45 @@ function updateWorkflow (workflow, gscan, options) {
127127 throw new Error ( `Updated node [${ workflow . id } ] not found in workflow lookup` )
128128 }
129129 mergeWith ( existingData . node , workflow , mergeWithCustomizer )
130- Vue . set ( gscan . lookup , existingData . id , existingData )
131- // FIXME: we need to sort its parent again!
130+ const hierarchical = options . hierarchical || true
131+ if ( hierarchical ) {
132+ updateHierarchicalWorkflow ( existingData , gscan . lookup , gscan . tree , options )
133+ }
132134 // TODO: create workflow hierarchy (from workflow object), then iterate
133135 // it and use lookup to fetch the existing node. Finally, combine
134136 // the gscan states (latestStateTasks & stateTotals).
137+ Vue . set ( gscan . lookup , existingData . id , existingData )
138+ }
139+
140+ function updateHierarchicalWorkflow ( existingData , lookup , tree , options ) {
141+ // We need to sort its parent again.
142+ const workflowNameParts = parseWorkflowNameParts ( existingData . id )
143+ const nodesIds = getWorkflowNamePartsNodesIds ( workflowNameParts )
144+ // Discard the last since it's the workflow ID that we already have
145+ // in the `existingData` object. Now if not empty, we have our parent.
146+ nodesIds . pop ( )
147+ const parentId = nodesIds . length > 0 ? nodesIds . pop ( ) : null
148+ const parent = parentId ? lookup [ parentId ] : tree
149+ if ( ! parent ) {
150+ throw new Error ( `Invalid orphan hierarchical node: ${ existingData . id } ` )
151+ }
152+ const siblings = parent . children
153+ // Where is this node at the moment?
154+ const currentIndex = siblings . findIndex ( node => node . id === existingData . id )
155+ // Where should it be now?
156+ const sortedIndex = sortedIndexBy (
157+ parent . children ,
158+ existingData ,
159+ ( n ) => n . name ,
160+ sortWorkflowNamePartNodeOrWorkflowNode
161+ )
162+ // If it is not where it is, we need to add it to its correct location.
163+ if ( currentIndex !== sortedIndex ) {
164+ // siblings.splice(currentIndex, 1)
165+ // siblings.splice(sortedIndex, 0, existingData)
166+ Vue . delete ( siblings , currentIndex )
167+ Vue . set ( siblings , sortedIndex , existingData )
168+ }
135169}
136170
137171// -- Pruned
0 commit comments