yet-another-bpmn-auto-layout 0.0.5 → 0.0.6
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.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 +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("bpmn-moddle"),e=require("graph-by-ivan-tulaev"),o=require("min-dash");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){if(!t)throw new Error("Element is not defined in getOutgoingElements");const e=(t.outgoing||[]).map(t=>t.targetRef);return[...new Set(e)]}const s=150,d=140;function g(t,e){return new Set(r(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 a(t,e,o){const{source:n,target:s}=t,a=n.di,l=s.di,f=a.get("bounds"),p=l.get("bounds"),E=c(f),m=c(p),x=e.getEdgeDirection(t),y=s.gridPosition.col-n.gridPosition.col,w=s.gridPosition.row-n.gridPosition.row,_=`${w>0?"bottom":"top"}-${y>0?"right":"left"}`,S=`${w>0?"top":"bottom"}-${y>0?"left":"right"}`,{x:b,y:C}=u(n,o),{x:P}=u(s,o),F="bpmn:BoundaryEvent"===n.$type;if("NO_DIRECTION"===x)return F?[h(E,f,"b",_),{x:E.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:P,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:b,y:m.y},h(m,p,"l",S)]:[h(E,f,"b",_),{x:E.x,y:n.isExpanded?C+(n.grid.rowCount+1)*d:C+d},{x:P,y:n.isExpanded?C+(n.grid.rowCount+1)*d:C+d},{x:b,y:m.y},h(m,p,"l",S)];if("S_N"===x){if(F)return[h(E,f,"b",_),{x:E.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:b,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:P,y:m.y},h(m,p,"l",S)];return[...g(s)].includes(n)?[h(E,f,"l",_),{x:b,y:E.y},{x:P,y:m.y},h(m,p,"l",S)]:[h(E,f,"t",_),h(m,p,"b",S)]}if("SW_NE"===x)return F?[h(E,f,"b",_),{x:E.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:m.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},h(m,p,"b",S)]:[h(E,f,"r"),{x:m.x,y:E.y},h(m,p,"b")];if("W_E"===x){if(F)return[h(E,f,"b",_),{x:E.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:m.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},h(m,p,"b",S)];const t=h(E,f,"r",_);n.isExpanded&&(t.y=f.y+i(n).height/2);const e=h(m,p,"l",S);return s.isExpanded&&(e.y=p.y+i(s).height/2),[t,e]}if("NW_SE"===x)return[h(E,f,"b"),{x:E.x,y:m.y},h(m,p,"l")];if("N_S"===x){if(F){const t=h(E,f,"b",_),e=h(m,p,"t",S);return n.attachedToRef.isExpanded?[t,{x:E.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:m.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},e]:[t,e]}const t=h(E,f,"b",_),e=h(m,p,"t",S);return n.isExpanded||s.isExpanded?[t,{x:E.x,y:n.isExpanded?C+(n.grid.rowCount+1)*d:C+d},{x:m.x,y:n.isExpanded?C+(n.grid.rowCount+1)*d:C+d},e]:[t,e]}if("NE_SW"===x)return[h(E,f,"b"),{x:E.x,y:m.y},h(m,p,"r")];if("E_W"===x){if(F){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,g=[...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 g.reduce((t,e)=>e.grid?.getGridDimensions()[0]>t?e.grid?.getGridDimensions()[0]:t,0)}(n,s,e);return[h(E,f,"b",_),{x:E.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d+t*d},{x:m.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d+t*d},h(m,p,"b",S)]}const t=r(s).includes(n);let o=e?e.getExistingOutgoingEdgesFor(s):[];if(o=o.some(t=>"SW_NE"===e.getEdgeDirection(t)),t||o)return[h(E,f,"b",_),{x:E.x,y:C+(n.isExpanded?d*(n.grid.colCount+1.5):d)},{x:m.x,y:C+(n.isExpanded?d*(n.grid.colCount+1.5):d)},h(m,p,"b",S)];{const t=h(E,f,"l",_);n.isExpanded&&(t.y=f.y+i(n).height/2);const e=h(m,p,"r",S);return s.isExpanded&&(e.y=p.y+i(s).height/2),[t,e]}}if("SE_NW"===x){if(F)return[h(E,f,"b",_),{x:E.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},{x:m.x,y:n.attachedToRef.isExpanded?C+(n.attachedToRef.grid.rowCount+1)*d:C+d},h(m,p,"b",S)];const t=[];for(let o=n.gridPosition.col-1;o>=s.gridPosition.col;o--){const i=e?e.get(n.gridPosition.row,o):null;i&&t.push(i)}return t.length>0?[h(E,f,"b",_),{x:E.x,y:n.isExpanded?C+(n.grid.rowCount+1)*d:C+d},{x:m.x,y:n.isExpanded?C+(n.grid.rowCount+1)*d:C+d},h(m,p,"b",S)]:[h(E,f,"l"),{x:m.x,y:E.y},h(m,p,"b")]}const v=function(t,e,o){const{row:i,col:n}=t.gridPosition,{row:r,col:s}=e.gridPosition,d=s-n,g=r-i;if(!(d>0&&0!==g))return;if(g>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,s,e);if(v){const t=h(E,f,v[0],_),e=h(m,p,v[1],S);return[t,"h"===v[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const T=-Math.sign(w)*d/2;return[h(E,f,"r",_),{x:E.x+75,y:E.y},{x:E.x+75,y:m.y+T},{x:m.x-75,y:m.y+T},{x:m.x-75,y:m.y},h(m,p,"l",S)]}function c(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function h(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 u(t,e={x:0,y:0}){const o=t.gridPosition.row;return{x:t.gridPosition.col*s+e.x,y:o*d+e.y}}function l(t,e,o,n,r){const{width:g,height:a}=i(t),{x:c,y:h}=n;if(!r)return{width:g,height:a,x:o*s+(s-g)/2+c,y:e*d+(d-a)/2+h};const u=r.di.bounds;return{width:g,height:a,x:Math.round(u.x+u.width/2-g/2),y:Math.round(u.y+u.height-a/2)}}function f(t,e){const o=[e].flat();let i=[];return o.length>0?o.forEach((e,r)=>{const s=t.filter(t=>n(t,e));if(i=i.concat(s),r===o.length-1&&i.length!==t.length)for(const e of t)i.includes(e)||i.push(e)}):i=t,i}class p{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 E{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){this.elements.has(t)&&this.isValidPosition(e)&&(this.removeElement(t),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))}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 m extends E{constructor(t,o,i){super(o,i),this.initialGraph=t,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].includes(t)}_addEdgeToGrid(t){this._edgeIsExist(t)||this.graph.addEdge(t)}_createNewEdgesFor(t){const e=[...this.initialGraph.getOutgoingEdgesFor(t),...this.initialGraph.getIncomingEdgesFor(t)].filter(t=>this.hasElement(t.source)&&this.hasElement(t.target));for(const t of e)this._addEdgeToGrid(t)}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.Graph.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new m(this.initialGraph,this.colCount,this.rowCount)];const o=[];for(const e of t){const t=new m(this.initialGraph,this.colCount,this.rowCount);let i=null,n=null;for(const o of f(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 m(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.getEdgeSource(t);return this.find(e)}getTargetPosition(t){const e=this.getEdgeTarget(t);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,g=n-s;return 0===d&&0===g?"NO_DIRECTION":d>0&&0===g?"S_N":d>0&&g<0?"SW_NE":0===d&&g<0?"W_E":d<0&&g<0?"NW_SE":d<0&&0===g?"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[]}getEdgeSource(t){return this.isFlipped?t.target:t.source}getEdgeTarget(t){return this.isFlipped?t.source:t.target}_pathForSouthToNorth(t){const e=[],[o,i]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id)return e;if(this.hasIntermediateElements(this.getSourcePosition(t),this.getTargetPosition(t),!0))return e;if([...this.getExistingOutgoingEdgesFor(this.getEdgeTarget(t))].map(t=>this.getEdgeTarget(t)).includes(this.getEdgeSource(t)))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.getEdgeSource(t).$type)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.getEdgeSource(t).$type||!t.id)for(let t=i-1;t>r;t--)e.push({position:[o,t],hCross:!0});"bpmn:BoundaryEvent"===this.getEdgeSource(t).$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,g]=this.getTargetPosition(t),a=this.getEdgeDirection(t);return"S_N"===a?o&&n===s&&i<r&&i>d:"SW_NE"===a?!!(o&&n===g&&i<=r&&i>d)||!o&&n>s&&n<=g&&i===r:"W_E"===a?!o&&n>s&&n<g&&i===r:"NW_SE"===a?!!(o&&n===s&&i>r&&i<=d)||!o&&n>=s&&n<g&&i===d:"N_S"===a?o&&n===s&&i>r&&i<d:"NE_SW"===a?!!(o&&n===s&&i>r&&i<=d)||!o&&n>g&&n<=s&&i===d:"E_W"===a?!o&&n>g&&n<s&&i===r:"SE_NW"===a?!!(o&&n===g&&i>d&&i<=r)||!o&&n>=g&&n<s&&i===r&&!this.getExistingIncomingEdgesFor(this.getEdgeSource(t)).some(t=>"NW_SE"===this.getEdgeDirection(t)||"W_E"===this.getEdgeDirection(t)):void 0}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.Graph;return this.graph.genericTraversing((t,e,i,n)=>{const r=this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeTarget(t),o=this.getEdgeSource(t);return!i.has(e)&&e!==o});o.addNode(t);const s=r.map(t=>this.getEdgeTarget(t));for(const t of s)o.addNode(t);for(const t of r)o.addEdge(t);return s},(e,o)=>e.has(t)?void 0:t),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 x(t,e,o,i,n){e.hasElement(t)||e.add(t);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.getEdgeSource(t)),[,g]=e.find(t),a=[...s].filter(t=>{const n=e.getEdgeTarget(t),r=e.find(n)[1];if(!y(n,o,e))return!1;return(!e.getExistingIncomingEdgesFor(n).map(t=>t.source).find(t=>d.includes(t))||!i)&&r<=g}).map(t=>e.getEdgeTarget(t)).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 a)o.splice(o.indexOf(t),1),r.delete(t),e.removeElement(t);return e.shrinkRows(),e.shrinkCols(),a}(t,e,o,r&&r.length>0,0,i);let d=[...r,...s],g=[];d.forEach(n=>{const r=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])],g=[];for(const t of d)[...e.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source).forEach(t=>g.push(t));for(let t=r[0];t<=e.rowCount;t++){const o=[t,r[1]],i=g.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,n);if(e.isFlipped&&"bpmn:BoundaryEvent"===n.$type&&n.attachedToRef!==t){const t=n.attachedToRef;i.has(t)||(e.add(t,r),i.add(t),w(t,e,o,g),g.unshift(n))}e.add(n,r),i.add(n),w(n,e,o,g),function(t,e){let o=e.getBackwardUpOutgoingEdgesFor(t);for(;o.length>0;){const i=o.shift(),n=e.getSourcePosition(i),r=e.getTargetPosition(i),s=e.getEdgeSource(i),d=e.getEdgeTarget(i);if(r[1]>e.find(t)[1])continue;const g=e.getGridCopy();g.removeEdge(i),g._grid.forEach((t,e)=>{t.forEach((t,o)=>{if(o<r[1])for(const t of g.get(e,o)||new Set)g.removeElement(t)})});const a=g.getGraphSegmentFrom(d);if(a.nodes.includes(s))continue;const c=Array.from(g.getSegmentLeftCoordinates(a)),h=c.reduce((t,e)=>void 0===t||e[1]<t[1]?e:t,void 0),u=c.reduce((t,e)=>void 0===t||e[0]<t[0]?e:t,void 0),l=c.reduce((t,e)=>void 0===t||e[0]>t[0]?e:t,void 0),f=n[1]-h[1]+1,p=new Map(c);let E;for(let o=0;o<e.rowCount;o++){const i=(()=>{if(o<u[0])return E=u[0],u[1]-1;if(p.has(o))return E=p.get(o),p.get(o)-1;const i=e.find(t)||[];return o>u[0]&&o<l[0]&&o<i[0]?E:o===i[0]||o>u[0]?(E=i,i[1]):E})();e.expandRow(o,i,f)}}e.toRectangle()}(n,e),g.unshift(n)});const a=[],c=[];for(const t of g)"bpmn:BoundaryEvent"===t.$type?a.push(t):c.push(t);return a.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),g=[...a,...c],g}function y(t,e,o){const i=e.includes(t),n=o.getExistingOutgoingEdgesFor(t);return i&&!n?.length>0}function w(t,e,o,i,n){const r=[...[...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(n=>{const{target:r,source:s}=n,d=e.getEdgeDirection(n);return r!==s&&((!i||!i.includes(r))&&((!o||!y(r,o,e))&&(s!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of r)_(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===e.getEdgeSource(t).$type&&t.id||S(t,e)}function _(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||F(i,e):P(i,e):F(i,e):P(i,e))}function S(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||b(i,e):C(i,e):b(i,e):C(i,e)}}function b(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 C(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 P(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 F(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 v(t,e,o,r,g,a){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 g=t.$parent.flowElements.filter(e=>e.attachedToRef===t.attachedToRef&&n.hasElement(t));return g=function(t,e){return t.sort((t,o)=>{const i=T(t,e),n=T(o,e);return i[0]-n[0]||i[1]-n[1]}).reverse()}(g,n),g.forEach((n,g,a)=>{n.gridPosition={row:e,col:o};const c=l(n,e,o,r,t.attachedToRef);c.x=s.x+(g+1)*(s.width/(a.length+1))-c.width/2;const h=i.createDiShape(n,c,{id:n.id+"_di"});n.di=h,n.gridPosition={row:e,col:o},d.push(h)}),d}(t,e,o,r,g,a);const c=l(t,e,o,a);if(t.isExpanded&&t.grid){const{width:e,height:o}=i(t),{rowCount:n,colCount:r}=t.grid;c.width=r*s+e,c.height=n*d+o}const h={id:t.id+"_di"};t.isExpanded&&(h.isExpanded=!0),n(t,"bpmn:ExclusiveGateway")&&(h.isMarkerVisible=!0);const u=r.createDiShape(t,c,h);return t.di=u,t.gridPosition={row:e,col:o},[u]}function T(t,e){return r(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 N{constructor(t,e){this.source=t,this.target=e}}class R{constructor(e){this.moddle=new t,this.diFactory=new p(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(),D(e),W(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:n,y:r}=t.bounds;const{width:s,height:g}=i(e);n+=75-s/4,r+=d-g-g/4,this.generateDi(e.grid,{x:n,y:r},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 g=this.diFactory.createDiEdge(t,d);this.diagram.diagrams[0].plane.get("planeElement").push(g)}}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 n=o.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},o=(t,e)=>{const{left:o,level:i}=t,n=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(n)return n.level=i+1,n.left=o+1,[n];const r=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).reduce((t,e)=>void 0===t||e.right>t.right?e.right:t,void 0);t.right=r?r+1:t.left+1;const s=[...e.getIncomingEdgesFor(t)].map(t=>t.source).find(t=>void 0===t.right);return s?[s]:void 0};t.genericTraversing(o,e)}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:g}=i(t),a=o>0?o*s+s:r,c=n>0?n*d+d:g,h=this.diFactory.createDiShape(t,{width:a,height:c,...e},{id:t.id+"_di"});return this.diagram.diagrams[0].plane.get("planeElement").push(h),h.bounds.y+h.bounds.height}cleanDi(){this.diagram.diagrams=[]}createGridLayout(t){const o=new e.Graph,i=new m(o);for(const e of t.flowElements||[])n(e,"bpmn:SequenceFlow")||n(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new N(t,t.attachedToRef)),o.addEdge(new N(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new N(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 N(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 N(e,i);n.id=t.id,o.addEdge(n)}}return o.genericTraversing((t,e,o,n)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const r=x(t,i,n,o);return this.currentDebugStep+=1,r},(t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const o=[...t].find(o=>(i.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(e=>!t.has(i.isFlipped?e.target:e.source)).length>0);if(o)return i.flipHorizontally(),o;const r=e.nodes.filter(o=>{const r=i.isFlipped?e.getOutgoingEdgesFor(o):e.getIncomingEdgesFor(o);return!(t.has(o)||0!==r.size||(s=o,(n(s,"bpmn:IntermediateThrowEvent")||n(s,"bpmn:IntermediateCatchEvent"))&&(void 0===s.incoming||0===s.incoming.length)));var s});if(r.length>0)return f(r,"bpmn:StartEvent")[0];const s=[...t].find(o=>(i.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(e=>!t.has(i.isFlipped?e.source:e.target)).length>0);if(s)return s;const d=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(i.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(t=>o!==(i.isFlipped?t.target:t.source)).length});if(d)return d;const g=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(i.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(t=>t.target!==t.source).length});return g?(i.flipHorizontally(),g):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))}),i.isFlipped&&i.flipHorizontally(),i}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 d=v(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){const r=a(t,e,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 D(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)=>void 0===t||e.grid.colCount>t?e.grid.colCount:t,void 0),n=i||2;t.createCol(e,n)}}function W(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)=>void 0===t||e.grid.rowCount>t?e.grid.rowCount:t,void 0),n=i||1;for(let o=0;o<n;o++)t.createRow(e)}}exports.layoutProcess=function(t,e){return new R(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 s(t){return i(t,"bpmn:SubProcess")||i(t,"bpmn:Task")?{width:100,height:80}:i(t,"bpmn:Gateway")?{width:50,height:50}:i(t,"bpmn:Event")?{width:36,height:36}:i(t,"bpmn:Participant")||i(t,"bpmn:Lane")?{width:400,height:100}:i(t,"bpmn:DataObjectReference")?{width:36,height:50}:i(t,"bpmn:DataStoreReference")?{width:50,height:50}:i(t,"bpmn:TextAnnotation")?{width:100,height:30}:{width:100,height:80}}function i(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)]}const r=150,d=140;function a(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 g(t,e,o){const{source:i,target:r}=t,g=i.di,l=r.di,f=g.get("bounds"),p=l.get("bounds"),E=h(f),m=h(p),w=e.getEdgeDirection(t),x=r.gridPosition.col-i.gridPosition.col,y=r.gridPosition.row-i.gridPosition.row,_=`${y>0?"bottom":"top"}-${x>0?"right":"left"}`,C=`${y>0?"top":"bottom"}-${x>0?"left":"right"}`,{x:S,y:b}=u(i,o),{x:F}=u(r,o),v="bpmn:BoundaryEvent"===i.$type;if("NO_DIRECTION"===w)return v?[c(E,f,"b",_),{x:E.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:F,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:S,y:m.y},c(m,p,"l",C)]:[c(E,f,"b",_),{x:E.x,y:i.isExpanded?b+(i.grid.rowCount+1)*d:b+d},{x:F,y:i.isExpanded?b+(i.grid.rowCount+1)*d:b+d},{x:S,y:m.y},c(m,p,"l",C)];if("S_N"===w){if(v)return[c(E,f,"b",_),{x:E.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:S,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:F,y:m.y},c(m,p,"l",C)];return[...a(r)].includes(i)?[c(E,f,"l",_),{x:S,y:E.y},{x:F,y:m.y},c(m,p,"l",C)]:[c(E,f,"t",_),c(m,p,"b",C)]}if("SW_NE"===w)return v?[c(E,f,"b",_),{x:E.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:m.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},c(m,p,"b",C)]:[c(E,f,"r"),{x:m.x,y:E.y},c(m,p,"b")];if("W_E"===w){if(v)return[c(E,f,"b",_),{x:E.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:m.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},c(m,p,"b",C)];const t=c(E,f,"r",_);i.isExpanded&&(t.y=f.y+s(i).height/2);const e=c(m,p,"l",C);return r.isExpanded&&(e.y=p.y+s(r).height/2),[t,e]}if("NW_SE"===w)return[c(E,f,"b"),{x:E.x,y:m.y},c(m,p,"l")];if("N_S"===w){if(v){const t=c(E,f,"b",_),e=c(m,p,"t",C);return i.attachedToRef.isExpanded?[t,{x:E.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:m.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},e]:[t,e]}const t=c(E,f,"b",_),e=c(m,p,"t",C);return i.isExpanded||r.isExpanded?[t,{x:E.x,y:i.isExpanded?b+(i.grid.rowCount+1)*d:b+d},{x:m.x,y:i.isExpanded?b+(i.grid.rowCount+1)*d:b+d},e]:[t,e]}if("NE_SW"===w)return[c(E,f,"b"),{x:E.x,y:m.y},c(m,p,"r")];if("E_W"===w){if(v){const t=function(t,e,o){const[s,i]=o.find(t),[,n]=o.find(e),r=i<n?i:n,d=i<n?n:i,a=[...o.elements].map(t=>void 0!==t.size?[...t]:t).flat().filter(t=>t.gridPosition.row===s&&t.gridPosition.col>r&&t.gridPosition.col<d);return a.reduce((t,e)=>e.grid?.getGridDimensions()[0]>t?e.grid?.getGridDimensions()[0]:t,0)}(i,r,e);return[c(E,f,"b",_),{x:E.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d+t*d},{x:m.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d+t*d},c(m,p,"b",C)]}const t=n(r).includes(i);let o=e?e.getExistingOutgoingEdgesFor(r):[];if(o=o.some(t=>"SW_NE"===e.getEdgeDirection(t)),t||o)return[c(E,f,"b",_),{x:E.x,y:b+(i.isExpanded?d*(i.grid.colCount+1.5):d)},{x:m.x,y:b+(i.isExpanded?d*(i.grid.colCount+1.5):d)},c(m,p,"b",C)];{const t=c(E,f,"l",_);i.isExpanded&&(t.y=f.y+s(i).height/2);const e=c(m,p,"r",C);return r.isExpanded&&(e.y=p.y+s(r).height/2),[t,e]}}if("SE_NW"===w){if(v)return[c(E,f,"b",_),{x:E.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},{x:m.x,y:i.attachedToRef.isExpanded?b+(i.attachedToRef.grid.rowCount+1)*d:b+d},c(m,p,"b",C)];const t=[];for(let o=i.gridPosition.col-1;o>=r.gridPosition.col;o--){const s=e?e.get(i.gridPosition.row,o):null;s&&t.push(s)}return t.length>0?[c(E,f,"b",_),{x:E.x,y:i.isExpanded?b+(i.grid.rowCount+1)*d:b+d},{x:m.x,y:i.isExpanded?b+(i.grid.rowCount+1)*d:b+d},c(m,p,"b",C)]:[c(E,f,"l"),{x:m.x,y:E.y},c(m,p,"b")]}const P=function(t,e,o){const{row:s,col:i}=t.gridPosition,{row:n,col:r}=e.gridPosition,d=r-i,a=n-s;if(!(d>0&&0!==a))return;if(a>0){let t=0;const e={row:n,col:i};return t+=o.getElementsInRange({row:s,col:i},e).length,t+=o.getElementsInRange(e,{row:n,col:r}).length,!(t>2)&&["v","h"]}{let t=0;const e={row:s,col:r};return t+=o.getElementsInRange({row:s,col:i},e).length,t+=o.getElementsInRange(e,{row:n,col:r}).length,!(t>2)&&["h","v"]}}(i,r,e);if(P){const t=c(E,f,P[0],_),e=c(m,p,P[1],C);return[t,"h"===P[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const T=-Math.sign(y)*d/2;return[c(E,f,"r",_),{x:E.x+75,y:E.y},{x:E.x+75,y:m.y+T},{x:m.x-75,y:m.y+T},{x:m.x-75,y:m.y},c(m,p,"l",C)]}function h(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function c(t,e,o="r",s="top-left"){if("h"===o&&(o=/left/.test(s)?"l":"r"),"v"===o&&(o=/top/.test(s)?"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 u(t,e={x:0,y:0}){const o=t.gridPosition.row;return{x:t.gridPosition.col*r+e.x,y:o*d+e.y}}function l(t,e,o,i,n){const{width:a,height:g}=s(t),{x:h,y:c}=i;if(!n)return{width:a,height:g,x:o*r+(r-a)/2+h,y:e*d+(d-g)/2+c};const u=n.di.bounds;return{width:a,height:g,x:Math.round(u.x+u.width/2-a/2),y:Math.round(u.y+u.height-g/2)}}function f(t,e){const o=[e].flat();let s=[];return o.length>0?o.forEach((e,n)=>{const r=t.filter(t=>i(t,e));if(s=s.concat(r),n===o.length-1&&s.length!==t.length)for(const e of t)s.includes(e)||s.push(e)}):s=t,s}class p{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,s){return this.create("bpmndi:BPMNShape",o.assign({bpmnElement:t,bounds:this.createDiBounds(e)},s))}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,s){return this.create("bpmndi:BPMNEdge",o.assign({bpmnElement:t,waypoint:this.createDiWaypoints(e)},s))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}class E{constructor(){this.isFlipped=!1,this._elements=new Map,this.rows={},this.cols={}}get rowCount(){return Object.keys(this.rows).length}get colCount(){return Object.keys(this.cols).length}get elementsCount(){return this._elements.size}get elements(){return new Set(this._elements.keys())}add(t,e){if(this._elements.has(t))throw new Error(`Cannot add element duplicated element ${JSON.stringify(t)}`);if(!e)return void this._addStart(t);const o=this.rowCount-1,s=this.colCount-1,i=e[0]-o,n=e[1]-s;this.addRowCol(!1,o>=0?o:void 0,i),this.addRowCol(!0,s>=0?s:void 0,n),this._elements.set(t,e),this._addElementToRowsCols(t,e)}_addElementToRowsCols(t){const e=this._elements.get(t);this.rows[e[0]]?this.rows[e[0]].add(t):this.rows[e[0]]=new Set([t]),this.cols[e[1]]?this.cols[e[1]].add(t):this.cols[e[1]]=new Set([t])}_removeElementFromRowsCols(t){const e=this._elements.get(t);this.rows[e[0]]&&this.rows[e[0]].delete(t),this.cols[e[1]]&&this.cols[e[1]].delete(t)}_addStart(t){const[e]=this.getGridDimensions();this._elements.set(t,[e,0]),this._addElementToRowsCols(t,[e,0])}move(t,e){if(!this.elements.has(t))throw new Error(`Cannot move element not exist element ${t.toString()}`);if(!this.isValidPosition(e))throw new Error(`Cannot move element ${t.toString()} to invalid position ${e}`);this._removeElementFromRowsCols(t),this._elements.set(t,e),this._addElementToRowsCols(t,e)}removeElement(t){this._elements.has(t)&&(this._removeElementFromRowsCols(t),this._elements.delete(t))}addRowCol(t,e,o=1){const s=t?this.colCount:this.rowCount,i=e>=s-1?e-(s-1)+o:o;for(let e=0;e<i;e++)t?this.cols[s+e]=new Set:this.rows[s+e]=new Set;for(const[s,i]of this._elements.entries()){((t?i[1]:i[0])>e||void 0===e)&&(this._removeElementFromRowsCols(s),i[t?1:0]+=o,this._addElementToRowsCols(s))}}expandRow(t,e,o=1){if(!Number.isInteger(t)||t<0||t>this.rowCount-1)throw new Error(`Can't expand row with index: ${t}. Grid row count is ${this.rowCount}`);const s=this.colCount,i=e>=s-1?e-(s-1)+o:o;for(let t=0;t<i;t++)this.cols[s+t]=new Set;[...this.rows[t]].forEach(t=>{const s=this.find(t);(s[1]>e||void 0===e)&&(this._removeElementFromRowsCols(t),s[1]+=o,this._addElementToRowsCols(t))})}find(t){return this._elements.get(t)}get(t,e){const o=new Set([...this._elements.entries()].filter(([o,s])=>s[0]===t&&s[1]===e).map(t=>t[0]));return o.size?o:null}getElementsInRange({row:t,col:e},{row:o,col:s}){return t>o&&([t,o]=[o,t]),e>s&&([e,s]=[s,e]),[...this._elements.entries()].filter(([,i])=>i[0]>=t&&i[0]<=o&&i[1]>=e&&i[1]<=s).map(t=>t[0])}getGridDimensions(){return[this.rowCount,this.colCount]}shrink(t){const e=[...this._elements.entries()].sort((e,o)=>(t?e[1][0]:e[1][1])-(t?o[1][0]:o[1][1]));let o=0,s=null;for(const i of e){const e=t?i[1][0]:i[1][1];this._removeElementFromRowsCols(i[0],i[1]),null===s?(o=e,this._elements.set(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o]),this._addElementToRowsCols(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o]),s=e):s===e?(this._elements.set(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o]),this._addElementToRowsCols(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o])):e-s===1?(this._elements.set(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o]),this._addElementToRowsCols(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o]),s=e):(o=o+e-s-1,this._elements.set(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o]),this._addElementToRowsCols(i[0],t?[i[1][0]-o,i[1][1]]:[i[1][0],i[1][1]-o]),s=e)}for(const[e,o]of Object.entries(t?this.rows:this.cols))0===o.size&&(t?delete this.rows[e]:delete this.cols[e])}flip(t){const[e,o]=this.getGridDimensions();for(const[s,i]of this._elements.entries())this._removeElementFromRowsCols(s,i),this._elements.set(s,t?[e-1-i[0],i[1]]:[i[0],o-1-i[1]]),this._addElementToRowsCols(s,t?[e-1-i[0],i[1]]:[i[0],o-1-i[1]]);this.isFlipped=!this.isFlipped}hasElement(t){return this.elements.has(t)}isValidPosition(t){if(!t||!Array.isArray(t))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?t[1]!==e[1]:t[0]!==e[0])return!1;const s=o?t[0]:t[1],[i,n]=o?t[0]<=e[0]?[t[0],e[0]]:[e[0],t[0]]:t[1]<=e[1]?[t[1],e[1]]:[e[1],t[1]];return[...this._elements.values()].some(t=>o?t[1]===s&&t[0]>i&&t[0]<n:t[0]===s&&t[1]>i&&t[1]<n)}hasElementAt(t){if(!this.isValidPosition(t))return!1;const[e,o]=t;return!!this.get(e,o)}}class m extends E{constructor(t){super(),this.initialGraph=t,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))],s=o.reduce((t,e)=>[...t,...this.getAllExistingEdgesFor(e)],[]);for(const t of o)this.removeElement(t),this.add(t,e);for(const t of s)this._addEdgeToGrid(t)}_edgeIsExist(t){return[...this.graph.edges].includes(t)}_addEdgeToGrid(t){this._edgeIsExist(t)||this.graph.addEdge(t)}_createNewEdgesFor(t){const e=[...this.initialGraph.getOutgoingEdgesFor(t),...this.initialGraph.getIncomingEdgesFor(t)].filter(t=>this.hasElement(t.source)&&this.hasElement(t.target));for(const t of e)this._addEdgeToGrid(t)}isCrossed(t,e=!1){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 s=o.find(t),i=o.find(e);return s[0]-i[0]||i[1]-s[1]}):function(t){return function(e,o){const s=t.find(e),i=t.find(o);return s[1]-i[1]||i[0]-s[0]}}(this)).reverse();for(var o;e.length>0;){const o=e.pop(),s=this.getChain(o,!t);for(const t of s){const o=e.indexOf(t);o>=0&&e.splice(o,1)}const[i,n]=this.find([...s][0]);if(!(t?i<=0:n<=0)){for(let e=t?i-1:n-1;e>=0;e--){const i=[...s].every(t=>{const e=this.find(t);return e&&!this.isCrossed(e,!0)&&!this.isCrossed(e,!1)});if(!i)break;const n=[...s].every(o=>{const s=this.find(o),i=t?e:s[0],n=t?s[1]:e;return!this.get(i,n)});if(!n)break;for(const o of s){const s=this.find(o);this.move(o,t?[e,s[1]]:[s[0],e])}const r=this.hasAnyCross(),d=t?this.getChain(o,!1):this.getChain(o,!0);if(r||d.size>s.size){for(const o of s){const s=this.find(o);this.move(o,t?[e+1,s[1]]:[s[0],e+1])}break}}this.shrink(t)}}}getChain(t,e,o){const s=o||new Set;if(!t)return s;const i=this.find(t);if(!i)return s;const n=[],r=this.get(i[0],i[1]);for(const t of r)s.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=>n.push(t));for(const o of n){const i=o.source===t?o.target:o.source;if(!s.has(i)){const t=this.getChain(i,e,s);for(const e of t)s.add(e)}}return s}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.Graph.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new m(this.initialGraph,this.colCount,this.rowCount)];const o=[];for(const e of t){const t=new m(this.initialGraph,this.colCount,this.rowCount);let s=null,i=null;for(const o of f(e.nodes,"bpmn:BoundaryEvent").reverse()){const e=this.find(o);(null===s||s>e[0])&&(s=e[0]),(null===i||i<e[0])&&(i=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=new m(t[0].initialGraph);return t.forEach(t=>{let o=e.rowCount;Object.keys(t.rows).forEach(s=>{0===t.rows[s].size?e.addRowCol(!1,o+Number.parseInt(s)-1):t.rows[s].forEach(s=>{const i=[...t.find(s)];i[0]=i[0]+o,e.add(s,i)})})}),e}getSourcePosition(t){const e=this.getEdgeSource(t);return this.find(e)}getTargetPosition(t){const e=this.getEdgeTarget(t);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.id}-${t.target.id} flipped:${this.isFlipped}`);const[s,i]=e,[n,r]=o,d=s-n,a=i-r;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":s<n&&i>r?"NE_SW":s===n&&i>r?"E_W":s>n&&i>r?"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[]}getEdgeSource(t){return this.isFlipped?t.target:t.source}getEdgeTarget(t){return this.isFlipped?t.source:t.target}_pathForSouthToNorth(t){const e=[],[o,s]=this.getSourcePosition(t),[i]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id)return e;if(this.hasIntermediateElements(this.getSourcePosition(t),this.getTargetPosition(t),!0))return e;if([...this.getExistingOutgoingEdgesFor(this.getEdgeTarget(t))].map(t=>this.getEdgeTarget(t)).includes(this.getEdgeSource(t)))return e;for(let t=o-1;t>i;t--)e.push({position:[t,s],vCross:!0});return e}_pathForSouthWestToNorthEast(t){const e=[],[o,s]=this.getSourcePosition(t),[i,n]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type)for(let t=s+1;t<n;t++)e.push({position:[o,t],hCross:!0});e.push({position:[o,n],hCross:!0,vCross:!0});for(let t=o-1;t>i;t--)e.push({position:[t,n],vCross:!0});return e}_pathForWestToEast(t){const e=[],[o,s]=this.getSourcePosition(t),[,i]=this.getTargetPosition(t);for(let t=s+1;t<i;t++)e.push({position:[o,t],hCross:!0});return e}_pathForNorthWestToSouthEast(t){const e=[],[o,s]=this.getSourcePosition(t),[i,n]=this.getTargetPosition(t);for(let t=o+1;t<i;t++)e.push({position:[t,s],vCross:!0});e.push({position:[i,s],vCross:!0,hCross:!0});for(let t=s+1;t<n;t++)e.push({position:[i,t],hCross:!0});return e}_pathForNorthToSouth(t){const e=[],[o,s]=this.getSourcePosition(t),[i]=this.getTargetPosition(t);for(let t=o+1;t<i;t++)e.push({position:[t,s],vCross:!0});return e}_pathForNorthEastToSouthWest(t){const e=[],[o,s]=this.getSourcePosition(t),[i,n]=this.getTargetPosition(t);for(let t=o+1;t<i;t++)e.push({position:[t,s],vCross:!0});e.push({position:[i,s],vCross:!0,hCross:!0});for(let t=s-1;t>n;t--)e.push({position:[i,t],hCross:!0});return e}_pathForEastToWest(t){const e=[],[o,s]=this.getSourcePosition(t),[,i]=this.getTargetPosition(t);for(let t=s-1;t>i;t--)e.push({position:[o,t],hCross:!0});return e}_pathForSouthEastToNorthWest(t){const e=[],[o,s]=this.getSourcePosition(t),[i,n]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type||!t.id)for(let t=s-1;t>n;t--)e.push({position:[o,t],hCross:!0});"bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id?e.push({position:[o,n],vCross:!0}):e.push({position:[o,n],hCross:!0,vCross:!0});for(let t=o-1;t>i;t--)e.push({position:[t,n],vCross:!0});return e}getCrossedElementsFor(t,e=!1){const o=[];for(const s of this.getPathFor(t)){const[t,i]=s.position,n=this.get(t,i);n&&(e&&s.vCross||!e&&s.hCross)&&o.push(n)}return o}isIntersect(t,e,o){const[s,i]=e,[n,r]=this.getSourcePosition(t),[d,a]=this.getTargetPosition(t),g=this.getEdgeDirection(t);return"S_N"===g?o&&i===r&&s<n&&s>d:"SW_NE"===g?!!(o&&i===a&&s<=n&&s>d)||!o&&i>r&&i<=a&&s===n:"W_E"===g?!o&&i>r&&i<a&&s===n:"NW_SE"===g?!!(o&&i===r&&s>n&&s<=d)||!o&&i>=r&&i<a&&s===d:"N_S"===g?o&&i===r&&s>n&&s<d:"NE_SW"===g?!!(o&&i===r&&s>n&&s<=d)||!o&&i>a&&i<=r&&s===d:"E_W"===g?!o&&i>a&&i<r&&s===n:"SE_NW"===g?!!(o&&i===a&&s>d&&s<=n)||!o&&i>=a&&i<r&&s===n&&!this.getExistingIncomingEdgesFor(this.getEdgeSource(t)).some(t=>"NW_SE"===this.getEdgeDirection(t)||"W_E"===this.getEdgeDirection(t)):void 0}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.Graph;return this.graph.genericTraversing((t,e,s,i)=>{const n=this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeTarget(t),o=this.getEdgeSource(t);return!s.has(e)&&e!==o});o.addNode(t);const r=n.map(t=>this.getEdgeTarget(t));for(const t of r)o.addNode(t);for(const t of n)o.addEdge(t);return r},(e,o)=>e.has(t)?void 0:t),o}getSegmentLeftCoordinates(t){const e=new Map;for(const o of t.nodes){const[t,s]=this.find(o);e.has(t)||e.set(t,s),s<e.get(t)&&e.set(t,s)}return e}}function w(t,e,o,s,i){e.hasElement(t)||e.add(t);const n=(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),r=function(t,e,o,s,i,n){const r=e.getExistingOutgoingEdgesFor(t),d=e.getExistingIncomingEdgesFor(t).map(t=>e.getEdgeSource(t)),[,a]=e.find(t),g=[...r].filter(t=>{const i=e.getEdgeTarget(t),n=e.find(i)[1];if(!x(i,o,e))return!1;return(!e.getExistingIncomingEdgesFor(i).map(t=>t.source).find(t=>d.includes(t))||!s)&&n<=a}).map(t=>e.getEdgeTarget(t)).sort(function(t){return function(e,o){const s=t.find(e),i=t.find(o);return s[0]-i[0]||s[1]-i[1]}}(e));for(const t of g)o.splice(o.indexOf(t),1),n.delete(t),e.removeElement(t);return e.shrink(!0),e.shrink(!1),g}(t,e,o,n&&n.length>0,0,s);let d=[...n,...r],a=[];d.forEach(i=>{const n=[...y(t,e,i)];if(e.isFlipped&&"bpmn:BoundaryEvent"===i.$type&&i.attachedToRef!==t){const t=i.attachedToRef;s.has(t)||(e.add(t,n),s.add(t),_(t,e,o,a),a.unshift(i))}e.add(i,n),s.add(i),_(i,e,o,a),function(t,e){let o=e.getBackwardUpOutgoingEdgesFor(t);for(;o.length>0;){const s=o.shift(),i=e.getSourcePosition(s),n=e.getTargetPosition(s),r=e.getEdgeSource(s),d=e.getEdgeTarget(s);if(n[1]>e.find(t)[1])continue;const a=e.getGridCopy();a.removeEdge(s),a._elements.forEach((t,e)=>{const[,o]=t;o<n[1]&&a.removeElement(e)});const g=a.getGraphSegmentFrom(d);if(g.nodes.includes(r))continue;const h=Array.from(a.getSegmentLeftCoordinates(g)),c=h.reduce((t,e)=>void 0===t||e[1]<t[1]?e:t,void 0),u=h.reduce((t,e)=>void 0===t||e[0]<t[0]?e:t,void 0),l=h.reduce((t,e)=>void 0===t||e[0]>t[0]?e:t,void 0),f=i[1]-c[1]+1,p=new Map(h);let E;for(let o=0;o<e.rowCount;o++){const s=(()=>{if(o<u[0])return E=u[0],u[1]-1;if(p.has(o))return E=p.get(o),p.get(o)-1;const s=e.find(t)||[];return o>u[0]&&o<l[0]&&o<s[0]?E:o===s[0]||o>u[0]?(E=s,s[1]):E})();e.expandRow(o,s,f)}}}(i,e),a.unshift(i)});const g=[],h=[];for(const t of a)"bpmn:BoundaryEvent"===t.$type?g.push(t):h.push(t);return g.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),a=[...g,...h],a}function x(t,e,o){const s=e.includes(t),i=o.getExistingOutgoingEdgesFor(t);return s&&!i?.length>0}function y(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 s=e.find(t);if(!s)return;const i="bpmn:BoundaryEvent"===t.$type&&!e.isFlipped?[s[0]+1,s[1]+1]:[s[0],s[1]+1],n=e.find(t),r=[...e.get(n[0],n[1])],d=[];for(const t of r)[...e.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source).forEach(t=>d.push(t));for(let t=i[0];t<=e.rowCount;t++){const o=[t,i[1]],s=d.some(s=>e.getTargetPosition(s)[0]===t&&e.getTargetPosition(s)[1]===i[1]||e.isIntersect(s,o,!1)||e.isIntersect(s,o,!0));if(s&&t===e.colCount-1)i[0]=t+1;else if(!s){i[0]=t;break}}return(e.get(i[0],i[1])||e.isCrossed(i,!0))&&e.addRowCol(!0,i[1]-1),e.isCrossed([i[0],i[1]])&&e.addRowCol(!1,i[0]-1),i}function _(t,e,o,s,i){const n=[...[...e.getExistingOutgoingEdgesFor(t)].sort((t,o)=>{const[s,i]=e.getTargetPosition(t),[n,r]=e.getTargetPosition(o);return s-n||i-r}),...[...e.getExistingIncomingEdgesFor(t)].sort((t,o)=>{const[s,i]=e.getSourcePosition(t),[n,r]=e.getSourcePosition(o);return s-n||i-r})].filter(i=>{const{target:n,source:r}=i,d=e.getEdgeDirection(i);return n!==r&&((!s||!s.includes(n))&&((!o||!x(n,o,e))&&(r!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of n)C(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===e.getEdgeSource(t).$type&&t.id||S(t,e)}function C(t,e){const o=e.getEdgeDirection(t);if("W_E"===o||"E_W"===o)return;const s=e.getCrossedElementsFor(t,!0);s.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||P(s,e):v(s,e):P(s,e):v(s,e))}function S(t,e){const o=e.getEdgeDirection(t);if("S_N"===o||"N_S"===o)return;const s=e.getCrossedElementsFor(t,!1);if(0!==s.length){if("SW_NE"===o){const o=function(t,e){const o=e.getSourcePosition(t),s=e.getTargetPosition(t);let i=o[0];for(let t=o[0];t<e.rowCount&&e.getElementsInRange({row:t,col:o[1]+1},{row:t,col:s[1]}).length>0;t++)i++;return i-o[0]}(t,e),[s]=e.getSourcePosition(t);e.addRowCol(!1,s-1,o);const i=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 i){const[s,i]=e.find(t);for(const t of[...e.get(s,i)])e.move(t,[s-o,i])}return}"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||b(s,e):F(s,e):b(s,e):F(s,e)}}function b(t,e){const[o]=e.find([...t[0]][0]);e.addRowCol(!1,o-1);for(const s of t){const[,t]=e.find([...s][0]);for(const i of[...s])e.move(i,[o,t])}}function F(t,e){const[o]=e.find([...t[0]][0]);e.addRowCol(!1,o);for(const s of t){const[,t]=e.find([...s][0]);for(const i of[...s])e.move(i,[o+1,t])}}function v(t,e){const[,o]=e.find([...t[0]][0]);e.addRowCol(!0,o);for(const s of t){const[t]=e.find([...s][0]);for(const i of[...s])e.move(i,[t,o+1])}}function P(t,e){const[,o]=e.find([...t[0]][0]);e.addRowCol(!0,o-1);for(const s of t){const[t]=e.find([...s][0]);for(const i of[...s])e.move(i,[t,o])}}function T(t,e,o,n,a){const[g,h,c,u]=e;if(t.di)return[];if("bpmn:BoundaryEvent"===t.$type)return function(t,e,o,s,i,n){const r=t.attachedToRef.di.bounds;if(!r)throw new Error(`Create DI for ${t.id}. Nо hostBounds`);const d=[];let a=t.$parent.flowElements.filter(e=>e.attachedToRef===t.attachedToRef&&i.hasElement(t));return a=function(t,e){return t.sort((t,o)=>{const s=R(t,e),i=R(o,e);return s[0]-i[0]||s[1]-i[1]}).reverse()}(a,i),a.forEach((i,a,g)=>{i.gridPosition={row:e,col:o};const h=l(i,e,o,n,t.attachedToRef);h.x=r.x+(a+1)*(r.width/(g.length+1))-h.width/2;const c=s.createDiShape(i,h,{id:i.id+"_di"});i.di=c,i.gridPosition={row:e,col:o},d.push(c)}),d}(t,g,h,o,n,a);const f=l(t,g,h,a);if(t.isExpanded){const{width:e,height:o}=s(t),{rowCount:i,colCount:n}=t.grid;f.width=(n||1)*r+e,f.height=(i||1)*d+o}const p={id:t.id+"_di"};t.isExpanded&&(p.isExpanded=!0),i(t,"bpmn:ExclusiveGateway")&&(p.isMarkerVisible=!0);const E=o.createDiShape(t,f,p);return t.di=E,t.gridPosition={row:g,col:h},[E]}function R(t,e){return n(t).reduce((t,o)=>{if(!e.hasElement(o))return t;const s=e.find(o);return void 0===t||t[0]<s[0]||t[1]<s[1]?s:t},[0,0])}class N{constructor(t,e){this.source=t,this.target=e}}class D{constructor(e){this.moddle=new t,this.diFactory=new p(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 s=this.getRootProcesses(),i=this.getCollaboration();return s.length>0&&(this.cleanDi(),this.createRootDi(s,i),this.drawParticipants(),this.drawProcesses(),this.drawCollaborationMessageFlows(i)),(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.shrink(!0),e.shrink(!1),e.shakeIt(!0),e.shakeIt(!1);e.grid=e.grid._mergeGrids(t)}}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.shrink(!0),e.shrink(!1),W(e,!1),W(e,!0);e.grid=e.grid._mergeGrids(t)}}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),s=this.getProcDi(o);let{x:i,y:n}=o.bounds;i+=75,n+=70,this.generateDi(e,{x:i,y:n},s);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:a}=s(e);i+=75-r/4,n+=d-a-a/4,this.generateDi(e,{x:i,y:n},o);continue}const o=this.diagram.diagrams.find(t=>t.plane.bpmnElement===e);this.generateDi(e,{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,s=this.existingNodes;if(!s.includes(e)||!s.includes(o))continue;const i=e.di.bounds,n=o.di.bounds,r=n.y-i.y,d=[{x:i.x+i.width/2},{x:n.x+n.width/2}];r>0?(d[0].y=i.y+i.height,d[1].y=n.y):(d[0].y=i.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,s=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 s)o.addNode(t);for(const t of s){const e=this.getSubProcesses(t);for(const s of e)o.addEdge({source:t,target:s})}const i=o.getSeparatedGraphs();for(const t of i){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 i){const e=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},o=(t,e)=>{const{left:o,level:s}=t,i=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(i)return i.level=s+1,i.left=o+1,[i];const n=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).reduce((t,e)=>void 0===t||e.right>t.right?e.right:t,void 0);t.right=n?n+1:t.left+1;const r=[...e.getIncomingEdgesFor(t)].map(t=>t.source).find(t=>void 0===t.right);return r?[r]:void 0};t.genericTraversing(o,e)}return i}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}),s=e.createDiDiagram({id:"BPMNDiagram_"+t.id,plane:o});return this.diagram.diagrams.push(s),s}createParticipantDi(t,e){const{colCount:o,rowCount:i}=t.processRef.grid,{width:n,height:a}=s(t),g=o>0?o*r+r:n,h=i>0?i*d+d:a,c=this.diFactory.createDiShape(t,{width:g,height:h,...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.Graph,s=new m(o);for(const e of t.flowElements||[])i(e,"bpmn:SequenceFlow")||i(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new N(t,t.attachedToRef)),o.addEdge(new N(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new N(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 s=new N(e,t.$parent);s.id=t.id,s.propRef=t.targetRef,o.addEdge(s)}const s=t.dataOutputAssociations;for(const t of s||[]){const e=t.$parent,s=t.targetRef,i=new N(e,s);i.id=t.id,o.addEdge(i)}}return o.genericTraversing((t,e,o,i)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const n=w(t,s,i,o);return this.currentDebugStep+=1,n},(t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const o=[...t].find(o=>(s.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(e=>!t.has(s.isFlipped?e.target:e.source)).length>0);if(o)return s.flip(!1),o;const n=e.nodes.filter(o=>{const n=s.isFlipped?e.getOutgoingEdgesFor(o):e.getIncomingEdgesFor(o);return!(t.has(o)||0!==n.size||(r=o,(i(r,"bpmn:IntermediateThrowEvent")||i(r,"bpmn:IntermediateCatchEvent"))&&(void 0===r.incoming||0===r.incoming.length)));var r});if(n.length>0)return f(n,"bpmn:StartEvent")[0];const r=[...t].find(o=>(s.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(e=>!t.has(s.isFlipped?e.source:e.target)).length>0);if(r)return r;const d=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(s.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(t=>o!==(s.isFlipped?t.target:t.source)).length});if(d)return d;const a=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(s.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(t=>t.target!==t.source).length});return a?(s.flip(!1),a):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))}),s.isFlipped&&s.flip(!1),s}generateDi(t,e,o){const{grid:s}=t,i=this.diFactory,n=(o||this.diagram.diagrams[0]).plane.get("planeElement");s._elements.forEach((t,o)=>{const r=T(o,t,i,s,e);n.push(...r)}),s._allEdges.forEach(t=>{const o=function(t,e,o,s){const{id:i}=t;if(i){const n=g(t,e,s);return o.createDiEdge(t,n,{id:i+"_di"})}}(t,s,i,e);o&&n.push(o)})}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 W(t,e){const o=new Map,s=new Set;Object.entries(e?t.rows:t.cols).forEach(([t,i])=>{i.forEach(i=>{if(i.isExpanded){s.add(i);const n=o.get(t),[r,d]=i.grid.getGridDimensions();let a=e?r:d;a||(a=1),(void 0===n||a>n)&&o.set(t,a)}})}),[...o.entries()].sort(([t],[e])=>e-t).forEach(([o,s])=>{t.addRowCol(!e,o,s)}),s.forEach(o=>{const s=t.find(o),[i,n]=o.grid.getGridDimensions();e?s[2]=i>=2?i:2:s[3]=n>=2?n:2})}exports.layoutProcess=function(t,e){return new D(e).layoutProcess(t)};//# sourceMappingURL=index.cjs.map
|