yet-another-bpmn-auto-layout 0.0.1 → 0.0.2
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/README.md +8 -6
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,21 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
Create and layout the graphical representation of a BPMN diagram.
|
|
4
4
|
|
|
5
|
-
## [DEMO](https://ivantulaev.github.io/bpmn-auto-layout
|
|
5
|
+
## [DEMO](https://ivantulaev.github.io/yet-another-bpmn-auto-layout/)
|
|
6
6
|
|
|
7
7
|
## Differences with original repository
|
|
8
8
|
|
|
9
9
|
* Given a collaboration **ALL** participant's process will be laid out.
|
|
10
10
|
* **Collapsed** and **expanded sub-processes** has different layout.
|
|
11
11
|
* Independent graphs of process are laid out on different grid lines.
|
|
12
|
+
* **Data Associations** are laid out as task or gateway
|
|
12
13
|
* Happy path not very happy, but large process graphs are more understandable for humans :)
|
|
14
|
+
* Example with **step-by-step debug mode**
|
|
13
15
|
|
|
14
16
|
## Usage
|
|
15
17
|
|
|
16
18
|
This library works with [Node.js](https://nodejs.org/) and in the browser.
|
|
17
19
|
|
|
18
20
|
```javascript
|
|
19
|
-
import { layoutProcess } from 'bpmn-auto-layout
|
|
21
|
+
import { layoutProcess } from 'yet-another-bpmn-auto-layout';
|
|
20
22
|
|
|
21
23
|
import diagramXML from './diagram.bpmn';
|
|
22
24
|
|
|
@@ -30,19 +32,19 @@ console.log(diagramWithLayoutXML);
|
|
|
30
32
|
* The following elements are not laid out:
|
|
31
33
|
* Groups
|
|
32
34
|
* Text annotations
|
|
33
|
-
*
|
|
35
|
+
* Lanes
|
|
34
36
|
|
|
35
37
|
## Resources
|
|
36
38
|
|
|
37
|
-
* [Issues](https://github.com/IvanTulaev/bpmn-auto-layout
|
|
39
|
+
* [Issues](https://github.com/IvanTulaev/yet-another-bpmn-auto-layout/issues)
|
|
38
40
|
|
|
39
41
|
|
|
40
42
|
|
|
41
43
|
## Run example (for BAs, SAs and other business people :) )
|
|
42
44
|
1. Run in terminal commands below
|
|
43
45
|
```sh
|
|
44
|
-
git clone https://github.com/IvanTulaev/bpmn-auto-layout
|
|
45
|
-
cd bpmn-auto-layout
|
|
46
|
+
git clone https://github.com/IvanTulaev/yet-another-bpmn-auto-layout.git
|
|
47
|
+
cd yet-another-bpmn-auto-layout
|
|
46
48
|
npm install
|
|
47
49
|
npm start
|
|
48
50
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("bpmn-moddle"),e=require("min-dash"),o=require("graph-by-ivan-tulaev");function i(t){return n(t,"bpmn:SubProcess")||n(t,"bpmn:Task")?{width:100,height:80}:n(t,"bpmn:Gateway")?{width:50,height:50}:n(t,"bpmn:Event")?{width:36,height:36}:n(t,"bpmn:Participant")||n(t,"bpmn:Lane")?{width:400,height:100}:n(t,"bpmn:DataObjectReference")?{width:36,height:50}:n(t,"bpmn:DataStoreReference")?{width:50,height:50}:n(t,"bpmn:TextAnnotation")?{width:100,height:30}:{width:100,height:80}}function n(t,e){return t.$instanceOf(e)}function r(t){return!!t.attachedToRef}function s(t){let e=new Set;if(t){(t.outgoing||[]).map((t=>t.targetRef)).filter((t=>t)).forEach((t=>e.add(t)))}return[...e]}function a(t){return(t.incoming||[]).map((t=>{if(!t.sourceRef)throw new Error(`${t.id} has no sourceRef`);return t.sourceRef})).filter((t=>!r(t)))}function c(t){let e=(t.incoming||[]).map((t=>t.sourceRef)).filter((t=>r(t))).map((t=>t.attachedToRef));return e=new Set(e),[...e]}function g(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]}function d(t){return(n(t,"bpmn:IntermediateThrowEvent")||n(t,"bpmn:IntermediateCatchEvent"))&&(void 0===t.incoming||0===t.incoming.length)}const h=150,u=140;function l(t,e){return new Set(s(t).concat(g(t)))}function f(t,e,o,n=!1,r){const a=t.di,c=e.di,g=a.get("bounds"),d=c.get("bounds"),h=p(g),f=p(d),_=e.gridPosition.col-t.gridPosition.col,x=e.gridPosition.row-t.gridPosition.row,y=`${x>0?"bottom":"top"}-${_>0?"right":"left"}`,w=`${x>0?"top":"bottom"}-${_>0?"left":"right"}`,{x:b,y:C}=E(t,r),{x:P}=E(e,r);if("bpmn:BoundaryEvent"===t.$type){if(0===_&&0===x)return[m(h,g,"b",y),{x:h.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:P,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:b,y:f.y},m(f,d,"l",w)];if(0===_&&x<0)return[m(h,g,"b",y),{x:h.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:b,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:P,y:f.y},m(f,d,"l",w)];if(_>0&&x<0)return[m(h,g,"b",y),{x:h.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:f.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},m(f,d,"b",w)];if(_>0&&0===x)return[m(h,g,"b",y),{x:h.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:f.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},m(f,d,"b",w)];if(_>0&&x>0)return[m(h,g,"b"),{x:h.x,y:f.y},m(f,d,"l")];if(0===_&&x>0){const e=m(h,g,"b",y),o=m(f,d,"t",w);return t.attachedToRef.isExpanded?[e,{x:h.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:f.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},o]:[e,o]}if(_<0&&x>0)return[m(h,g,"b"),{x:h.x,y:f.y},m(f,d,"r")];if(_<0&&0===x){const i=function(t,e,o){const i=t.attachedToRef?t.attachedToRef:t,n=e.attachedToRef?e.attachedToRef:e,[r,s]=o.find(i),[,a]=o.find(n),c=s<a?s:a,g=s<a?a:s,d=[...o.elements].map((t=>void 0!==t.size?[...t]:t)).flat().filter((t=>t.gridPosition.row===r&&t.gridPosition.col>c&&t.gridPosition.col<g));return d.reduce(((t,e)=>e.grid?.getGridDimensions()[0]>t?e.grid?.getGridDimensions()[0]:t),0)}(t,e,o);return[m(h,g,"b",y),{x:h.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u+i*u},{x:f.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u+i*u},m(f,d,"b",w)]}if(_<0&&x<0)return[m(h,g,"b",y),{x:h.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},{x:f.x,y:t.attachedToRef.isExpanded?C+(t.attachedToRef.grid.rowCount+1)*u:C+u},m(f,d,"b",w)]}else{if(0===_&&0===x)return[m(h,g,"b",y),{x:h.x,y:t.isExpanded?C+(t.grid.rowCount+1)*u:C+u},{x:P,y:t.isExpanded?C+(t.grid.rowCount+1)*u:C+u},{x:b,y:f.y},m(f,d,"l",w)];if(0===_&&x<0){const i=[];for(let n=t.gridPosition.row-1;n>e.gridPosition.row;n--){const e=o.get(n,t.gridPosition.col);e&&i.push(e)}const n=[...l(e)].includes(t);return i.length>0||n?[m(h,g,"l",y),{x:b,y:h.y},{x:P,y:f.y},m(f,d,"l",w)]:[m(h,g,"t",y),m(f,d,"b",w)]}if(_>0&&x<0)return[m(h,g,"r"),{x:f.x,y:h.y},m(f,d,"b")];if(_>0&&0===x){const o=m(h,g,"r",y);t.isExpanded&&(o.y=g.y+i(t).height/2);const n=m(f,d,"l",w);return e.isExpanded&&(n.y=d.y+i(e).height/2),[o,n]}if(_>0&&x>0)return[m(h,g,"b"),{x:h.x,y:f.y},m(f,d,"l")];if(0===_&&x>0){const o=m(h,g,"b",y),i=m(f,d,"t",w);return t.isExpanded||e.isExpanded?[o,{x:h.x,y:t.isExpanded?C+(t.grid.rowCount+1)*u:C+u},{x:f.x,y:t.isExpanded?C+(t.grid.rowCount+1)*u:C+u},i]:[o,i]}if(_<0&&x>0)return[m(h,g,"b"),{x:h.x,y:f.y},m(f,d,"r")];if(_<0&&0===x){const n=[];for(let i=t.gridPosition.col-1;i>e.gridPosition.col;i--){const e=o?o.get(t.gridPosition.row,i):null;e&&n.push(e)}const r=s(e).includes(t);let a=o?o.outgoing.get(e):[];if(a=a.some((t=>"SW_NE"===t.direction)),n.length>0||r||a)return[m(h,g,"b",y),{x:h.x,y:C+(t.isExpanded?u*(t.grid.colCount+1.5):u)},{x:f.x,y:C+(t.isExpanded?u*(t.grid.colCount+1.5):u)},m(f,d,"b",w)];{const o=m(h,g,"l",y);t.isExpanded&&(o.y=g.y+i(t).height/2);const n=m(f,d,"r",w);return e.isExpanded&&(n.y=d.y+i(e).height/2),[o,n]}}if(_<0&&x<0){const i=[];for(let n=t.gridPosition.col-1;n>=e.gridPosition.col;n--){const e=o?o.get(t.gridPosition.row,n):null;e&&i.push(e)}return i.length>0?[m(h,g,"b",y),{x:h.x,y:t.isExpanded?C+(t.grid.rowCount+1)*u:C+u},{x:f.x,y:t.isExpanded?C+(t.grid.rowCount+1)*u:C+u},m(f,d,"b",w)]:[m(h,g,"l"),{x:f.x,y:h.y},m(f,d,"b")]}}const S=function(t,e,o){const{row:i,col:n}=t.gridPosition,{row:r,col:s}=e.gridPosition,a=s-n,c=r-i;if(!(a>0&&0!==c))return;if(c>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"]}}(t,e,o);if(S){const t=m(h,g,S[0],y),e=m(f,d,S[1],w);return[t,"h"===S[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const F=-Math.sign(x)*u/2;return[m(h,g,"r",y),{x:h.x+75,y:h.y},{x:h.x+75,y:f.y+F},{x:f.x-75,y:f.y+F},{x:f.x-75,y:f.y},m(f,d,"l",w)]}function p(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function m(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 E(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 _(t,e,o,n,r){const{width:s,height:a}=i(t),{x:c,y:g}=n;if(!r)return{width:s,height:a,x:o*h+(h-s)/2+c,y:e*u+(u-a)/2+g};const d=r.di.bounds;return{width:s,height:a,x:Math.round(d.x+d.width/2-s/2),y:Math.round(d.y+d.height-a/2)}}function x(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}}class y{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,o,i){return this.create("bpmndi:BPMNShape",e.assign({bpmnElement:t,bounds:this.createDiBounds(o)},i))}createDiWaypoints(t){var o=this;return e.map(t,(function(t){return o.createDiWaypoint(t)}))}createDiWaypoint(t){return this.create("dc:Point",e.pick(t,["x","y"]))}createDiEdge(t,o,i){return this.create("bpmndi:BPMNEdge",e.assign({bpmnElement:t,waypoint:this.createDiWaypoints(o)},i))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}const w=[{createElementDi:({element:t,row:e,col:o,diFactory:r,shift:s})=>{if(t.di)return;if("bpmn:BoundaryEvent"===t.$type)return function({element:t,row:e,col:o,diFactory:i,shift:n}){const r=t.attachedToRef.di.bounds;if(!r)throw new Error(`Create DI for ${t.id}. Nо hostBounds`);const s=[],a=t.$parent.flowElements.filter((e=>e.attachedToRef===t.attachedToRef));return a.sort(((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0))).forEach(((a,c,g)=>{a.gridPosition={row:e,col:o};const d=_(a,e,o,n,t.attachedToRef);d.x=r.x+(c+1)*(r.width/(g.length+1))-d.width/2;const h=i.createDiShape(a,d,{id:a.id+"_di"});a.di=h,a.gridPosition={row:e,col:o},s.push(h)})),s}({element:t,row:e,col:o,diFactory:r,shift:s});const a=_(t,e,o,s);if(t.isExpanded&&t.grid){const{width:e,height:o}=i(t),{rowCount:n,colCount:r}=t.grid;a.width=r*h+e,a.height=n*u+o}const c={id:t.id+"_di"};t.isExpanded&&(c.isExpanded=!0),n(t,"bpmn:ExclusiveGateway")&&(c.isMarkerVisible=!0);const g=r.createDiShape(t,a,c);return t.di=g,t.gridPosition={row:e,col:o},g}},{createConnectionDi:({element:t,row:e,col:o,layoutGrid:i,diFactory:n,shift:r})=>(t.outgoing||[]).filter((t=>i.elements.has(t.targetRef))).map((e=>{const o=e.targetRef,s=f(t,o,i,!1,r);return n.createDiEdge(e,s,{id:e.id+"_di"})}))},{createConnectionDi:({element:t,row:e,col:o,layoutGrid:i,diFactory:n,shift:r})=>(t.attachers||[]).sort(((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0))).flatMap((t=>(t.outgoing||[]).filter((t=>i.elements.has(t.targetRef))).map((s=>{const a=s.targetRef,c=f(t,a,i,!0,r);return function(t,e,[o,i]){const n=t.di.get("bounds"),r=m(p(n),n,"b");if(e[0].x===r.x&&e[0].y===r.y)return;const s=t.grid||t.attachedToRef?.grid;if(2===e.length){const t=[r,{x:r.x,y:s?(o+s.getGridDimensions()[0]+1)*u:(o+1)*u},{x:s?(i+s.getGridDimensions()[1]+1)*h:(i+1)*h,y:s?(o+s.getGridDimensions()[0]+1)*u:(o+1)*u},{x:s?(i+s.getGridDimensions()[1]+1)*h:(i+1)*h,y:s?o*u+70:(o+.5)*u}];return void e.splice(0,1,...t)}const a=[r,{x:r.x,y:s?(o+s.getGridDimensions()[0]+1)*u:(o+1)*u},{x:e[1].x,y:s?(o+s.getGridDimensions()[0]+1)*u:(o+1)*u}];e.splice(0,1,...a)}(t,c,[e,o]),n.createDiEdge(s,c,{id:s.id+"_di"})}))))}];class b{constructor(){this._grid=[],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);this.removeElementAt(e)}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 C{constructor(t,e,o,i){this._originalSource=t,this._originalTarget=e,this._grid=o,this._originalSourceIsBoundary=i}get source(){return this._grid.isFlipped?this._originalTarget:this._originalSource}get target(){return this._grid.isFlipped?this._originalSource:this._originalTarget}get sourcePosition(){const t=this.source;return this._grid.find(t)}get targetPosition(){const t=this.target;return this._grid.find(t)}get direction(){return this.getDirection(this.sourcePosition,this.targetPosition)}get path(){const t=this.direction;return"NO_DIRECTION"===t?this._pathForNoDirection():"S_N"===t?this._pathForSouthToNorth():"SW_NE"===t?this._pathForSouthWestToNorthEast():"W_E"===t?this._pathForWestToEast():"NW_SE"===t?this._pathForNorthWestToSouthEast():"N_S"===t?this._pathForNorthToSouth():"NE_SW"===t?this._pathForNorthEastToSouthWest():"E_W"===t?this._pathForEastToWest():"SE_NW"===t?this._pathForSouthEastToNorthWest():[]}_normalizePathCols(t){if(!this._grid.isFlipped)return t;const e=this._grid.colCount-1;for(const o of t)o.position[1]=e-o.position[1];return t}_pathForNoDirection(){return[]}_pathForSouthToNorth(){const t=[],[e,o]=this.sourcePosition,[i]=this.targetPosition;if(this._originalSourceIsBoundary)return t;if(this._grid.hasIntermediateElements(this.sourcePosition,this.targetPosition,!0))return t;if([...this._grid.getExistingOutgoingEdgesFor(this.target)].map((t=>t.target)).includes(this.source))return t;for(let n=e-1;n>i;n--)t.push({position:[n,o],vCross:!0});return t}_pathForSouthWestToNorthEast(){const t=[],[e,o]=this.sourcePosition,[i,n]=this.targetPosition;if(!this._originalSourceIsBoundary)for(let i=o+1;i<n;i++)t.push({position:[e,i],hCross:!0});t.push({position:[e,n],hCross:!0,vCross:!0});for(let o=e-1;o>i;o--)t.push({position:[o,n],vCross:!0});return t}_pathForWestToEast(){const t=[],[e,o]=this.sourcePosition,[,i]=this.targetPosition;for(let n=o+1;n<i;n++)t.push({position:[e,n],hCross:!0});return t}_pathForNorthWestToSouthEast(){const t=[],[e,o]=this.sourcePosition,[i,n]=this.targetPosition;for(let n=e+1;n<i;n++)t.push({position:[n,o],vCross:!0});t.push({position:[i,o],vCross:!0,hCross:!0});for(let e=o+1;e<n;e++)t.push({position:[i,e],hCross:!0});return t}_pathForNorthToSouth(){const t=[],[e,o]=this.sourcePosition,[i]=this.targetPosition;for(let n=e+1;n<i;n++)t.push({position:[n,o],vCross:!0});return t}_pathForNorthEastToSouthWest(){const t=[],[e,o]=this.sourcePosition,[i,n]=this.targetPosition;for(let n=e+1;n<i;n++)t.push({position:[n,o],vCross:!0});t.push({position:[i,o],vCross:!0,hCross:!0});for(let e=o-1;e>n;e--)t.push({position:[i,e],hCross:!0});return t}_pathForEastToWest(){const t=[],[e,o]=this.sourcePosition,[,i]=this.targetPosition;for(let n=o-1;n>i;n--)t.push({position:[e,n],hCross:!0});return t}_pathForSouthEastToNorthWest(){const t=[],[e,o]=this.sourcePosition,[i,n]=this.targetPosition;if(!this._originalSourceIsBoundary)for(let i=o-1;i>n;i--)t.push({position:[e,i],hCross:!0});this._originalSourceIsBoundary?t.push({position:[e,n],vCross:!0}):t.push({position:[e,n],hCross:!0,vCross:!0});for(let o=e-1;o>i;o--)t.push({position:[o,n],vCross:!0});return t}crossedElements(t){const e=[];for(const o of this.path){const[i,n]=o.position,r=this._grid.get(i,n);r&&(t&&o.vCross||!t&&o.hCross)&&e.push(r)}return e}crossedElementsPositions(t){const e=[];for(const o of this.path){const[i,n]=o.position;this._grid.get(i,n)&&(t&&o.vCross||!t&&o.hCross)&&e.push([i,n])}return e}getDirection(t,e){if(!this._grid.isValidPosition(t)||!this._grid.isValidPosition(e))return"NO_DIRECTION";const[o,i]=t,[n,r]=e,s=o-n,a=i-r;return s>0&&0===a?"S_N":s>0&&a<0?"SW_NE":0===s&&a<0?"W_E":s<0&&a<0?"NW_SE":s<0&&0===a?"N_S":o<n&&i>r?"NE_SW":o===n&&i>r?"E_W":o>n&&i>r?"SE_NW":void 0}isIntersect(t,e){const[o,i]=t,[n,r]=this.sourcePosition,[s,a]=this.targetPosition,c=this.direction;return"S_N"===c?!!(e&&i===r&&o<n&&o>s):"SW_NE"===c?!!(e&&i===a&&o<=n&&o>s)||!e&&i>r&&i<=a&&o===n:"W_E"===c?!e&&i>r&&i<a&&o===n:"NW_SE"===c?!!(e&&i===r&&o>n&&o<=s)||!e&&i>=r&&i<a&&o===s:"N_S"===c?!!(e&&i===r&&o>n&&o<s):"NE_SW"===c?!!(e&&i===r&&o>n&&o<=s)||!e&&i>a&&i<=r&&o===s:"E_W"===c?!e&&i>a&&i<r&&o===n:"SE_NW"===c&&(!!(e&&i===a&&o>s&&o<=n)||!e&&i>=a&&i<r&&o===n&&!this._grid.getExistingIncomingEdgesFor(this.source).some((t=>"NW_SE"===t.direction)))}}class P extends b{constructor(){super(),this._originalOutgoing=new Map,this._originalIncoming=new Map}get incoming(){return this.isFlipped?this._originalOutgoing:this._originalIncoming}get outgoing(){return this.isFlipped?this._originalIncoming:this._originalOutgoing}add(t,e){super.add(t,e),this._createNewEdgesFor(t)}removeElement(t){this._removeEdgesFor(t),super.removeElement(t)}move(t,e){this.removeElement(t),this.add(t,e)}_removeEdgesFor(t){if(this.hasElement(t)){this._originalOutgoing.delete(t),this._originalIncoming.delete(t);for(const[e,o]of this._originalOutgoing){if(!o.some((e=>e.target===t)))continue;const i=o.filter((e=>e.target!==t));i.length!==o.length&&this._originalOutgoing.set(e,i)}for(const[e,o]of this._originalIncoming){if(!o.some((e=>e.source===t)))continue;const i=o.filter((e=>e.source!==t));i.length!==o.length&&this._originalIncoming.set(e,i)}}}_edgeIsExist(t){const{source:e,sourcePosition:o,target:i,targetPosition:n}=t;return e&&o&&i&&n}_addEdgeToGrid(t){if(!this._edgeIsExist(t))return;if([...this._allEdges].some((e=>e.target===t.target&&e.source===t.source)))return;const{_originalSource:e,_originalTarget:o}=t,i=this._originalOutgoing.get(e);i?i.push(t):this._originalOutgoing.set(e,[t]);const n=this._originalIncoming.get(o);n?n.push(t):this._originalIncoming.set(o,[t])}_createNewEdgesFor(t){const e=s(t).filter((t=>this.hasElement(t)));for(const o of e){const e=new C(t,o,this);this._addEdgeToGrid(e)}const o=g(t).filter((t=>this.hasElement(t)));for(const e of o){const o=new C(t,e,this,!0);this._addEdgeToGrid(o)}const i=a(t).filter((t=>this.hasElement(t)));for(const e of i){const o=new C(e,t,this);this._addEdgeToGrid(o)}const n=c(t).filter((t=>this.hasElement(t)));for(const e of n){const o=new C(e,t,this,!0);this._addEdgeToGrid(o)}}getOutgoingFromHost(t){return this.isFlipped?new Set(a(t)):new Set(s(t))}getOutgoingFromBoundary(t){return this.isFlipped?new Set:new Set(g(t))}getIncomingFromHost(t){return this.isFlipped?new Set(s(t)):new Set(a(t))}getIncomingFromBoundaryHost(t){return this.isFlipped?new Set:new Set(c(t))}getOutgoingElementsFor(t){if(!t)return[];const e=this.getOutgoingFromHost(t),o=this.getOutgoingFromBoundary(t);return new Set([...e,...o])}getIncomingElementsFor(t){if(!t)return[];const e=this.getIncomingFromHost(t),o=this.getIncomingFromBoundaryHost(t);return new Set([...e,...o])}isCrossed(t,e){for(const o of this._allEdges)if(o.isIntersect(t,e))return!0}get _allEdges(){return new Set([...this._originalOutgoing.values()].flat())}shakeItHorizontal(){const t=[...this.elements].sort((e=this,function(t,o){const i=e.find(t),n=e.find(o);return i&&!n?-1:!i&&n?1:i||n?i[1]-n[1]||n[0]-i[0]:0})).reverse();for(var e;t.length>0;){const e=t.pop(),o=this.getVerticalChain(e);for(const e of o){t.indexOf(e)>=0&&t.splice(t.indexOf(e),1)}const[,i]=this.find([...o][0]),n=[...o].map((t=>this.find(t)[0]));if(!(i<=0)){for(let t=i-1;t>=0;t--){if(!n.every((e=>!this.isCrossed([e,t],!0)&&!this.get(e,t))))break;for(const e of o){const o=this.find(e);this.move(e,[o[0],t])}const i=this.hasAnyCross(),r=this.getVerticalChain(e);if(i||r.size>o.size){for(const e of o){const o=this.find(e);this.move(e,[o[0],t+1])}break}}this.shrinkCols()}}}shakeItVertical(){}hasAnyCross(t){const e=t||this._allEdges;for(const t of e)for(const e of t.path){const{position:[o,i],hCross:n,vCross:r}=e;if((n||r)&&this.get(o,i))return[o,i,t]}return!1}getVerticalChain(t,e){const o=e||new Set;if(!t)return o;if(!this.find(t))return o;o.add(t);const i=[...this.getAllExistingEdgesFor(t)].filter((t=>"N_S"===t.direction||"S_N"===t.direction));for(const e of i){const i=e.source===t?e.target:e.source;if(!o.has(i)){const t=this.getVerticalChain(i,o);for(const e of t)o.add(e)}}return o}getHorizontalChain(t,e){const o=e||new Set;if(!t)return o;if(!this.find(t))return o;o.add(t);const i=[...this.getAllExistingEdgesFor(t)].filter((t=>"W_E"===t.direction||"E_W"===t.direction));for(const e of i){const i=e.source===t?e.target:e.source;if(!o.has(i)){const t=this.getHorizontalChain(i,o);for(const e of t)o.add(e)}}return o}getExistingOutgoingEdgesFor(t){return this.outgoing.get(t)||[]}getExistingIncomingEdgesFor(t){return this.incoming.get(t)||[]}getAllExistingEdgesFor(t){const e=this.getExistingOutgoingEdgesFor(t),o=this.getExistingIncomingEdgesFor(t);return new Set([...e,...o])}getElementsInRow(t){return new Set(this._grid[t].filter((t=>null!=t)))}_separateGrid(){}_mergeGrids(t){const e=new P;for(const o of t)e._grid=e._grid.concat(o._grid),e._elements=new Set([...e._elements,...o._elements]),e._originalIncoming=new Map([...e._originalIncoming,...o._originalIncoming]),e._originalOutgoing=new Map([...e._originalOutgoing,...o._originalOutgoing]);return e}getResultGrid(){const t=this._separateGrid();for(const e of t)e.shrinkCols(),e.shrinkRows();const e=this._mergeGrids(t);return e.toRectangle(),e}_createCrossGrid(){const t=[];for(let e=0;e<this.rowCount;e++)t.push(Array(this.colCount));for(const e of this._elements){const[o,i]=this.find(e),n=this.incoming.get(e)||[],r=this.outgoing.get(e)||[],s=[...r,...n].some((t=>{const{source:o,direction:i}=t;return o===e&&("N_S"===i||"NW_SE"===i||"NE_SW"===i)||(o!==e&&("S_N"===i||"SW_NE"===i||"SE_SW"===i)||void 0)}));for(const e of r)for(const o of e.path){const{position:[e,i],vCross:n,hCross:r}=o,s=t[e][i]||{};n&&(s.vCross=n),r&&(s.hCross=r),t[e][i]=s}t[o][i]={...t[o][i]||{},element:e,incoming:n,outgoing:r,hasToSouth:s}}return t}_createGridWith(t,e){const o=new P;for(let i=0;i<t;i++)o._grid.push(Array(e));return o}}function S(t,e,i,n,r){t.notMoveForvard||function(t,e,i,n,r){const s=R(t,n,r,e.isFlipped);if(!s||0===s.length)return;const a=s.filter((t=>{const s=e.isFlipped?t.source:t.target,a=e.isFlipped?t.target:t.source,c=e.find(s),g=e.find(a);return a!==s&&(!(c[0]>=g[0])&&(!(c[1]>g[1])&&(!F(s,n,i,r)&&!function(t,e,i,n,r,s){const a=new o.Graph;for(const t of i.nodes)r.has(t)&&a.addNode(t);for(const t of i.edges)r.has(t.target)&&r.has(t.source)&&a.addEdge(t);return a.deleteEdge(n),a.isNodeTraced(e,t,s)}(s,a,n,t,r,!0))))})).map((t=>t.target)).sort(x(e));if(0===a.length)return;for(;a.length>0;){const o=a.shift(),[i,s]=e.find(o),c=a.filter((t=>{const[o]=e.find(t);return o===i}));for(const t of c){const e=c.indexOf(t);e<0||a.splice(e,1)}const[g,d]=e.find(t),h=d-s+1;for(let t=e.rowCount-1;t>=0;t--)t>=g?e.expandRow(t,d,h):e.expandRow(t,s-1,h);const u=[0,d+1],l=[e.rowCount-1,e.colCount-1];D(e,n,r,u,l)}}(t,e,i,r,n),t.notMoveForvard=!1;const s=function(t,e,o,i){if(i){const i=[...e.getOutgoingEdgesFor(t)].filter((t=>!o.has(t.target)&&"bpmn:BoundaryEvent"===t.target.$type));return i.length>0?[...e.getIncomingEdgesFor(t)].filter((t=>!o.has(t.source))).concat(i).map((e=>e.target!==t?e.target:e.source)):[...e.getIncomingEdgesFor(t)].filter((t=>!o.has(t.source))).map((t=>"bpmn:BoundaryEvent"===t.source.$type?t.source.attachedToRef:t.source))}return[...e.getOutgoingEdgesFor(t)].filter((t=>!o.has(t.target))).map((t=>t.target))}(t,r,n,e.isFlipped),a=function(t,e,o,i,n,r){const s=R(t,n,r,e.isFlipped),a=v(t,n,r,e.isFlipped).map((t=>t.source)),[,c]=e.find(t),g=[...s].filter((t=>{const s=e.isFlipped?t.source:t.target,g=e.find(s)[1];if(!F(s,n,o,r,e.isFlipped))return!1;return(!v(s,n,r,e.isFlipped).map((t=>t.source)).find((t=>a.includes(t)))||!i)&&g<=c})).map((t=>e.isFlipped?t.source:t.target)).sort(x(e));for(const t of g)o.splice(o.indexOf(t),1),r.delete(t),e.removeElement(t);return e.shrinkRows(),e.shrinkCols(),g}(t,e,i,s&&s.length>0,r,n);let c=[...s,...a],g=[],d=null;c.forEach((o=>{const s=function(t,e,o,i,n,r,s){if("bpmn:BoundaryEvent"===i.$type)return o.find(t);const a=o.find(t);if(!a)return;const c=s?[a[0]+1,a[1]+1]:[a[0],a[1]+1],g=o.find(t),d=[...o.get(g[0],g[1])],h=[];for(const t of d)[...o.getExistingOutgoingEdgesFor(t)].filter((t=>t.target!==t.source&&r.has(t.source)&&r.has(t.target))).forEach((t=>h.push(t)));for(let t=c[0];t<=o.rowCount;t++){const e=[t,c[1]],i=h.some((o=>o.targetPosition[0]===t&&o.targetPosition[1]===c[1]||o.isIntersect(e)));if(i&&t===o.colCount-1)c[0]=t+1;else if(!i){c[0]=t;break}}(o.get(c[0],c[1])||o.isCrossed(c,!0))&&o.createCol(c[1]-1);o.isCrossed([c[0],c[1]])&&o.createRow(c[0]-1);return c}(t,0,e,o,0,n,"bpmn:BoundaryEvent"===t.$type);e.add(o,s),n.add(o),N(o,e,r,n,i,g,!0),d=o,g.unshift(o)}));const h=[],u=[];for(const t of g)"bpmn:BoundaryEvent"===t.$type?h.push(t):u.push(t);return h.sort(((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0))),g=[...h,...u],g}function F(t,e,o,i,n){const r=o.includes(t),s=R(t,e,i,n);return r&&(!s||0===s.length)}function R(t,e,o,i){return[...i?e.getIncomingEdgesFor(t):e.getOutgoingEdgesFor(t)].filter((e=>o.has(i?e.source:e.target)&&o.has(t)))}function v(t,e,o,i){return[...i?e.getOutgoingEdgesFor(t):e.getIncomingEdgesFor(t)].filter((e=>o.has(i?e.source:e.target)&&o.has(t)))}function D(t,e,o,i,n){if(!t.isValidPosition(i)||!t.isValidPosition(n))throw new Error("fixCrossesInGridPart: invalid position");const[r,s]=i,[a,c]=n;for(let i=r;i<=a;i++)for(let n=s;n<=c;n++){const r=t.get(i,n);r&&N(r,t,e,o)}}function N(t,e,o,i,n,r,s,a){const c=[...[...e.getExistingOutgoingEdgesFor(t)].sort(((t,e)=>{const[o,i]=t.targetPosition,[n,r]=e.targetPosition;return o-n||i-r})),...[...e.getExistingIncomingEdgesFor(t)].sort(((t,e)=>{const[o,i]=t.sourcePosition,[n,r]=e.sourcePosition;return o-n||i-r}))].filter((e=>{const{target:a,source:c,direction:g}=e;return a!==c&&((!r||!r.includes(a))&&((!n||!F(a,o,n,i))&&(!s||c!==t||"SE_NW"!==g&&"S_N"!==g)))}));for(const t of c)T(t,e),"E_W"===t.direction&&t._originalSourceIsBoundary||I(t,e)}function T(t,e){const{direction:o}=t;if("W_E"===o||"E_W"===o)return;const i=t.crossedElements(!0);i.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||B(i,e):G(i,e):B(i,e):G(i,e))}function I(t,e){const{direction:o}=t;if("S_N"===o||"N_S"===o)return;const i=t.crossedElements(!1);if(0!==i.length)if("SW_NE"!==o)"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||W(i,e):O(i,e):W(i,e):O(i,e);else{const o=function(t){const e=t.sourcePosition,o=t.targetPosition;let i=e[0];for(let n=e[0];n<t._grid.rowCount&&t._grid.getElementsInRange({row:n,col:e[1]+1},{row:n,col:o[1]}).length>0;n++)i++;return i-e[0]}(t),[i]=t.sourcePosition;for(let t=o;t>0;t--)e.createRow(i-1);const n=t._grid.getElementsInRange({row:t.sourcePosition[0],col:t.sourcePosition[1]+1},{row:t._grid.rowCount-1,col:t._grid.colCount-1});for(const e of n){const[i,n]=t._grid.find([...e][0]);for(const r of[...e])t._grid.move(r,[i-o,n])}}}function W(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 O(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 G(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 B(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])}}class M{constructor(e){this.moddle=new t,this.diFactory=new y(this.moddle),this._handlers=w,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();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}handle(t,o){return this._handlers.filter((o=>e.isFunction(o[t]))).map((e=>e[t](o)))}createGridsForProcesses(){const t=this.processTrees.map((t=>[...t.nodes])).flat();t.sort(((t,e)=>e.level-t.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.shrinkRows(),e.shrinkCols(),e.toRectangle(),$(e),A(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(){for(const t of this.processTrees){const e=t.nodes.sort(((t,e)=>t.level-e.level));for(const t of e){const e=this.getParticipantForProcess(t);if(e){const o=this.getElementDi(e),i=this.getProcDi(o);let{x:n,y:r}=o.bounds;n+=75,r+=70,this.generateDi(t.grid,{x:n,y:r},i);continue}if(t.isExpanded){const e=this.getElementDi(t),o=this.getProcDi(e);let{x:n,y:r}=e.bounds;const{width:s,height:a}=i(t);n+=75-s/4,r+=u-a-a/4,this.generateDi(t.grid,{x:n,y:r},o);continue}const o=this.diagram.diagrams.find((e=>e.plane.bpmnElement===t));this.generateDi(t.grid,{x:0,y:0},o)}}}drawCollaborationMessageFlows(t){const e=t[0]?t[0].messageFlows:null;if(e)for(const t of e){const{sourceRef:e,targetRef:o}=t,i=e.di.bounds,n=o.di.bounds,r=n.y-i.y,s=[{x:i.x+i.width/2},{x:n.x+n.width/2}];r>0?(s[0].y=i.y+i.height,s[1].y=n.y):(s[0].y=i.y,s[1].y=n.y+n.height);const a=this.diFactory.createDiEdge(t,s);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 e=new o.Graph,i=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 i)e.addNode(t);for(const t of i){const o=this.getSubProcesses(t);for(const i of o)e.addEdge({source:t,target:i})}const n=e.getSeparatedGraphs();for(const t of n){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 n){const e=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},i=(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 a=[...e.getIncomingEdgesFor(t)].map((t=>t.source)).find((t=>void 0===t.right));return a?[a]:void 0};t.genericTraversing(e,o.getLast,i,o.addToEnd)}return n}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:n}=t.processRef.grid,{width:r,height:s}=i(t),a=o>0?o*h+h:r,c=n>0?n*u+u:s,g=this.diFactory.createDiShape(t,{width:a,height:c,...e},{id:t.id+"_di"});return this.diagram.diagrams[0].plane.get("planeElement").push(g),g.bounds.y+g.bounds.height}cleanDi(){this.diagram.diagrams=[]}createGridLayout(t){const e=new P,i=new o.Graph;for(const e of t.flowElements||[])n(e,"bpmn:SequenceFlow")||n(e,"bpmn:DataObject")||i.addNode(e);for(const t of i.nodes){for(const e of t.outgoing||[])i.addEdge({source:e.sourceRef,target:e.targetRef});const e=t.attachedToRef;e&&i.addEdge({source:e,target:t});const o=t.dataInputAssociations;for(const t of o||[]){const e=t.sourceRef[0],o=t.$parent;i.addEdge({source:e,target:o})}const n=t.dataOutputAssociations;for(const t of n||[]){const e=t.$parent,o=t.targetRef;i.addEdge({source:e,target:o})}}return i.genericTraversing(((t,o)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const i=[...t].find((i=>e.isFlipped?[...o.getOutgoingEdgesFor(i)].filter((e=>!t.has(e.target))).length>0:[...o.getIncomingEdgesFor(i)].filter((e=>!t.has(e.source))).length>0));if(i)return e.flipHorizontally(),i;const r=o.nodes.filter((i=>e.isFlipped?!t.has(i)&&0===o.getOutgoingEdgesFor(i).size&&!d(i):!t.has(i)&&0===o.getIncomingEdgesFor(i).size&&!d(i)));if(r.length>0)return function(t,e){const o=t.filter((t=>!n(t,e)));return[...t.filter((t=>n(t,e))),...o]}(r,"bpmn:StartEvent")[0];const s=[...t].find((i=>(e.isFlipped?[...o.getIncomingEdgesFor(i)].filter((e=>!t.has(e.source))):[...o.getOutgoingEdgesFor(i)].filter((e=>!t.has(e.target)))).length>0));if(s)return s;const a=o.nodes.find((i=>{if(t.has(i))return!1;return 0===(e.isFlipped?[...o.getOutgoingEdgesFor(i)].filter((t=>t.target!==i)):[...o.getIncomingEdgesFor(i)].filter((t=>t.source!==i))).length}));if(a)return a;const c=o.nodes.find((i=>{if(t.has(i))return!1;return 0===(e.isFlipped?[...o.getIncomingEdgesFor(i)].filter((t=>t.target!==t.source)):[...o.getOutgoingEdgesFor(i)]).length}));return c?(e.flipHorizontally(),c):(this.currentDebugStep+=1,o.nodes.find((e=>!t.has(e))))}),(t=>t.pop()),((t,o,i,n)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const r=S(t,e,n,i,o);return this.currentDebugStep+=1,r}),((t,e)=>{for(const o of t)e.push(o)}),(t=>{e.hasElement(t)||e.add(t)})),e.isFlipped&&e.flipHorizontally(),e}generateDi(t,e,o){const i=this.diFactory,n=(o||this.diagram.diagrams[0]).plane.get("planeElement");t.elementsByPosition().forEach((({element:o,row:r,col:s})=>{const a=this.handle("createElementDi",{element:o,row:r,col:s,layoutGrid:t,diFactory:i,shift:e}).filter((t=>null!=t)).flat();n.push(...a)})),t.elementsByPosition().forEach((({element:o,row:r,col:s})=>{const a=this.handle("createConnectionDi",{element:o,row:r,col:s,layoutGrid:t,diFactory:i,shift:e}).flat();n.push(...a)}))}handleGrid(t,e){for(;e.length>0&&(void 0===this.maxDebugStep||this.maxDebugStep>0&&this.currentDebugStep<this.maxDebugStep);){const o=e.pop();this.handle("addToGrid",{element:o,grid:t,stack:e}).flat().forEach((t=>{e.push(t)})),this.currentDebugStep+=1}}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 $(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 A(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)}}exports.layoutProcess=function(t,e){return new M(e).layoutProcess(t)};//# sourceMappingURL=index.cjs.map
|
|
1
|
+
"use strict";var t=require("bpmn-moddle"),e=require("graph-by-ivan-tulaev"),o=require("min-dash");function i(t){return r(t,"bpmn:SubProcess")||r(t,"bpmn:Task")?{width:100,height:80}:r(t,"bpmn:Gateway")?{width:50,height:50}:r(t,"bpmn:Event")?{width:36,height:36}:r(t,"bpmn:Participant")||r(t,"bpmn:Lane")?{width:400,height:100}:r(t,"bpmn:DataObjectReference")?{width:36,height:50}:r(t,"bpmn:DataStoreReference")?{width:50,height:50}:r(t,"bpmn:TextAnnotation")?{width:100,height:30}:{width:100,height:80}}function r(t,e){return t.$instanceOf(e)}function n(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 s(t){return(r(t,"bpmn:IntermediateThrowEvent")||r(t,"bpmn:IntermediateCatchEvent"))&&(void 0===t.incoming||0===t.incoming.length)}const d=150,a=140;function c(t,e){return new Set(n(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 h(t,e,o,r=!1,s){const d=t.di,h=e.di,f=d.get("bounds"),p=h.get("bounds"),E=g(f),m=g(p),x=e.gridPosition.col-t.gridPosition.col,y=e.gridPosition.row-t.gridPosition.row,_=`${y>0?"bottom":"top"}-${x>0?"right":"left"}`,w=`${y>0?"top":"bottom"}-${x>0?"left":"right"}`,{x:b,y:P}=l(t,s),{x:C}=l(e,s);if("bpmn:BoundaryEvent"===t.$type){if(0===x&&0===y)return[u(E,f,"b",_),{x:E.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:C,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:b,y:m.y},u(m,p,"l",w)];if(0===x&&y<0)return[u(E,f,"b",_),{x:E.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:b,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:C,y:m.y},u(m,p,"l",w)];if(x>0&&y<0)return[u(E,f,"b",_),{x:E.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:m.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},u(m,p,"b",w)];if(x>0&&0===y)return[u(E,f,"b",_),{x:E.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:m.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},u(m,p,"b",w)];if(x>0&&y>0)return[u(E,f,"b"),{x:E.x,y:m.y},u(m,p,"l")];if(0===x&&y>0){const e=u(E,f,"b",_),o=u(m,p,"t",w);return t.attachedToRef.isExpanded?[e,{x:E.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:m.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},o]:[e,o]}if(x<0&&y>0)return[u(E,f,"b"),{x:E.x,y:m.y},u(m,p,"r")];if(x<0&&0===y){const i=function(t,e,o){const[i,r]=o.find(t),[,n]=o.find(e),s=r<n?r:n,d=r<n?n:r,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)}(t,e,o);return[u(E,f,"b",_),{x:E.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a+i*a},{x:m.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a+i*a},u(m,p,"b",w)]}if(x<0&&y<0)return[u(E,f,"b",_),{x:E.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},{x:m.x,y:t.attachedToRef.isExpanded?P+(t.attachedToRef.grid.rowCount+1)*a:P+a},u(m,p,"b",w)]}else{if(0===x&&0===y)return[u(E,f,"b",_),{x:E.x,y:t.isExpanded?P+(t.grid.rowCount+1)*a:P+a},{x:C,y:t.isExpanded?P+(t.grid.rowCount+1)*a:P+a},{x:b,y:m.y},u(m,p,"l",w)];if(0===x&&y<0){const i=[];for(let r=t.gridPosition.row-1;r>e.gridPosition.row;r--){const e=o.get(r,t.gridPosition.col);e&&i.push(e)}const r=[...c(e)].includes(t);return i.length>0||r?[u(E,f,"l",_),{x:b,y:E.y},{x:C,y:m.y},u(m,p,"l",w)]:[u(E,f,"t",_),u(m,p,"b",w)]}if(x>0&&y<0)return[u(E,f,"r"),{x:m.x,y:E.y},u(m,p,"b")];if(x>0&&0===y){const o=u(E,f,"r",_);t.isExpanded&&(o.y=f.y+i(t).height/2);const r=u(m,p,"l",w);return e.isExpanded&&(r.y=p.y+i(e).height/2),[o,r]}if(x>0&&y>0)return[u(E,f,"b"),{x:E.x,y:m.y},u(m,p,"l")];if(0===x&&y>0){const o=u(E,f,"b",_),i=u(m,p,"t",w);return t.isExpanded||e.isExpanded?[o,{x:E.x,y:t.isExpanded?P+(t.grid.rowCount+1)*a:P+a},{x:m.x,y:t.isExpanded?P+(t.grid.rowCount+1)*a:P+a},i]:[o,i]}if(x<0&&y>0)return[u(E,f,"b"),{x:E.x,y:m.y},u(m,p,"r")];if(x<0&&0===y){const r=[];for(let i=t.gridPosition.col-1;i>e.gridPosition.col;i--){const e=o?o.get(t.gridPosition.row,i):null;e&&r.push(e)}const s=n(e).includes(t);let d=o?o.getExistingOutgoingEdgesFor(e):[];if(d=d.some(t=>"SW_NE"===t.direction),r.length>0||s||d)return[u(E,f,"b",_),{x:E.x,y:P+(t.isExpanded?a*(t.grid.colCount+1.5):a)},{x:m.x,y:P+(t.isExpanded?a*(t.grid.colCount+1.5):a)},u(m,p,"b",w)];{const o=u(E,f,"l",_);t.isExpanded&&(o.y=f.y+i(t).height/2);const r=u(m,p,"r",w);return e.isExpanded&&(r.y=p.y+i(e).height/2),[o,r]}}if(x<0&&y<0){const i=[];for(let r=t.gridPosition.col-1;r>=e.gridPosition.col;r--){const e=o?o.get(t.gridPosition.row,r):null;e&&i.push(e)}return i.length>0?[u(E,f,"b",_),{x:E.x,y:t.isExpanded?P+(t.grid.rowCount+1)*a:P+a},{x:m.x,y:t.isExpanded?P+(t.grid.rowCount+1)*a:P+a},u(m,p,"b",w)]:[u(E,f,"l"),{x:m.x,y:E.y},u(m,p,"b")]}}const v=function(t,e,o){const{row:i,col:r}=t.gridPosition,{row:n,col:s}=e.gridPosition,d=s-r,a=n-i;if(!(d>0&&0!==a))return;if(a>0){let t=0;const e={row:n,col:r};return t+=o.getElementsInRange({row:i,col:r},e).length,t+=o.getElementsInRange(e,{row:n,col:s}).length,!(t>2)&&["v","h"]}{let t=0;const e={row:i,col:s};return t+=o.getElementsInRange({row:i,col:r},e).length,t+=o.getElementsInRange(e,{row:n,col:s}).length,!(t>2)&&["h","v"]}}(t,e,o);if(v){const t=u(E,f,v[0],_),e=u(m,p,v[1],w);return[t,"h"===v[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const S=-Math.sign(y)*a/2;return[u(E,f,"r",_),{x:E.x+75,y:E.y},{x:E.x+75,y:m.y+S},{x:m.x-75,y:m.y+S},{x:m.x-75,y:m.y},u(m,p,"l",w)]}function g(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function u(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 l(t,e={x:0,y:0}){const o=t.gridPosition.row,i=t.gridPosition.col;return{width:d,height:a,x:i*d+e.x,y:o*a+e.y}}function f(t,e,o,r,n){const{width:s,height:c}=i(t),{x:h,y:g}=r;if(!n)return{width:s,height:c,x:o*d+(d-s)/2+h,y:e*a+(a-c)/2+g};const u=n.di.bounds;return{width:s,height:c,x:Math.round(u.x+u.width/2-s/2),y:Math.round(u.y+u.height-c/2)}}function p(t){return function(e,o){const i=t.find(e),r=t.find(o);return i&&!r?-1:!i&&r?1:i||r?i[0]-r[0]||i[1]-r[1]:0}}function E(t,e){const o=[e].flat();if(0===o.length)return t;let i=[];return o.forEach((e,n)=>{const s=t.filter(t=>r(t,e));if(i=i.concat(s),n===o.length-1&&i.length!==t.length)for(const e of t)i.includes(e)||i.push(e)}),i}class m{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,i){return this.create("bpmndi:BPMNShape",o.assign({bpmnElement:t,bounds:this.createDiBounds(e)},i))}createDiWaypoints(t){var e=this;return o.map(t,function(t){return e.createDiWaypoint(t)})}createDiWaypoint(t){return this.create("dc:Point",o.pick(t,["x","y"]))}createDiEdge(t,e,i){return this.create("bpmndi:BPMNEdge",o.assign({bpmnElement:t,waypoint:this.createDiWaypoints(e)},i))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}class x{constructor(){this._grid=[],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),r=this._grid[t];e||Number.isInteger(e)?r.splice(e+1,0,...i):r.splice(r.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 r=[];t>o&&([t,o]=[o,t]),e>i&&([e,i]=[i,e]);for(let n=t;n<=o;n++)for(let t=e;t<=i;t++){const e=this.get(n,t);e&&r.push(e)}return r}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 r of[...e])t.push({element:r,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 y{constructor(t,e,o,i){this._originalSource=t,this._originalTarget=e,this._grid=o,this._originalSourceIsBoundary=i}get source(){return this._grid.isFlipped?this._originalTarget:this._originalSource}get target(){return this._grid.isFlipped?this._originalSource:this._originalTarget}get sourcePosition(){const t=this.source;return this._grid.find(t)}get targetPosition(){const t=this.target;return this._grid.find(t)}get direction(){return this.getDirection(this.sourcePosition,this.targetPosition)}get path(){const t=this.direction;return"NO_DIRECTION"===t?this._pathForNoDirection():"S_N"===t?this._pathForSouthToNorth():"SW_NE"===t?this._pathForSouthWestToNorthEast():"W_E"===t?this._pathForWestToEast():"NW_SE"===t?this._pathForNorthWestToSouthEast():"N_S"===t?this._pathForNorthToSouth():"NE_SW"===t?this._pathForNorthEastToSouthWest():"E_W"===t?this._pathForEastToWest():"SE_NW"===t?this._pathForSouthEastToNorthWest():[]}_normalizePathCols(t){if(!this._grid.isFlipped)return t;const e=this._grid.colCount-1;for(const o of t)o.position[1]=e-o.position[1];return t}_pathForNoDirection(){return[]}_pathForSouthToNorth(){const t=[],[e,o]=this.sourcePosition,[i]=this.targetPosition;if(this._originalSourceIsBoundary)return t;if(this._grid.hasIntermediateElements(this.sourcePosition,this.targetPosition,!0))return t;if([...this._grid.getExistingOutgoingEdgesFor(this.target)].map(t=>t.target).includes(this.source))return t;for(let r=e-1;r>i;r--)t.push({position:[r,o],vCross:!0});return t}_pathForSouthWestToNorthEast(){const t=[],[e,o]=this.sourcePosition,[i,r]=this.targetPosition;if(!this._originalSourceIsBoundary)for(let i=o+1;i<r;i++)t.push({position:[e,i],hCross:!0});t.push({position:[e,r],hCross:!0,vCross:!0});for(let o=e-1;o>i;o--)t.push({position:[o,r],vCross:!0});return t}_pathForWestToEast(){const t=[],[e,o]=this.sourcePosition,[,i]=this.targetPosition;for(let r=o+1;r<i;r++)t.push({position:[e,r],hCross:!0});return t}_pathForNorthWestToSouthEast(){const t=[],[e,o]=this.sourcePosition,[i,r]=this.targetPosition;for(let r=e+1;r<i;r++)t.push({position:[r,o],vCross:!0});t.push({position:[i,o],vCross:!0,hCross:!0});for(let e=o+1;e<r;e++)t.push({position:[i,e],hCross:!0});return t}_pathForNorthToSouth(){const t=[],[e,o]=this.sourcePosition,[i]=this.targetPosition;for(let r=e+1;r<i;r++)t.push({position:[r,o],vCross:!0});return t}_pathForNorthEastToSouthWest(){const t=[],[e,o]=this.sourcePosition,[i,r]=this.targetPosition;for(let r=e+1;r<i;r++)t.push({position:[r,o],vCross:!0});t.push({position:[i,o],vCross:!0,hCross:!0});for(let e=o-1;e>r;e--)t.push({position:[i,e],hCross:!0});return t}_pathForEastToWest(){const t=[],[e,o]=this.sourcePosition,[,i]=this.targetPosition;for(let r=o-1;r>i;r--)t.push({position:[e,r],hCross:!0});return t}_pathForSouthEastToNorthWest(){const t=[],[e,o]=this.sourcePosition,[i,r]=this.targetPosition;if(!this._originalSourceIsBoundary)for(let i=o-1;i>r;i--)t.push({position:[e,i],hCross:!0});this._originalSourceIsBoundary?t.push({position:[e,r],vCross:!0}):t.push({position:[e,r],hCross:!0,vCross:!0});for(let o=e-1;o>i;o--)t.push({position:[o,r],vCross:!0});return t}crossedElements(t){const e=[];for(const o of this.path){const[i,r]=o.position,n=this._grid.get(i,r);n&&(t&&o.vCross||!t&&o.hCross)&&e.push(n)}return e}crossedElementsPositions(t){const e=[];for(const o of this.path){const[i,r]=o.position;this._grid.get(i,r)&&(t&&o.vCross||!t&&o.hCross)&&e.push([i,r])}return e}getDirection(t,e){if(!this._grid.isValidPosition(t)||!this._grid.isValidPosition(e))return"NO_DIRECTION";const[o,i]=t,[r,n]=e,s=o-r,d=i-n;return s>0&&0===d?"S_N":s>0&&d<0?"SW_NE":0===s&&d<0?"W_E":s<0&&d<0?"NW_SE":s<0&&0===d?"N_S":o<r&&i>n?"NE_SW":o===r&&i>n?"E_W":o>r&&i>n?"SE_NW":void 0}isIntersect(t,e){const[o,i]=t,[r,n]=this.sourcePosition,[s,d]=this.targetPosition,a=this.direction;return"S_N"===a?!!(e&&i===n&&o<r&&o>s):"SW_NE"===a?!!(e&&i===d&&o<=r&&o>s)||!e&&i>n&&i<=d&&o===r:"W_E"===a?!e&&i>n&&i<d&&o===r:"NW_SE"===a?!!(e&&i===n&&o>r&&o<=s)||!e&&i>=n&&i<d&&o===s:"N_S"===a?!!(e&&i===n&&o>r&&o<s):"NE_SW"===a?!!(e&&i===n&&o>r&&o<=s)||!e&&i>d&&i<=n&&o===s:"E_W"===a?!e&&i>d&&i<n&&o===r:"SE_NW"===a&&(!!(e&&i===d&&o>s&&o<=r)||!e&&i>=d&&i<n&&o===r&&!this._grid.getExistingIncomingEdgesFor(this.source).some(t=>"NW_SE"===t.direction||"W_E"===t.direction))}}class _ extends x{constructor(){super(),this.graph=new e.Graph}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)this._addEdgeToGrid(t)}_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){if("bpmn:DataObjectReference"===t.$type||"bpmn:DataStoreReference"===t.$type){const e=[...this.baseGraph.edges].filter(e=>(e.source===t||e.target===t)&&this.hasElement(e.source)&&this.hasElement(e.target));for(const t of e){const{source:e,target:o,id:i}=t,r=new y(e,o,this);r.id=i,this._addEdgeToGrid(r)}}"bpmn:BoundaryEvent"===t.$type&&(this._addEdgeToGrid(new y(t,t.attachedToRef,this)),this._addEdgeToGrid(new y(t.attachedToRef,t,this)));const e=(t.outgoing||[]).filter(t=>this.elements.has(t.targetRef)),o=(t.incoming||[]).filter(t=>this.elements.has(t.sourceRef));for(const t of[...e,...o]){const e=new y(t.sourceRef,t.targetRef,this);e.id=t.id,this._addEdgeToGrid(e)}}isCrossed(t,e){return[...this._allEdges].some(o=>o.isIntersect(t,e))}get _allEdges(){return this.graph.edges}hasAnyCross(t){return[...t||this._allEdges].some(t=>t.crossedElements().length>0||t.crossedElements(!0).length>0)}shakeIt(t){const e=[...this.elements].sort(t?(o=this,function(t,e){const i=o.find(t),r=o.find(e);return i&&!r?-1:!i&&r?1:i||r?i[0]-r[0]||r[1]-i[1]:0}):function(t){return function(e,o){const i=t.find(e),r=t.find(o);return i&&!r?-1:!i&&r?1:i||r?i[1]-r[1]||r[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[r,n]=this.find([...i][0]);if(!(t?r<=0:n<=0)){for(let e=t?r-1:n-1;e>=0;e--){const r=[...i].every(t=>{const e=this.find(t);return e&&!this.isCrossed(e,!0)&&!this.isCrossed(e,!1)});if(!r)break;const n=[...i].every(o=>{const i=this.find(o),r=t?e:i[0],n=t?i[1]:e;return!this.get(r,n)});if(!n)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 r=this.find(t);if(!r)return i;const n=[],s=this.get(r[0],r[1]);for(const t of s)i.add(t),[...this.getAllExistingEdgesFor(t)].filter(t=>e?"N_S"===t.direction||"S_N"===t.direction:"W_E"===t.direction||"E_W"===t.direction).forEach(t=>n.push(t));for(const o of n){const r=o.source===t?o.target:o.source;if(!i.has(r)){const t=this.getChain(r,e,i);for(const e of t)i.add(e)}}return i}getExistingOutgoingEdgesFor(t){return[...this.graph.getOutgoingEdgesFor(t)]}getExistingIncomingEdgesFor(t){return[...this.graph.getIncomingEdgesFor(t)]}getAllExistingEdgesFor(t){const e=this.getExistingOutgoingEdgesFor(t),o=this.getExistingIncomingEdgesFor(t);return new Set([...e,...o])}get existGraph(){if(this.isFlipped)throw new Error("Don't use with flipped grid!");const t=new e.Graph;for(const e of this.elements)t.addNode(e);for(const e of this._allEdges)t.addEdge(e);return t}_separateGrid(){const t=this.existGraph.getSeparatedGraphs(),e=[];for(const o of t){const t=this._createGridWith(this.rowCount,this.colCount);t.baseGraph=this.baseGraph;let i=null,r=null;for(const e of E(o.nodes,"bpmn:BoundaryEvent").reverse()){const o=this.find(e);(null===i||i>o[0])&&(i=o[0]),(null===r||r<o[0])&&(r=o[0]),t._superAdd(e,o),t.graph.addNode(e)}o.edges.forEach(e=>{t._addEdgeToGrid(e)}),e.push(t)}return e}_superAdd(t,e){super.add(t,e)}_mergeGrids(t){const o=new _;for(const i of t)o.baseGraph=i.baseGraph,o._grid=o._grid.concat(i._grid),o._elements=new Set([...o._elements,...i._elements]),o.graph=e.Graph.mergeGraphs(new Set([o.graph,i.graph]));return o}_createGridWith(t,e){const o=new _;for(let i=0;i<t;i++)o._grid.push(Array(e));return o}}function w(t,o,i,r,n){t.notMoveForvard||function(t,o,i,r,n){const s=P(t,r,n,o.isFlipped);if(!s||0===s.length)return;const d=s.filter(t=>{const s=o.isFlipped?t.source:t.target,d=o.isFlipped?t.target:t.source,a=o.find(s),c=o.find(d);return d!==s&&(!(a[0]>=c[0])&&(!(a[1]>c[1])&&(!b(s,r,i,n)&&!function(t,o,i,r,n,s,d){const a=new e.Graph;for(const t of i.nodes)n.has(t)&&a.addNode(t);for(const t of i.edges)n.has(t.target)&&n.has(t.source)&&a.addEdge(t);a.deleteEdge(r);for(const t of a.nodes)s.find(t)[1]<s.find(o)[1]&&a.deleteNode(t);return a.isNodeTraced(t,o,d)}(d,s,r,t,n,o,!0))))}).map(t=>t.target).sort(p(o));if(0===d.length)return;for(;d.length>0;){const e=d.shift(),[i,s]=o.find(e),a=d.filter(t=>{const[e]=o.find(t);return e===i});for(const t of a){const e=a.indexOf(t);e<0||d.splice(e,1)}const[c,h]=o.find(t),g=h-s+1;for(let t=o.rowCount-1;t>=0;t--)t>=c?o.expandRow(t,h,g):o.expandRow(t,s-1,g);const u=[0,h+1],l=[o.rowCount-1,o.colCount-1];v(o,r,n,u,l)}}(t,o,i,n,r),t.notMoveForvard=!1;const s=function(t,e,o,i){if(i){const i=[...e.getOutgoingEdgesFor(t)].filter(t=>!o.has(t.target)&&"bpmn:BoundaryEvent"===t.target.$type);return i.length>0?[...e.getIncomingEdgesFor(t)].filter(t=>!o.has(t.source)).concat(i).map(e=>e.target!==t?e.target:e.source):[...e.getIncomingEdgesFor(t)].filter(t=>!o.has(t.source)).map(t=>"bpmn:BoundaryEvent"===t.source.$type?t.source.attachedToRef:t.source)}return[...e.getOutgoingEdgesFor(t)].filter(t=>!o.has(t.target)).map(t=>t.target)}(t,n,r,o.isFlipped),d=function(t,e,o,i,r,n){const s=P(t,r,n,e.isFlipped),d=C(t,r,n,e.isFlipped).map(t=>t.source),[,a]=e.find(t),c=[...s].filter(t=>{const s=e.isFlipped?t.source:t.target,c=e.find(s)[1];if(!b(s,r,o,n,e.isFlipped))return!1;return(!C(s,r,n,e.isFlipped).map(t=>t.source).find(t=>d.includes(t))||!i)&&c<=a}).map(t=>e.isFlipped?t.source:t.target).sort(p(e));for(const t of c)o.splice(o.indexOf(t),1),n.delete(t),e.removeElement(t);return e.shrinkRows(),e.shrinkCols(),c}(t,o,i,s&&s.length>0,n,r);let a=[...s,...d],c=[],h=null;a.forEach(e=>{const s=function(t,e,o,i,r,n,s){if("bpmn:BoundaryEvent"===i.$type)return o.find(t);const d=o.find(t);if(!d)return;const a=s?[d[0]+1,d[1]+1]:[d[0],d[1]+1],c=o.find(t),h=[...o.get(c[0],c[1])],g=[];for(const t of h)[...o.isFlipped?o.getExistingIncomingEdgesFor(t):o.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source&&n.has(t.source)&&n.has(t.target)).forEach(t=>g.push(t));for(let t=a[0];t<=o.rowCount;t++){const e=[t,a[1]],i=g.some(o=>o.targetPosition[0]===t&&o.targetPosition[1]===a[1]||o.isIntersect(e));if(i&&t===o.colCount-1)a[0]=t+1;else if(!i){a[0]=t;break}}(o.get(a[0],a[1])||o.isCrossed(a,!0))&&o.createCol(a[1]-1);o.isCrossed([a[0],a[1]])&&o.createRow(a[0]-1);return a}(t,0,o,e,0,r,"bpmn:BoundaryEvent"===t.$type);o.add(e,s),r.add(e),S(e,o,n,r,i,c,!0),h=e,c.unshift(e)});const g=[],u=[];for(const t of c)"bpmn:BoundaryEvent"===t.$type?g.push(t):u.push(t);return g.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),c=[...g,...u],c}function b(t,e,o,i,r){const n=o.includes(t),s=P(t,e,i,r);return n&&(!s||0===s.length)}function P(t,e,o,i){return[...i?e.getIncomingEdgesFor(t):e.getOutgoingEdgesFor(t)].filter(e=>o.has(i?e.source:e.target)&&o.has(t))}function C(t,e,o,i){return[...i?e.getOutgoingEdgesFor(t):e.getIncomingEdgesFor(t)].filter(e=>o.has(i?e.source:e.target)&&o.has(t))}function v(t,e,o,i,r){if(!t.isValidPosition(i)||!t.isValidPosition(r))throw new Error("fixCrossesInGridPart: invalid position");const[n,s]=i,[d,a]=r;for(let i=n;i<=d;i++)for(let r=s;r<=a;r++){const n=t.get(i,r);n&&S(n,t,e,o)}}function S(t,e,o,i,r,n,s,d){const a=[...[...e.getExistingOutgoingEdgesFor(t)].sort((t,e)=>{const[o,i]=t.targetPosition,[r,n]=e.targetPosition;return o-r||i-n}),...[...e.getExistingIncomingEdgesFor(t)].sort((t,e)=>{const[o,i]=t.sourcePosition,[r,n]=e.sourcePosition;return o-r||i-n})].filter(e=>{const{target:d,source:a,direction:c}=e;return d!==a&&((!n||!n.includes(d))&&((!r||!b(d,o,r,i))&&(!s||a!==t||"SE_NW"!==c&&"S_N"!==c)))});for(const t of a)F(t,e),"E_W"===t.direction&&t._originalSourceIsBoundary||R(t,e)}function F(t,e){const{direction:o}=t;if("W_E"===o||"E_W"===o)return;const i=t.crossedElements(!0);i.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||I(i,e):D(i,e):I(i,e):D(i,e))}function R(t,e){const{direction:o}=t;if("S_N"===o||"N_S"===o)return;const i=t.crossedElements(!1);if(0!==i.length){if("SW_NE"===o){const o=function(t){const e=t.sourcePosition,o=t.targetPosition;let i=e[0];for(let r=e[0];r<t._grid.rowCount&&t._grid.getElementsInRange({row:r,col:e[1]+1},{row:r,col:o[1]}).length>0;r++)i++;return i-e[0]}(t),[i]=t.sourcePosition;for(let t=o;t>0;t--)e.createRow(i-1);const r=t._grid.getElementsInRange({row:t.sourcePosition[0],col:t.sourcePosition[1]+1},{row:t._grid.rowCount-1,col:t._grid.colCount-1});for(const e of r){const[i,r]=t._grid.find([...e][0]);for(const n of[...e])t._grid.move(n,[i-o,r])}return}"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||N(i,e):T(i,e):N(i,e):T(i,e)}}function N(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 r of[...i])e.move(r,[o,t])}}function T(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 r of[...i])e.move(r,[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 r of[...i])e.move(r,[t,o+1])}}function I(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 r of[...i])e.move(r,[t,o])}}function W(t,e,o,n,s,c){if(t.di)return[];if("bpmn:BoundaryEvent"===t.$type)return function(t,e,o,i,r,n){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&&r.hasElement(t));return a=function(t,e){return t.sort((t,o)=>{const i=G(t,e),r=G(o,e);return i[0]-r[0]||i[1]-r[1]}).reverse()}(a,r),a.forEach((r,a,c)=>{r.gridPosition={row:e,col:o};const h=f(r,e,o,n,t.attachedToRef);h.x=s.x+(a+1)*(s.width/(c.length+1))-h.width/2;const g=i.createDiShape(r,h,{id:r.id+"_di"});r.di=g,r.gridPosition={row:e,col:o},d.push(g)}),d}(t,e,o,n,s,c);const h=f(t,e,o,c);if(t.isExpanded&&t.grid){const{width:e,height:o}=i(t),{rowCount:r,colCount:n}=t.grid;h.width=n*d+e,h.height=r*a+o}const g={id:t.id+"_di"};t.isExpanded&&(g.isExpanded=!0),r(t,"bpmn:ExclusiveGateway")&&(g.isMarkerVisible=!0);const u=n.createDiShape(t,h,g);return t.di=u,t.gridPosition={row:e,col:o},[u]}function G(t,e){return n(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 B{constructor(e){this.moddle=new t,this.diFactory=new m(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(),r=this.getCollaboration();return i.length>0&&(this.cleanDi(),this.createRootDi(i,r),this.drawParticipants(),this.drawProcesses(),this.drawCollaborationMessageFlows(r)),(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(),O(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:r,y:n}=o.bounds;r+=75,n+=70,this.generateDi(e.grid,{x:r,y:n},i);continue}if(e.isExpanded&&!this.existingNodes.includes(e))continue;if(e.isExpanded){const t=this.getElementDi(e),o=this.getProcDi(t);let{x:r,y:n}=t.bounds;const{width:s,height:d}=i(e);r+=75-s/4,n+=a-d-d/4,this.generateDi(e.grid,{x:r,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 r=e.di.bounds,n=o.di.bounds,s=n.y-r.y,d=[{x:r.x+r.width/2},{x:n.x+n.width/2}];s>0?(d[0].y=r.y+r.height,d[1].y=n.y):(d[0].y=r.y,d[1].y=n.y+n.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 o=new e.Graph,i=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 i)o.addNode(t);for(const t of i){const e=this.getSubProcesses(t);for(const i of e)o.addEdge({source:t,target:i})}const r=o.getSeparatedGraphs();for(const t of r){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 r){const o=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},i=(t,e,o)=>{const{left:i,level:r}=t,n=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(n)return n.level=r+1,n.left=i+1,[n];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(o,e.getLast,i,e.addToEnd)}return r}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:r}=t.processRef.grid,{width:n,height:s}=i(t),c=o>0?o*d+d:n,h=r>0?r*a+a:s,g=this.diFactory.createDiShape(t,{width:c,height:h,...e},{id:t.id+"_di"});return this.diagram.diagrams[0].plane.get("planeElement").push(g),g.bounds.y+g.bounds.height}cleanDi(){this.diagram.diagrams=[]}createGridLayout(t){const o=new _,i=new e.Graph;for(const e of t.flowElements||[])r(e,"bpmn:SequenceFlow")||r(e,"bpmn:DataObject")||i.addNode(e);for(const t of i.nodes){"bpmn:BoundaryEvent"===t.$type&&(i.addEdge({source:t,target:t.attachedToRef}),i.addEdge({source:t.attachedToRef,target:t}));for(const e of t.outgoing||[])i.addEdge({source:e.sourceRef,target:e.targetRef,id:e.id});const e=t.dataInputAssociations;for(const t of e||[])for(const e of t.sourceRef||[])i.addEdge({source:e,target:t.$parent,id:t.id,propRef:t.targetRef});const o=t.dataOutputAssociations;for(const t of o||[]){const e=t.$parent,o=t.targetRef;i.addEdge({source:e,target:o,id:t.id})}}o.baseGraph=i;return i.genericTraversing((t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const i=[...t].find(i=>o.isFlipped?[...e.getOutgoingEdgesFor(i)].filter(e=>!t.has(e.target)).length>0:[...e.getIncomingEdgesFor(i)].filter(e=>!t.has(e.source)).length>0);if(i)return o.flipHorizontally(),i;const r=e.nodes.filter(i=>o.isFlipped?!t.has(i)&&0===e.getOutgoingEdgesFor(i).size&&!s(i):!t.has(i)&&0===e.getIncomingEdgesFor(i).size&&!s(i));if(r.length>0)return E(r,"bpmn:StartEvent")[0];const n=[...t].find(i=>(o.isFlipped?[...e.getIncomingEdgesFor(i)].filter(e=>!t.has(e.source)):[...e.getOutgoingEdgesFor(i)].filter(e=>!t.has(e.target))).length>0);if(n)return n;const d=e.nodes.find(i=>{if(t.has(i))return!1;return 0===(o.isFlipped?[...e.getOutgoingEdgesFor(i)].filter(t=>t.target!==i):[...e.getIncomingEdgesFor(i)].filter(t=>t.source!==i)).length});if(d)return d;const a=e.nodes.find(i=>{if(t.has(i))return!1;return 0===(o.isFlipped?[...e.getIncomingEdgesFor(i)].filter(t=>t.target!==t.source):[...e.getOutgoingEdgesFor(i)]).length});return a?(o.flipHorizontally(),a):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))},t=>t.pop(),(t,e,i,r)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const n=w(t,o,r,i,e);return this.currentDebugStep+=1,n},(t,e)=>{for(const o of t)e.push(o)},t=>{o.hasElement(t)||o.add(t)}),o.isFlipped&&o.flipHorizontally(),o}generateDi(t,e,o){const i=this.diFactory,r=(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:n,col:s})=>{const d=W(o,n,s,i,t,e);r.push(...d)}),t._allEdges.forEach(o=>{const n=function(t,e,o,i){const{source:r,target:n,id:s}=t;if(!s)return;const d=h(r,n,e,!1,i);return o.createDiEdge(t,d,{id:s+"_di"})}(o,t,i,e);n&&r.push(n)})}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 O(t){for(let e=t.colCount-1;e>=0;e--){const o=[];for(let i=0;i<t.rowCount;i++){const r=[...t.get(i,e)||[]].find(t=>t.isExpanded);r&&o.push(r)}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),r=i||2;t.createCol(e,r)}}function $(t){for(let e=t.rowCount-1;e>=0;e--){const o=[];for(let i=0;i<t.colCount;i++){const r=[...t.get(e,i)||[]].find(t=>t.isExpanded);r&&o.push(r)}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),r=i||1;for(let o=0;o<r;o++)t.createRow(e)}}exports.layoutProcess=function(t,e){return new B(e).layoutProcess(t)};//# sourceMappingURL=index.cjs.map
|