1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var __extends = (this && this.__extends) || (function () {
|
7 | var extendStatics = function (d, b) {
|
8 | extendStatics = Object.setPrototypeOf ||
|
9 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
10 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
11 | return extendStatics(d, b);
|
12 | };
|
13 | return function (d, b) {
|
14 | extendStatics(d, b);
|
15 | function __() { this.constructor = d; }
|
16 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
17 | };
|
18 | })();
|
19 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
20 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
21 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | exports.DagreLayout = void 0;
|
24 | var dagre_1 = __importDefault(require("dagre"));
|
25 | var util_1 = require("../util");
|
26 | var base_1 = require("./base");
|
27 |
|
28 |
|
29 |
|
30 | var DagreLayout = (function (_super) {
|
31 | __extends(DagreLayout, _super);
|
32 | function DagreLayout(options) {
|
33 | var _this = _super.call(this) || this;
|
34 |
|
35 | _this.rankdir = 'TB';
|
36 |
|
37 | _this.nodesep = 50;
|
38 |
|
39 | _this.ranksep = 50;
|
40 |
|
41 | _this.controlPoints = false;
|
42 |
|
43 | _this.sortByCombo = false;
|
44 | _this.nodes = [];
|
45 | _this.edges = [];
|
46 | _this.updateCfg(options);
|
47 | return _this;
|
48 | }
|
49 | DagreLayout.prototype.getDefaultCfg = function () {
|
50 | return {
|
51 | rankdir: 'TB',
|
52 | align: undefined,
|
53 | nodeSize: undefined,
|
54 | nodesepFunc: undefined,
|
55 | ranksepFunc: undefined,
|
56 | nodesep: 50,
|
57 | ranksep: 50,
|
58 | controlPoints: false,
|
59 | };
|
60 | };
|
61 | |
62 |
|
63 |
|
64 | DagreLayout.prototype.execute = function () {
|
65 | var self = this;
|
66 | var nodes = self.nodes, nodeSize = self.nodeSize, rankdir = self.rankdir;
|
67 | if (!nodes)
|
68 | return;
|
69 | var edges = self.edges || [];
|
70 | var g = new dagre_1.default.graphlib.Graph();
|
71 | var nodeSizeFunc;
|
72 | if (!nodeSize) {
|
73 | nodeSizeFunc = function (d) {
|
74 | if (d.size) {
|
75 | if (util_1.isArray(d.size)) {
|
76 | return d.size;
|
77 | }
|
78 | return [d.size, d.size];
|
79 | }
|
80 | return [40, 40];
|
81 | };
|
82 | }
|
83 | else if (util_1.isArray(nodeSize)) {
|
84 | nodeSizeFunc = function () { return nodeSize; };
|
85 | }
|
86 | else {
|
87 | nodeSizeFunc = function () { return [nodeSize, nodeSize]; };
|
88 | }
|
89 | var horisep = getFunc(self.nodesepFunc, self.nodesep, 50);
|
90 | var vertisep = getFunc(self.ranksepFunc, self.ranksep, 50);
|
91 | if (rankdir === 'LR' || rankdir === 'RL') {
|
92 | horisep = getFunc(self.ranksepFunc, self.ranksep, 50);
|
93 | vertisep = getFunc(self.nodesepFunc, self.nodesep, 50);
|
94 | }
|
95 | g.setDefaultEdgeLabel(function () { return ({}); });
|
96 | g.setGraph(self);
|
97 | nodes.forEach(function (node) {
|
98 | var size = nodeSizeFunc(node);
|
99 | var verti = vertisep(node);
|
100 | var hori = horisep(node);
|
101 | var width = size[0] + 2 * hori;
|
102 | var height = size[1] + 2 * verti;
|
103 | g.setNode(node.id, { width: width, height: height });
|
104 | });
|
105 | edges.forEach(function (edge) {
|
106 |
|
107 | g.setEdge(edge.source, edge.target, {
|
108 | weight: edge.weight || 1,
|
109 | });
|
110 | });
|
111 | dagre_1.default.layout(g);
|
112 | var coord;
|
113 | g.nodes().forEach(function (node) {
|
114 | coord = g.node(node);
|
115 | var i = nodes.findIndex(function (it) { return it.id === node; });
|
116 | nodes[i].x = coord.x;
|
117 | nodes[i].y = coord.y;
|
118 | });
|
119 | g.edges().forEach(function (edge) {
|
120 | coord = g.edge(edge);
|
121 | var i = edges.findIndex(function (it) { return it.source === edge.v && it.target === edge.w; });
|
122 | if (self.controlPoints && edges[i].type !== 'loop') {
|
123 | edges[i].controlPoints = coord.points.slice(1, coord.points.length - 1);
|
124 | }
|
125 | });
|
126 | if (self.sortByCombo) {
|
127 | self.sortLevel('comboId');
|
128 | }
|
129 | return {
|
130 | nodes: nodes,
|
131 | edges: edges,
|
132 | };
|
133 | };
|
134 | DagreLayout.prototype.sortLevel = function (propertyName) {
|
135 | var self = this;
|
136 | var nodes = self.nodes;
|
137 | var levels = {};
|
138 | nodes.forEach(function (node) {
|
139 | if (!levels[node.y])
|
140 | levels[node.y] = { y: node.y, nodes: [] };
|
141 | levels[node.y].nodes.push(node);
|
142 | });
|
143 | Object.keys(levels).forEach(function (key) {
|
144 | var levelNodes = levels[key].nodes;
|
145 | var nodesNum = levelNodes.length;
|
146 | var comboCenters = {};
|
147 | levelNodes.forEach(function (lnode) {
|
148 | var lnodeCombo = lnode.comboId;
|
149 | if (!comboCenters[lnodeCombo])
|
150 | comboCenters[lnodeCombo] = { x: 0, y: 0, count: 0 };
|
151 | comboCenters[lnodeCombo].x += lnode.x;
|
152 | comboCenters[lnodeCombo].y += lnode.y;
|
153 | comboCenters[lnodeCombo].count++;
|
154 | });
|
155 | Object.keys(comboCenters).forEach(function (ckey) {
|
156 | comboCenters[ckey].x /= comboCenters[ckey].count;
|
157 | comboCenters[ckey].y /= comboCenters[ckey].count;
|
158 | });
|
159 | if (nodesNum === 1)
|
160 | return;
|
161 | var sortedByX = levelNodes.sort(function (a, b) {
|
162 | return a.x - b.x;
|
163 | });
|
164 | var minX = sortedByX[0].x;
|
165 | var maxX = sortedByX[nodesNum - 1].x;
|
166 | var gap = (maxX - minX) / (nodesNum - 1);
|
167 | var sortedByCombo = levelNodes.sort(function (a, b) {
|
168 | var aValue = a[propertyName] || 'undefined';
|
169 | var bValue = b[propertyName] || 'undefined';
|
170 | if (aValue < bValue) {
|
171 | return -1;
|
172 | }
|
173 | if (aValue > bValue) {
|
174 | return 1;
|
175 | }
|
176 | return 0;
|
177 | });
|
178 | sortedByCombo.forEach(function (node, i) {
|
179 | node.x = minX + i * gap;
|
180 | });
|
181 | });
|
182 | };
|
183 | DagreLayout.prototype.getType = function () {
|
184 | return 'dagre';
|
185 | };
|
186 | return DagreLayout;
|
187 | }(base_1.Base));
|
188 | exports.DagreLayout = DagreLayout;
|
189 | function getFunc(func, value, defaultValue) {
|
190 | var resultFunc;
|
191 | if (func) {
|
192 | resultFunc = func;
|
193 | }
|
194 | else if (util_1.isNumber(value)) {
|
195 | resultFunc = function () { return value; };
|
196 | }
|
197 | else {
|
198 | resultFunc = function () { return defaultValue; };
|
199 | }
|
200 | return resultFunc;
|
201 | }
|
202 |
|
\ | No newline at end of file |