yet-another-bpmn-auto-layout 0.0.6 → 0.0.8
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 +2 -1
- 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.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"bpmn-moddle";import{Graph as e}from"graph-by-ivan-tulaev";import{assign as o,map as s,pick as i}from"min-dash";function n(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 d(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 a=150,g=140;function h(t,e){return new Set(d(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 c(t,e,o){const{source:s,target:i}=t,r=s.di,a=i.di,c=r.get("bounds"),p=a.get("bounds"),E=u(c),m=u(p),w=e.getEdgeDirection(t),x=i.gridPosition.col-s.gridPosition.col,y=i.gridPosition.row-s.gridPosition.row,_=`${y>0?"bottom":"top"}-${x>0?"right":"left"}`,C=`${y>0?"top":"bottom"}-${x>0?"left":"right"}`,{x:S,y:b}=f(s,o),{x:F}=f(i,o),v="bpmn:BoundaryEvent"===s.$type;if("NO_DIRECTION"===w)return v?[l(E,c,"b",_),{x:E.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:F,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:S,y:m.y},l(m,p,"l",C)]:[l(E,c,"b",_),{x:E.x,y:s.isExpanded?b+(s.grid.rowCount+1)*g:b+g},{x:F,y:s.isExpanded?b+(s.grid.rowCount+1)*g:b+g},{x:S,y:m.y},l(m,p,"l",C)];if("S_N"===w){if(v)return[l(E,c,"b",_),{x:E.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:S,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:F,y:m.y},l(m,p,"l",C)];return[...h(i)].includes(s)?[l(E,c,"l",_),{x:S,y:E.y},{x:F,y:m.y},l(m,p,"l",C)]:[l(E,c,"t",_),l(m,p,"b",C)]}if("SW_NE"===w)return v?[l(E,c,"b",_),{x:E.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:m.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},l(m,p,"b",C)]:[l(E,c,"r"),{x:m.x,y:E.y},l(m,p,"b")];if("W_E"===w){if(v)return[l(E,c,"b",_),{x:E.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:m.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},l(m,p,"b",C)];const t=l(E,c,"r",_);s.isExpanded&&(t.y=c.y+n(s).height/2);const e=l(m,p,"l",C);return i.isExpanded&&(e.y=p.y+n(i).height/2),[t,e]}if("NW_SE"===w)return[l(E,c,"b"),{x:E.x,y:m.y},l(m,p,"l")];if("N_S"===w){if(v){const t=l(E,c,"b",_),e=l(m,p,"t",C);return s.attachedToRef.isExpanded?[t,{x:E.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:m.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},e]:[t,e]}const t=l(E,c,"b",_),e=l(m,p,"t",C);return s.isExpanded||i.isExpanded?[t,{x:E.x,y:s.isExpanded?b+(s.grid.rowCount+1)*g:b+g},{x:m.x,y:s.isExpanded?b+(s.grid.rowCount+1)*g:b+g},e]:[t,e]}if("NE_SW"===w)return[l(E,c,"b"),{x:E.x,y:m.y},l(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)}(s,i,e);return[l(E,c,"b",_),{x:E.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g+t*g},{x:m.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g+t*g},l(m,p,"b",C)]}const t=d(i).includes(s);let o=e?e.getExistingOutgoingEdgesFor(i):[];if(o=o.some(t=>"SW_NE"===e.getEdgeDirection(t)),t||o)return[l(E,c,"b",_),{x:E.x,y:b+(s.isExpanded?g*(s.grid.colCount+1.5):g)},{x:m.x,y:b+(s.isExpanded?g*(s.grid.colCount+1.5):g)},l(m,p,"b",C)];{const t=l(E,c,"l",_);s.isExpanded&&(t.y=c.y+n(s).height/2);const e=l(m,p,"r",C);return i.isExpanded&&(e.y=p.y+n(i).height/2),[t,e]}}if("SE_NW"===w){if(v)return[l(E,c,"b",_),{x:E.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},{x:m.x,y:s.attachedToRef.isExpanded?b+(s.attachedToRef.grid.rowCount+1)*g:b+g},l(m,p,"b",C)];const t=[];for(let o=s.gridPosition.col-1;o>=i.gridPosition.col;o--){const i=e?e.get(s.gridPosition.row,o):null;i&&t.push(i)}return t.length>0?[l(E,c,"b",_),{x:E.x,y:s.isExpanded?b+(s.grid.rowCount+1)*g:b+g},{x:m.x,y:s.isExpanded?b+(s.grid.rowCount+1)*g:b+g},l(m,p,"b",C)]:[l(E,c,"l"),{x:m.x,y:E.y},l(m,p,"b")]}const T=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"]}}(s,i,e);if(T){const t=l(E,c,T[0],_),e=l(m,p,T[1],C);return[t,"h"===T[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const P=-Math.sign(y)*g/2;return[l(E,c,"r",_),{x:E.x+75,y:E.y},{x:E.x+75,y:m.y+P},{x:m.x-75,y:m.y+P},{x:m.x-75,y:m.y},l(m,p,"l",C)]}function u(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function l(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 f(t,e={x:0,y:0}){const o=t.gridPosition.row;return{x:t.gridPosition.col*a+e.x,y:o*g+e.y}}function p(t,e,o,s,i){const{width:r,height:d}=n(t),{x:h,y:c}=s;if(!i)return{width:r,height:d,x:o*a+(a-r)/2+h,y:e*g+(g-d)/2+c};const u=i.di.bounds;return{width:r,height:d,x:Math.round(u.x+u.width/2-r/2),y:Math.round(u.y+u.height-d/2)}}function E(t,e){const o=[e].flat();let s=[];return o.length>0?o.forEach((e,i)=>{const n=t.filter(t=>r(t,e));if(s=s.concat(n),i===o.length-1&&s.length!==t.length)for(const e of t)s.includes(e)||s.push(e)}):s=t,s}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,s){return this.create("bpmndi:BPMNShape",o({bpmnElement:t,bounds:this.createDiBounds(e)},s))}createDiWaypoints(t){var e=this;return s(t,function(t){return e.createDiWaypoint(t)})}createDiWaypoint(t){return this.create("dc:Point",i(t,["x","y"]))}createDiEdge(t,e,s){return this.create("bpmndi:BPMNEdge",o({bpmnElement:t,waypoint:this.createDiWaypoints(e)},s))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}class w{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 x extends w{constructor(t){super(),this.initialGraph=t,this.graph=new e}add(t,e){super.add(t,e),this.graph.addNode(t),this._createNewEdgesFor(t)}removeElement(t){this.graph.deleteNode(t),super.removeElement(t)}move(t,e){const o=[...this.get(...this.find(t))],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.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new x(this.initialGraph,this.colCount,this.rowCount)];const o=[];for(const e of t){const t=new x(this.initialGraph,this.colCount,this.rowCount);let s=null,i=null;for(const o of E(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 x(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;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 y(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(!_(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=[...C(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),S(t,e,o,a),a.unshift(i))}e.add(i,n),s.add(i),S(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 _(t,e,o){const s=e.includes(t),i=o.getExistingOutgoingEdgesFor(t);return s&&!i?.length>0}function C(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 S(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||!_(n,o,e))&&(r!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of n)b(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===e.getEdgeSource(t).$type&&t.id||F(t,e)}function b(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||R(s,e):P(s,e):R(s,e):P(s,e))}function F(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||v(s,e):T(s,e):v(s,e):T(s,e)}}function v(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 T(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 P(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 R(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 N(t,e,o,s,i){const[d,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=D(t,e),i=D(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=p(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,d,h,o,s,i);const l=p(t,d,h,i);if(t.isExpanded){const{width:e,height:o}=n(t),{rowCount:s,colCount:i}=t.grid;l.width=(i||1)*a+e,l.height=(s||1)*g+o}const f={id:t.id+"_di"};t.isExpanded&&(f.isExpanded=!0),r(t,"bpmn:ExclusiveGateway")&&(f.isMarkerVisible=!0);const E=o.createDiShape(t,l,f);return t.di=E,t.gridPosition={row:d,col:h},[E]}function D(t,e){return d(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 W{constructor(t,e){this.source=t,this.target=e}}class I{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 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),O(e,!1),O(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:s,y:i}=t.bounds;const{width:r,height:d}=n(e);s+=75-r/4,i+=g-d-d/4,this.generateDi(e,{x:s,y:i},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,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:s}=t.processRef.grid,{width:i,height:r}=n(t),d=o>0?o*a+a:i,h=s>0?s*g+g:r,c=this.diFactory.createDiShape(t,{width:d,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,s=new x(o);for(const e of t.flowElements||[])r(e,"bpmn:SequenceFlow")||r(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new W(t,t.attachedToRef)),o.addEdge(new W(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new W(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 W(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 W(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=y(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 i=e.nodes.filter(o=>{const i=s.isFlipped?e.getOutgoingEdgesFor(o):e.getIncomingEdgesFor(o);return!(t.has(o)||0!==i.size||(n=o,(r(n,"bpmn:IntermediateThrowEvent")||r(n,"bpmn:IntermediateCatchEvent"))&&(void 0===n.incoming||0===n.incoming.length)));var n});if(i.length>0)return E(i,"bpmn:StartEvent")[0];const n=[...t].find(o=>(s.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(e=>!t.has(s.isFlipped?e.source:e.target)).length>0);if(n)return n;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=N(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=c(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 O(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})}function G(t,e){return new I(e).layoutProcess(t)}export{G as layoutProcess};//# sourceMappingURL=index.js.map
|
|
1
|
+
import t from"bpmn-moddle";import{Graph as e}from"graph-by-ivan-tulaev";import{assign as o,map as n,pick as s}from"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 d(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 a=150,c=140;function g(t,e){return new Set(d(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){return{x:t.x+t.width/2,y:t.y+t.height/2}}function l(t,e,o="r",n="top-left"){if("h"===o&&(o=/left/.test(n)?"l":"r"),"v"===o&&(o=/top/.test(n)?"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,o={x:0,y:0}){const[n,s]=e.find(t);return{x:s*a+o.x,y:n*c+o.y}}function f(t,e,o,n){const[s,r,d,g]=e,{width:h,height:l}=i(t);let u=((d||1)-1)*a+h,f=((g||1)-1)*c+l,p=r*a+(a-h)/2+o.x,E=s*c+(c-l)/2+o.y;if("bpmn:Lane"===t.$type){const t=30;u=(d||1)*a+a-t,p=r*a+o.x-75+t,E=s*c+o.y-70,f=(g||1)*c}return{width:u,height:f,x:p,y:E}}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,n){return this.create("bpmndi:BPMNShape",o({bpmnElement:t,bounds:this.createDiBounds(e)},n))}createDiWaypoints(t){var e=this;return n(t,function(t){return e.createDiWaypoint(t)})}createDiWaypoint(t){return this.create("dc:Point",s(t,["x","y"]))}createDiEdge(t,e,n){return this.create("bpmndi:BPMNEdge",o({bpmnElement:t,waypoint:this.createDiWaypoints(e)},n))}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 duplicated element ${JSON.stringify(t)}`);if(!e)return void this._addStart(t);const o=this.rowCount-1,n=this.colCount-1,s=e[0]-o,i=e[1]-n;this.addRowCol(!1,o>=0?o:void 0,s),this.addRowCol(!0,n>=0?n:void 0,i),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 not exist element ${JSON.stringify(t)}`);if(!this.isValidPosition(e))throw new Error(`Cannot move element ${JSON.stringify(t)} 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 n=t?this.colCount:this.rowCount,s=e>=n-1?e-(n-1)+o:o;for(let e=0;e<s;e++)t?this.cols[n+e]=new Set:this.rows[n+e]=new Set;for(const[n,s]of this._elements.entries()){((t?s[1]:s[0])>e||void 0===e)&&(this._removeElementFromRowsCols(n),s[t?1:0]+=o,this._addElementToRowsCols(n))}}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 n=this.colCount,s=e>=n-1?e-(n-1)+o:o;for(let t=0;t<s;t++)this.cols[n+t]=new Set;[...this.rows[t]].forEach(t=>{const n=this.find(t);(n[1]>e||void 0===e)&&(this._removeElementFromRowsCols(t),n[1]+=o,this._addElementToRowsCols(t))})}find(t){return this._elements.get(t)}get(t,e){const o=new Set([...this._elements.entries()].filter(([o,n])=>n[0]===t&&n[1]===e).map(t=>t[0]));return o.size?o:null}getElementsInRange({row:t,col:e},{row:o,col:n}){return t>o&&([t,o]=[o,t]),e>n&&([e,n]=[n,e]),[...this._elements.entries()].filter(([,s])=>s[0]>=t&&s[0]<=o&&s[1]>=e&&s[1]<=n).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,n=null;for(const s of e){const e=t?s[1][0]:s[1][1];this._removeElementFromRowsCols(s[0]),null===n?o=e:n!==e&&(o=o+e-n-1),n=e;const i=[...s[1]];t?i[0]-=o:i[1]-=o,this._elements.set(s[0],i),this._addElementToRowsCols(s[0])}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[n,s]of this._elements.entries())this._removeElementFromRowsCols(n,s),this._elements.set(n,t?[e-1-s[0],s[1]]:[s[0],o-1-s[1]]),this._addElementToRowsCols(n,t?[e-1-s[0],s[1]]:[s[0],o-1-s[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 n=o?t[0]:t[1],[s,i]=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]===n&&t[0]>s&&t[0]<i:t[0]===n&&t[1]>s&&t[1]<i)}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}getLanes(){return[...this.elements].filter(t=>"bpmn:Lane"===t.$type)}add(t,e){if("bpmn:Lane"===t.$type)return void super.add(t,e);if(0===this.getLanes().length)return super.add(t,e),this.graph.addNode(t),void this._createNewEdgesFor(t);const o=this.find(t.laneRef);if(!e){const e=o[0],n=o[0]+o[3]-1;for(let o=e;o<=n;o++){const e=new Set(this.rows[o]);if(e.delete(t.laneRef),0===e.size)return super.add(t,[o,0]),this.graph.addNode(t),void this._createNewEdgesFor(t)}return this.addRowCol(!1,o[0]+o[3]-1),super.add(t,[o[0]+o[3],0]),o[3]+=1,this.graph.addNode(t),void this._createNewEdgesFor(t)}const n=e[0]+(e[3]||1)-(o[0]+(o[3]||1));n>0&&(this.addRowCol(!1,o[0]+(o[3]||1)-1,n),o[3]+=n),super.add(t,e),this.graph.addNode(t),this._createNewEdgesFor(t)}removeElement(t){this.graph.deleteNode(t),super.removeElement(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].filter(t=>"bpmn:Lane"!==t.$type).sort(t?(o=this,function(t,e){const n=o.find(t),s=o.find(e);return n[0]-s[0]||s[1]-n[1]}):function(t){return function(e,o){const n=t.find(e),s=t.find(o);return n[1]-s[1]||s[0]-n[0]}}(this)).reverse();for(var o;e.length>0;){const o=e.pop(),n=this.getChain(o,!t);for(const t of n){const o=e.indexOf(t);o>=0&&e.splice(o,1)}const[s,i]=this.find([...n][0]);if(!(t?s<=0:i<=0)){for(let e=t?s-1:i-1;e>=0;e--){const s=[...n].every(t=>{const e=this.find(t);return e&&!this.isCrossed(e,!0)&&!this.isCrossed(e,!1)});if(!s)break;const i=[...n].every(o=>{const n=this.find(o),s=t?e:n[0],i=t?n[1]:e;return!this.get(s,i)});if(!i)break;for(const o of n){const n=this.find(o);this.move(o,t?[e,n[1]]:[n[0],e])}const r=this.hasAnyCross(),d=this.getChain(o,!t);if(r||d.size>n.size){for(const o of n){const n=this.find(o);this.move(o,t?[e+1,n[1]]:[n[0],e+1])}break}}this.shrink(t)}}}getChain(t,e,o){const n=o||new Set;if(!t)return n;const s=this.find(t);if(!s)return n;const i=[],r=[...this.get(s[0],s[1])].filter(t=>"bpmn:Lane"!==t.$type);for(const t of r)n.add(t),[...this.getAllExistingEdgesFor(t)].filter(t=>{const o=this.getEdgeDirection(t);return e?"N_S"===o||"S_N"===o:"W_E"===o||"E_W"===o}).forEach(t=>i.push(t));for(const o of i){const s=o.source===t?o.target:o.source;if(!n.has(s)){const t=this.getChain(s,e,n);for(const e of t)n.add(e)}}return n}getExistingOutgoingEdgesFor(t){return this.isFlipped?[...this.graph.getIncomingEdgesFor(t)]:[...this.graph.getOutgoingEdgesFor(t)]}getExistingIncomingEdgesFor(t){return this.isFlipped?[...this.graph.getOutgoingEdgesFor(t)]:[...this.graph.getIncomingEdgesFor(t)]}getAllExistingEdgesFor(t){const e=this.getExistingOutgoingEdgesFor(t),o=this.getExistingIncomingEdgesFor(t);return new Set([...e,...o])}_separateGrid(){const t=e.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new m(this.initialGraph)];const o=[];for(const e of t){const t=new m(this.initialGraph);let n=null,s=null;for(const o of e.nodes){const e=[...this.find(o)];(null===n||n>e[0])&&(n=e[0]),(null===s||s<e[0])&&(s=e[0]),t.add(o,e)}o.push(t)}return o}_mergeGrids(t){const e=new m(t[0].initialGraph);return t.forEach(t=>{let o=e.rowCount;Object.keys(t.rows).forEach(n=>{0===t.rows[n].size?e.addRowCol(!1,o+Number.parseInt(n)-1):t.rows[n].forEach(n=>{const s=[...t.find(n)];s[0]=s[0]+o,e.add(n,s)})})}),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[n,s]=e,[i,r]=o,d=n-i,a=s-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":n<i&&s>r?"NE_SW":n===i&&s>r?"E_W":n>i&&s>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,n]=this.getSourcePosition(t),[s]=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>s;t--)e.push({position:[t,n],vCross:!0});return e}_pathForSouthWestToNorthEast(t){const e=[],[o,n]=this.getSourcePosition(t),[s,i]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type)for(let t=n+1;t<i;t++)e.push({position:[o,t],hCross:!0});e.push({position:[o,i],hCross:!0,vCross:!0});for(let t=o-1;t>s;t--)e.push({position:[t,i],vCross:!0});return e}_pathForWestToEast(t){const e=[],[o,n]=this.getSourcePosition(t),[,s]=this.getTargetPosition(t);for(let t=n+1;t<s;t++)e.push({position:[o,t],hCross:!0});return e}_pathForNorthWestToSouthEast(t){const e=[],[o,n]=this.getSourcePosition(t),[s,i]=this.getTargetPosition(t);for(let t=o+1;t<s;t++)e.push({position:[t,n],vCross:!0});e.push({position:[s,n],vCross:!0,hCross:!0});for(let t=n+1;t<i;t++)e.push({position:[s,t],hCross:!0});return e}_pathForNorthToSouth(t){const e=[],[o,n]=this.getSourcePosition(t),[s]=this.getTargetPosition(t);for(let t=o+1;t<s;t++)e.push({position:[t,n],vCross:!0});return e}_pathForNorthEastToSouthWest(t){const e=[],[o,n]=this.getSourcePosition(t),[s,i]=this.getTargetPosition(t);for(let t=o+1;t<s;t++)e.push({position:[t,n],vCross:!0});e.push({position:[s,n],vCross:!0,hCross:!0});for(let t=n-1;t>i;t--)e.push({position:[s,t],hCross:!0});return e}_pathForEastToWest(t){const e=[],[o,n]=this.getSourcePosition(t),[,s]=this.getTargetPosition(t);for(let t=n-1;t>s;t--)e.push({position:[o,t],hCross:!0});return e}_pathForSouthEastToNorthWest(t){const e=[],[o,n]=this.getSourcePosition(t),[s,i]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type||!t.id)for(let t=n-1;t>i;t--)e.push({position:[o,t],hCross:!0});"bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id?e.push({position:[o,i],vCross:!0}):e.push({position:[o,i],hCross:!0,vCross:!0});for(let t=o-1;t>s;t--)e.push({position:[t,i],vCross:!0});return e}getCrossedElementsFor(t,e=!1){const o=[];for(const n of this.getPathFor(t)){const[t,s]=n.position,i=this.get(t,s);i&&(e&&n.vCross||!e&&n.hCross)&&o.push(i)}return o}isIntersect(t,e,o){const[n,s]=e,[i,r]=this.getSourcePosition(t),[d,a]=this.getTargetPosition(t),c=this.getEdgeDirection(t);return"S_N"===c?o&&s===r&&n<i&&n>d:"SW_NE"===c?!!(o&&s===a&&n<=i&&n>d)||!o&&s>r&&s<=a&&n===i:"W_E"===c?!o&&s>r&&s<a&&n===i:"NW_SE"===c?!!(o&&s===r&&n>i&&n<=d)||!o&&s>=r&&s<a&&n===d:"N_S"===c?o&&s===r&&n>i&&n<d:"NE_SW"===c?!!(o&&s===r&&n>i&&n<=d)||!o&&s>a&&s<=r&&n===d:"E_W"===c?!o&&s>a&&s<r&&n===i:"SE_NW"===c?!!(o&&s===a&&n>d&&n<=i)||!o&&s>=a&&s<r&&n===i&&!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;return this.graph.genericTraversing((t,e,n,s)=>{const i=this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeTarget(t),o=this.getEdgeSource(t);return!n.has(e)&&e!==o});o.addNode(t);const r=i.map(t=>this.getEdgeTarget(t));for(const t of r)o.addNode(t);for(const t of i)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,n]=this.find(o);e.has(t)||e.set(t,n),n<e.get(t)&&e.set(t,n)}return e}}function w(t,e,o,n,s){e.hasElement(t)||e.add(t);const i=(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,n,s,i){const r=e.getExistingOutgoingEdgesFor(t),d=e.getExistingIncomingEdgesFor(t).map(t=>e.getEdgeSource(t)),[,a]=e.find(t),c=[...r].filter(t=>{const s=e.getEdgeTarget(t),i=e.find(s)[1];if(!x(s,o,e))return!1;return(!e.getExistingIncomingEdgesFor(s).map(t=>t.source).find(t=>d.includes(t))||!n)&&i<=a}).map(t=>e.getEdgeTarget(t)).sort(function(t){return function(e,o){const n=t.find(e),s=t.find(o);return n[0]-s[0]||n[1]-s[1]}}(e));for(const t of c)o.splice(o.indexOf(t),1),i.delete(t),e.removeElement(t);return c}(t,e,o,i&&i.length>0,0,n);let d=[...i,...r],a=[];d.forEach(s=>{const i=[...y(t,e,s)];if(e.isFlipped&&"bpmn:BoundaryEvent"===s.$type&&s.attachedToRef!==t){const t=s.attachedToRef;n.has(t)||(e.add(t,i),n.add(t),_(t,e,o,a),a.unshift(s))}e.add(s,i),n.add(s),_(s,e,o,a),function(t,e){let o=e.getBackwardUpOutgoingEdgesFor(t);for(;o.length>0;){const n=o.shift(),s=e.getSourcePosition(n),i=e.getTargetPosition(n),r=e.getEdgeSource(n),d=e.getEdgeTarget(n);if(i[1]>e.find(t)[1])continue;const a=e.getGridCopy();a.removeEdge(n),a._elements.forEach((t,e)=>{const[,o]=t;o<i[1]&&a.removeElement(e)});const c=a.getGraphSegmentFrom(d);if(c.nodes.includes(r))continue;const g=Array.from(a.getSegmentLeftCoordinates(c)),h=g.reduce((t,e)=>void 0===t||e[1]<t[1]?e:t,void 0),l=g.reduce((t,e)=>void 0===t||e[0]<t[0]?e:t,void 0),u=g.reduce((t,e)=>void 0===t||e[0]>t[0]?e:t,void 0),f=s[1]-h[1]+1,p=new Map(g);let E;for(let o=0;o<e.rowCount;o++){const n=(()=>{if(o<l[0])return E=l[0],l[1]-1;if(p.has(o))return E=p.get(o),p.get(o)-1;const n=e.find(t)||[];return o>l[0]&&o<u[0]&&o<n[0]?E:o===n[0]||o>l[0]?(E=n,n[1]):E})();e.expandRow(o,n,f)}}}(s,e),a.unshift(s)});const c=[],g=[];for(const t of a)"bpmn:BoundaryEvent"===t.$type?c.push(t):g.push(t);return c.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),a=[...c,...g],a}function x(t,e,o){const n=e.includes(t),s=o.getExistingOutgoingEdgesFor(t);return n&&!s?.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 n=e.find(t);if(!n)throw new Error("No source position");const s=[n[0],n[1]+1],i="bpmn:BoundaryEvent"===t.$type&&!e.isFlipped,r=t.laneRef,d=o.laneRef;if(i&&r===d&&(s[0]+=1),r!==d){const[t]=e.find(r),[o,,,n]=e.find(d),i=t-o;s[0]=i>0?o+n-1:o}const a=e.find(t),c=[...e.get(a[0],a[1])],g=[];for(const t of c)[...e.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source).forEach(t=>g.push(t));for(let t=s[0];t<=e.rowCount;t++){const o=[t,s[1]],n=g.some(n=>e.getTargetPosition(n)[0]===t&&e.getTargetPosition(n)[1]===s[1]||e.isIntersect(n,o,!1)||e.isIntersect(n,o,!0));if(n&&t===e.colCount-1)s[0]=t+1;else if(!n){s[0]=t;break}}return(e.get(s[0],s[1])||e.isCrossed(s,!0))&&e.addRowCol(!0,s[1]-1),e.isCrossed([s[0],s[1]])&&e.addRowCol(!1,s[0]-1),s}function _(t,e,o,n,s){const i=[...[...e.getExistingOutgoingEdgesFor(t)].sort((t,o)=>{const[n,s]=e.getTargetPosition(t),[i,r]=e.getTargetPosition(o);return n-i||s-r}),...[...e.getExistingIncomingEdgesFor(t)].sort((t,o)=>{const[n,s]=e.getSourcePosition(t),[i,r]=e.getSourcePosition(o);return n-i||s-r})].filter(s=>{const{target:i,source:r}=s,d=e.getEdgeDirection(s);return i!==r&&((!n||!n.includes(i))&&((!o||!x(i,o,e))&&(r!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of i)S(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===e.getEdgeSource(t).$type&&t.id||b(t,e)}function S(t,e){const o=e.getEdgeDirection(t);if("W_E"===o||"E_W"===o)return;const n=e.getCrossedElementsFor(t,!0).map(t=>[...t]).flat().filter(t=>"bpmn:Lane"!==t.$type);n.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||N(n,e):v(n,e):N(n,e):v(n,e))}function b(t,e){const o=e.getEdgeDirection(t);if("S_N"===o||"N_S"===o)return;const n=e.getCrossedElementsFor(t,!1).map(t=>[...t]).flat().filter(t=>"bpmn:Lane"!==t.$type);if(0!==n.length){if("SW_NE"===o){const o=function(t,e){const o=e.getSourcePosition(t),n=e.getTargetPosition(t);let s=o[0];for(let t=o[0];t<e.rowCount&&e.getElementsInRange({row:t,col:o[1]+1},{row:t,col:n[1]}).length>0;t++)s++;return s-o[0]}(t,e),[n]=e.getSourcePosition(t);e.addRowCol(!1,n-1,o);const s=e.getElementsInRange({row:e.getSourcePosition(t)[0],col:e.getSourcePosition(t)[1]+1},{row:e.rowCount-1,col:e.colCount-1}).filter(t=>"bpmn:Lane"!==t.$type);for(const t of s){const[n,s]=e.find(t);for(const t of[...e.get(n,s)])e.move(t,[n-o,s])}return}"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||C(n,e):F(n,e):C(n,e):F(n,e)}}function C(t,e){const[o]=e.find(t[0]);e.addRowCol(!1,o-1);for(const n of t){const[,t]=e.find(n);e.move(n,[o,t])}}function F(t,e){const[o]=e.find(t[0]);e.addRowCol(!1,o);for(const n of t){const[,t]=e.find(n);e.move(n,[o+1,t])}}function v(t,e){const[,o]=e.find(t[0]);e.addRowCol(!0,o);for(const n of t){const[t]=e.find(n);e.move(n,[t,o+1])}}function N(t,e){const[,o]=e.find(t[0]);e.addRowCol(!0,o-1);for(const n of t){const[t]=e.find(n);e.move(n,[t,o])}}function R(t,e,o,n){const{id:s}=t;if(s){const i=function(t,e,o){const{source:n,target:s}=t,i=n.di,r=s.di,a=i.get("bounds"),f=r.get("bounds"),p=h(a),E=h(f),[m,w,x=1]=e.find(n),[y,_]=e.find(s),S=e.getEdgeDirection(t),b=_-w,C=y-m,F=`${C>0?"bottom":"top"}-${b>0?"right":"left"}`,v=`${C>0?"top":"bottom"}-${b>0?"left":"right"}`,{x:N,y:R}=u(n,e,o),{x:T,y:P}=u(s,e,o),D="bpmn:BoundaryEvent"===n.$type;if("NO_DIRECTION"===S)return D?[l(p,a,"b",F),{x:p.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:T,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:N,y:E.y},l(E,f,"l",v)]:[l(p,a,"b",F),{x:p.x,y:R+x*c},{x:T,y:R+x*c},{x:N,y:E.y},l(E,f,"l",v)];if("S_N"===S)return D?[l(p,a,"b",F),{x:p.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:N,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:T,y:E.y},l(E,f,"l",v)]:[...g(s)].includes(n)?[l(p,a,"l",F),{x:N,y:p.y},{x:T,y:E.y},l(E,f,"l",v)]:[l(p,a,"t",F),l(E,f,"b",v)];if("SW_NE"===S)return D?[l(p,a,"b",F),{x:p.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:E.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},l(E,f,"b",v)]:[l(p,a,"r"),{x:E.x,y:p.y},l(E,f,"b")];if("W_E"===S){if(D)return[l(p,a,"b",F),{x:p.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:E.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},l(E,f,"b",v)];const t=l(p,a,"r",F);n.isExpanded&&(t.y=R+70);const e=l(E,f,"l",v);return s.isExpanded&&(e.y=P+70),[t,e]}if("NW_SE"===S)return[l(p,a,"b"),{x:p.x,y:E.y},l(E,f,"l")];if("N_S"===S){if(D){const t=l(p,a,"b",F),e=l(E,f,"t",v);return n.attachedToRef.isExpanded?[t,{x:p.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:E.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},e]:[t,e]}const t=l(p,a,"b",F),e=l(E,f,"t",v);return n.isExpanded||s.isExpanded?[t,{x:p.x,y:n.isExpanded?R+(n.grid.rowCount+1)*c:R+c},{x:E.x,y:n.isExpanded?R+(n.grid.rowCount+1)*c:R+c},e]:[t,e]}if("NE_SW"===S)return[l(p,a,"b"),{x:p.x,y:E.y},l(E,f,"r")];if("E_W"===S){const t=function(t,e,o){const[n,s]=o.find(t),[,i]=o.find(e),r=s<i?s:i,d=s<i?i:s;return[...o.rows[n]].filter(t=>{const[,e]=o.find(t);return e>=r&&e<=d}).reduce((t,e)=>{const[,,,n]=o.find(e);return n>t?n:t},0)}(n,s,e),o=d(s).includes(n);let i=e?e.getExistingOutgoingEdgesFor(s):[];if(i=i.some(t=>"SW_NE"===e.getEdgeDirection(t)),D||o||i)return[l(p,a,"b",F),{x:p.x,y:R+c+(t?t-1:0)*c},{x:E.x,y:R+c+(t?t-1:0)*c},l(E,f,"b",v)];{const t=l(p,a,"l",F);n.isExpanded&&(t.y=R+70);const e=l(E,f,"r",v);return s.isExpanded&&(e.y=P+70),[t,e]}}if("SE_NW"===S){if(D)return[l(p,a,"b",F),{x:p.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},{x:E.x,y:n.attachedToRef.isExpanded?R+(n.attachedToRef.grid.rowCount+1)*c:R+c},l(E,f,"b",v)];const t=[];for(let o=w-1;o>=_;o--){const n=e?e.get(m,o):null;n&&t.push(n)}return t.length>0?[l(p,a,"b",F),{x:p.x,y:n.isExpanded?R+(n.grid.rowCount+1)*c:R+c},{x:E.x,y:n.isExpanded?R+(n.grid.rowCount+1)*c:R+c},l(E,f,"b",v)]:[l(p,a,"l"),{x:E.x,y:p.y},l(E,f,"b")]}const I=function(t,e,o){const[n,s]=o.find(t),[i,r]=o.find(e),d=i-n;if(r-s>0&&0!==d){if(d>0){let t=0;const e={row:i,col:s};return t+=o.getElementsInRange({row:n,col:s},e).length,t+=o.getElementsInRange(e,{row:i,col:r}).length,!(t>2)&&["v","h"]}{let t=0;const e={row:n,col:r};return t+=o.getElementsInRange({row:n,col:s},e).length,t+=o.getElementsInRange(e,{row:i,col:r}).length,!(t>2)&&["h","v"]}}}(n,s,e);if(I){const t=l(p,a,I[0],F),e=l(E,f,I[1],v);return[t,"h"===I[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const W=-Math.sign(C)*c/2;return[l(p,a,"r",F),{x:p.x+75,y:p.y},{x:p.x+75,y:E.y+W},{x:E.x-75,y:E.y+W},{x:E.x-75,y:E.y},l(E,f,"l",v)]}(t,e,n);return o.createDiEdge(t,i,{id:s+"_di"})}}function T(t,e,o,n,s){if(t.di)return[];if("bpmn:BoundaryEvent"===t.$type)return function(t,e,o,n,s){const i=t.attachedToRef,r=n.find(i),d=f(i,r,s);if(!d)throw new Error(`Create DI for ${t.id}. Nо hostBounds`);const a=[];let c=t.$parent.flowElements.filter(e=>e.attachedToRef===t.attachedToRef&&n.hasElement(t));return c=function(t,e){return t.sort((t,o)=>{const n=P(t,e),s=P(o,e);return n[0]-s[0]||n[1]-s[1]}).reverse()}(c,n),c.forEach((n,i,r)=>{const c=f(n,e,s,t.attachedToRef);c.x=d.x+(i+1)*(d.width/(r.length+1))-c.width/2,c.y=d.y+d.height-c.height/2;const g=o.createDiShape(n,c,{id:n.id+"_di"});n.di=g,a.push(g)}),a}(t,e,o,n,s);"bpmn:Lane"===t.$type&&(e[2]=n.colCount);const i=f(t,e,s),d={id:t.id+"_di"};t.isExpanded&&(d.isExpanded=!0),r(t,"bpmn:ExclusiveGateway")&&(d.isMarkerVisible=!0);const a=o.createDiShape(t,i,d);return t.di=a,[a]}function P(t,e){return d(t).reduce((t,o)=>{if(!e.hasElement(o))return t;const n=e.find(o);return t[0]<n[0]||t[1]<n[1]?n:t},[0,0])}class D{constructor(t,e){this.source=t,this.target=e}}class I{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))if("bpmndi:BPMNShape"===t.$type&&!0===t.isExpanded&&(t.bpmnElement.isExpanded=!0),"bpmn:Participant"===t.$type){const e=t.processRef.laneSets||[],o=e[0]?.lanes;o&&o.forEach((t,e)=>{const o=t.flowNodeRef;o&&o.forEach(e=>e.laneRef=t)})}}(e),this.processTrees=this.createNestedSets(e),this.createGridsForProcesses(),this.expandProcessesGrids(),this.expandParticipants();const n=this.getRootProcesses(),s=this.getCollaboration();return n.length>0&&(this.cleanDi(),this.createRootDi(n,s),this.drawParticipants(),this.drawProcesses(),this.drawCollaborationMessageFlows(s)),(await this.moddle.toXML(this.diagram,{format:!0})).xml}expandParticipants(){const t=this.processTrees.map(t=>[...t.nodes]).flat();for(const e of t){const t=(e.laneSets||[])[0]?.lanes;for(const o of t||[]){const t=e.grid.find(o);e.grid.addRowCol(!1,t[0]+t[3]-1,1),t[3]+=1}}}createGridsForProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){const t=(e.laneSets||[])[0]?.lanes.length>0;e.grid=this.createGridLayout(e);const o=t?[e.grid]:e.grid._separateGrid()||[e.grid];for(const t of o)t.shrink(!0),t.shrink(!1),t.shakeIt(!0),t.shakeIt(!1);t||(e.grid=e.grid._mergeGrids(o))}}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.laneSets||[])[0]?.lanes.length>0,o=t?[e.grid]:e.grid._separateGrid()||[e.grid];for(const t of o)t.shrink(!0),t.shrink(!1),W(t,!1),W(t,!0);t||(e.grid=e.grid._mergeGrids(o))}}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),n=this.getProcDi(o);let{x:s,y:i}=o.bounds;s+=75,i+=70,this.generateDi(e,{x:s,y:i},n);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:s}=t.bounds;const{width:r,height:d}=i(e);n+=75-r/4,s+=c-d-d/4,this.generateDi(e,{x:n,y:s},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,n=this.existingNodes;if(!n.includes(e)||!n.includes(o))continue;const s=e.di.bounds,i=o.di.bounds,r=i.y-s.y,d=[{x:s.x+s.width/2},{x:i.x+i.width/2}];r>0?(d[0].y=s.y+s.height,d[1].y=i.y):(d[0].y=s.y,d[1].y=i.y+i.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,n=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 n)o.addNode(t);for(const t of n){const e=this.getSubProcesses(t);for(const n of e)o.addEdge({source:t,target:n})}const s=o.getSeparatedGraphs();for(const t of s){const e=t.nodes.filter(e=>0===t.getIncomingEdgesFor(e).size);if(e.length>1)throw new Error("Process tree has more than 1 root elements");if(0===e.length)throw new Error("Process tree has more than 0 root elements");t.rootProcess=e[0]}for(const t of s){const e=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},o=(t,e)=>{const{left:o,level:n}=t,s=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(s)return s.level=n+1,s.left=o+1,[s];const i=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).reduce((t,e)=>void 0===t||e.right>t.right?e.right:t,void 0);t.right=i?i+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 s}getSubProcesses(t){return t.flowElements?t.flowElements.filter(t=>"bpmn:SubProcess"===t.$type):[]}createRootDi(t,e){const o=e&&e.length>0?e[0]:t[0];this.createProcessDi(o)}createProcessDi(t){const e=this.diFactory,o=e.createDiPlane({id:"BPMNPlane_"+t.id,bpmnElement:t}),n=e.createDiDiagram({id:"BPMNDiagram_"+t.id,plane:o});return this.diagram.diagrams.push(n),n}createParticipantDi(t,e){const{colCount:o,rowCount:n}=t.processRef.grid,{width:s,height:r}=i(t),d=o>0?o*a+a:s;let g=r;g=t.processRef.grid.getLanes().length>0?n*c:n>0?n*c+c:r;const h=this.diFactory.createDiShape(t,{width:d,height:g,...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,n=new m(o),s=t.laneSets?t.laneSets[0].lanes:[];for(const t of s)n.add(t),n.find(t)[3]=1;for(const e of t.flowElements||[])r(e,"bpmn:SequenceFlow")||r(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new D(t,t.attachedToRef)),o.addEdge(new D(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new D(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 n=new D(e,t.$parent);n.id=t.id,n.propRef=t.targetRef,o.addEdge(n)}const n=t.dataOutputAssociations;for(const t of n||[]){const e=t.$parent,n=t.targetRef,s=new D(e,n);s.id=t.id,o.addEdge(s)}}return o.genericTraversing((t,e,o,s)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const i=w(t,n,s,o);return this.currentDebugStep+=1,i},(t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const o=[...t].find(o=>(n.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(e=>!t.has(n.isFlipped?e.target:e.source)).length>0);if(o)return n.flip(!1),o;const s=e.nodes.filter(o=>{const s=n.isFlipped?e.getOutgoingEdgesFor(o):e.getIncomingEdgesFor(o);return!(t.has(o)||0!==s.size||(i=o,(r(i,"bpmn:IntermediateThrowEvent")||r(i,"bpmn:IntermediateCatchEvent"))&&(void 0===i.incoming||0===i.incoming.length)));var i});if(s.length>0)return function(t,e){const o=[e].flat();let n=[];return o.length>0?o.forEach((e,s)=>{const i=t.filter(t=>r(t,e));if(n=n.concat(i),s===o.length-1&&n.length!==t.length)for(const e of t)n.includes(e)||n.push(e)}):n=t,n}(s,"bpmn:StartEvent")[0];const i=[...t].find(o=>(n.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(e=>!t.has(n.isFlipped?e.source:e.target)).length>0);if(i)return i;const d=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(n.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(t=>o!==(n.isFlipped?t.target:t.source)).length});if(d)return d;const a=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(n.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(t=>t.target!==t.source).length});return a?(n.flip(!1),a):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))}),n.isFlipped&&n.flip(!1),n}generateDi(t,e,o){const{grid:n}=t,s=this.diFactory,i=(o||this.diagram.diagrams[0]).plane.get("planeElement"),r=[];n._elements.forEach((t,o)=>{const i=T(o,t,s,n,e);r.push(...i)}),n._allEdges.forEach(t=>{const o=R(t,n,s,e);o&&r.push(o)}),this.sortDIsByDefinitionPosition(r,t).forEach(t=>i.push(t))}sortDIsByDefinitionPosition(t,e){return t.sort((t,o)=>{const n=e.flowElements?.findIndex(e=>e.id===t.bpmnElement.id),s=e.flowElements?.findIndex(t=>t.id===o.bpmnElement.id);return n-s})}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,n=new Set;Object.entries(e?t.rows:t.cols).forEach(([t,s])=>{s.forEach(s=>{if(s.isExpanded){n.add(s);const i=o.get(Number.parseInt(t)),[r,d]=s.grid.getGridDimensions();let a=e?r:d;a||(a=1),(void 0===i||a>i)&&o.set(Number.parseInt(t),a)}})}),[...o.entries()].sort(([t],[e])=>e-t).forEach(([o,n])=>{if(e){const e=t.getLanes();for(const s of e){const e=t.find(s);Number.parseInt(o)<=e[0]+e[3]-1&&Number.parseInt(o)>=e[0]&&(e[3]+=Number.parseInt(n))}}t.addRowCol(!e,o,n)}),n.forEach(o=>{const n=t.find(o),[s,i]=o.grid.getGridDimensions();e?n[3]=s<1?2:s+1:n[2]=i<1?2:i+1})}function O(t,e){return new I(e).layoutProcess(t)}export{O as layoutProcess};//# sourceMappingURL=index.js.map
|