diff --git a/bzGraflow.js b/bzGraflow.js index cbfacc7..55d8ea3 100644 --- a/bzGraflow.js +++ b/bzGraflow.js @@ -155,30 +155,24 @@ class BZgraflow extends Buildoz{ }) const layers = this.computeLayers(this.flow.nodes, parents) -// const maxLayers = Math.max(...layers.map(item=>item.length)) let maxHeight = 0 const layerHeights = [] + const indexes = {} // Todo: a for(const layer of layers){ let totHeight = 0 - for(const nid of layer){ + for(const [idx, nid] of layer.entries()){ const bb = this.stagedNodes[nid].getBoundingClientRect() totHeight += bb.height+gapy + indexes[nid] = idx } if(totHeight>maxHeight) maxHeight = totHeight layerHeights.push(totHeight) } + + this.reorderLayers(layers, parents, indexes) let x = gapx for(const [idx, layer] of layers.entries()){ - for(const nid of layer){ - // some parents can be in far layers, but at least one is in the prev layer (by definition of layer) - const localParent = parents[nid].find((nid => layers[idx-1].includes(nid))) - if(localParent){ //undefined for 1st node - const ports = this.stagedNodes[localParent].ports - console.log(`parent of ${nid} is ${localParent}`, ports) - } - } - let wMax = 0 let y = ((maxHeight - layerHeights[idx]) / 2) + gapy for(const nid of layer){ @@ -191,7 +185,32 @@ class BZgraflow extends Buildoz{ } } - moveNode(nid, destx, desty, duration = 200) { + reorderLayers(layers, parents, indexes){ + const swap = (vect, todo) => { + for(const s of todo){ + [vect[s[0]], vect[s[1]]] = [vect[s[1]], vect[s[0]]] + } + } + for(const [lidx, layer] of layers.entries()){ + if(lidx==0) continue + const toSwap = [] + for(let i=0; i layers[lidx-1].includes(nid))) + for(let j=i+1; j layers[lidx-1].includes(nid))) + if(((indexes[pnid1] - indexes[pnid2]) * (indexes[nid1] - indexes[nid2])) < 0) { // crossing ! + toSwap.push([i, j]) + } + } + } + swap(layer, toSwap) + } + } + + moveNode(nid, destx, desty, duration = 200, cb) { const t0 = performance.now() const bb = this.stagedNodes[nid].getBoundingClientRect() const x0=bb.x @@ -275,5 +294,3 @@ class BZgraflow extends Buildoz{ } Buildoz.define('graflow', BZgraflow) - -