UNPKG

@joint/layout-directed-graph

Version:

LayoutDirectedGraph module for JointJS

9 lines (6 loc) 4.33 kB
/*! JointJS LayoutDirectedGraph v4.2.3 (2026-01-21) - LayoutDirectedGraph module for JointJS This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@dagrejs/dagre"),require("@dagrejs/graphlib"),require("@joint/core")):"function"==typeof define&&define.amd?define(["exports","@dagrejs/dagre","@dagrejs/graphlib","@joint/core"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).joint=e.joint||{},e.joint.layout=e.joint.layout||{}),e.dagre,e.graphlib,e.joint)}(this,function(e,t,i,n){"use strict";function r(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(i){if("default"!==i){var n=Object.getOwnPropertyDescriptor(e,i);Object.defineProperty(t,i,n.get?n:{enumerable:!0,get:function(){return e[i]}})}}),t.default=e,Object.freeze(t)}var o=r(t),s=r(i);const a={exportElement:function(e){return e.size()},exportLink:function(e){var t=e.get("labelSize")||{};return{minLen:e.get("minLen")||1,weight:e.get("weight")||1,labelpos:e.get("labelPosition")||"c",labeloffset:e.get("labelOffset")||0,width:t.width||0,height:t.height||0}},importElement:function(e,t,i,r){const o=i.getCell(e),s=t.node(e);n.util.isFunction(r.setPosition)?r.setPosition(o,s):o.set("position",{x:s.x-s.width/2,y:s.y-s.height/2}),r.resizeClusters&&"default"===r.clusterPadding&&void 0===s.rank&&o.set("size",{width:s.width,height:s.height})},importLink:function(e,t,i,r){const o=i.getCell(e.name),s=t.edge(e),a=s.points||[],l=new n.g.Polyline(a);if(r.setVertices||r.setLinkVertices)if(n.util.isFunction(r.setVertices))r.setVertices(o,a);else{l.simplify({threshold:.001});const e=l.points.map(e=>e.toJSON()),t=e.length;o.set("vertices",e.slice(1,t-1))}if(r.setLabels&&"x"in s&&"y"in s){const e={x:s.x,y:s.y};if(n.util.isFunction(r.setLabels))r.setLabels(o,e,a);else{const t=l.closestPointLength(e),i=l.pointAtLength(t),r=t/l.length(),s=new n.g.Point(e).difference(i).toJSON();o.label(0,{position:{distance:r,offset:s}})}}},layout:function(e,t){var i;e instanceof n.dia.Graph?i=e:(i=new n.dia.Graph(null,{ignoreLayers:!0})).resetCells(e,{dry:!0,sort:!1}),e=null,t=n.util.defaults(t||{},{resizeClusters:!0,clusterPadding:10,exportElement:this.exportElement,exportLink:this.exportLink,disableOptimalOrderHeuristic:!1});var r=a.toGraphLib(i,{directed:!0,multigraph:!0,compound:!0,setNodeLabel:t.exportElement,setEdgeLabel:t.exportLink,setEdgeName:function(e){return e.id}}),s={},l=n.util.isNumber(t.marginX)?t.marginX:0,d=n.util.isNumber(t.marginY)?t.marginY:0;let u;if(t.rankDir&&(s.rankdir=t.rankDir),t.align&&(s.align=t.align),n.util.isNumber(t.nodeSep)&&(s.nodesep=t.nodeSep),n.util.isNumber(t.edgeSep)&&(s.edgesep=t.edgeSep),n.util.isNumber(t.rankSep)&&(s.ranksep=t.rankSep),t.ranker&&(s.ranker=t.ranker),s.marginx=l,s.marginy=d,r.setGraph(s),n.util.isFunction(t.customOrder)&&(u=(e,n)=>{t.customOrder(e,i,n)}),o.layout(r,{debugTiming:!!t.debugTiming,disableOptimalOrderHeuristic:!!t.disableOptimalOrderHeuristic,customOrder:u}),i.startBatch("layout"),a.fromGraphLib(r,{importNode:this.importElement,importEdge:this.importLink,...t,graph:i}),t.resizeClusters&&"default"!==t.clusterPadding){const e=r.nodes().filter(e=>!r.parent(e)&&r.children(e).length>0).map(i.getCell.bind(i));n.util.invoke(e,"fitToChildren",{padding:t.clusterPadding,deep:!0})}i.stopBatch("layout");var g=r.graph();return new n.g.Rect(l,d,Math.abs(g.width-2*l),Math.abs(g.height-2*d))},fromGraphLib:function(e,t){var i=(t=t||{}).importNode||n.util.noop,r=t.importEdge||n.util.noop,o=t.graph;return o||(o=this instanceof n.dia.Graph?this:new n.dia.Graph),e.nodes().forEach(function(n){i.call(o,n,e,o,t)}),e.edges().forEach(function(i){r.call(o,i,e,o,t)}),o},toGraphLib:function(e,t={}){const i=n.util.pick(t,"directed","compound","multigraph"),r=new s.Graph(i),o=t.setNodeLabel||n.util.noop,a=t.setEdgeLabel||n.util.noop,l=t.setEdgeName||n.util.noop;for(const t of e.getCells())if(t.isLink()){var d=t.get("source"),u=t.get("target");if(!d.id||!u.id)break;r.setEdge(d.id,u.id,a(t),l(t))}else if(r.setNode(t.id,o(t)),r.isCompound()&&t.has("parent")){const i=t.get("parent");e.getCell(i)&&r.setParent(t.id,i)}return r}};e.DirectedGraph=a});