UNPKG

@joint/layout-directed-graph

Version:

LayoutDirectedGraph module for JointJS

9 lines (6 loc) 3.92 kB
/*! JointJS LayoutDirectedGraph v4.1.4 (2025-03-13) - 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/. */ ((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,h){function n(i){var n=Object.create(null);return i&&Object.keys(i).forEach(function(e){var t;"default"!==e&&(t=Object.getOwnPropertyDescriptor(i,e),Object.defineProperty(n,e,t.get?t:{enumerable:!0,get:function(){return i[e]}}))}),n.default=i,Object.freeze(n)}var a=n(t),c=n(i);let s={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,n){i=i.getCell(e),t=t.node(e);n.setPosition?n.setPosition(i,t):i.set("position",{x:t.x-t.width/2,y:t.y-t.height/2})},importLink:function(e,t,i,n){var r,o,i=i.getCell(e.name),t=t.edge(e),e=t.points||[],a=new h.g.Polyline(e);(n.setVertices||n.setLinkVertices)&&(h.util.isFunction(n.setVertices)?n.setVertices(i,e):(a.simplify({threshold:.001}),o=(r=a.points.map(e=>e.toJSON())).length,i.set("vertices",r.slice(1,o-1)))),n.setLabels&&"x"in t&&"y"in t&&(r={x:t.x,y:t.y},h.util.isFunction(n.setLabels)?n.setLabels(i,r,e):(o=a.closestPointLength(r),t=a.pointAtLength(o),n=o/a.length(),e=new h.g.Point(r).difference(t).toJSON(),i.label(0,{position:{distance:n,offset:e}})))},layout:function(e,t){var i=e instanceof h.dia.Graph?e:(new h.dia.Graph).resetCells(e,{dry:!0,sort:!1}),n=(t=h.util.defaults(t||{},{resizeClusters:!(e=null),clusterPadding:10,exportElement:this.exportElement,exportLink:this.exportLink}),s.toGraphLib(i,{directed:!0,multigraph:!0,compound:!0,setNodeLabel:t.exportElement,setEdgeLabel:t.exportLink,setEdgeName:function(e){return e.id}})),e={},r=h.util.isNumber(t.marginX)?t.marginX:0,o=h.util.isNumber(t.marginY)?t.marginY:0,e=(t.rankDir&&(e.rankdir=t.rankDir),t.align&&(e.align=t.align),h.util.isNumber(t.nodeSep)&&(e.nodesep=t.nodeSep),h.util.isNumber(t.edgeSep)&&(e.edgesep=t.edgeSep),h.util.isNumber(t.rankSep)&&(e.ranksep=t.rankSep),t.ranker&&(e.ranker=t.ranker),e.marginx=r,e.marginy=o,n.setGraph(e),a.layout(n,{debugTiming:!!t.debugTiming}),i.startBatch("layout"),s.fromGraphLib(n,{importNode:this.importElement,importEdge:this.importLink,...t,graph:i}),t.resizeClusters&&(e=n.nodes().filter(e=>!n.parent(e)&&0<n.children(e).length).map(i.getCell.bind(i)),h.util.invoke(e,"fitToChildren",{padding:t.clusterPadding,deep:!0})),i.stopBatch("layout"),n.graph());return new h.g.Rect(r,o,Math.abs(e.width-2*r),Math.abs(e.height-2*o))},fromGraphLib:function(t,i){var n=(i=i||{}).importNode||h.util.noop,r=i.importEdge||h.util.noop,o=(o=i.graph)||(this instanceof h.dia.Graph?this:new h.dia.Graph);return t.nodes().forEach(function(e){n.call(o,e,t,o,i)}),t.edges().forEach(function(e){r.call(o,e,t,o,i)}),o},toGraphLib:function(e,t){for(var i=h.util.pick(t=t||{},"directed","compound","multigraph"),n=new c.Graph(i),r=t.setNodeLabel||h.util.noop,o=t.setEdgeLabel||h.util.noop,a=t.setEdgeName||h.util.noop,s=e.get("cells"),l=0,d=s.length;l<d;l++){var p=s.at(l);if(p.isLink()){var g=p.get("source"),u=p.get("target");if(!g.id||!u.id)break;n.setEdge(g.id,u.id,o(p),a(p))}else n.setNode(p.id,r(p)),n.isCompound()&&p.has("parent")&&(g=p.get("parent"),s.has(g))&&n.setParent(p.id,g)}return n}};e.DirectedGraph=s});