yet-another-bpmn-auto-layout 0.0.3 → 0.0.4

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.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import t from"bpmn-moddle";import{Graph as e,getLast as o,addToEnd as i}from"graph-by-ivan-tulaev";import{assign as n,map as r,pick as s}from"min-dash";function d(t){return a(t,"bpmn:SubProcess")||a(t,"bpmn:Task")?{width:100,height:80}:a(t,"bpmn:Gateway")?{width:50,height:50}:a(t,"bpmn:Event")?{width:36,height:36}:a(t,"bpmn:Participant")||a(t,"bpmn:Lane")?{width:400,height:100}:a(t,"bpmn:DataObjectReference")?{width:36,height:50}:a(t,"bpmn:DataStoreReference")?{width:50,height:50}:a(t,"bpmn:TextAnnotation")?{width:100,height:30}:{width:100,height:80}}function a(t,e){return t.$instanceOf(e)}function c(t){if(!t)throw new Error("Element is not defined in getOutgoingElements");const e=(t.outgoing||[]).map(t=>t.targetRef);return[...new Set(e)]}function g(t){return(a(t,"bpmn:IntermediateThrowEvent")||a(t,"bpmn:IntermediateCatchEvent"))&&(void 0===t.incoming||0===t.incoming.length)}const h=150,u=140;function l(t,e){return new Set(c(t).concat(function(t){const e=new Set;if(t){const o=(t.attachers||[]).sort((t,e)=>(e.outgoing?e.outgoing.length:0)-(t.outgoing?t.outgoing.length:0)).map(t=>(t.outgoing||[]).reverse()).flat().map(t=>t.targetRef).filter((t,e,o)=>o.indexOf(t)===e);for(const t of o)e.add(t)}return[...e]}(t)))}function f(t,e,o=!1,i){const{source:n,target:r}=t,s=n.di,a=r.di,g=s.get("bounds"),h=a.get("bounds"),f=p(g),x=p(h),y=e.getEdgeDirection(t),w=r.gridPosition.col-n.gridPosition.col,_=r.gridPosition.row-n.gridPosition.row,b=`${_>0?"bottom":"top"}-${w>0?"right":"left"}`,C=`${_>0?"top":"bottom"}-${w>0?"left":"right"}`,{x:F,y:P}=m(n,i),{x:S}=m(r,i),v="bpmn:BoundaryEvent"===n.$type;if("NO_DIRECTION"===y)return v?[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:S,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:F,y:x.y},E(x,h,"l",C)]:[E(f,g,"b",b),{x:f.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:S,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:F,y:x.y},E(x,h,"l",C)];if("S_N"===y){if(v)return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:F,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:S,y:x.y},E(x,h,"l",C)];const t=[];for(let o=n.gridPosition.row-1;o>r.gridPosition.row;o--){const i=e.get(o,n.gridPosition.col);i&&t.push(i)}const o=[...l(r)].includes(n);return t.length>0||o?[E(f,g,"l",b),{x:F,y:f.y},{x:S,y:x.y},E(x,h,"l",C)]:[E(f,g,"t",b),E(x,h,"b",C)]}if("SW_NE"===y)return v?[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},E(x,h,"b",C)]:[E(f,g,"r"),{x:x.x,y:f.y},E(x,h,"b")];if("W_E"===y){if(v)return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},E(x,h,"b",C)];const t=E(f,g,"r",b);n.isExpanded&&(t.y=g.y+d(n).height/2);const e=E(x,h,"l",C);return r.isExpanded&&(e.y=h.y+d(r).height/2),[t,e]}if("NW_SE"===y)return[E(f,g,"b"),{x:f.x,y:x.y},E(x,h,"l")];if("N_S"===y){if(v){const t=E(f,g,"b",b),e=E(x,h,"t",C);return n.attachedToRef.isExpanded?[t,{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},e]:[t,e]}const t=E(f,g,"b",b),e=E(x,h,"t",C);return n.isExpanded||r.isExpanded?[t,{x:f.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:x.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},e]:[t,e]}if("NE_SW"===y)return[E(f,g,"b"),{x:f.x,y:x.y},E(x,h,"r")];if("E_W"===y){if(v){const t=function(t,e,o){const[i,n]=o.find(t),[,r]=o.find(e),s=n<r?n:r,d=n<r?r:n,a=[...o.elements].map(t=>void 0!==t.size?[...t]:t).flat().filter(t=>t.gridPosition.row===i&&t.gridPosition.col>s&&t.gridPosition.col<d);return a.reduce((t,e)=>e.grid?.getGridDimensions()[0]>t?e.grid?.getGridDimensions()[0]:t,0)}(n,r,e);return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u+t*u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u+t*u},E(x,h,"b",C)]}const t=[];for(let o=n.gridPosition.col-1;o>r.gridPosition.col;o--){const i=e?e.get(n.gridPosition.row,o):null;i&&t.push(i)}const o=c(r).includes(n);let i=e?e.getExistingOutgoingEdgesFor(r):[];if(i=i.some(t=>"SW_NE"===e.getEdgeDirection(t)),t.length>0||o||i)return[E(f,g,"b",b),{x:f.x,y:P+(n.isExpanded?u*(n.grid.colCount+1.5):u)},{x:x.x,y:P+(n.isExpanded?u*(n.grid.colCount+1.5):u)},E(x,h,"b",C)];{const t=E(f,g,"l",b);n.isExpanded&&(t.y=g.y+d(n).height/2);const e=E(x,h,"r",C);return r.isExpanded&&(e.y=h.y+d(r).height/2),[t,e]}}if("SE_NW"===y){if(v)return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},E(x,h,"b",C)];const t=[];for(let o=n.gridPosition.col-1;o>=r.gridPosition.col;o--){const i=e?e.get(n.gridPosition.row,o):null;i&&t.push(i)}return t.length>0?[E(f,g,"b",b),{x:f.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:x.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},E(x,h,"b",C)]:[E(f,g,"l"),{x:x.x,y:f.y},E(x,h,"b")]}const N=function(t,e,o){const{row:i,col:n}=t.gridPosition,{row:r,col:s}=e.gridPosition,d=s-n,a=r-i;if(!(d>0&&0!==a))return;if(a>0){let t=0;const e={row:r,col:n};return t+=o.getElementsInRange({row:i,col:n},e).length,t+=o.getElementsInRange(e,{row:r,col:s}).length,!(t>2)&&["v","h"]}{let t=0;const e={row:i,col:s};return t+=o.getElementsInRange({row:i,col:n},e).length,t+=o.getElementsInRange(e,{row:r,col:s}).length,!(t>2)&&["h","v"]}}(n,r,e);if(N){const t=E(f,g,N[0],b),e=E(x,h,N[1],C);return[t,"h"===N[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const T=-Math.sign(_)*u/2;return[E(f,g,"r",b),{x:f.x+75,y:f.y},{x:f.x+75,y:x.y+T},{x:x.x-75,y:x.y+T},{x:x.x-75,y:x.y},E(x,h,"l",C)]}function p(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function E(t,e,o="r",i="top-left"){if("h"===o&&(o=/left/.test(i)?"l":"r"),"v"===o&&(o=/top/.test(i)?"t":"b"),"t"===o)return{original:t,x:t.x,y:e.y};if("r"===o)return{original:t,x:e.x+e.width,y:t.y};if("b"===o)return{original:t,x:t.x,y:e.y+e.height};if("l"===o)return{original:t,x:e.x,y:t.y};throw new Error("unexpected dockingDirection: <"+o+">")}function m(t,e={x:0,y:0}){const o=t.gridPosition.row,i=t.gridPosition.col;return{width:h,height:u,x:i*h+e.x,y:o*u+e.y}}function x(t,e,o,i,n){const{width:r,height:s}=d(t),{x:a,y:c}=i;if(!n)return{width:r,height:s,x:o*h+(h-r)/2+a,y:e*u+(u-s)/2+c};const g=n.di.bounds;return{width:r,height:s,x:Math.round(g.x+g.width/2-r/2),y:Math.round(g.y+g.height-s/2)}}function y(t,e){const o=[e].flat();if(0===o.length)return t;let i=[];return o.forEach((e,n)=>{const r=t.filter(t=>a(t,e));if(i=i.concat(r),n===o.length-1&&i.length!==t.length)for(const e of t)i.includes(e)||i.push(e)}),i}class w{constructor(t){this.moddle=t}create(t,e){return this.moddle.create(t,e||{})}createDiBounds(t){return this.create("dc:Bounds",t)}createDiLabel(){return this.create("bpmndi:BPMNLabel",{bounds:this.createDiBounds()})}createDiShape(t,e,o){return this.create("bpmndi:BPMNShape",n({bpmnElement:t,bounds:this.createDiBounds(e)},o))}createDiWaypoints(t){var e=this;return r(t,function(t){return e.createDiWaypoint(t)})}createDiWaypoint(t){return this.create("dc:Point",s(t,["x","y"]))}createDiEdge(t,e,o){return this.create("bpmndi:BPMNEdge",n({bpmnElement:t,waypoint:this.createDiWaypoints(e)},o))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}class _{constructor(t=0,e=0){const o=Array(t);for(let t=0;t<o.length;t++)o[t]=Array(e);this._grid=o,this.isFlipped=!1,this._elements=new Set}get rowCount(){return this._grid.length}get elementsCount(){return this._elements.size}get elements(){return this._elements}get colCount(){const t=this._grid[0];return t&&t.length}add(t,e){if(!this.isValidPosition(e))return void this._addStart(t);const[o,i]=e;this._grid[o]||(this._grid[o]=[]),this._grid[o][i]?this._grid[o][i].add(t):this._grid[o][i]=new Set([t]),this._elements.add(t),this.toRectangle()}move(t,e){if(!this.elements.has(t))return;if(!this.isValidPosition(e))return;const o=this.find(t);this.isValidPosition(o)&&(this.removeElementAt(o),this.add(t,e))}removeElement(t){if(!t)return;if(!this.elements.has(t))return;const e=this.find(t);if(e){const o=this.get(e[0],e[1]);o.delete(t),this.elements.delete(t),0===o.size&&(this._grid[e[0]][e[1]]=null)}}createRow(t){t||Number.isInteger(t)?this._grid.splice(t+1,0,Array(this.colCount)):this._grid.push(Array(this.colCount))}createCol(t,e){this._grid.forEach((o,i)=>{this.expandRow(i,t,e)})}expandRow(t,e,o){if(!Number.isInteger(t)||t<0||t>this.rowCount-1)return;const i=Number.isInteger(o)&&o>0?Array(o):Array(1),n=this._grid[t];e||Number.isInteger(e)?n.splice(e+1,0,...i):n.splice(n.length,0,...i)}_addStart(t){this._grid.push([new Set([t])]),this._elements.add(t)}find(t){let e,o;if(e=this._grid.findIndex(e=>(o=e.findIndex(e=>e?.has(t)),-1!==o)),this.isValidPosition([e,o]))return[e,o]}get(t,e){return(this._grid[t]||[])[e]}getElementsInRange({row:t,col:e},{row:o,col:i}){const n=[];t>o&&([t,o]=[o,t]),e>i&&([e,i]=[i,e]);for(let r=t;r<=o;r++)for(let t=e;t<=i;t++){const e=this.get(r,t);e&&n.push(e)}return n}getGridDimensions(){const t=this._grid.length;let e=0;for(let o=0;o<t;o++){const t=this._grid[o]?.length;t>e&&(e=t)}return[t,e]}elementsByPosition(){const t=[];return this._grid.forEach((e,o)=>{e.forEach((e,i)=>{if(e)for(const n of[...e])t.push({element:n,row:o,col:i})})}),t}shrinkCols(){for(let t=this.colCount-1;t>=0;t--){if(this._grid.every(e=>null==e[t]))for(const e of this._grid)e.splice(t,1)}}shrinkRows(){this._grid=this._grid.filter(t=>!t.every(t=>null==t))}removeElementAt(t){const[e,o]=t,i=this.get(e,o);i&&(this._grid[e][o]=null,this.elements.delete(i))}toRectangle(){const[,t]=this.getGridDimensions();this._grid.forEach(e=>{if(e.length<t){const o=t-e.length;for(let t=0;t<o;t++)e.splice(e.length,0,null)}})}flipHorizontally(){for(const t of this._grid)t.reverse();this.isFlipped=!this.isFlipped}hasElement(t){return this.elements.has(t)}isValidPosition(t){if(!t||!Array.isArray(t)||2!==t.length)return!1;const[e,o]=t;return Number.isInteger(e)&&Number.isInteger(o)&&e>=0&&o>=0}hasIntermediateElements(t,e,o){if(!this.isValidPosition(t)||!this.isValidPosition(e))return!1;if(o){const[o,i]=t[0]<=e[0]?[t[0],e[0]]:[e[0],t[0]];for(let e=o+1;e<i;e++)if(this.hasElementAt([e,t[1]]))return!0;return!1}{const[o,i]=t[1]<=e[1]?[t[1],e[1]]:[e[1],t[1]];for(let e=o+1;e<i;e++)return this.hasElementAt([t[0],e]),!0;return!1}}hasElementAt(t){if(!this.isValidPosition(t))return!1;const[e,o]=t;return!!this.get(e,o)}}class b{constructor(t,e){this.source=t,this.target=e}}class C extends _{constructor(t,o,i){super(o,i),this.initialGraph=t,this.graph=new e}add(t,e){super.add(t,e),this.graph.addNode(t),this._createNewEdgesFor(t)}removeElement(t){this.graph.deleteNode(t),super.removeElement(t)}move(t,e){const o=[...this.get(...this.find(t))],i=o.reduce((t,e)=>[...t,...this.getAllExistingEdgesFor(e)],[]);for(const t of o)this.removeElement(t),this.add(t,e);for(const t of i){const e=new b(t.source,t.target);e.id=t.id,this._addEdgeToGrid(e)}}_edgeIsExist(t){return[...this.graph.edges].some(e=>e.id===t.id&&e.source===t.source&&e.target===t.target)}_addEdgeToGrid(t){this._edgeIsExist(t)||this.graph.addEdge(t)}_createNewEdgesFor(t){const e=[...this.initialGraph.edges].filter(e=>(e.source===t||e.target===t)&&this.hasElement(e.source)&&this.hasElement(e.target));for(const t of e){const e=new b(t.source,t.target);e.id=t.id,this._addEdgeToGrid(e)}}isCrossed(t,e){return[...this._allEdges].some(o=>this.isIntersect(o,t,e))}get _allEdges(){return this.graph.edges}hasAnyCross(t){return[...t||this._allEdges].some(t=>this.getCrossedElementsFor(t).length>0||this.getCrossedElementsFor(t,!0).length>0)}shakeIt(t){const e=[...this.elements].sort(t?(o=this,function(t,e){const i=o.find(t),n=o.find(e);return i&&!n?-1:!i&&n?1:i||n?i[0]-n[0]||n[1]-i[1]:0}):function(t){return function(e,o){const i=t.find(e),n=t.find(o);return i&&!n?-1:!i&&n?1:i||n?i[1]-n[1]||n[0]-i[0]:0}}(this)).reverse();for(var o;e.length>0;){const o=e.pop(),i=this.getChain(o,!t);for(const t of i){const o=e.indexOf(t);o>=0&&e.splice(o,1)}const[n,r]=this.find([...i][0]);if(!(t?n<=0:r<=0)){for(let e=t?n-1:r-1;e>=0;e--){const n=[...i].every(t=>{const e=this.find(t);return e&&!this.isCrossed(e,!0)&&!this.isCrossed(e,!1)});if(!n)break;const r=[...i].every(o=>{const i=this.find(o),n=t?e:i[0],r=t?i[1]:e;return!this.get(n,r)});if(!r)break;for(const o of i){const i=this.find(o);this.move(o,t?[e,i[1]]:[i[0],e])}const s=this.hasAnyCross(),d=t?this.getChain(o,!1):this.getChain(o,!0);if(s||d.size>i.size){for(const o of i){const i=this.find(o);this.move(o,t?[e+1,i[1]]:[i[0],e+1])}break}}t?this.shrinkRows():this.shrinkCols()}}}getChain(t,e,o){const i=o||new Set;if(!t)return i;const n=this.find(t);if(!n)return i;const r=[],s=this.get(n[0],n[1]);for(const t of s)i.add(t),[...this.getAllExistingEdgesFor(t)].filter(t=>e?"N_S"===this.getEdgeDirection(t)||"S_N"===this.getEdgeDirection(t):"W_E"===this.getEdgeDirection(t)||"E_W"===this.getEdgeDirection(t)).forEach(t=>r.push(t));for(const o of r){const n=o.source===t?o.target:o.source;if(!i.has(n)){const t=this.getChain(n,e,i);for(const e of t)i.add(e)}}return i}getExistingOutgoingEdgesFor(t){return this.isFlipped?[...this.graph.getIncomingEdgesFor(t)]:[...this.graph.getOutgoingEdgesFor(t)]}getExistingIncomingEdgesFor(t){return this.isFlipped?[...this.graph.getOutgoingEdgesFor(t)]:[...this.graph.getIncomingEdgesFor(t)]}getAllExistingEdgesFor(t){const e=this.getExistingOutgoingEdgesFor(t),o=this.getExistingIncomingEdgesFor(t);return new Set([...e,...o])}_separateGrid(){const t=e.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new C(this.initialGraph,this.colCount,this.rowCount)];const o=[];for(const e of t){const t=new C(this.initialGraph,this.colCount,this.rowCount);let i=null,n=null;for(const o of y(e.nodes,"bpmn:BoundaryEvent").reverse()){const e=this.find(o);(null===i||i>e[0])&&(i=e[0]),(null===n||n<e[0])&&(n=e[0]),t._superAdd(o,e),t.graph.addNode(o)}e.edges.forEach(e=>{t._addEdgeToGrid(e)}),o.push(t)}return o}_superAdd(t,e){super.add(t,e)}_mergeGrids(t){const e=t.reduce((t,e)=>t+e.rowCount,0),o=t.reduce((t,e)=>e.colCount>t?e.colCount:t,0),i=new C(t[0].initialGraph,e,o);return t.forEach((e,o)=>{let n=0;for(let e=0;e<o;e++)n+=t[e].rowCount;for(const t of e.elements){const[o,r]=e.find(t);i.add(t,[o+n,r])}}),i}getSourcePosition(t){const e=this.isFlipped?t.target:t.source;return this.find(e)}getTargetPosition(t){const e=this.isFlipped?t.source:t.target;return this.find(e)}getEdgeDirection(t){const e=this.getSourcePosition(t),o=this.getTargetPosition(t);if(!this.isValidPosition(e)||!this.isValidPosition(o))throw new Error(`Invalid position of source or target in ${t.source}-${t.target} flipped:${this.isFlipped}`);const[i,n]=e,[r,s]=o,d=i-r,a=n-s;return 0===d&&0===a?"NO_DIRECTION":d>0&&0===a?"S_N":d>0&&a<0?"SW_NE":0===d&&a<0?"W_E":d<0&&a<0?"NW_SE":d<0&&0===a?"N_S":i<r&&n>s?"NE_SW":i===r&&n>s?"E_W":i>r&&n>s?"SE_NW":void 0}getPathFor(t){const e=this.getEdgeDirection(t);return"NO_DIRECTION"===e?this._pathForNoDirection():"S_N"===e?this._pathForSouthToNorth(t):"SW_NE"===e?this._pathForSouthWestToNorthEast(t):"W_E"===e?this._pathForWestToEast(t):"NW_SE"===e?this._pathForNorthWestToSouthEast(t):"N_S"===e?this._pathForNorthToSouth(t):"NE_SW"===e?this._pathForNorthEastToSouthWest(t):"E_W"===e?this._pathForEastToWest(t):"SE_NW"===e?this._pathForSouthEastToNorthWest(t):[]}_pathForNoDirection(){return[]}_pathForSouthToNorth(t){const e=[],[o,i]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"===(this.isFlipped?t.target:t.source).$type&&t.id)return e;if(this.hasIntermediateElements(this.getSourcePosition(t),this.getTargetPosition(t),!0))return e;if([...this.getExistingOutgoingEdgesFor(this.isFlipped?t.source:t.target)].map(t=>this.isFlipped?t.source:t.target).includes(this.isFlipped?t.target:t.source))return e;for(let t=o-1;t>n;t--)e.push({position:[t,i],vCross:!0});return e}_pathForSouthWestToNorthEast(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==(this.isFlipped?t.target:t.source).$type||!t.id)for(let t=i+1;t<r;t++)e.push({position:[o,t],hCross:!0});e.push({position:[o,r],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,r],vCross:!0});return e}_pathForWestToEast(t){const e=[],[o,i]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=i+1;t<n;t++)e.push({position:[o,t],hCross:!0});return e}_pathForNorthWestToSouthEast(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,i],vCross:!0});e.push({position:[n,i],vCross:!0,hCross:!0});for(let t=i+1;t<r;t++)e.push({position:[n,t],hCross:!0});return e}_pathForNorthToSouth(t){const e=[],[o,i]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,i],vCross:!0});return e}_pathForNorthEastToSouthWest(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,i],vCross:!0});e.push({position:[n,i],vCross:!0,hCross:!0});for(let t=i-1;t>r;t--)e.push({position:[n,t],hCross:!0});return e}_pathForEastToWest(t){const e=[],[o,i]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=i-1;t>n;t--)e.push({position:[o,t],hCross:!0});return e}_pathForSouthEastToNorthWest(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==(this.isFlipped?t.target:t.source).$type||!t.id)for(let t=i-1;t>r;t--)e.push({position:[o,t],hCross:!0});"bpmn:BoundaryEvent"===(this.isFlipped?t.target:t.source).$type&&t.id?e.push({position:[o,r],vCross:!0}):e.push({position:[o,r],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,r],vCross:!0});return e}getCrossedElementsFor(t,e=!1){const o=[];for(const i of this.getPathFor(t)){const[t,n]=i.position,r=this.get(t,n);r&&(e&&i.vCross||!e&&i.hCross)&&o.push(r)}return o}isIntersect(t,e,o){const[i,n]=e,[r,s]=this.getSourcePosition(t),[d,a]=this.getTargetPosition(t),c=this.getEdgeDirection(t);return"S_N"===c?!!(o&&n===s&&i<r&&i>d):"SW_NE"===c?!!(o&&n===a&&i<=r&&i>d)||!o&&n>s&&n<=a&&i===r:"W_E"===c?!o&&n>s&&n<a&&i===r:"NW_SE"===c?!!(o&&n===s&&i>r&&i<=d)||!o&&n>=s&&n<a&&i===d:"N_S"===c?!!(o&&n===s&&i>r&&i<d):"NE_SW"===c?!!(o&&n===s&&i>r&&i<=d)||!o&&n>a&&n<=s&&i===d:"E_W"===c?!o&&n>a&&n<s&&i===r:"SE_NW"===c&&(!!(o&&n===a&&i>d&&i<=r)||!o&&n>=a&&n<s&&i===r&&!this.getExistingIncomingEdgesFor(this.isFlipped?t.target:t.source).some(t=>"NW_SE"===this.getEdgeDirection(t)||"W_E"===this.getEdgeDirection(t)))}getBackwardUpOutgoingEdgesFor(t){return this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeDirection(t);return"S_N"===e||"SE_NW"===e})}getGridCopy(){return this._mergeGrids([this])}removeEdge(t){this.graph.deleteEdge(t)}getGraphSegmentFrom(t){const o=new e;return this.graph.genericTraversing((e,o)=>{if(!e.has(t))return t},t=>t.pop(),(t,e,i,n)=>{const r=this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.isFlipped?t.source:t.target,o=this.isFlipped?t.target:t.source;return!i.has(e)&&e!==o});o.addNode(t);const s=r.map(t=>this.isFlipped?t.source:t.target);for(const t of s)o.addNode(t);for(const t of r)o.addEdge(t);return s},(t,e)=>{for(const o of t)e.push(o)}),o}getSegmentLeftCoordinates(t){const e=new Map;for(const o of t.nodes){const[t,i]=this.find(o);e.has(t)||e.set(t,i),i<e.get(t)&&e.set(t,i)}return e}}function F(t,e,o,i,n){!function(t,e){let o=e.getBackwardUpOutgoingEdgesFor(t);for(;o.length>0;){const i=o.shift(),n=e.isFlipped?i.source:i.target;if(e.find(n)[1]>e.find(t)[1])continue;const r=e.getGridCopy(),s=[...r._allEdges].find(t=>t.source===i.source&&t.target===i.target&&t.id===i.id);r.removeEdge(s);const d=e.isFlipped?s.target:s.source,a=e.isFlipped?s.source:s.target,c=r.find(d),g=r.find(a);for(const t of r.elements){const e=r.find(t);(e[1]<g[1]||e[0]>c[0])&&r.removeElement(t)}const h=r.getGraphSegmentFrom(a);if(h.nodes.includes(d))continue;const u=Array.from(r.getSegmentLeftCoordinates(h)),l=u.reduce((t,e)=>void 0===t||e[1]<t[1]?e:t,void 0),f=u.reduce((t,e)=>void 0===t||e[0]<t[0]?e:t,void 0),p=u.reduce((t,e)=>void 0===t||e[0]>t[0]?e:t,void 0),E=c[1]-l[1]+1,m=new Map(u);let x;for(let o=0;o<e.rowCount;o++){const i=(()=>{if(o<f[0])return x=f[0],f[1]-1;if(m.has(o))return x=m.get(o),m.get(o)-1;if(o>f[0]&&o<p[0]&&o<e.find(t)[0])return x;if(o===e.find(t)[0]||o>f[0]){const o=e.find(t);return x=o,o[1]}return x})();e.expandRow(o,i,E)}}e.toRectangle()}(t,e);const r=(e.isFlipped?[...e.initialGraph.getIncomingEdgesFor(t)]:[...e.initialGraph.getOutgoingEdgesFor(t)]).filter(t=>!e.hasElement(e.isFlipped?t.source:t.target)).map(t=>e.isFlipped?t.source:t.target),s=function(t,e,o,i,n,r){const s=e.getExistingOutgoingEdgesFor(t),d=e.getExistingIncomingEdgesFor(t).map(t=>e.isFlipped?t.target:t.source),[,a]=e.find(t),c=[...s].filter(t=>{const n=e.isFlipped?t.source:t.target,r=e.find(n)[1];if(!P(n,o,e))return!1;return(!e.getExistingIncomingEdgesFor(n).map(t=>t.source).find(t=>d.includes(t))||!i)&&r<=a}).map(t=>e.isFlipped?t.source:t.target).sort(function(t){return function(e,o){const i=t.find(e),n=t.find(o);return i&&!n?-1:!i&&n?1:i||n?i[0]-n[0]||i[1]-n[1]:0}}(e));for(const t of c)o.splice(o.indexOf(t),1),r.delete(t),e.removeElement(t);return e.shrinkRows(),e.shrinkCols(),c}(t,e,o,r&&r.length>0,0,i);let d=[...r,...s],a=[];d.forEach(r=>{const s=function(t,e,o){if(e.isFlipped&&"bpmn:BoundaryEvent"===o.$type&&e.hasElement(o.attachedToRef))return e.find(o.attachedToRef);if(o.attachedToRef===t)return e.find(t);const i=e.find(t);if(!i)return;const n="bpmn:BoundaryEvent"===t.$type&&!e.isFlipped,r=n?[i[0]+1,i[1]+1]:[i[0],i[1]+1],s=e.find(t),d=[...e.get(s[0],s[1])],a=[];for(const t of d)[...e.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source).forEach(t=>a.push(t));for(let t=r[0];t<=e.rowCount;t++){const o=[t,r[1]],i=a.some(i=>e.getTargetPosition(i)[0]===t&&e.getTargetPosition(i)[1]===r[1]||e.isIntersect(i,o,!1)||e.isIntersect(i,o,!0));if(i&&t===e.colCount-1)r[0]=t+1;else if(!i){r[0]=t;break}}(e.get(r[0],r[1])||e.isCrossed(r,!0))&&e.createCol(r[1]-1);e.isCrossed([r[0],r[1]])&&e.createRow(r[0]-1);return r}(t,e,r);if(e.isFlipped&&"bpmn:BoundaryEvent"===r.$type&&r.attachedToRef!==t){const t=r.attachedToRef;i.has(t)||(e.add(t,s),i.add(t),S(t,e,n,i,o,a),a.unshift(r))}e.add(r,s),i.add(r),S(r,e,n,i,o,a),a.unshift(r)});const c=[],g=[];for(const t of a)"bpmn:BoundaryEvent"===t.$type?c.push(t):g.push(t);return c.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),a=[...c,...g],a}function P(t,e,o){const i=e.includes(t),n=o.getExistingOutgoingEdgesFor(t);return i&&(!n||0===n.length)}function S(t,e,o,i,n,r,s,d){const a=[...[...e.getExistingOutgoingEdgesFor(t)].sort((t,o)=>{const[i,n]=e.getTargetPosition(t),[r,s]=e.getTargetPosition(o);return i-r||n-s}),...[...e.getExistingIncomingEdgesFor(t)].sort((t,o)=>{const[i,n]=e.getSourcePosition(t),[r,s]=e.getSourcePosition(o);return i-r||n-s})].filter(o=>{const{target:i,source:s}=o,d=e.getEdgeDirection(o);return i!==s&&((!r||!r.includes(i))&&((!n||!P(i,n,e))&&(s!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of a)v(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===(e.isFlipped?t.target:t.source).$type&&t.id||N(t,e)}function v(t,e){const o=e.getEdgeDirection(t);if("W_E"===o||"E_W"===o)return;const i=e.getCrossedElementsFor(t,!0);i.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||W(i,e):D(i,e):W(i,e):D(i,e))}function N(t,e){const o=e.getEdgeDirection(t);if("S_N"===o||"N_S"===o)return;const i=e.getCrossedElementsFor(t,!1);if(0!==i.length){if("SW_NE"===o){const o=function(t,e){const o=e.getSourcePosition(t),i=e.getTargetPosition(t);let n=o[0];for(let t=o[0];t<e.rowCount&&e.getElementsInRange({row:t,col:o[1]+1},{row:t,col:i[1]}).length>0;t++)n++;return n-o[0]}(t,e),[i]=e.getSourcePosition(t);for(let t=o;t>0;t--)e.createRow(i-1);const n=e.getElementsInRange({row:e.getSourcePosition(t)[0],col:e.getSourcePosition(t)[1]+1},{row:e.rowCount-1,col:e.colCount-1});for(const t of n){const[i,n]=e.find([...t][0]);for(const r of[...t])e.move(r,[i-o,n])}return}"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||T(i,e):R(i,e):T(i,e):R(i,e)}}function T(t,e){const[o]=e.find([...t[0]][0]);e.createRow(o-1);for(const i of t){const[,t]=e.find([...i][0]);for(const n of[...i])e.move(n,[o,t])}}function R(t,e){const[o]=e.find([...t[0]][0]);e.createRow(o);for(const i of t){const[,t]=e.find([...i][0]);for(const n of[...i])e.move(n,[o+1,t])}}function D(t,e){const[,o]=e.find([...t[0]][0]);e.createCol(o);for(const i of t){const[t]=e.find([...i][0]);for(const n of[...i])e.move(n,[t,o+1])}}function W(t,e){const[,o]=e.find([...t[0]][0]);e.createCol(o-1);for(const i of t){const[t]=e.find([...i][0]);for(const n of[...i])e.move(n,[t,o])}}function I(t,e,o,i,n,r){if(t.di)return[];if("bpmn:BoundaryEvent"===t.$type)return function(t,e,o,i,n,r){const s=t.attachedToRef.di.bounds;if(!s)throw new Error(`Create DI for ${t.id}. Nо hostBounds`);const d=[];let a=t.$parent.flowElements.filter(e=>e.attachedToRef===t.attachedToRef&&n.hasElement(t));return a=function(t,e){return t.sort((t,o)=>{const i=O(t,e),n=O(o,e);return i[0]-n[0]||i[1]-n[1]}).reverse()}(a,n),a.forEach((n,a,c)=>{n.gridPosition={row:e,col:o};const g=x(n,e,o,r,t.attachedToRef);g.x=s.x+(a+1)*(s.width/(c.length+1))-g.width/2;const h=i.createDiShape(n,g,{id:n.id+"_di"});n.di=h,n.gridPosition={row:e,col:o},d.push(h)}),d}(t,e,o,i,n,r);const s=x(t,e,o,r);if(t.isExpanded&&t.grid){const{width:e,height:o}=d(t),{rowCount:i,colCount:n}=t.grid;s.width=n*h+e,s.height=i*u+o}const c={id:t.id+"_di"};t.isExpanded&&(c.isExpanded=!0),a(t,"bpmn:ExclusiveGateway")&&(c.isMarkerVisible=!0);const g=i.createDiShape(t,s,c);return t.di=g,t.gridPosition={row:e,col:o},[g]}function O(t,e){return c(t).reduce((t,o)=>{if(!e.hasElement(o))return t;const i=e.find(o);return void 0===t||t[0]<i[0]||t[1]<i[1]?i:t},[0,0])}class G{constructor(e){this.moddle=new t,this.diFactory=new w(this.moddle),this.maxDebugStep=e,this.currentDebugStep=0}async layoutProcess(t){const e=await this.moddle.fromXML(t),{rootElement:o}=e;this.diagram=o,function(t){const e=t.elementsById;if(e)for(const t of Object.values(e))"bpmndi:BPMNShape"===t.$type&&!0===t.isExpanded&&(t.bpmnElement.isExpanded=!0)}(e),this.processTrees=this.createNestedSets(e),this.createGridsForProcesses(),this.expandProcessesGrids();const i=this.getRootProcesses(),n=this.getCollaboration();return i.length>0&&(this.cleanDi(),this.createRootDi(i,n),this.drawParticipants(),this.drawProcesses(),this.drawCollaborationMessageFlows(n)),(await this.moddle.toXML(this.diagram,{format:!0})).xml}createGridsForProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){e.grid=this.createGridLayout(e);const t=e.grid._separateGrid()||[e.grid];for(const e of t)e.shrinkRows(),e.shrinkCols(),e.shakeIt(!0),e.shakeIt(!1);e.grid=e.grid._mergeGrids(t),e.grid.toRectangle()}}expandProcessesGrids(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>e.level-t.level);for(const e of t){const t=e.grid._separateGrid()||[e.grid];for(const e of t)e.shrinkRows(),e.shrinkCols(),B(e),$(e);if(e.grid=e.grid._mergeGrids(t),e.grid.toRectangle(),e.isExpanded){const{colCount:t,rowCount:o}=e.grid;0===o&&e.grid.createRow(),0==t&&e.grid.createCol()}}}drawParticipants(){const t=this.getCollaboration();if(!t||!t[0])return;const e=t[0].participants;let o=0;for(const t of e)o=this.createParticipantDi(t,{x:0,y:o})+70}drawProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){const t=this.getParticipantForProcess(e);if(t){const o=this.getElementDi(t),i=this.getProcDi(o);let{x:n,y:r}=o.bounds;n+=75,r+=70,this.generateDi(e.grid,{x:n,y:r},i);continue}if(e.isExpanded&&!this.existingNodes.includes(e))continue;if(e.isExpanded){const t=this.getElementDi(e),o=this.getProcDi(t);let{x:i,y:n}=t.bounds;const{width:r,height:s}=d(e);i+=75-r/4,n+=u-s-s/4,this.generateDi(e.grid,{x:i,y:n},o);continue}const o=this.diagram.diagrams.find(t=>t.plane.bpmnElement===e);this.generateDi(e.grid,{x:0,y:0},o)}}get existingNodes(){return this.processTrees.map(t=>[...t.nodes]).flat().map(t=>[...t.grid.elements]).flat()}drawCollaborationMessageFlows(t){const e=t[0]?t[0].messageFlows:null;if(e)for(const t of e){const{sourceRef:e,targetRef:o}=t,i=this.existingNodes;if(!i.includes(e)||!i.includes(o))continue;const n=e.di.bounds,r=o.di.bounds,s=r.y-n.y,d=[{x:n.x+n.width/2},{x:r.x+r.width/2}];s>0?(d[0].y=n.y+n.height,d[1].y=r.y):(d[0].y=n.y,d[1].y=r.y+r.height);const a=this.diFactory.createDiEdge(t,d);this.diagram.diagrams[0].plane.get("planeElement").push(a)}}getParticipantForProcess(t){const e=this.getCollaboration();if(!e||!e[0])return;const o=this.getCollaboration()[0].participants;return o?o.find(e=>e.processRef===t):void 0}getElementDi(t){return this.diagram.diagrams.map(t=>t.plane.planeElement).flat().find(e=>e.bpmnElement===t)}getProcDi(t){return this.diagram.diagrams.find(e=>e.plane.planeElement.includes(t))}createNestedSets(t){const n=new e,r=function(t){const e=t.elementsById;return e?Object.values(e).filter(t=>"bpmn:Process"===t.$type||"bpmn:SubProcess"===t.$type):[]}(t);for(const t of r)n.addNode(t);for(const t of r){const e=this.getSubProcesses(t);for(const o of e)n.addEdge({source:t,target:o})}const s=n.getSeparatedGraphs();for(const t of s){const e=t.nodes.filter(e=>0===t.getIncomingEdgesFor(e).size);if(e.length>1)throw new Error("Process tree has more than 1 root elements");if(0===e.length)throw new Error("Process tree has more than 0 root elements");t.rootProcess=e[0]}for(const t of s){const e=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},n=(t,e,o)=>{const{left:i,level:n}=t,r=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(r)return r.level=n+1,r.left=i+1,[r];const s=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).reduce((t,e)=>{if(void 0===t||e.right>t.right)return e.right},void 0);t.right=s?s+1:t.left+1;const d=[...e.getIncomingEdgesFor(t)].map(t=>t.source).find(t=>void 0===t.right);return d?[d]:void 0};t.genericTraversing(e,o,n,i)}return s}getSubProcesses(t){return t.flowElements?t.flowElements.filter(t=>"bpmn:SubProcess"===t.$type):[]}createRootDi(t,e){const o=e&&e.length>0?e[0]:t[0];this.createProcessDi(o)}createProcessDi(t){const e=this.diFactory,o=e.createDiPlane({id:"BPMNPlane_"+t.id,bpmnElement:t}),i=e.createDiDiagram({id:"BPMNDiagram_"+t.id,plane:o});return this.diagram.diagrams.push(i),i}createParticipantDi(t,e){const{colCount:o,rowCount:i}=t.processRef.grid,{width:n,height:r}=d(t),s=o>0?o*h+h:n,a=i>0?i*u+u:r,c=this.diFactory.createDiShape(t,{width:s,height:a,...e},{id:t.id+"_di"});return this.diagram.diagrams[0].plane.get("planeElement").push(c),c.bounds.y+c.bounds.height}cleanDi(){this.diagram.diagrams=[]}createGridLayout(t){const o=new e,i=new C(o);for(const e of t.flowElements||[])a(e,"bpmn:SequenceFlow")||a(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new b(t,t.attachedToRef)),o.addEdge(new b(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new b(e.sourceRef,e.targetRef);t.id=e.id,o.addEdge(t)}const e=t.dataInputAssociations;for(const t of e||[])for(const e of t.sourceRef||[]){const i=new b(e,t.$parent);i.id=t.id,i.propRef=t.targetRef,o.addEdge(i)}const i=t.dataOutputAssociations;for(const t of i||[]){const e=t.$parent,i=t.targetRef,n=new b(e,i);n.id=t.id,o.addEdge(n)}}return o.genericTraversing((t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const o=[...t].find(o=>i.isFlipped?[...e.getOutgoingEdgesFor(o)].filter(e=>!t.has(e.target)).length>0:[...e.getIncomingEdgesFor(o)].filter(e=>!t.has(e.source)).length>0);if(o)return i.flipHorizontally(),o;const n=e.nodes.filter(o=>i.isFlipped?!t.has(o)&&0===e.getOutgoingEdgesFor(o).size&&!g(o):!t.has(o)&&0===e.getIncomingEdgesFor(o).size&&!g(o));if(n.length>0)return y(n,"bpmn:StartEvent")[0];const r=[...t].find(o=>(i.isFlipped?[...e.getIncomingEdgesFor(o)].filter(e=>!t.has(e.source)):[...e.getOutgoingEdgesFor(o)].filter(e=>!t.has(e.target))).length>0);if(r)return r;const s=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(i.isFlipped?[...e.getOutgoingEdgesFor(o)].filter(t=>t.target!==o):[...e.getIncomingEdgesFor(o)].filter(t=>t.source!==o)).length});if(s)return s;const d=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(i.isFlipped?[...e.getIncomingEdgesFor(o)].filter(t=>t.target!==t.source):[...e.getOutgoingEdgesFor(o)]).length});return d?(i.flipHorizontally(),d):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))},t=>t.pop(),(t,e,o,n)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const r=F(t,i,n,o,e);return this.currentDebugStep+=1,r},(t,e)=>{for(const o of t)e.push(o)},t=>{i.hasElement(t)||i.add(t)}),i.isFlipped&&i.flipHorizontally(),i}generateDi(t,e,o){const i=this.diFactory,n=(o||this.diagram.diagrams[0]).plane.get("planeElement");t.elementsByPosition().sort((t,e)=>{const o="bpmn:BoundaryEvent"===t.element.$type?0:1;return("bpmn:BoundaryEvent"===e.element.$type?0:1)-o}).forEach(({element:o,row:r,col:s})=>{const d=I(o,r,s,i,t,e);n.push(...d)}),t._allEdges.forEach(o=>{const r=function(t,e,o,i){const{id:n}=t;if(!n)return;const r=f(t,e,!1,i);return o.createDiEdge(t,r,{id:n+"_di"})}(o,t,i,e);r&&n.push(r)})}getRootProcesses(){return this.diagram.get("rootElements").filter(t=>"bpmn:Process"===t.$type)}getCollaboration(){return this.diagram.get("rootElements").filter(t=>"bpmn:Collaboration"===t.$type)}}function B(t){for(let e=t.colCount-1;e>=0;e--){const o=[];for(let i=0;i<t.rowCount;i++){const n=[...t.get(i,e)||[]].find(t=>t.isExpanded);n&&o.push(n)}if(0===o.length)continue;const i=o.reduce((t,e)=>{if(void 0===t||e.grid.colCount>t)return e.grid.colCount},void 0),n=i||2;t.createCol(e,n)}}function $(t){for(let e=t.rowCount-1;e>=0;e--){const o=[];for(let i=0;i<t.colCount;i++){const n=[...t.get(e,i)||[]].find(t=>t.isExpanded);n&&o.push(n)}if(0===o.length)continue;const i=o.reduce((t,e)=>{if(void 0===t||e.grid.rowCount>t)return e.grid.rowCount},void 0),n=i||1;for(let o=0;o<n;o++)t.createRow(e)}}function A(t,e){return new G(e).layoutProcess(t)}export{A as layoutProcess};//# sourceMappingURL=index.js.map
1
+ import t from"bpmn-moddle";import{Graph as e,getLast as o,addToEnd as i}from"graph-by-ivan-tulaev";import{assign as n,map as r,pick as s}from"min-dash";function d(t){return a(t,"bpmn:SubProcess")||a(t,"bpmn:Task")?{width:100,height:80}:a(t,"bpmn:Gateway")?{width:50,height:50}:a(t,"bpmn:Event")?{width:36,height:36}:a(t,"bpmn:Participant")||a(t,"bpmn:Lane")?{width:400,height:100}:a(t,"bpmn:DataObjectReference")?{width:36,height:50}:a(t,"bpmn:DataStoreReference")?{width:50,height:50}:a(t,"bpmn:TextAnnotation")?{width:100,height:30}:{width:100,height:80}}function a(t,e){return t.$instanceOf(e)}function c(t){if(!t)throw new Error("Element is not defined in getOutgoingElements");const e=(t.outgoing||[]).map(t=>t.targetRef);return[...new Set(e)]}function g(t){return(a(t,"bpmn:IntermediateThrowEvent")||a(t,"bpmn:IntermediateCatchEvent"))&&(void 0===t.incoming||0===t.incoming.length)}const h=150,u=140;function l(t,e){return new Set(c(t).concat(function(t){const e=new Set;if(t){const o=(t.attachers||[]).sort((t,e)=>(e.outgoing?e.outgoing.length:0)-(t.outgoing?t.outgoing.length:0)).map(t=>(t.outgoing||[]).reverse()).flat().map(t=>t.targetRef).filter((t,e,o)=>o.indexOf(t)===e);for(const t of o)e.add(t)}return[...e]}(t)))}function f(t,e,o=!1,i){const{source:n,target:r}=t,s=n.di,a=r.di,g=s.get("bounds"),h=a.get("bounds"),f=p(g),x=p(h),y=e.getEdgeDirection(t),w=r.gridPosition.col-n.gridPosition.col,_=r.gridPosition.row-n.gridPosition.row,b=`${_>0?"bottom":"top"}-${w>0?"right":"left"}`,C=`${_>0?"top":"bottom"}-${w>0?"left":"right"}`,{x:F,y:P}=m(n,i),{x:S}=m(r,i),v="bpmn:BoundaryEvent"===n.$type;if("NO_DIRECTION"===y)return v?[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:S,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:F,y:x.y},E(x,h,"l",C)]:[E(f,g,"b",b),{x:f.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:S,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:F,y:x.y},E(x,h,"l",C)];if("S_N"===y){if(v)return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:F,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:S,y:x.y},E(x,h,"l",C)];const t=[];for(let o=n.gridPosition.row-1;o>r.gridPosition.row;o--){const i=e.get(o,n.gridPosition.col);i&&t.push(i)}const o=[...l(r)].includes(n);return t.length>0||o?[E(f,g,"l",b),{x:F,y:f.y},{x:S,y:x.y},E(x,h,"l",C)]:[E(f,g,"t",b),E(x,h,"b",C)]}if("SW_NE"===y)return v?[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},E(x,h,"b",C)]:[E(f,g,"r"),{x:x.x,y:f.y},E(x,h,"b")];if("W_E"===y){if(v)return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},E(x,h,"b",C)];const t=E(f,g,"r",b);n.isExpanded&&(t.y=g.y+d(n).height/2);const e=E(x,h,"l",C);return r.isExpanded&&(e.y=h.y+d(r).height/2),[t,e]}if("NW_SE"===y)return[E(f,g,"b"),{x:f.x,y:x.y},E(x,h,"l")];if("N_S"===y){if(v){const t=E(f,g,"b",b),e=E(x,h,"t",C);return n.attachedToRef.isExpanded?[t,{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},e]:[t,e]}const t=E(f,g,"b",b),e=E(x,h,"t",C);return n.isExpanded||r.isExpanded?[t,{x:f.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:x.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},e]:[t,e]}if("NE_SW"===y)return[E(f,g,"b"),{x:f.x,y:x.y},E(x,h,"r")];if("E_W"===y){if(v){const t=function(t,e,o){const[i,n]=o.find(t),[,r]=o.find(e),s=n<r?n:r,d=n<r?r:n,a=[...o.elements].map(t=>void 0!==t.size?[...t]:t).flat().filter(t=>t.gridPosition.row===i&&t.gridPosition.col>s&&t.gridPosition.col<d);return a.reduce((t,e)=>e.grid?.getGridDimensions()[0]>t?e.grid?.getGridDimensions()[0]:t,0)}(n,r,e);return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u+t*u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u+t*u},E(x,h,"b",C)]}const t=[];for(let o=n.gridPosition.col-1;o>r.gridPosition.col;o--){const i=e?e.get(n.gridPosition.row,o):null;i&&t.push(i)}const o=c(r).includes(n);let i=e?e.getExistingOutgoingEdgesFor(r):[];if(i=i.some(t=>"SW_NE"===e.getEdgeDirection(t)),t.length>0||o||i)return[E(f,g,"b",b),{x:f.x,y:P+(n.isExpanded?u*(n.grid.colCount+1.5):u)},{x:x.x,y:P+(n.isExpanded?u*(n.grid.colCount+1.5):u)},E(x,h,"b",C)];{const t=E(f,g,"l",b);n.isExpanded&&(t.y=g.y+d(n).height/2);const e=E(x,h,"r",C);return r.isExpanded&&(e.y=h.y+d(r).height/2),[t,e]}}if("SE_NW"===y){if(v)return[E(f,g,"b",b),{x:f.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},{x:x.x,y:n.attachedToRef.isExpanded?P+(n.attachedToRef.grid.rowCount+1)*u:P+u},E(x,h,"b",C)];const t=[];for(let o=n.gridPosition.col-1;o>=r.gridPosition.col;o--){const i=e?e.get(n.gridPosition.row,o):null;i&&t.push(i)}return t.length>0?[E(f,g,"b",b),{x:f.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},{x:x.x,y:n.isExpanded?P+(n.grid.rowCount+1)*u:P+u},E(x,h,"b",C)]:[E(f,g,"l"),{x:x.x,y:f.y},E(x,h,"b")]}const N=function(t,e,o){const{row:i,col:n}=t.gridPosition,{row:r,col:s}=e.gridPosition,d=s-n,a=r-i;if(!(d>0&&0!==a))return;if(a>0){let t=0;const e={row:r,col:n};return t+=o.getElementsInRange({row:i,col:n},e).length,t+=o.getElementsInRange(e,{row:r,col:s}).length,!(t>2)&&["v","h"]}{let t=0;const e={row:i,col:s};return t+=o.getElementsInRange({row:i,col:n},e).length,t+=o.getElementsInRange(e,{row:r,col:s}).length,!(t>2)&&["h","v"]}}(n,r,e);if(N){const t=E(f,g,N[0],b),e=E(x,h,N[1],C);return[t,"h"===N[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const T=-Math.sign(_)*u/2;return[E(f,g,"r",b),{x:f.x+75,y:f.y},{x:f.x+75,y:x.y+T},{x:x.x-75,y:x.y+T},{x:x.x-75,y:x.y},E(x,h,"l",C)]}function p(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function E(t,e,o="r",i="top-left"){if("h"===o&&(o=/left/.test(i)?"l":"r"),"v"===o&&(o=/top/.test(i)?"t":"b"),"t"===o)return{original:t,x:t.x,y:e.y};if("r"===o)return{original:t,x:e.x+e.width,y:t.y};if("b"===o)return{original:t,x:t.x,y:e.y+e.height};if("l"===o)return{original:t,x:e.x,y:t.y};throw new Error("unexpected dockingDirection: <"+o+">")}function m(t,e={x:0,y:0}){const o=t.gridPosition.row,i=t.gridPosition.col;return{width:h,height:u,x:i*h+e.x,y:o*u+e.y}}function x(t,e,o,i,n){const{width:r,height:s}=d(t),{x:a,y:c}=i;if(!n)return{width:r,height:s,x:o*h+(h-r)/2+a,y:e*u+(u-s)/2+c};const g=n.di.bounds;return{width:r,height:s,x:Math.round(g.x+g.width/2-r/2),y:Math.round(g.y+g.height-s/2)}}function y(t,e){const o=[e].flat();if(0===o.length)return t;let i=[];return o.forEach((e,n)=>{const r=t.filter(t=>a(t,e));if(i=i.concat(r),n===o.length-1&&i.length!==t.length)for(const e of t)i.includes(e)||i.push(e)}),i}class w{constructor(t){this.moddle=t}create(t,e){return this.moddle.create(t,e||{})}createDiBounds(t){return this.create("dc:Bounds",t)}createDiLabel(){return this.create("bpmndi:BPMNLabel",{bounds:this.createDiBounds()})}createDiShape(t,e,o){return this.create("bpmndi:BPMNShape",n({bpmnElement:t,bounds:this.createDiBounds(e)},o))}createDiWaypoints(t){var e=this;return r(t,function(t){return e.createDiWaypoint(t)})}createDiWaypoint(t){return this.create("dc:Point",s(t,["x","y"]))}createDiEdge(t,e,o){return this.create("bpmndi:BPMNEdge",n({bpmnElement:t,waypoint:this.createDiWaypoints(e)},o))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}class _{constructor(t=0,e=0){const o=Array(t);for(let t=0;t<o.length;t++)o[t]=Array(e);this._grid=o,this.isFlipped=!1,this._elements=new Set}get rowCount(){return this._grid.length}get elementsCount(){return this._elements.size}get elements(){return this._elements}get colCount(){const t=this._grid[0];return t&&t.length}add(t,e){if(!this.isValidPosition(e))return void this._addStart(t);const[o,i]=e;this._grid[o]||(this._grid[o]=[]),this._grid[o][i]?this._grid[o][i].add(t):this._grid[o][i]=new Set([t]),this._elements.add(t),this.toRectangle()}move(t,e){if(!this.elements.has(t))return;if(!this.isValidPosition(e))return;const o=this.find(t);this.isValidPosition(o)&&(this.removeElementAt(o),this.add(t,e))}removeElement(t){if(!t)return;if(!this.elements.has(t))return;const e=this.find(t);if(e){const o=this.get(e[0],e[1]);o.delete(t),this.elements.delete(t),0===o.size&&(this._grid[e[0]][e[1]]=null)}}createRow(t){t||Number.isInteger(t)?this._grid.splice(t+1,0,Array(this.colCount)):this._grid.push(Array(this.colCount))}createCol(t,e){this._grid.forEach((o,i)=>{this.expandRow(i,t,e)})}expandRow(t,e,o){if(!Number.isInteger(t)||t<0||t>this.rowCount-1)return;const i=Number.isInteger(o)&&o>0?Array(o):Array(1),n=this._grid[t];e||Number.isInteger(e)?n.splice(e+1,0,...i):n.splice(n.length,0,...i)}_addStart(t){this._grid.push([new Set([t])]),this._elements.add(t)}find(t){let e,o;if(e=this._grid.findIndex(e=>(o=e.findIndex(e=>e?.has(t)),-1!==o)),this.isValidPosition([e,o]))return[e,o]}get(t,e){return(this._grid[t]||[])[e]}getElementsInRange({row:t,col:e},{row:o,col:i}){const n=[];t>o&&([t,o]=[o,t]),e>i&&([e,i]=[i,e]);for(let r=t;r<=o;r++)for(let t=e;t<=i;t++){const e=this.get(r,t);e&&n.push(e)}return n}getGridDimensions(){const t=this._grid.length;let e=0;for(let o=0;o<t;o++){const t=this._grid[o]?.length;t>e&&(e=t)}return[t,e]}elementsByPosition(){const t=[];return this._grid.forEach((e,o)=>{e.forEach((e,i)=>{if(e)for(const n of[...e])t.push({element:n,row:o,col:i})})}),t}shrinkCols(){for(let t=this.colCount-1;t>=0;t--){if(this._grid.every(e=>null==e[t]))for(const e of this._grid)e.splice(t,1)}}shrinkRows(){this._grid=this._grid.filter(t=>!t.every(t=>null==t))}removeElementAt(t){const[e,o]=t,i=this.get(e,o);i&&(this._grid[e][o]=null,this.elements.delete(i))}toRectangle(){const[,t]=this.getGridDimensions();this._grid.forEach(e=>{if(e.length<t){const o=t-e.length;for(let t=0;t<o;t++)e.splice(e.length,0,null)}})}flipHorizontally(){for(const t of this._grid)t.reverse();this.isFlipped=!this.isFlipped}hasElement(t){return this.elements.has(t)}isValidPosition(t){if(!t||!Array.isArray(t)||2!==t.length)return!1;const[e,o]=t;return Number.isInteger(e)&&Number.isInteger(o)&&e>=0&&o>=0}hasIntermediateElements(t,e,o){if(!this.isValidPosition(t)||!this.isValidPosition(e))return!1;if(o){const[o,i]=t[0]<=e[0]?[t[0],e[0]]:[e[0],t[0]];for(let e=o+1;e<i;e++)if(this.hasElementAt([e,t[1]]))return!0;return!1}{const[o,i]=t[1]<=e[1]?[t[1],e[1]]:[e[1],t[1]];for(let e=o+1;e<i;e++)return this.hasElementAt([t[0],e]),!0;return!1}}hasElementAt(t){if(!this.isValidPosition(t))return!1;const[e,o]=t;return!!this.get(e,o)}}class b{constructor(t,e){this.source=t,this.target=e}}class C extends _{constructor(t,o,i){super(o,i),this.initialGraph=t,this.graph=new e}add(t,e){super.add(t,e),this.graph.addNode(t),this._createNewEdgesFor(t)}removeElement(t){this.graph.deleteNode(t),super.removeElement(t)}move(t,e){const o=[...this.get(...this.find(t))],i=o.reduce((t,e)=>[...t,...this.getAllExistingEdgesFor(e)],[]);for(const t of o)this.removeElement(t),this.add(t,e);for(const t of i){const e=new b(t.source,t.target);e.id=t.id,this._addEdgeToGrid(e)}}_edgeIsExist(t){return[...this.graph.edges].some(e=>e.id===t.id&&e.source===t.source&&e.target===t.target)}_addEdgeToGrid(t){this._edgeIsExist(t)||this.graph.addEdge(t)}_createNewEdgesFor(t){const e=[...this.initialGraph.edges].filter(e=>(e.source===t||e.target===t)&&this.hasElement(e.source)&&this.hasElement(e.target));for(const t of e){const e=new b(t.source,t.target);e.id=t.id,this._addEdgeToGrid(e)}}isCrossed(t,e){return[...this._allEdges].some(o=>this.isIntersect(o,t,e))}get _allEdges(){return this.graph.edges}hasAnyCross(t){return[...t||this._allEdges].some(t=>this.getCrossedElementsFor(t).length>0||this.getCrossedElementsFor(t,!0).length>0)}shakeIt(t){const e=[...this.elements].sort(t?(o=this,function(t,e){const i=o.find(t),n=o.find(e);return i&&!n?-1:!i&&n?1:i||n?i[0]-n[0]||n[1]-i[1]:0}):function(t){return function(e,o){const i=t.find(e),n=t.find(o);return i&&!n?-1:!i&&n?1:i||n?i[1]-n[1]||n[0]-i[0]:0}}(this)).reverse();for(var o;e.length>0;){const o=e.pop(),i=this.getChain(o,!t);for(const t of i){const o=e.indexOf(t);o>=0&&e.splice(o,1)}const[n,r]=this.find([...i][0]);if(!(t?n<=0:r<=0)){for(let e=t?n-1:r-1;e>=0;e--){const n=[...i].every(t=>{const e=this.find(t);return e&&!this.isCrossed(e,!0)&&!this.isCrossed(e,!1)});if(!n)break;const r=[...i].every(o=>{const i=this.find(o),n=t?e:i[0],r=t?i[1]:e;return!this.get(n,r)});if(!r)break;for(const o of i){const i=this.find(o);this.move(o,t?[e,i[1]]:[i[0],e])}const s=this.hasAnyCross(),d=t?this.getChain(o,!1):this.getChain(o,!0);if(s||d.size>i.size){for(const o of i){const i=this.find(o);this.move(o,t?[e+1,i[1]]:[i[0],e+1])}break}}t?this.shrinkRows():this.shrinkCols()}}}getChain(t,e,o){const i=o||new Set;if(!t)return i;const n=this.find(t);if(!n)return i;const r=[],s=this.get(n[0],n[1]);for(const t of s)i.add(t),[...this.getAllExistingEdgesFor(t)].filter(t=>e?"N_S"===this.getEdgeDirection(t)||"S_N"===this.getEdgeDirection(t):"W_E"===this.getEdgeDirection(t)||"E_W"===this.getEdgeDirection(t)).forEach(t=>r.push(t));for(const o of r){const n=o.source===t?o.target:o.source;if(!i.has(n)){const t=this.getChain(n,e,i);for(const e of t)i.add(e)}}return i}getExistingOutgoingEdgesFor(t){return this.isFlipped?[...this.graph.getIncomingEdgesFor(t)]:[...this.graph.getOutgoingEdgesFor(t)]}getExistingIncomingEdgesFor(t){return this.isFlipped?[...this.graph.getOutgoingEdgesFor(t)]:[...this.graph.getIncomingEdgesFor(t)]}getAllExistingEdgesFor(t){const e=this.getExistingOutgoingEdgesFor(t),o=this.getExistingIncomingEdgesFor(t);return new Set([...e,...o])}_separateGrid(){const t=e.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new C(this.initialGraph,this.colCount,this.rowCount)];const o=[];for(const e of t){const t=new C(this.initialGraph,this.colCount,this.rowCount);let i=null,n=null;for(const o of y(e.nodes,"bpmn:BoundaryEvent").reverse()){const e=this.find(o);(null===i||i>e[0])&&(i=e[0]),(null===n||n<e[0])&&(n=e[0]),t._superAdd(o,e),t.graph.addNode(o)}e.edges.forEach(e=>{t._addEdgeToGrid(e)}),o.push(t)}return o}_superAdd(t,e){super.add(t,e)}_mergeGrids(t){const e=t.reduce((t,e)=>t+e.rowCount,0),o=t.reduce((t,e)=>e.colCount>t?e.colCount:t,0),i=new C(t[0].initialGraph,e,o);return t.forEach((e,o)=>{let n=0;for(let e=0;e<o;e++)n+=t[e].rowCount;for(const t of e.elements){const[o,r]=e.find(t);i.add(t,[o+n,r])}}),i}getSourcePosition(t){const e=this.isFlipped?t.target:t.source;return this.find(e)}getTargetPosition(t){const e=this.isFlipped?t.source:t.target;return this.find(e)}getEdgeDirection(t){const e=this.getSourcePosition(t),o=this.getTargetPosition(t);if(!this.isValidPosition(e)||!this.isValidPosition(o))throw new Error(`Invalid position of source or target in ${t.source}-${t.target} flipped:${this.isFlipped}`);const[i,n]=e,[r,s]=o,d=i-r,a=n-s;return 0===d&&0===a?"NO_DIRECTION":d>0&&0===a?"S_N":d>0&&a<0?"SW_NE":0===d&&a<0?"W_E":d<0&&a<0?"NW_SE":d<0&&0===a?"N_S":i<r&&n>s?"NE_SW":i===r&&n>s?"E_W":i>r&&n>s?"SE_NW":void 0}getPathFor(t){const e=this.getEdgeDirection(t);return"NO_DIRECTION"===e?this._pathForNoDirection():"S_N"===e?this._pathForSouthToNorth(t):"SW_NE"===e?this._pathForSouthWestToNorthEast(t):"W_E"===e?this._pathForWestToEast(t):"NW_SE"===e?this._pathForNorthWestToSouthEast(t):"N_S"===e?this._pathForNorthToSouth(t):"NE_SW"===e?this._pathForNorthEastToSouthWest(t):"E_W"===e?this._pathForEastToWest(t):"SE_NW"===e?this._pathForSouthEastToNorthWest(t):[]}_pathForNoDirection(){return[]}_pathForSouthToNorth(t){const e=[],[o,i]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"===(this.isFlipped?t.target:t.source).$type&&t.id)return e;if(this.hasIntermediateElements(this.getSourcePosition(t),this.getTargetPosition(t),!0))return e;if([...this.getExistingOutgoingEdgesFor(this.isFlipped?t.source:t.target)].map(t=>this.isFlipped?t.source:t.target).includes(this.isFlipped?t.target:t.source))return e;for(let t=o-1;t>n;t--)e.push({position:[t,i],vCross:!0});return e}_pathForSouthWestToNorthEast(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==(this.isFlipped?t.target:t.source).$type||!t.id)for(let t=i+1;t<r;t++)e.push({position:[o,t],hCross:!0});e.push({position:[o,r],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,r],vCross:!0});return e}_pathForWestToEast(t){const e=[],[o,i]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=i+1;t<n;t++)e.push({position:[o,t],hCross:!0});return e}_pathForNorthWestToSouthEast(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,i],vCross:!0});e.push({position:[n,i],vCross:!0,hCross:!0});for(let t=i+1;t<r;t++)e.push({position:[n,t],hCross:!0});return e}_pathForNorthToSouth(t){const e=[],[o,i]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,i],vCross:!0});return e}_pathForNorthEastToSouthWest(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,i],vCross:!0});e.push({position:[n,i],vCross:!0,hCross:!0});for(let t=i-1;t>r;t--)e.push({position:[n,t],hCross:!0});return e}_pathForEastToWest(t){const e=[],[o,i]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=i-1;t>n;t--)e.push({position:[o,t],hCross:!0});return e}_pathForSouthEastToNorthWest(t){const e=[],[o,i]=this.getSourcePosition(t),[n,r]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==(this.isFlipped?t.target:t.source).$type||!t.id)for(let t=i-1;t>r;t--)e.push({position:[o,t],hCross:!0});"bpmn:BoundaryEvent"===(this.isFlipped?t.target:t.source).$type&&t.id?e.push({position:[o,r],vCross:!0}):e.push({position:[o,r],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,r],vCross:!0});return e}getCrossedElementsFor(t,e=!1){const o=[];for(const i of this.getPathFor(t)){const[t,n]=i.position,r=this.get(t,n);r&&(e&&i.vCross||!e&&i.hCross)&&o.push(r)}return o}isIntersect(t,e,o){const[i,n]=e,[r,s]=this.getSourcePosition(t),[d,a]=this.getTargetPosition(t),c=this.getEdgeDirection(t);return"S_N"===c?!!(o&&n===s&&i<r&&i>d):"SW_NE"===c?!!(o&&n===a&&i<=r&&i>d)||!o&&n>s&&n<=a&&i===r:"W_E"===c?!o&&n>s&&n<a&&i===r:"NW_SE"===c?!!(o&&n===s&&i>r&&i<=d)||!o&&n>=s&&n<a&&i===d:"N_S"===c?!!(o&&n===s&&i>r&&i<d):"NE_SW"===c?!!(o&&n===s&&i>r&&i<=d)||!o&&n>a&&n<=s&&i===d:"E_W"===c?!o&&n>a&&n<s&&i===r:"SE_NW"===c&&(!!(o&&n===a&&i>d&&i<=r)||!o&&n>=a&&n<s&&i===r&&!this.getExistingIncomingEdgesFor(this.isFlipped?t.target:t.source).some(t=>"NW_SE"===this.getEdgeDirection(t)||"W_E"===this.getEdgeDirection(t)))}getBackwardUpOutgoingEdgesFor(t){return this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeDirection(t);return"S_N"===e||"SE_NW"===e})}getGridCopy(){return this._mergeGrids([this])}removeEdge(t){this.graph.deleteEdge(t)}getGraphSegmentFrom(t){const o=new e;return this.graph.genericTraversing((e,o)=>{if(!e.has(t))return t},t=>t.pop(),(t,e,i,n)=>{const r=this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.isFlipped?t.source:t.target,o=this.isFlipped?t.target:t.source;return!i.has(e)&&e!==o});o.addNode(t);const s=r.map(t=>this.isFlipped?t.source:t.target);for(const t of s)o.addNode(t);for(const t of r)o.addEdge(t);return s},(t,e)=>{for(const o of t)e.push(o)}),o}getSegmentLeftCoordinates(t){const e=new Map;for(const o of t.nodes){const[t,i]=this.find(o);e.has(t)||e.set(t,i),i<e.get(t)&&e.set(t,i)}return e}}function F(t,e,o,i,n){const r=(e.isFlipped?[...e.initialGraph.getIncomingEdgesFor(t)]:[...e.initialGraph.getOutgoingEdgesFor(t)]).filter(t=>!e.hasElement(e.isFlipped?t.source:t.target)).map(t=>e.isFlipped?t.source:t.target),s=function(t,e,o,i,n,r){const s=e.getExistingOutgoingEdgesFor(t),d=e.getExistingIncomingEdgesFor(t).map(t=>e.isFlipped?t.target:t.source),[,a]=e.find(t),c=[...s].filter(t=>{const n=e.isFlipped?t.source:t.target,r=e.find(n)[1];if(!P(n,o,e))return!1;return(!e.getExistingIncomingEdgesFor(n).map(t=>t.source).find(t=>d.includes(t))||!i)&&r<=a}).map(t=>e.isFlipped?t.source:t.target).sort(function(t){return function(e,o){const i=t.find(e),n=t.find(o);return i&&!n?-1:!i&&n?1:i||n?i[0]-n[0]||i[1]-n[1]:0}}(e));for(const t of c)o.splice(o.indexOf(t),1),r.delete(t),e.removeElement(t);return e.shrinkRows(),e.shrinkCols(),c}(t,e,o,r&&r.length>0,0,i);let d=[...r,...s],a=[];d.forEach(r=>{const s=function(t,e,o){if(e.isFlipped&&"bpmn:BoundaryEvent"===o.$type&&e.hasElement(o.attachedToRef))return e.find(o.attachedToRef);if(o.attachedToRef===t)return e.find(t);const i=e.find(t);if(!i)return;const n="bpmn:BoundaryEvent"===t.$type&&!e.isFlipped,r=n?[i[0]+1,i[1]+1]:[i[0],i[1]+1],s=e.find(t),d=[...e.get(s[0],s[1])],a=[];for(const t of d)[...e.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source).forEach(t=>a.push(t));for(let t=r[0];t<=e.rowCount;t++){const o=[t,r[1]],i=a.some(i=>e.getTargetPosition(i)[0]===t&&e.getTargetPosition(i)[1]===r[1]||e.isIntersect(i,o,!1)||e.isIntersect(i,o,!0));if(i&&t===e.colCount-1)r[0]=t+1;else if(!i){r[0]=t;break}}(e.get(r[0],r[1])||e.isCrossed(r,!0))&&e.createCol(r[1]-1);e.isCrossed([r[0],r[1]])&&e.createRow(r[0]-1);return r}(t,e,r);if(e.isFlipped&&"bpmn:BoundaryEvent"===r.$type&&r.attachedToRef!==t){const t=r.attachedToRef;i.has(t)||(e.add(t,s),i.add(t),S(t,e,n,i,o,a),a.unshift(r))}e.add(r,s),i.add(r),S(r,e,n,i,o,a),function(t,e){let o=e.getBackwardUpOutgoingEdgesFor(t);for(;o.length>0;){const i=o.shift(),n=e.isFlipped?i.source:i.target;if(e.find(n)[1]>e.find(t)[1])continue;const r=e.getGridCopy(),s=[...r._allEdges].find(t=>t.source===i.source&&t.target===i.target&&t.id===i.id);r.removeEdge(s);const d=e.isFlipped?s.target:s.source,a=e.isFlipped?s.source:s.target,c=r.find(d),g=r.find(a);for(const t of r.elements){r.find(t)[1]<g[1]&&r.removeElement(t)}const h=r.getGraphSegmentFrom(a);if(h.nodes.includes(d))continue;const u=Array.from(r.getSegmentLeftCoordinates(h)),l=u.reduce((t,e)=>void 0===t||e[1]<t[1]?e:t,void 0),f=u.reduce((t,e)=>void 0===t||e[0]<t[0]?e:t,void 0),p=u.reduce((t,e)=>void 0===t||e[0]>t[0]?e:t,void 0),E=c[1]-l[1]+1,m=new Map(u);let x;for(let o=0;o<e.rowCount;o++){const i=(()=>{if(o<f[0])return x=f[0],f[1]-1;if(m.has(o))return x=m.get(o),m.get(o)-1;if(o>f[0]&&o<p[0]&&o<e.find(t)[0])return x;if(o===e.find(t)[0]||o>f[0]){const o=e.find(t);return x=o,o[1]}return x})();e.expandRow(o,i,E)}}e.toRectangle()}(r,e),a.unshift(r)});const c=[],g=[];for(const t of a)"bpmn:BoundaryEvent"===t.$type?c.push(t):g.push(t);return c.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),a=[...c,...g],a}function P(t,e,o){const i=e.includes(t),n=o.getExistingOutgoingEdgesFor(t);return i&&(!n||0===n.length)}function S(t,e,o,i,n,r,s,d){const a=[...[...e.getExistingOutgoingEdgesFor(t)].sort((t,o)=>{const[i,n]=e.getTargetPosition(t),[r,s]=e.getTargetPosition(o);return i-r||n-s}),...[...e.getExistingIncomingEdgesFor(t)].sort((t,o)=>{const[i,n]=e.getSourcePosition(t),[r,s]=e.getSourcePosition(o);return i-r||n-s})].filter(o=>{const{target:i,source:s}=o,d=e.getEdgeDirection(o);return i!==s&&((!r||!r.includes(i))&&((!n||!P(i,n,e))&&(s!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of a)v(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===(e.isFlipped?t.target:t.source).$type&&t.id||N(t,e)}function v(t,e){const o=e.getEdgeDirection(t);if("W_E"===o||"E_W"===o)return;const i=e.getCrossedElementsFor(t,!0);i.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||W(i,e):D(i,e):W(i,e):D(i,e))}function N(t,e){const o=e.getEdgeDirection(t);if("S_N"===o||"N_S"===o)return;const i=e.getCrossedElementsFor(t,!1);if(0!==i.length){if("SW_NE"===o){const o=function(t,e){const o=e.getSourcePosition(t),i=e.getTargetPosition(t);let n=o[0];for(let t=o[0];t<e.rowCount&&e.getElementsInRange({row:t,col:o[1]+1},{row:t,col:i[1]}).length>0;t++)n++;return n-o[0]}(t,e),[i]=e.getSourcePosition(t);for(let t=o;t>0;t--)e.createRow(i-1);const n=e.getElementsInRange({row:e.getSourcePosition(t)[0],col:e.getSourcePosition(t)[1]+1},{row:e.rowCount-1,col:e.colCount-1});for(const t of n){const[i,n]=e.find([...t][0]);for(const r of[...t])e.move(r,[i-o,n])}return}"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||T(i,e):R(i,e):T(i,e):R(i,e)}}function T(t,e){const[o]=e.find([...t[0]][0]);e.createRow(o-1);for(const i of t){const[,t]=e.find([...i][0]);for(const n of[...i])e.move(n,[o,t])}}function R(t,e){const[o]=e.find([...t[0]][0]);e.createRow(o);for(const i of t){const[,t]=e.find([...i][0]);for(const n of[...i])e.move(n,[o+1,t])}}function D(t,e){const[,o]=e.find([...t[0]][0]);e.createCol(o);for(const i of t){const[t]=e.find([...i][0]);for(const n of[...i])e.move(n,[t,o+1])}}function W(t,e){const[,o]=e.find([...t[0]][0]);e.createCol(o-1);for(const i of t){const[t]=e.find([...i][0]);for(const n of[...i])e.move(n,[t,o])}}function I(t,e,o,i,n,r){if(t.di)return[];if("bpmn:BoundaryEvent"===t.$type)return function(t,e,o,i,n,r){const s=t.attachedToRef.di.bounds;if(!s)throw new Error(`Create DI for ${t.id}. Nо hostBounds`);const d=[];let a=t.$parent.flowElements.filter(e=>e.attachedToRef===t.attachedToRef&&n.hasElement(t));return a=function(t,e){return t.sort((t,o)=>{const i=O(t,e),n=O(o,e);return i[0]-n[0]||i[1]-n[1]}).reverse()}(a,n),a.forEach((n,a,c)=>{n.gridPosition={row:e,col:o};const g=x(n,e,o,r,t.attachedToRef);g.x=s.x+(a+1)*(s.width/(c.length+1))-g.width/2;const h=i.createDiShape(n,g,{id:n.id+"_di"});n.di=h,n.gridPosition={row:e,col:o},d.push(h)}),d}(t,e,o,i,n,r);const s=x(t,e,o,r);if(t.isExpanded&&t.grid){const{width:e,height:o}=d(t),{rowCount:i,colCount:n}=t.grid;s.width=n*h+e,s.height=i*u+o}const c={id:t.id+"_di"};t.isExpanded&&(c.isExpanded=!0),a(t,"bpmn:ExclusiveGateway")&&(c.isMarkerVisible=!0);const g=i.createDiShape(t,s,c);return t.di=g,t.gridPosition={row:e,col:o},[g]}function O(t,e){return c(t).reduce((t,o)=>{if(!e.hasElement(o))return t;const i=e.find(o);return void 0===t||t[0]<i[0]||t[1]<i[1]?i:t},[0,0])}class G{constructor(e){this.moddle=new t,this.diFactory=new w(this.moddle),this.maxDebugStep=e,this.currentDebugStep=0}async layoutProcess(t){const e=await this.moddle.fromXML(t),{rootElement:o}=e;this.diagram=o,function(t){const e=t.elementsById;if(e)for(const t of Object.values(e))"bpmndi:BPMNShape"===t.$type&&!0===t.isExpanded&&(t.bpmnElement.isExpanded=!0)}(e),this.processTrees=this.createNestedSets(e),this.createGridsForProcesses(),this.expandProcessesGrids();const i=this.getRootProcesses(),n=this.getCollaboration();return i.length>0&&(this.cleanDi(),this.createRootDi(i,n),this.drawParticipants(),this.drawProcesses(),this.drawCollaborationMessageFlows(n)),(await this.moddle.toXML(this.diagram,{format:!0})).xml}createGridsForProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){e.grid=this.createGridLayout(e);const t=e.grid._separateGrid()||[e.grid];for(const e of t)e.shrinkRows(),e.shrinkCols(),e.shakeIt(!0),e.shakeIt(!1);e.grid=e.grid._mergeGrids(t),e.grid.toRectangle()}}expandProcessesGrids(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>e.level-t.level);for(const e of t){const t=e.grid._separateGrid()||[e.grid];for(const e of t)e.shrinkRows(),e.shrinkCols(),B(e),$(e);if(e.grid=e.grid._mergeGrids(t),e.grid.toRectangle(),e.isExpanded){const{colCount:t,rowCount:o}=e.grid;0===o&&e.grid.createRow(),0==t&&e.grid.createCol()}}}drawParticipants(){const t=this.getCollaboration();if(!t||!t[0])return;const e=t[0].participants;let o=0;for(const t of e)o=this.createParticipantDi(t,{x:0,y:o})+70}drawProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){const t=this.getParticipantForProcess(e);if(t){const o=this.getElementDi(t),i=this.getProcDi(o);let{x:n,y:r}=o.bounds;n+=75,r+=70,this.generateDi(e.grid,{x:n,y:r},i);continue}if(e.isExpanded&&!this.existingNodes.includes(e))continue;if(e.isExpanded){const t=this.getElementDi(e),o=this.getProcDi(t);let{x:i,y:n}=t.bounds;const{width:r,height:s}=d(e);i+=75-r/4,n+=u-s-s/4,this.generateDi(e.grid,{x:i,y:n},o);continue}const o=this.diagram.diagrams.find(t=>t.plane.bpmnElement===e);this.generateDi(e.grid,{x:0,y:0},o)}}get existingNodes(){return this.processTrees.map(t=>[...t.nodes]).flat().map(t=>[...t.grid.elements]).flat()}drawCollaborationMessageFlows(t){const e=t[0]?t[0].messageFlows:null;if(e)for(const t of e){const{sourceRef:e,targetRef:o}=t,i=this.existingNodes;if(!i.includes(e)||!i.includes(o))continue;const n=e.di.bounds,r=o.di.bounds,s=r.y-n.y,d=[{x:n.x+n.width/2},{x:r.x+r.width/2}];s>0?(d[0].y=n.y+n.height,d[1].y=r.y):(d[0].y=n.y,d[1].y=r.y+r.height);const a=this.diFactory.createDiEdge(t,d);this.diagram.diagrams[0].plane.get("planeElement").push(a)}}getParticipantForProcess(t){const e=this.getCollaboration();if(!e||!e[0])return;const o=this.getCollaboration()[0].participants;return o?o.find(e=>e.processRef===t):void 0}getElementDi(t){return this.diagram.diagrams.map(t=>t.plane.planeElement).flat().find(e=>e.bpmnElement===t)}getProcDi(t){return this.diagram.diagrams.find(e=>e.plane.planeElement.includes(t))}createNestedSets(t){const n=new e,r=function(t){const e=t.elementsById;return e?Object.values(e).filter(t=>"bpmn:Process"===t.$type||"bpmn:SubProcess"===t.$type):[]}(t);for(const t of r)n.addNode(t);for(const t of r){const e=this.getSubProcesses(t);for(const o of e)n.addEdge({source:t,target:o})}const s=n.getSeparatedGraphs();for(const t of s){const e=t.nodes.filter(e=>0===t.getIncomingEdgesFor(e).size);if(e.length>1)throw new Error("Process tree has more than 1 root elements");if(0===e.length)throw new Error("Process tree has more than 0 root elements");t.rootProcess=e[0]}for(const t of s){const e=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},n=(t,e,o)=>{const{left:i,level:n}=t,r=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(r)return r.level=n+1,r.left=i+1,[r];const s=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).reduce((t,e)=>{if(void 0===t||e.right>t.right)return e.right},void 0);t.right=s?s+1:t.left+1;const d=[...e.getIncomingEdgesFor(t)].map(t=>t.source).find(t=>void 0===t.right);return d?[d]:void 0};t.genericTraversing(e,o,n,i)}return s}getSubProcesses(t){return t.flowElements?t.flowElements.filter(t=>"bpmn:SubProcess"===t.$type):[]}createRootDi(t,e){const o=e&&e.length>0?e[0]:t[0];this.createProcessDi(o)}createProcessDi(t){const e=this.diFactory,o=e.createDiPlane({id:"BPMNPlane_"+t.id,bpmnElement:t}),i=e.createDiDiagram({id:"BPMNDiagram_"+t.id,plane:o});return this.diagram.diagrams.push(i),i}createParticipantDi(t,e){const{colCount:o,rowCount:i}=t.processRef.grid,{width:n,height:r}=d(t),s=o>0?o*h+h:n,a=i>0?i*u+u:r,c=this.diFactory.createDiShape(t,{width:s,height:a,...e},{id:t.id+"_di"});return this.diagram.diagrams[0].plane.get("planeElement").push(c),c.bounds.y+c.bounds.height}cleanDi(){this.diagram.diagrams=[]}createGridLayout(t){const o=new e,i=new C(o);for(const e of t.flowElements||[])a(e,"bpmn:SequenceFlow")||a(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new b(t,t.attachedToRef)),o.addEdge(new b(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new b(e.sourceRef,e.targetRef);t.id=e.id,o.addEdge(t)}const e=t.dataInputAssociations;for(const t of e||[])for(const e of t.sourceRef||[]){const i=new b(e,t.$parent);i.id=t.id,i.propRef=t.targetRef,o.addEdge(i)}const i=t.dataOutputAssociations;for(const t of i||[]){const e=t.$parent,i=t.targetRef,n=new b(e,i);n.id=t.id,o.addEdge(n)}}return o.genericTraversing((t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const o=[...t].find(o=>i.isFlipped?[...e.getOutgoingEdgesFor(o)].filter(e=>!t.has(e.target)).length>0:[...e.getIncomingEdgesFor(o)].filter(e=>!t.has(e.source)).length>0);if(o)return i.flipHorizontally(),o;const n=e.nodes.filter(o=>i.isFlipped?!t.has(o)&&0===e.getOutgoingEdgesFor(o).size&&!g(o):!t.has(o)&&0===e.getIncomingEdgesFor(o).size&&!g(o));if(n.length>0)return y(n,"bpmn:StartEvent")[0];const r=[...t].find(o=>(i.isFlipped?[...e.getIncomingEdgesFor(o)].filter(e=>!t.has(e.source)):[...e.getOutgoingEdgesFor(o)].filter(e=>!t.has(e.target))).length>0);if(r)return r;const s=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(i.isFlipped?[...e.getOutgoingEdgesFor(o)].filter(t=>t.target!==o):[...e.getIncomingEdgesFor(o)].filter(t=>t.source!==o)).length});if(s)return s;const d=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(i.isFlipped?[...e.getIncomingEdgesFor(o)].filter(t=>t.target!==t.source):[...e.getOutgoingEdgesFor(o)]).length});return d?(i.flipHorizontally(),d):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))},t=>t.pop(),(t,e,o,n)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const r=F(t,i,n,o,e);return this.currentDebugStep+=1,r},(t,e)=>{for(const o of t)e.push(o)},t=>{i.hasElement(t)||i.add(t)}),i.isFlipped&&i.flipHorizontally(),i}generateDi(t,e,o){const i=this.diFactory,n=(o||this.diagram.diagrams[0]).plane.get("planeElement");t.elementsByPosition().sort((t,e)=>{const o="bpmn:BoundaryEvent"===t.element.$type?0:1;return("bpmn:BoundaryEvent"===e.element.$type?0:1)-o}).forEach(({element:o,row:r,col:s})=>{const d=I(o,r,s,i,t,e);n.push(...d)}),t._allEdges.forEach(o=>{const r=function(t,e,o,i){const{id:n}=t;if(!n)return;const r=f(t,e,!1,i);return o.createDiEdge(t,r,{id:n+"_di"})}(o,t,i,e);r&&n.push(r)})}getRootProcesses(){return this.diagram.get("rootElements").filter(t=>"bpmn:Process"===t.$type)}getCollaboration(){return this.diagram.get("rootElements").filter(t=>"bpmn:Collaboration"===t.$type)}}function B(t){for(let e=t.colCount-1;e>=0;e--){const o=[];for(let i=0;i<t.rowCount;i++){const n=[...t.get(i,e)||[]].find(t=>t.isExpanded);n&&o.push(n)}if(0===o.length)continue;const i=o.reduce((t,e)=>{if(void 0===t||e.grid.colCount>t)return e.grid.colCount},void 0),n=i||2;t.createCol(e,n)}}function $(t){for(let e=t.rowCount-1;e>=0;e--){const o=[];for(let i=0;i<t.colCount;i++){const n=[...t.get(e,i)||[]].find(t=>t.isExpanded);n&&o.push(n)}if(0===o.length)continue;const i=o.reduce((t,e)=>{if(void 0===t||e.grid.rowCount>t)return e.grid.rowCount},void 0),n=i||1;for(let o=0;o<n;o++)t.createRow(e)}}function A(t,e){return new G(e).layoutProcess(t)}export{A as layoutProcess};//# sourceMappingURL=index.js.map