UNPKG

@reactodia/workspace

Version:

Reactodia Workspace -- library for visual interaction with graphs in a form of a diagram.

1,651 lines 162 kB
var dist = {}; var adaptor = {}; var layout = {}; var powergraph = {}; var hasRequiredPowergraph; function requirePowergraph() { if (hasRequiredPowergraph) return powergraph; hasRequiredPowergraph = 1; Object.defineProperty(powergraph, "__esModule", { value: true }); var PowerEdge = /* @__PURE__ */ (function() { function PowerEdge2(source, target, type) { this.source = source; this.target = target; this.type = type; } return PowerEdge2; })(); powergraph.PowerEdge = PowerEdge; var Configuration = (function() { function Configuration2(n, edges, linkAccessor, rootGroup) { var _this = this; this.linkAccessor = linkAccessor; this.modules = new Array(n); this.roots = []; if (rootGroup) { this.initModulesFromGroup(rootGroup); } else { this.roots.push(new ModuleSet()); for (var i = 0; i < n; ++i) this.roots[0].add(this.modules[i] = new Module(i)); } this.R = edges.length; edges.forEach(function(e) { var s = _this.modules[linkAccessor.getSourceIndex(e)], t = _this.modules[linkAccessor.getTargetIndex(e)], type = linkAccessor.getType(e); s.outgoing.add(type, t); t.incoming.add(type, s); }); } Configuration2.prototype.initModulesFromGroup = function(group) { var moduleSet = new ModuleSet(); this.roots.push(moduleSet); for (var i = 0; i < group.leaves.length; ++i) { var node = group.leaves[i]; var module = new Module(node.id); this.modules[node.id] = module; moduleSet.add(module); } if (group.groups) { for (var j = 0; j < group.groups.length; ++j) { var child = group.groups[j]; var definition = {}; for (var prop in child) if (prop !== "leaves" && prop !== "groups" && child.hasOwnProperty(prop)) definition[prop] = child[prop]; moduleSet.add(new Module(-1 - j, new LinkSets(), new LinkSets(), this.initModulesFromGroup(child), definition)); } } return moduleSet; }; Configuration2.prototype.merge = function(a, b, k) { if (k === void 0) { k = 0; } var inInt = a.incoming.intersection(b.incoming), outInt = a.outgoing.intersection(b.outgoing); var children = new ModuleSet(); children.add(a); children.add(b); var m = new Module(this.modules.length, outInt, inInt, children); this.modules.push(m); var update = function(s, i, o) { s.forAll(function(ms, linktype) { ms.forAll(function(n) { var nls = n[i]; nls.add(linktype, m); nls.remove(linktype, a); nls.remove(linktype, b); a[o].remove(linktype, n); b[o].remove(linktype, n); }); }); }; update(outInt, "incoming", "outgoing"); update(inInt, "outgoing", "incoming"); this.R -= inInt.count() + outInt.count(); this.roots[k].remove(a); this.roots[k].remove(b); this.roots[k].add(m); return m; }; Configuration2.prototype.rootMerges = function(k) { if (k === void 0) { k = 0; } var rs = this.roots[k].modules(); var n = rs.length; var merges = new Array(n * (n - 1)); var ctr = 0; for (var i = 0, i_ = n - 1; i < i_; ++i) { for (var j = i + 1; j < n; ++j) { var a = rs[i], b = rs[j]; merges[ctr] = { id: ctr, nEdges: this.nEdges(a, b), a, b }; ctr++; } } return merges; }; Configuration2.prototype.greedyMerge = function() { for (var i = 0; i < this.roots.length; ++i) { if (this.roots[i].modules().length < 2) continue; var ms = this.rootMerges(i).sort(function(a, b) { return a.nEdges == b.nEdges ? a.id - b.id : a.nEdges - b.nEdges; }); var m = ms[0]; if (m.nEdges >= this.R) continue; this.merge(m.a, m.b, i); return true; } }; Configuration2.prototype.nEdges = function(a, b) { var inInt = a.incoming.intersection(b.incoming), outInt = a.outgoing.intersection(b.outgoing); return this.R - inInt.count() - outInt.count(); }; Configuration2.prototype.getGroupHierarchy = function(retargetedEdges) { var _this = this; var groups = []; var root = {}; toGroups(this.roots[0], root, groups); var es = this.allEdges(); es.forEach(function(e) { var a = _this.modules[e.source]; var b = _this.modules[e.target]; retargetedEdges.push(new PowerEdge(typeof a.gid === "undefined" ? e.source : groups[a.gid], typeof b.gid === "undefined" ? e.target : groups[b.gid], e.type)); }); return groups; }; Configuration2.prototype.allEdges = function() { var es = []; Configuration2.getEdges(this.roots[0], es); return es; }; Configuration2.getEdges = function(modules, es) { modules.forAll(function(m) { m.getEdges(es); Configuration2.getEdges(m.children, es); }); }; return Configuration2; })(); powergraph.Configuration = Configuration; function toGroups(modules, group, groups) { modules.forAll(function(m) { if (m.isLeaf()) { if (!group.leaves) group.leaves = []; group.leaves.push(m.id); } else { var g = group; m.gid = groups.length; if (!m.isIsland() || m.isPredefined()) { g = { id: m.gid }; if (m.isPredefined()) for (var prop in m.definition) g[prop] = m.definition[prop]; if (!group.groups) group.groups = []; group.groups.push(m.gid); groups.push(g); } toGroups(m.children, g, groups); } }); } var Module = (function() { function Module2(id, outgoing, incoming, children, definition) { if (outgoing === void 0) { outgoing = new LinkSets(); } if (incoming === void 0) { incoming = new LinkSets(); } if (children === void 0) { children = new ModuleSet(); } this.id = id; this.outgoing = outgoing; this.incoming = incoming; this.children = children; this.definition = definition; } Module2.prototype.getEdges = function(es) { var _this = this; this.outgoing.forAll(function(ms, edgetype) { ms.forAll(function(target) { es.push(new PowerEdge(_this.id, target.id, edgetype)); }); }); }; Module2.prototype.isLeaf = function() { return this.children.count() === 0; }; Module2.prototype.isIsland = function() { return this.outgoing.count() === 0 && this.incoming.count() === 0; }; Module2.prototype.isPredefined = function() { return typeof this.definition !== "undefined"; }; return Module2; })(); powergraph.Module = Module; function intersection(m, n) { var i = {}; for (var v in m) if (v in n) i[v] = m[v]; return i; } var ModuleSet = (function() { function ModuleSet2() { this.table = {}; } ModuleSet2.prototype.count = function() { return Object.keys(this.table).length; }; ModuleSet2.prototype.intersection = function(other) { var result = new ModuleSet2(); result.table = intersection(this.table, other.table); return result; }; ModuleSet2.prototype.intersectionCount = function(other) { return this.intersection(other).count(); }; ModuleSet2.prototype.contains = function(id) { return id in this.table; }; ModuleSet2.prototype.add = function(m) { this.table[m.id] = m; }; ModuleSet2.prototype.remove = function(m) { delete this.table[m.id]; }; ModuleSet2.prototype.forAll = function(f) { for (var mid in this.table) { f(this.table[mid]); } }; ModuleSet2.prototype.modules = function() { var vs = []; this.forAll(function(m) { if (!m.isPredefined()) vs.push(m); }); return vs; }; return ModuleSet2; })(); powergraph.ModuleSet = ModuleSet; var LinkSets = (function() { function LinkSets2() { this.sets = {}; this.n = 0; } LinkSets2.prototype.count = function() { return this.n; }; LinkSets2.prototype.contains = function(id) { var result = false; this.forAllModules(function(m) { if (!result && m.id == id) { result = true; } }); return result; }; LinkSets2.prototype.add = function(linktype, m) { var s = linktype in this.sets ? this.sets[linktype] : this.sets[linktype] = new ModuleSet(); s.add(m); ++this.n; }; LinkSets2.prototype.remove = function(linktype, m) { var ms = this.sets[linktype]; ms.remove(m); if (ms.count() === 0) { delete this.sets[linktype]; } --this.n; }; LinkSets2.prototype.forAll = function(f) { for (var linktype in this.sets) { f(this.sets[linktype], Number(linktype)); } }; LinkSets2.prototype.forAllModules = function(f) { this.forAll(function(ms, lt) { return ms.forAll(f); }); }; LinkSets2.prototype.intersection = function(other) { var result = new LinkSets2(); this.forAll(function(ms, lt) { if (lt in other.sets) { var i = ms.intersection(other.sets[lt]), n = i.count(); if (n > 0) { result.sets[lt] = i; result.n += n; } } }); return result; }; return LinkSets2; })(); powergraph.LinkSets = LinkSets; function getGroups(nodes, links, la, rootGroup) { var n = nodes.length, c = new Configuration(n, links, la, rootGroup); while (c.greedyMerge()) ; var powerEdges = []; var g = c.getGroupHierarchy(powerEdges); powerEdges.forEach(function(e) { var f = function(end) { var g2 = e[end]; if (typeof g2 == "number") e[end] = nodes[g2]; }; f("source"); f("target"); }); return { groups: g, powerEdges }; } powergraph.getGroups = getGroups; return powergraph; } var linklengths = {}; var hasRequiredLinklengths; function requireLinklengths() { if (hasRequiredLinklengths) return linklengths; hasRequiredLinklengths = 1; Object.defineProperty(linklengths, "__esModule", { value: true }); function unionCount(a, b) { var u = {}; for (var i in a) u[i] = {}; for (var i in b) u[i] = {}; return Object.keys(u).length; } function intersectionCount(a, b) { var n = 0; for (var i in a) if (typeof b[i] !== "undefined") ++n; return n; } function getNeighbours(links, la) { var neighbours = {}; var addNeighbours = function(u, v) { if (typeof neighbours[u] === "undefined") neighbours[u] = {}; neighbours[u][v] = {}; }; links.forEach(function(e) { var u = la.getSourceIndex(e), v = la.getTargetIndex(e); addNeighbours(u, v); addNeighbours(v, u); }); return neighbours; } function computeLinkLengths(links, w, f, la) { var neighbours = getNeighbours(links, la); links.forEach(function(l) { var a = neighbours[la.getSourceIndex(l)]; var b = neighbours[la.getTargetIndex(l)]; la.setLength(l, 1 + w * f(a, b)); }); } function symmetricDiffLinkLengths(links, la, w) { if (w === void 0) { w = 1; } computeLinkLengths(links, w, function(a, b) { return Math.sqrt(unionCount(a, b) - intersectionCount(a, b)); }, la); } linklengths.symmetricDiffLinkLengths = symmetricDiffLinkLengths; function jaccardLinkLengths(links, la, w) { if (w === void 0) { w = 1; } computeLinkLengths(links, w, function(a, b) { return Math.min(Object.keys(a).length, Object.keys(b).length) < 1.1 ? 0 : intersectionCount(a, b) / unionCount(a, b); }, la); } linklengths.jaccardLinkLengths = jaccardLinkLengths; function generateDirectedEdgeConstraints(n, links, axis, la) { var components = stronglyConnectedComponents(n, links, la); var nodes = {}; components.forEach(function(c, i) { return c.forEach(function(v) { return nodes[v] = i; }); }); var constraints = []; links.forEach(function(l) { var ui = la.getSourceIndex(l), vi = la.getTargetIndex(l), u = nodes[ui], v = nodes[vi]; if (u !== v) { constraints.push({ axis, left: ui, right: vi, gap: la.getMinSeparation(l) }); } }); return constraints; } linklengths.generateDirectedEdgeConstraints = generateDirectedEdgeConstraints; function stronglyConnectedComponents(numVertices, edges, la) { var nodes = []; var index = 0; var stack = []; var components = []; function strongConnect(v2) { v2.index = v2.lowlink = index++; stack.push(v2); v2.onStack = true; for (var _i2 = 0, _a2 = v2.out; _i2 < _a2.length; _i2++) { var w2 = _a2[_i2]; if (typeof w2.index === "undefined") { strongConnect(w2); v2.lowlink = Math.min(v2.lowlink, w2.lowlink); } else if (w2.onStack) { v2.lowlink = Math.min(v2.lowlink, w2.index); } } if (v2.lowlink === v2.index) { var component = []; while (stack.length) { w2 = stack.pop(); w2.onStack = false; component.push(w2); if (w2 === v2) break; } components.push(component.map(function(v3) { return v3.id; })); } } for (var i = 0; i < numVertices; i++) { nodes.push({ id: i, out: [] }); } for (var _i = 0, edges_1 = edges; _i < edges_1.length; _i++) { var e = edges_1[_i]; var v_1 = nodes[la.getSourceIndex(e)], w = nodes[la.getTargetIndex(e)]; v_1.out.push(w); } for (var _a = 0, nodes_1 = nodes; _a < nodes_1.length; _a++) { var v = nodes_1[_a]; if (typeof v.index === "undefined") strongConnect(v); } return components; } linklengths.stronglyConnectedComponents = stronglyConnectedComponents; return linklengths; } var descent = {}; var hasRequiredDescent; function requireDescent() { if (hasRequiredDescent) return descent; hasRequiredDescent = 1; Object.defineProperty(descent, "__esModule", { value: true }); var Locks = (function() { function Locks2() { this.locks = {}; } Locks2.prototype.add = function(id, x) { this.locks[id] = x; }; Locks2.prototype.clear = function() { this.locks = {}; }; Locks2.prototype.isEmpty = function() { for (var l in this.locks) return false; return true; }; Locks2.prototype.apply = function(f) { for (var l in this.locks) { f(Number(l), this.locks[l]); } }; return Locks2; })(); descent.Locks = Locks; var Descent = (function() { function Descent2(x, D, G) { if (G === void 0) { G = null; } this.D = D; this.G = G; this.threshold = 1e-4; this.numGridSnapNodes = 0; this.snapGridSize = 100; this.snapStrength = 1e3; this.scaleSnapByMaxH = false; this.random = new PseudoRandom(); this.project = null; this.x = x; this.k = x.length; var n = this.n = x[0].length; this.H = new Array(this.k); this.g = new Array(this.k); this.Hd = new Array(this.k); this.a = new Array(this.k); this.b = new Array(this.k); this.c = new Array(this.k); this.d = new Array(this.k); this.e = new Array(this.k); this.ia = new Array(this.k); this.ib = new Array(this.k); this.xtmp = new Array(this.k); this.locks = new Locks(); this.minD = Number.MAX_VALUE; var i = n, j; while (i--) { j = n; while (--j > i) { var d = D[i][j]; if (d > 0 && d < this.minD) { this.minD = d; } } } if (this.minD === Number.MAX_VALUE) this.minD = 1; i = this.k; while (i--) { this.g[i] = new Array(n); this.H[i] = new Array(n); j = n; while (j--) { this.H[i][j] = new Array(n); } this.Hd[i] = new Array(n); this.a[i] = new Array(n); this.b[i] = new Array(n); this.c[i] = new Array(n); this.d[i] = new Array(n); this.e[i] = new Array(n); this.ia[i] = new Array(n); this.ib[i] = new Array(n); this.xtmp[i] = new Array(n); } } Descent2.createSquareMatrix = function(n, f) { var M = new Array(n); for (var i = 0; i < n; ++i) { M[i] = new Array(n); for (var j = 0; j < n; ++j) { M[i][j] = f(i, j); } } return M; }; Descent2.prototype.offsetDir = function() { var _this = this; var u = new Array(this.k); var l = 0; for (var i = 0; i < this.k; ++i) { var x = u[i] = this.random.getNextBetween(0.01, 1) - 0.5; l += x * x; } l = Math.sqrt(l); return u.map(function(x2) { return x2 *= _this.minD / l; }); }; Descent2.prototype.computeDerivatives = function(x) { var _this = this; var n = this.n; if (n < 1) return; var i; var d = new Array(this.k); var d2 = new Array(this.k); var Huu = new Array(this.k); var maxH = 0; for (var u = 0; u < n; ++u) { for (i = 0; i < this.k; ++i) Huu[i] = this.g[i][u] = 0; for (var v = 0; v < n; ++v) { if (u === v) continue; var maxDisplaces = n; while (maxDisplaces--) { var sd2 = 0; for (i = 0; i < this.k; ++i) { var dx = d[i] = x[i][u] - x[i][v]; sd2 += d2[i] = dx * dx; } if (sd2 > 1e-9) break; var rd = this.offsetDir(); for (i = 0; i < this.k; ++i) x[i][v] += rd[i]; } var l = Math.sqrt(sd2); var D = this.D[u][v]; var weight = this.G != null ? this.G[u][v] : 1; if (weight > 1 && l > D || !isFinite(D)) { for (i = 0; i < this.k; ++i) this.H[i][u][v] = 0; continue; } if (weight > 1) { weight = 1; } var D2 = D * D; var gs = 2 * weight * (l - D) / (D2 * l); var l3 = l * l * l; var hs = 2 * -weight / (D2 * l3); if (!isFinite(gs)) console.log(gs); for (i = 0; i < this.k; ++i) { this.g[i][u] += d[i] * gs; Huu[i] -= this.H[i][u][v] = hs * (l3 + D * (d2[i] - sd2) + l * sd2); } } for (i = 0; i < this.k; ++i) maxH = Math.max(maxH, this.H[i][u][u] = Huu[i]); } var r = this.snapGridSize / 2; var g = this.snapGridSize; var w = this.snapStrength; var k = w / (r * r); var numNodes = this.numGridSnapNodes; for (var u = 0; u < numNodes; ++u) { for (i = 0; i < this.k; ++i) { var xiu = this.x[i][u]; var m = xiu / g; var f = m % 1; var q = m - f; var a = Math.abs(f); var dx = a <= 0.5 ? xiu - q * g : xiu > 0 ? xiu - (q + 1) * g : xiu - (q - 1) * g; if (-r < dx && dx <= r) { if (this.scaleSnapByMaxH) { this.g[i][u] += maxH * k * dx; this.H[i][u][u] += maxH * k; } else { this.g[i][u] += k * dx; this.H[i][u][u] += k; } } } } if (!this.locks.isEmpty()) { this.locks.apply(function(u2, p) { for (i = 0; i < _this.k; ++i) { _this.H[i][u2][u2] += maxH; _this.g[i][u2] -= maxH * (p[i] - x[i][u2]); } }); } }; Descent2.dotProd = function(a, b) { var x = 0, i = a.length; while (i--) x += a[i] * b[i]; return x; }; Descent2.rightMultiply = function(m, v, r) { var i = m.length; while (i--) r[i] = Descent2.dotProd(m[i], v); }; Descent2.prototype.computeStepSize = function(d) { var numerator = 0, denominator = 0; for (var i = 0; i < this.k; ++i) { numerator += Descent2.dotProd(this.g[i], d[i]); Descent2.rightMultiply(this.H[i], d[i], this.Hd[i]); denominator += Descent2.dotProd(d[i], this.Hd[i]); } if (denominator === 0 || !isFinite(denominator)) return 0; return 1 * numerator / denominator; }; Descent2.prototype.reduceStress = function() { this.computeDerivatives(this.x); var alpha = this.computeStepSize(this.g); for (var i = 0; i < this.k; ++i) { this.takeDescentStep(this.x[i], this.g[i], alpha); } return this.computeStress(); }; Descent2.copy = function(a, b) { var m = a.length, n = b[0].length; for (var i = 0; i < m; ++i) { for (var j = 0; j < n; ++j) { b[i][j] = a[i][j]; } } }; Descent2.prototype.stepAndProject = function(x0, r, d, stepSize) { Descent2.copy(x0, r); this.takeDescentStep(r[0], d[0], stepSize); if (this.project) this.project[0](x0[0], x0[1], r[0]); this.takeDescentStep(r[1], d[1], stepSize); if (this.project) this.project[1](r[0], x0[1], r[1]); for (var i = 2; i < this.k; i++) this.takeDescentStep(r[i], d[i], stepSize); }; Descent2.mApply = function(m, n, f) { var i = m; while (i-- > 0) { var j = n; while (j-- > 0) f(i, j); } }; Descent2.prototype.matrixApply = function(f) { Descent2.mApply(this.k, this.n, f); }; Descent2.prototype.computeNextPosition = function(x0, r) { var _this = this; this.computeDerivatives(x0); var alpha = this.computeStepSize(this.g); this.stepAndProject(x0, r, this.g, alpha); if (this.project) { this.matrixApply(function(i, j) { return _this.e[i][j] = x0[i][j] - r[i][j]; }); var beta = this.computeStepSize(this.e); beta = Math.max(0.2, Math.min(beta, 1)); this.stepAndProject(x0, r, this.e, beta); } }; Descent2.prototype.run = function(iterations) { var stress = Number.MAX_VALUE, converged = false; while (!converged && iterations-- > 0) { var s = this.rungeKutta(); converged = Math.abs(stress / s - 1) < this.threshold; stress = s; } return stress; }; Descent2.prototype.rungeKutta = function() { var _this = this; this.computeNextPosition(this.x, this.a); Descent2.mid(this.x, this.a, this.ia); this.computeNextPosition(this.ia, this.b); Descent2.mid(this.x, this.b, this.ib); this.computeNextPosition(this.ib, this.c); this.computeNextPosition(this.c, this.d); var disp = 0; this.matrixApply(function(i, j) { var x = (_this.a[i][j] + 2 * _this.b[i][j] + 2 * _this.c[i][j] + _this.d[i][j]) / 6, d = _this.x[i][j] - x; disp += d * d; _this.x[i][j] = x; }); return disp; }; Descent2.mid = function(a, b, m) { Descent2.mApply(a.length, a[0].length, function(i, j) { return m[i][j] = a[i][j] + (b[i][j] - a[i][j]) / 2; }); }; Descent2.prototype.takeDescentStep = function(x, d, stepSize) { for (var i = 0; i < this.n; ++i) { x[i] = x[i] - stepSize * d[i]; } }; Descent2.prototype.computeStress = function() { var stress = 0; for (var u = 0, nMinus1 = this.n - 1; u < nMinus1; ++u) { for (var v = u + 1, n = this.n; v < n; ++v) { var l = 0; for (var i = 0; i < this.k; ++i) { var dx = this.x[i][u] - this.x[i][v]; l += dx * dx; } l = Math.sqrt(l); var d = this.D[u][v]; if (!isFinite(d)) continue; var rl = d - l; var d2 = d * d; stress += rl * rl / d2; } } return stress; }; Descent2.zeroDistance = 1e-10; return Descent2; })(); descent.Descent = Descent; var PseudoRandom = (function() { function PseudoRandom2(seed) { if (seed === void 0) { seed = 1; } this.seed = seed; this.a = 214013; this.c = 2531011; this.m = 2147483648; this.range = 32767; } PseudoRandom2.prototype.getNext = function() { this.seed = (this.seed * this.a + this.c) % this.m; return (this.seed >> 16) / this.range; }; PseudoRandom2.prototype.getNextBetween = function(min, max) { return min + this.getNext() * (max - min); }; return PseudoRandom2; })(); descent.PseudoRandom = PseudoRandom; return descent; } var rectangle = {}; var vpsc = {}; var hasRequiredVpsc; function requireVpsc() { if (hasRequiredVpsc) return vpsc; hasRequiredVpsc = 1; Object.defineProperty(vpsc, "__esModule", { value: true }); var PositionStats = (function() { function PositionStats2(scale) { this.scale = scale; this.AB = 0; this.AD = 0; this.A2 = 0; } PositionStats2.prototype.addVariable = function(v) { var ai = this.scale / v.scale; var bi = v.offset / v.scale; var wi = v.weight; this.AB += wi * ai * bi; this.AD += wi * ai * v.desiredPosition; this.A2 += wi * ai * ai; }; PositionStats2.prototype.getPosn = function() { return (this.AD - this.AB) / this.A2; }; return PositionStats2; })(); vpsc.PositionStats = PositionStats; var Constraint = (function() { function Constraint2(left, right, gap, equality) { if (equality === void 0) { equality = false; } this.left = left; this.right = right; this.gap = gap; this.equality = equality; this.active = false; this.unsatisfiable = false; this.left = left; this.right = right; this.gap = gap; this.equality = equality; } Constraint2.prototype.slack = function() { return this.unsatisfiable ? Number.MAX_VALUE : this.right.scale * this.right.position() - this.gap - this.left.scale * this.left.position(); }; return Constraint2; })(); vpsc.Constraint = Constraint; var Variable = (function() { function Variable2(desiredPosition, weight, scale) { if (weight === void 0) { weight = 1; } if (scale === void 0) { scale = 1; } this.desiredPosition = desiredPosition; this.weight = weight; this.scale = scale; this.offset = 0; } Variable2.prototype.dfdv = function() { return 2 * this.weight * (this.position() - this.desiredPosition); }; Variable2.prototype.position = function() { return (this.block.ps.scale * this.block.posn + this.offset) / this.scale; }; Variable2.prototype.visitNeighbours = function(prev, f) { var ff = function(c, next) { return c.active && prev !== next && f(c, next); }; this.cOut.forEach(function(c) { return ff(c, c.right); }); this.cIn.forEach(function(c) { return ff(c, c.left); }); }; return Variable2; })(); vpsc.Variable = Variable; var Block = (function() { function Block2(v) { this.vars = []; v.offset = 0; this.ps = new PositionStats(v.scale); this.addVariable(v); } Block2.prototype.addVariable = function(v) { v.block = this; this.vars.push(v); this.ps.addVariable(v); this.posn = this.ps.getPosn(); }; Block2.prototype.updateWeightedPosition = function() { this.ps.AB = this.ps.AD = this.ps.A2 = 0; for (var i = 0, n = this.vars.length; i < n; ++i) this.ps.addVariable(this.vars[i]); this.posn = this.ps.getPosn(); }; Block2.prototype.compute_lm = function(v, u, postAction) { var _this = this; var dfdv = v.dfdv(); v.visitNeighbours(u, function(c, next) { var _dfdv = _this.compute_lm(next, v, postAction); if (next === c.right) { dfdv += _dfdv * c.left.scale; c.lm = _dfdv; } else { dfdv += _dfdv * c.right.scale; c.lm = -_dfdv; } postAction(c); }); return dfdv / v.scale; }; Block2.prototype.populateSplitBlock = function(v, prev) { var _this = this; v.visitNeighbours(prev, function(c, next) { next.offset = v.offset + (next === c.right ? c.gap : -c.gap); _this.addVariable(next); _this.populateSplitBlock(next, v); }); }; Block2.prototype.traverse = function(visit, acc, v, prev) { var _this = this; if (v === void 0) { v = this.vars[0]; } if (prev === void 0) { prev = null; } v.visitNeighbours(prev, function(c, next) { acc.push(visit(c)); _this.traverse(visit, acc, next, v); }); }; Block2.prototype.findMinLM = function() { var m = null; this.compute_lm(this.vars[0], null, function(c) { if (!c.equality && (m === null || c.lm < m.lm)) m = c; }); return m; }; Block2.prototype.findMinLMBetween = function(lv, rv) { this.compute_lm(lv, null, function() { }); var m = null; this.findPath(lv, null, rv, function(c, next) { if (!c.equality && c.right === next && (m === null || c.lm < m.lm)) m = c; }); return m; }; Block2.prototype.findPath = function(v, prev, to, visit) { var _this = this; var endFound = false; v.visitNeighbours(prev, function(c, next) { if (!endFound && (next === to || _this.findPath(next, v, to, visit))) { endFound = true; visit(c, next); } }); return endFound; }; Block2.prototype.isActiveDirectedPathBetween = function(u, v) { if (u === v) return true; var i = u.cOut.length; while (i--) { var c = u.cOut[i]; if (c.active && this.isActiveDirectedPathBetween(c.right, v)) return true; } return false; }; Block2.split = function(c) { c.active = false; return [Block2.createSplitBlock(c.left), Block2.createSplitBlock(c.right)]; }; Block2.createSplitBlock = function(startVar) { var b = new Block2(startVar); b.populateSplitBlock(startVar, null); return b; }; Block2.prototype.splitBetween = function(vl, vr) { var c = this.findMinLMBetween(vl, vr); if (c !== null) { var bs = Block2.split(c); return { constraint: c, lb: bs[0], rb: bs[1] }; } return null; }; Block2.prototype.mergeAcross = function(b, c, dist2) { c.active = true; for (var i = 0, n = b.vars.length; i < n; ++i) { var v = b.vars[i]; v.offset += dist2; this.addVariable(v); } this.posn = this.ps.getPosn(); }; Block2.prototype.cost = function() { var sum = 0, i = this.vars.length; while (i--) { var v = this.vars[i], d = v.position() - v.desiredPosition; sum += d * d * v.weight; } return sum; }; return Block2; })(); vpsc.Block = Block; var Blocks = (function() { function Blocks2(vs) { this.vs = vs; var n = vs.length; this.list = new Array(n); while (n--) { var b = new Block(vs[n]); this.list[n] = b; b.blockInd = n; } } Blocks2.prototype.cost = function() { var sum = 0, i = this.list.length; while (i--) sum += this.list[i].cost(); return sum; }; Blocks2.prototype.insert = function(b) { b.blockInd = this.list.length; this.list.push(b); }; Blocks2.prototype.remove = function(b) { var last = this.list.length - 1; var swapBlock = this.list[last]; this.list.length = last; if (b !== swapBlock) { this.list[b.blockInd] = swapBlock; swapBlock.blockInd = b.blockInd; } }; Blocks2.prototype.merge = function(c) { var l = c.left.block, r = c.right.block; var dist2 = c.right.offset - c.left.offset - c.gap; if (l.vars.length < r.vars.length) { r.mergeAcross(l, c, dist2); this.remove(l); } else { l.mergeAcross(r, c, -dist2); this.remove(r); } }; Blocks2.prototype.forEach = function(f) { this.list.forEach(f); }; Blocks2.prototype.updateBlockPositions = function() { this.list.forEach(function(b) { return b.updateWeightedPosition(); }); }; Blocks2.prototype.split = function(inactive) { var _this = this; this.updateBlockPositions(); this.list.forEach(function(b) { var v = b.findMinLM(); if (v !== null && v.lm < Solver.LAGRANGIAN_TOLERANCE) { b = v.left.block; Block.split(v).forEach(function(nb) { return _this.insert(nb); }); _this.remove(b); inactive.push(v); } }); }; return Blocks2; })(); vpsc.Blocks = Blocks; var Solver = (function() { function Solver2(vs, cs) { this.vs = vs; this.cs = cs; this.vs = vs; vs.forEach(function(v) { v.cIn = [], v.cOut = []; }); this.cs = cs; cs.forEach(function(c) { c.left.cOut.push(c); c.right.cIn.push(c); }); this.inactive = cs.map(function(c) { c.active = false; return c; }); this.bs = null; } Solver2.prototype.cost = function() { return this.bs.cost(); }; Solver2.prototype.setStartingPositions = function(ps) { this.inactive = this.cs.map(function(c) { c.active = false; return c; }); this.bs = new Blocks(this.vs); this.bs.forEach(function(b, i) { return b.posn = ps[i]; }); }; Solver2.prototype.setDesiredPositions = function(ps) { this.vs.forEach(function(v, i) { return v.desiredPosition = ps[i]; }); }; Solver2.prototype.mostViolated = function() { var minSlack = Number.MAX_VALUE, v = null, l = this.inactive, n = l.length, deletePoint = n; for (var i = 0; i < n; ++i) { var c = l[i]; if (c.unsatisfiable) continue; var slack = c.slack(); if (c.equality || slack < minSlack) { minSlack = slack; v = c; deletePoint = i; if (c.equality) break; } } if (deletePoint !== n && (minSlack < Solver2.ZERO_UPPERBOUND && !v.active || v.equality)) { l[deletePoint] = l[n - 1]; l.length = n - 1; } return v; }; Solver2.prototype.satisfy = function() { if (this.bs == null) { this.bs = new Blocks(this.vs); } this.bs.split(this.inactive); var v = null; while ((v = this.mostViolated()) && (v.equality || v.slack() < Solver2.ZERO_UPPERBOUND && !v.active)) { var lb = v.left.block, rb = v.right.block; if (lb !== rb) { this.bs.merge(v); } else { if (lb.isActiveDirectedPathBetween(v.right, v.left)) { v.unsatisfiable = true; continue; } var split = lb.splitBetween(v.left, v.right); if (split !== null) { this.bs.insert(split.lb); this.bs.insert(split.rb); this.bs.remove(lb); this.inactive.push(split.constraint); } else { v.unsatisfiable = true; continue; } if (v.slack() >= 0) { this.inactive.push(v); } else { this.bs.merge(v); } } } }; Solver2.prototype.solve = function() { this.satisfy(); var lastcost = Number.MAX_VALUE, cost = this.bs.cost(); while (Math.abs(lastcost - cost) > 1e-4) { this.satisfy(); lastcost = cost; cost = this.bs.cost(); } return cost; }; Solver2.LAGRANGIAN_TOLERANCE = -1e-4; Solver2.ZERO_UPPERBOUND = -1e-10; return Solver2; })(); vpsc.Solver = Solver; function removeOverlapInOneDimension(spans, lowerBound, upperBound) { var vs = spans.map(function(s) { return new Variable(s.desiredCenter); }); var cs = []; var n = spans.length; for (var i = 0; i < n - 1; i++) { var left = spans[i], right = spans[i + 1]; cs.push(new Constraint(vs[i], vs[i + 1], (left.size + right.size) / 2)); } var leftMost = vs[0], rightMost = vs[n - 1], leftMostSize = spans[0].size / 2, rightMostSize = spans[n - 1].size / 2; var vLower = null, vUpper = null; if (lowerBound) { vLower = new Variable(lowerBound, leftMost.weight * 1e3); vs.push(vLower); cs.push(new Constraint(vLower, leftMost, leftMostSize)); } if (upperBound) { vUpper = new Variable(upperBound, rightMost.weight * 1e3); vs.push(vUpper); cs.push(new Constraint(rightMost, vUpper, rightMostSize)); } var solver = new Solver(vs, cs); solver.solve(); return { newCenters: vs.slice(0, spans.length).map(function(v) { return v.position(); }), lowerBound: vLower ? vLower.position() : leftMost.position() - leftMostSize, upperBound: vUpper ? vUpper.position() : rightMost.position() + rightMostSize }; } vpsc.removeOverlapInOneDimension = removeOverlapInOneDimension; return vpsc; } var rbtree = {}; var hasRequiredRbtree; function requireRbtree() { if (hasRequiredRbtree) return rbtree; hasRequiredRbtree = 1; var __extends = rbtree && rbtree.__extends || (function() { var extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d, b) { d.__proto__ = b; } || function(d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(rbtree, "__esModule", { value: true }); var TreeBase = (function() { function TreeBase2() { this.findIter = function(data) { var res = this._root; var iter = this.iterator(); while (res !== null) { var c = this._comparator(data, res.data); if (c === 0) { iter._cursor = res; return iter; } else { iter._ancestors.push(res); res = res.get_child(c > 0); } } return null; }; } TreeBase2.prototype.clear = function() { this._root = null; this.size = 0; }; TreeBase2.prototype.find = function(data) { var res = this._root; while (res !== null) { var c = this._comparator(data, res.data); if (c === 0) { return res.data; } else { res = res.get_child(c > 0); } } return null; }; TreeBase2.prototype.lowerBound = function(data) { return this._bound(data, this._comparator); }; TreeBase2.prototype.upperBound = function(data) { var cmp = this._comparator; function reverse_cmp(a, b) { return cmp(b, a); } return this._bound(data, reverse_cmp); }; TreeBase2.prototype.min = function() { var res = this._root; if (res === null) { return null; } while (res.left !== null) { res = res.left; } return res.data; }; TreeBase2.prototype.max = function() { var res = this._root; if (res === null) { return null; } while (res.right !== null) { res = res.right; } return res.data; }; TreeBase2.prototype.iterator = function() { return new Iterator(this); }; TreeBase2.prototype.each = function(cb) { var it = this.iterator(), data; while ((data = it.next()) !== null) { cb(data); } }; TreeBase2.prototype.reach = function(cb) { var it = this.iterator(), data; while ((data = it.prev()) !== null) { cb(data); } }; TreeBase2.prototype._bound = function(data, cmp) { var cur = this._root; var iter = this.iterator(); while (cur !== null) { var c = this._comparator(data, cur.data); if (c === 0) { iter._cursor = cur; return iter; } iter._ancestors.push(cur); cur = cur.get_child(c > 0); } for (var i = iter._ancestors.length - 1; i >= 0; --i) { cur = iter._ancestors[i]; if (cmp(data, cur.data) > 0) { iter._cursor = cur; iter._ancestors.length = i; return iter; } } iter._ancestors.length = 0; return iter; }; return TreeBase2; })(); rbtree.TreeBase = TreeBase; var Iterator = (function() { function Iterator2(tree) { this._tree = tree; this._ancestors = []; this._cursor = null; } Iterator2.prototype.data = function() { return this._cursor !== null ? this._cursor.data : null; }; Iterator2.prototype.next = function() { if (this._cursor === null) { var root = this._tree._root; if (root !== null) { this._minNode(root); } } else { if (this._cursor.right === null) { var save; do { save = this._cursor; if (this._ancestors.length) { this._cursor = this._ancestors.pop(); } else { this._cursor = null; break; } } while (this._cursor.right === save); } else { this._ancestors.push(this._cursor); this._minNode(this._cursor.right); } } return this._cursor !== null ? this._cursor.data : null; }; Iterator2.prototype.prev = function() { if (this._cursor === null) { var root = this._tree._root; if (root !== null) { this._maxNode(root); } } else { if (this._cursor.left === null) { var save; do { save = this._cursor; if (this._ancestors.length) { this._cursor = this._ancestors.pop(); } else { this._cursor = null; break; } } while (this._cursor.left === save); } else { this._ancestors.push(this._cursor); this._maxNode(this._cursor.left); } } return this._cursor !== null ? this._cursor.data : null; }; Iterator2.prototype._minNode = function(start) { while (start.left !== null) { this._ancestors.push(start); start = start.left; } this._cursor = start; }; Iterator2.prototype._maxNode = function(start) { while (start.right !== null) { this._ancestors.push(start); start = start.right; } this._cursor = start; }; return Iterator2; })(); rbtree.Iterator = Iterator; var Node = (function() { function Node2(data) { this.data = data; this.left = null; this.right = null; this.red = true; } Node2.prototype.get_child = function(dir) { return dir ? this.right : this.left; }; Node2.prototype.set_child = function(dir, val) { if (dir) { this.right = val; } else { this.left = val; } }; return Node2; })(); var RBTree = (function(_super) { __extends(RBTree2, _super); function RBTree2(comparator) { var _this = _super.call(this) || this; _this._root = null; _this._comparator = comparator; _this.size = 0; return _this; } RBTree2.prototype.insert = function(data) { var ret = false; if (this._root === null) { this._root = new Node(data); ret = true; this.size++; } else { var head = new Node(void 0); var dir = false; var last = false; var gp = null; var ggp = head; var p = null; var node = this._root; ggp.right = this._root; while (true) { if (node === null) { node = new Node(data); p.set_child(dir, node); ret = true; this.size++; } else if (RBTree2.is_red(node.left) && RBTree2.is_red(node.right)) { node.red = true; node.left.red = false; node.right.red = false; } if (RBTree2.is_red(node) && RBTree2.is_red(p)) { var dir2 = ggp.right === gp; if (node === p.get_child(last)) { ggp.set_child(dir2, RBTree2.single_rotate(gp, !last)); } else { ggp.set_child(dir2, RBTree2.double_rotate(gp, !last)); } } var cmp = this._comparator(node.data, data); if (cmp === 0) { break; } last = dir; dir = cmp < 0; if (gp !== null) { ggp = gp; } gp = p; p = node; node = node.get_child(dir); } this._root = head.right; } this._root.red = false; return ret; }; RBTree2.prototype.remove = function(data) { if (this._root === null) { return false; } var head = new Node(void 0); var node = head; node.right = this._root; var p = null; var gp = null; var found = null; var dir = true; while (node.get_child(dir) !== null) { var last = dir; gp = p; p = node; node = node.get_child(dir); var cmp = this._comparator(data, node.data); dir = cmp > 0; if (cmp === 0) { found = node; } if (!RBTree2.is_red(node) && !RBTree2.is_red(node.get_child(dir))) { if (RBTree2.is_red(node.get_child(!dir))) { var sr = RBTree2.single_rotate(node, dir); p.set_child(last, sr); p = sr; } else if (!RBTree2.is_red(node.get_child(!dir))) { var sibling = p.get_child(!last); if (sibling !== null) { if (!RBTree2.is_red(sibling.get_child(!last)) && !RBTree2.is_red(sibling.get_child(last))) { p.red = false; sibling.red = true; node.red = true; } else { var dir2 = gp.right === p; if (RBTree2.is_red(sibling.get_child(last))) { gp.set_child(dir2, RBTree2.double_rotate(p, last)); } else if (RBTree2.is_red(sibling.get_child(!last))) { gp.set_child(dir2, RBTree2.single_rotate(p, last)); } var gpc = gp.get_child(dir2); gpc.red = true; node.red = true; gpc.left.red = false; gpc.right.red = false; } } } } } if (found !== null) { found.data = node.data; p.set_child(p.right === node, node.get_child(node.left === null)); this.size--; } this._root = head.right; if (this._root !== null) { this._root.red = false; } return found !== null; }; RBTree2.is_red = function(node) { return node !== null && node.red; }; RBTree2.single_rotate = function(root, dir) { var save = root.get_child(!dir); root.set_child(!dir, save.get_child(dir)); save.set_child(dir, root); root.red = true; save.red = false; return save; }; RBTree2.double_rotate = function(root, dir) { root.set_child(!dir, RBTree2.single_rotate(root.get_child(!dir), !dir)); return RBTree2.single_rotate(root, dir); }; return RBTree2; })(TreeBase); rbtree.RBTree = RBTree; return rbtree; } var hasRequiredRectangle; function requireRectangle() { if (hasRequiredRectangle) return rectangle; hasRequiredRectangle = 1; var __extends = rectangle && rectangle.__extends || (function() { var extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d, b) { d.__proto__ = b; } || function(d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(rectangle, "__esModule", { value: true }); var vpsc_1 = requireVpsc(); var rbtree_1 = requireRbtree(); function computeGroupBounds(g) { g.bounds = typeof g.leaves !== "undefined" ? g.leaves.reduce(function(r, c) { return c.bounds.union(r); }, Rectangle.empty()) : Rectangle.empty(); if (typeof g.groups !== "undefined") g.bounds = g.groups.reduce(function(r, c) { return computeGroupBounds(c).union(r); }, g.bounds); g.bounds = g.bounds.inflate(g.padding); return g.bounds; } rectangle.computeGroupBounds = computeGroupBounds; var Rectangle = (function() { function Rectangle2(x, X, y, Y) { this.x = x; this.X = X; this.y = y; this.Y = Y; } Rectangle2.empty = funct