all files / diagram/objects/ connector-bridging.js

100% Statements 263/263
96.97% Branches 128/132
100% Functions 21/21
100% Lines 263/263
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371     142296× 142296× 142296× 142296× 1898× 1898× 1898× 1898× 1898× 1898× 1898× 40971× 40971× 39072× 39072× 39072× 5178× 5178× 2048× 2356× 2356× 2356× 2356× 2356× 2356× 1168× 1168× 1168× 1168× 1168× 1168× 1168× 1168× 1168×   1168× 1168× 1168×     1159×   1168× 1168× 705× 705× 705× 705× 705× 705× 965× 965× 965× 965× 381× 381×     705× 381× 381× 381× 381× 381× 381× 381×     324× 324× 324× 324× 324×       463× 461× 461× 461× 461×     461× 461× 461× 461× 461× 461× 461× 461× 461×                 1898× 461×       461× 461× 461× 705× 76× 76× 76× 76× 76× 76× 76×     461× 461× 1090× 1090× 1090×       1166×       1166× 1166× 1166× 1166× 1166× 1166× 1166× 1166× 1166×   1242× 1242×   1166× 1166× 1166×     1160× 1160× 1095×   1160×         1166×         1166×   2336× 2336× 2336× 2336× 9551× 9551× 2336× 2336×     7215× 7215× 2322× 2322× 2322× 2322× 2322× 2322×     4893×     4893×   2336×   40970× 40970× 40970× 169564×     40970×   39072×     5179×   5179× 5179× 23861× 23861× 2346× 2356×     23861×     5179×   23861× 23861× 107841× 107841× 4704× 4704× 4704× 4704× 4704×     12×   12×     4692× 2346×   4692×   4704×       23861×   9408× 9408× 9408× 1509×     7899×   9408×   109472×   2356× 2356× 2356× 2356× 2356× 2356× 2356× 2356× 2356× 2356× 14960× 14960×   2356× 14960× 14960× 14960× 1194× 1194× 1194× 1194×   14960× 14960× 14960×   2356× 2356×       2356×   14960× 14960× 14960× 14960× 14960× 14960× 14376× 7494× 2356×   5138× 20×     6882× 6882× 1266×         584× 227× 227×       11091×   10576× 10576× 10576×   10079× 10079×     46813×        
define(["require", "exports", "../primitives/point", "../primitives/rect", "../utility/diagram-util", "../utility/constraints-util"], function (require, exports, point_1, rect_1, diagram_util_1, constraints_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ConnectorBridging = (function () {
        function ConnectorBridging() {
        }
        ConnectorBridging.prototype.updateBridging = function (conn, diagram) {
            var lastBridge = [];
            var bounds;
            conn.bridges = [];
            if (constraints_util_1.canBridge(conn, diagram)) {
                var points1 = this.getPoints(conn);
                bounds = rect_1.Rect.toBounds(points1);
                var bridgeSpacing = conn.bridgeSpace;
                var bgedir = diagram.bridgeDirection;
                var count = -1;
                var quads = diagram.connectors;
                for (var q = 0; q < quads.length; q++) {
                    var connector1 = quads[parseInt(q.toString(), 10)];
                    if (conn && connector1 && conn.id !== connector1.id && connector1.visible) {
                        var points2 = this.getPoints(connector1);
                        var bounds1 = rect_1.Rect.toBounds(points2);
                        if (this.intersectsRect(bounds, bounds1)) {
                            var intersectPts = this.intersect(points1, points2, false, bgedir, true);
                            if (intersectPts.length > 0) {
                                for (var i = 0; i < intersectPts.length; i++) {
                                    var fullLength = 0;
                                    var length_1 = 0;
                                    var segmentIndex = 0;
                                    var pointIndex = 0;
                                    var obj = this.getLengthAtFractionPoint(conn, intersectPts[parseInt(i.toString(), 10)]);
                                    if (obj.pointIndex !== -1) {
                                        length_1 = obj.lengthFractionIndex;
                                        fullLength = obj.fullLength;
                                        segmentIndex = obj.segmentIndex;
                                        pointIndex = obj.pointIndex;
                                        var stBridge = this.getPointAtLength((length_1 - (bridgeSpacing / 2)), points1);
                                        var enBridge = this.getPointAtLength((length_1 + (bridgeSpacing / 2)), points1);
                                        var fractLength = (length_1 - (bridgeSpacing / 2)) / fullLength;
                                        fractLength = (length_1 + (bridgeSpacing / 2)) / fullLength;
                                        if (this.isEmptyPoint(enBridge)) {
                                            enBridge = stBridge;
                                        }
                                        var end = void 0;
                                        var start = conn.sourcePoint;
                                        if (conn.type === 'Straight') {
                                            end = conn.targetPoint;
                                        }
                                        else {
                                            end = conn.intermediatePoints[parseInt(pointIndex.toString(), 10)];
                                        }
                                        var angle = this.angleCalculation(start, end);
                                        if (lastBridge.length) {
                                            var fixedPoint = conn.sourcePoint;
                                            var fix = Math.abs(this.lengthCalculation(fixedPoint, enBridge));
                                            var var1 = 0;
                                            var insertAt = -1;
                                            count = -1;
                                            for (var k = 0; k < lastBridge[parseInt(segmentIndex.toString(), 10)].bridges.length; k++) {
                                                count++;
                                                var arcSeg = lastBridge[parseInt(segmentIndex.toString(), 10)].bridges[parseInt(k.toString(), 10)];
                                                var1 = Math.abs(this.lengthCalculation(fixedPoint, arcSeg.endPoint));
                                                if (fix < var1) {
                                                    insertAt = count;
                                                    break;
                                                }
                                            }
                                            if (insertAt >= 0) {
                                                var paths = this.createSegment(stBridge, enBridge, angle, bgedir, pointIndex, conn, diagram);
                                                paths.target = connector1.id;
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridges.splice(insertAt, 0, paths);
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridges.join();
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridgeStartPoint.splice(insertAt, 0, stBridge);
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridgeStartPoint.join();
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].segmentIndex = segmentIndex;
                                            }
                                            else {
                                                var paths = this.createSegment(stBridge, enBridge, angle, bgedir, pointIndex, conn, diagram);
                                                paths.target = connector1.id;
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridges.push(paths);
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridgeStartPoint.push(stBridge);
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].segmentIndex = segmentIndex;
                                            }
                                        }
                                        else {
                                            if (!isNaN(stBridge.x) && !isNaN(stBridge.y) && !this.isEmptyPoint(enBridge)) {
                                                var bges = [];
                                                var bgept = [];
                                                var arcs = this.createSegment(stBridge, enBridge, angle, bgedir, pointIndex, conn, diagram);
                                                var bgseg = {
                                                    bridges: bges, bridgeStartPoint: bgept, segmentIndex: segmentIndex
                                                };
                                                arcs.target = connector1.id;
                                                var stPoints = [];
                                                var edPoints = [];
                                                stPoints.push(stBridge);
                                                edPoints.push(enBridge);
                                                lastBridge[parseInt(segmentIndex.toString(), 10)] = bgseg;
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridges.push(arcs);
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].bridgeStartPoint = stPoints;
                                                lastBridge[parseInt(segmentIndex.toString(), 10)].segmentIndex = segmentIndex;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (lastBridge.length !== 0) {
                    this.firstBridge(lastBridge, conn, bridgeSpacing);
                }
            }
        };
        ConnectorBridging.prototype.firstBridge = function (bridgeList, connector, bridgeSpacing) {
            for (var i = 0; i < bridgeList.length; i++) {
                var bridge = bridgeList[parseInt(i.toString(), 10)];
                for (var k = 1; k < bridge.bridges.length; k++) {
                    if (point_1.Point.findLength(bridge.bridges[parseInt(k.toString(), 10)].endPoint, bridge.bridges[k - 1].endPoint) < bridgeSpacing) {
                        bridge.bridges[k - 1].endPoint = bridge.bridges[parseInt(k.toString(), 10)].endPoint;
                        var subBridge = bridge.bridges[k - 1];
                        var arc = this.createBridgeSegment(subBridge.startPoint, subBridge.endPoint, subBridge.angle, bridgeSpacing, subBridge.sweep);
                        bridge.bridges[k - 1].path = arc;
                        bridge.bridges.splice(k, 1);
                        bridge.bridgeStartPoint.splice(k, 1);
                        k--;
                    }
                }
                var pre = connector.sourcePoint;
                for (var j = 0; j < bridge.bridges.length; j++) {
                    var subBridge = bridge.bridges[parseInt(j.toString(), 10)];
                    pre = subBridge.endPoint;
                    connector.bridges.push(subBridge);
                }
            }
        };
        ConnectorBridging.prototype.createSegment = function (st, end, angle, direction, index, conn, diagram) {
            var path = {
                angle: 0, endPoint: { x: 0, y: 0 }, target: '', path: '',
                segmentPointIndex: -1, startPoint: { x: 0, y: 0 }, sweep: 1, rendered: false
            };
            var sweep = this.sweepDirection(angle, direction, conn, diagram);
            var arc = this.createBridgeSegment(st, end, angle, conn.bridgeSpace, sweep);
            path.path = arc;
            path.startPoint = st;
            path.endPoint = end;
            path.angle = angle;
            path.segmentPointIndex = index;
            path.sweep = sweep;
            return path;
        };
        ConnectorBridging.prototype.createBridgeSegment = function (startPt, endPt, angle, bridgeSpace, sweep) {
            var path = 'A ' + bridgeSpace / 2 + ' ' + bridgeSpace / 2 + ' ' + angle + ' , 1 ' + sweep + ' ' + endPt.x + ',' + endPt.y;
            return path;
        };
        ConnectorBridging.prototype.sweepDirection = function (angle, bridgeDirection, connector, diagram) {
            var angle1 = Math.abs(angle);
            var sweep;
            switch (bridgeDirection) {
                case 'Top':
                case 'Bottom':
                    sweep = 1;
                    if (angle1 >= 0 && angle1 <= 90) {
                        sweep = 0;
                    }
                    break;
                case 'Left':
                case 'Right':
                    sweep = 1;
                    Eif (angle < 0 && angle >= -180) {
                        sweep = 0;
                    }
                    break;
            }
            if (bridgeDirection === 'Right' || bridgeDirection === 'Bottom') {
                if (sweep === 0) {
                    sweep = 1;
                }
                else {
                    sweep = 0;
                }
            }
            return sweep;
        };
        ConnectorBridging.prototype.getPointAtLength = function (length, pts) {
            var run = 0;
            var pre;
            var found = { x: 0, y: 0 };
            for (var i = 0; i < pts.length; i++) {
                var pt = pts[parseInt(i.toString(), 10)];
                if (!pre) {
                    pre = pt;
                    continue;
                }
                else {
                    var l = this.lengthCalculation(pre, pt);
                    if (run + l > length) {
                        var r = length - run;
                        var deg = point_1.Point.findAngle(pre, pt);
                        var x = r * Math.cos(deg * Math.PI / 180);
                        var y = r * Math.sin(deg * Math.PI / 180);
                        found = { x: pre.x + x, y: pre.y + y };
                        break;
                    }
                    else {
                        run += l;
                    }
                }
                pre = pt;
            }
            return found;
        };
        ConnectorBridging.prototype.getPoints = function (connector) {
            var points = [];
            Eif (connector.intermediatePoints && (connector.type === 'Straight' || connector.type === 'Orthogonal')) {
                for (var j = 0; j < connector.intermediatePoints.length; j++) {
                    points.push(connector.intermediatePoints[parseInt(j.toString(), 10)]);
                }
            }
            return points;
        };
        ConnectorBridging.prototype.intersectsRect = function (rect1, rect2) {
            return ((((rect2.x < (rect1.x + rect1.width)) && (rect1.x < (rect2.x + rect2.width)))
                && (rect2.y < (rect1.y + rect1.height))) && (rect1.y < (rect2.y + rect2.height)));
        };
        ConnectorBridging.prototype.intersect = function (points1, points2, self, bridgeDirection, zOrder) {
            if (self && points2.length >= 2) {
                points2.splice(0, 1);
                points2.splice(0, 1);
            }
            var points = [];
            for (var i = 0; i < points1.length - 1; i++) {
                var pt = this.inter1(points1[parseInt(i.toString(), 10)], points1[i + 1], points2, zOrder, bridgeDirection);
                if (pt.length > 0) {
                    for (var k = 0; k < pt.length; k++) {
                        points.push(pt[parseInt(k.toString(), 10)]);
                    }
                }
                if (self && points2.length >= 1) {
                    points2.splice(0, 1);
                }
            }
            return points;
        };
        ConnectorBridging.prototype.inter1 = function (startPt, endPt, pts, zOrder, bridgeDirection) {
            var points1 = [];
            for (var i = 0; i < pts.length - 1; i++) {
                var point = diagram_util_1.intersect2(startPt, endPt, pts[parseInt(i.toString(), 10)], pts[i + 1]);
                if (!this.isEmptyPoint(point)) {
                    var angle = this.angleCalculation(startPt, endPt);
                    var angle1 = this.angleCalculation(pts[parseInt(i.toString(), 10)], pts[i + 1]);
                    angle = this.checkForHorizontalLine(angle);
                    angle1 = this.checkForHorizontalLine(angle1);
                    switch (bridgeDirection) {
                        case 'Left':
                        case 'Right':
                            if (angle > angle1) {
                                points1.push(point);
                            }
                            break;
                        case 'Top':
                        case 'Bottom':
                            if (angle < angle1) {
                                points1.push(point);
                            }
                            break;
                    }
                    if (angle === angle1 && zOrder) {
                        points1.push(point);
                    }
                }
            }
            return points1;
        };
        ConnectorBridging.prototype.checkForHorizontalLine = function (angle) {
            var temp = 0;
            var roundedAngle = Math.abs(angle);
            if (roundedAngle > 90) {
                temp = 180 - roundedAngle;
            }
            else {
                temp = roundedAngle;
            }
            return temp;
        };
        ConnectorBridging.prototype.isEmptyPoint = function (point) {
            return point.x === 0 && point.y === 0;
        };
        ConnectorBridging.prototype.getLengthAtFractionPoint = function (connector, pointAt) {
            var confirm = 100;
            var pointIndex = -1;
            var fullLength = 0;
            var segmentIndex = -1;
            var count = 0;
            var lengthAtFractionPt = 0;
            var pt1 = connector.sourcePoint;
            var previouspt2 = pt1;
            var points = [];
            for (var i = 0; i < connector.intermediatePoints.length; i++) {
                var point2 = connector.intermediatePoints[parseInt(i.toString(), 10)];
                points.push(point2);
            }
            for (var j = 0; j < points.length; j++) {
                var pt2 = points[parseInt(j.toString(), 10)];
                var suspect = this.getSlope(pt2, pt1, pointAt, connector);
                if (suspect < confirm) {
                    confirm = suspect;
                    lengthAtFractionPt = fullLength + this.lengthCalculation(pointAt, previouspt2);
                    segmentIndex = count;
                    pointIndex = j;
                }
                fullLength += point_1.Point.findLength(pt2, pt1);
                pt1 = pt2;
                previouspt2 = pt2;
            }
            count++;
            var lengthFraction = {
                lengthFractionIndex: lengthAtFractionPt, fullLength: fullLength,
                segmentIndex: segmentIndex, pointIndex: pointIndex
            };
            return lengthFraction;
        };
        ConnectorBridging.prototype.getSlope = function (startPt, endPt, point, connector) {
            var three = 3.0;
            var delX = Math.abs(startPt.x - endPt.x);
            var delY = Math.abs(startPt.y - endPt.y);
            var lhs = ((point.y - startPt.y) / (endPt.y - startPt.y));
            var rhs = ((point.x - startPt.x) / (endPt.x - startPt.x));
            if (!isFinite(lhs) || !isFinite(rhs) || isNaN(lhs) || isNaN(rhs)) {
                if (startPt.x === endPt.x) {
                    if (startPt.y === endPt.y) {
                        return 10000;
                    }
                    else if (((startPt.y > point.y) && (point.y > endPt.y)) || ((startPt.y < point.y) && (point.y < endPt.y))) {
                        return Math.abs(startPt.x - point.x);
                    }
                }
                else Eif (startPt.y === endPt.y) {
                    if (((startPt.x > point.x) && (point.x > endPt.x)) || ((startPt.x < point.x) && (point.x < endPt.x))) {
                        return Math.abs(startPt.y - point.y);
                    }
                }
            }
            else {
                if ((startPt.x >= point.x && point.x >= endPt.x) || (startPt.x <= point.x && point.x <= endPt.x) || delX < three) {
                    Eif ((startPt.y >= point.y && point.y >= endPt.y) || (startPt.y <= point.y && point.y <= endPt.y) || delY < three) {
                        return Math.abs(lhs - rhs);
                    }
                }
            }
            return 10000;
        };
        ConnectorBridging.prototype.angleCalculation = function (startPt, endPt) {
            var xDiff = startPt.x - endPt.x;
            var yDiff = startPt.y - endPt.y;
            return Math.atan2(yDiff, xDiff) * (180 / Math.PI);
        };
        ConnectorBridging.prototype.lengthCalculation = function (startPt, endPt) {
            var len = Math.sqrt(((startPt.x - endPt.x) * (startPt.x - endPt.x)) + ((startPt.y - endPt.y) * (startPt.y - endPt.y)));
            return len;
        };
        ConnectorBridging.prototype.destroy = function () {
        };
        ConnectorBridging.prototype.getModuleName = function () {
            return 'Bridging';
        };
        return ConnectorBridging;
    }());
    exports.ConnectorBridging = ConnectorBridging;
});