all files / diagram/interaction/ line-distribution.js

99% Statements 395/399
92.42% Branches 195/211
100% Functions 25/25
98.99% Lines 394/398
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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581       46813×   26× 26× 23× 23×   22×   20× 14× 14×   23× 23× 22× 375× 375×         1172× 1172× 1172× 1172× 1172× 626× 626× 196× 196×     430× 430×       546× 546× 174× 174×     372× 372×     1172×   23×   23× 23× 23× 23× 23× 23× 23× 23× 23× 421× 421× 421× 430× 430× 1602× 1602× 1593×       421× 421× 1172×     1172×   421× 421× 421× 182× 509× 509× 175×         239× 663× 663× 212×       421× 387× 387× 387×   387× 387× 2304× 2304× 387× 387×     387×   421×   23× 23× 23× 387× 387× 387× 387× 387× 387× 387× 387× 387× 387× 387× 260× 260×     127× 127× 127× 127×     387× 514× 514× 514× 387× 387× 387×     514× 514× 514× 387× 387× 187×     514× 514× 387× 387× 200×     514× 514× 136× 136× 136× 38× 24× 24×     14×       514× 514× 514× 514× 514× 514× 514× 514× 514×   514× 514×   514× 514×     514× 21× 21× 24× 24×       493× 493× 321× 321×     514× 61× 61×     61× 61× 61× 178× 178× 192× 192× 192× 192×   192× 192× 1818× 1818× 192× 192×     192×           23× 421× 421× 421× 1172× 421×   751× 424× 212×     327× 327× 167×     1172×   421× 421× 1593× 1593× 1593×     421× 410×       23× 387× 387× 356× 184× 741× 741× 741×     172× 172× 789× 789× 789×             410× 410×   410× 410× 410× 410× 410× 410× 1152× 1152× 1152× 1152× 1152× 410×       410×     1152× 1152× 1152× 1152×   410× 410× 410×           1172× 626× 196×   430×   546× 174×   372×   800× 414× 145×   269×   386× 136×   250×   1542× 1542× 1028×   1542× 387× 387× 483× 373×   483×     387× 387×   96×     1542×   842× 182×   842× 182×   842× 239×   842× 239×     375× 174× 174×   375× 375× 375× 375× 375×   750× 750× 750× 61×   750× 842× 842× 842× 842× 842× 842× 842× 842× 421×     421×     842× 842× 842× 842× 842× 842× 842×     61× 61× 138× 99× 99× 99× 99× 99× 99× 60×       39×                 61×   301× 301× 303×     301× 29× 29× 29× 29× 227× 737× 29×   737× 29×       29× 29× 14× 106× 24×         15× 121× 25×       29× 29× 49× 250× 135× 135×       29× 29×   29× 116× 116× 116× 116× 331× 331× 331× 228× 228× 228× 13× 13×               228× 228× 228× 228× 228× 228× 228× 228× 50916×   228×   228× 228× 47389× 47389×       13×     215×   3565×   579× 6539×       579×        
define(["require", "exports", "../objects/connector", "../utility/diagram-util", "../utility/base-util", "../primitives/point", "../objects/port", "../primitives/size"], function (require, exports, connector_1, diagram_util_1, base_util_1, point_1, port_1, size_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var LineDistribution = (function () {
        function LineDistribution() {
        }
        LineDistribution.prototype.destroy = function () {
        };
        LineDistribution.prototype.getModuleName = function () {
            return 'LineDistribution';
        };
        LineDistribution.prototype.initLineDistribution = function (graph, diagram) {
            var srcDirection = 'Bottom';
            if (diagram.layout.connectionPointOrigin === 'DifferentPoint' || diagram.layout.enableRouting) {
                var tarDirection = 'Top';
                if (graph.orientation === 'BottomToTop') {
                    srcDirection = 'Top';
                    tarDirection = 'Bottom';
                }
                else if (graph.orientation === 'RightToLeft') {
                    srcDirection = 'Left';
                    tarDirection = 'Right';
                }
                else if (graph.orientation === 'LeftToRight') {
                    srcDirection = 'Right';
                    tarDirection = 'Left';
                }
                var graphnodes = diagram.nodes;
                if (graphnodes.length > 0) {
                    for (var i = 0; i < graphnodes.length; i++) {
                        var node = diagram.nameTable[graphnodes[parseInt(i.toString(), 10)].id];
                        this.addDynamicPortandDistrrbuteLine(graph, node, srcDirection, tarDirection, diagram);
                    }
                }
            }
        };
        LineDistribution.prototype.ObstacleSegment = function (options) {
            options.direction = diagram_util_1.getConnectorDirection(options.startpt, options.endpt);
            options.distance = point_1.Point.findLength(options.startpt, options.endpt);
            options.orientation = options.direction === 'Left' || options.direction === 'Right' ? 'horizontal' : 'vertical';
            options.id = options.id;
            if (options.orientation === 'horizontal') {
                options.coord = options.startpt.y;
                if (options.direction === 'Left') {
                    options.start = options.endpt.x;
                    options.end = options.startpt.x;
                }
                else {
                    options.start = options.startpt.x;
                    options.end = options.endpt.x;
                }
            }
            else {
                options.coord = options.startpt.x;
                if (options.direction === 'Top') {
                    options.start = options.endpt.y;
                    options.end = options.startpt.y;
                }
                else {
                    options.start = options.startpt.y;
                    options.end = options.endpt.y;
                }
            }
            return options;
        };
        LineDistribution.prototype.distributeLines = function (layout, diagram) {
            var isHorizontal = layout.orientation === 'LeftToRight'
                || layout.orientation === 'RightToLeft';
            var inversespacing = !isHorizontal ? layout.verticalSpacing : layout.horizontalSpacing;
            var srcdecoratorSize = 8.0;
            var obstacleCollection = 'obstaclePointCollection';
            var tardecoratorSize = 10.0;
            var avaibaleSpace = inversespacing - srcdecoratorSize - tardecoratorSize;
            var graph = [];
            var connectorObstacles = [];
            var globalConnectors = diagram.connectors;
            for (var i = 0; i < globalConnectors.length; i++) {
                var connector = globalConnectors[parseInt(i.toString(), 10)];
                var pts = [];
                for (var key = 0; key < connector.segments.length; key++) {
                    var seg = connector.segments[parseInt(key.toString(), 10)];
                    for (var k = 0; k < seg.points.length; k++) {
                        var pt = seg.points[parseInt(k.toString(), 10)];
                        if (pts.length === 0 || !(point_1.Point.equals(pt, pts[pts.length - 1]))) {
                            pts.push(pt);
                        }
                    }
                }
                var obssegments = [];
                for (var j = 1; j < pts.length; j++) {
                    var obstacle = this.ObstacleSegment({
                        startpt: pts[j - 1], endpt: pts[parseInt(j.toString(), 10)], id: connector.id
                    });
                    obssegments.push(obstacle);
                }
                var connectorObstacle = { wrapper: connector, segments: obssegments };
                var segments = [];
                if (!isHorizontal) {
                    for (var key = 0; key < connectorObstacle.segments.length; key++) {
                        var obstacle = connectorObstacle.segments[parseInt(key.toString(), 10)];
                        if (obstacle.orientation === 'horizontal') {
                            segments.push(obstacle);
                        }
                    }
                }
                else {
                    for (var key = 0; key < connectorObstacle.segments.length; key++) {
                        var obstacle = connectorObstacle.segments[parseInt(key.toString(), 10)];
                        if (obstacle.orientation === 'vertical') {
                            segments.push(obstacle);
                        }
                    }
                }
                for (var j = 0; j < segments.length; j++) {
                    var obstacleSegment = segments[parseInt(j.toString(), 10)];
                    Eif (!this.containsValue(graph, obstacleSegment.coord)) {
                        graph.push({ key: obstacleSegment.coord, value: [] });
                    }
                    var index = void 0;
                    for (var k = 0; k < graph.length; k++) {
                        var key = graph[parseInt(k.toString(), 10)].key;
                        if (Number(key) === obstacleSegment.coord) {
                            index = k;
                            break;
                        }
                    }
                    graph[parseInt(index.toString(), 10)].value.push(obstacleSegment);
                }
                connectorObstacles.push(connectorObstacle);
            }
            this.sortConnectors(graph, diagram);
            var modifiedgrap = [];
            for (var m = 0; m < graph.length; m++) {
                var row = graph[parseInt(m.toString(), 10)];
                var sortedrow = row.value;
                sortedrow.sort();
                var groupby = void 0;
                groupby = [];
                var index = 0;
                var maxEnd = Number.MIN_VALUE;
                groupby.push([]);
                for (var n = 0; n < sortedrow.length; n++) {
                    var obstacleSegment = sortedrow[parseInt(n.toString(), 10)];
                    if (!(groupby[parseInt(index.toString(), 10)].length > 0) || maxEnd >= obstacleSegment.start) {
                        groupby[parseInt(index.toString(), 10)].push(obstacleSegment);
                        maxEnd = Math.max(maxEnd, groupby[parseInt(index.toString(), 10)][groupby[parseInt(index.toString(), 10)].length - 1].end);
                    }
                    else {
                        index++;
                        groupby.push([]);
                        groupby[parseInt(index.toString(), 10)].push(obstacleSegment);
                        maxEnd = groupby[parseInt(index.toString(), 10)][groupby[parseInt(index.toString(), 10)].length - 1].end;
                    }
                }
                for (var n = 0; n < groupby.length; n++) {
                    var group = groupby[parseInt(n.toString(), 10)];
                    var sortedGroup = [];
                    for (var j = 0; j < group.length; j++) {
                        var e = group[parseInt(j.toString(), 10)];
                        Eif (e.start) {
                            sortedGroup.push(e);
                        }
                    }
                    var comparingDir = isHorizontal ? 'Bottom' : 'Right';
                    var directed = [];
                    for (var j = 0; j < sortedGroup.length; j++) {
                        var e = sortedGroup[parseInt(j.toString(), 10)];
                        if (e.direction === comparingDir) {
                            directed.push(e);
                        }
                    }
                    var reversedirected = [];
                    for (var j = 0; j < sortedGroup.length; j++) {
                        var e = sortedGroup[parseInt(j.toString(), 10)];
                        if (e.direction !== comparingDir) {
                            reversedirected.push(e);
                        }
                    }
                    var mutual = [];
                    if (directed.length > 0) {
                        var temp = directed[0].start;
                        var j = 0;
                        while (j < reversedirected.length) {
                            if (reversedirected[parseInt(j.toString(), 10)].end > temp) {
                                mutual.push(reversedirected[parseInt(j.toString(), 10)]);
                                reversedirected.splice(j, 1);
                            }
                            else {
                                j++;
                            }
                        }
                    }
                    var mutualRow = [];
                    mutualRow = this.updateSegmentRow(mutual, mutualRow);
                    var directedRow = [];
                    directedRow = [];
                    directedRow = this.updateSegmentRow(reversedirected, directedRow);
                    directed.reverse();
                    directedRow = this.updateSegmentRow(directed, directedRow);
                    Eif (!(mutualRow[mutualRow.length - 1].length > 0)) {
                        mutualRow.splice(mutualRow.length - 1, 1);
                    }
                    Eif (!(directedRow[directedRow.length - 1].length > 0)) {
                        directedRow.splice(directedRow.length - 1, 1);
                    }
                    var subrow = [];
                    var descAdding = mutual.length > 0 && (sortedGroup[sortedGroup.length - 1].direction
                        === mutual[mutual.length - 1].direction
                        || sortedGroup[0].direction === mutual[0].direction);
                    if (descAdding) {
                        subrow = directedRow;
                        for (var p = 0; p < mutualRow.length; p++) {
                            var obj = mutualRow[parseInt(p.toString(), 10)];
                            subrow[subrow.length] = obj;
                        }
                    }
                    else {
                        subrow = mutualRow;
                        for (var p = 0; p < directedRow.length; p++) {
                            var obj = directedRow[parseInt(p.toString(), 10)];
                            subrow[subrow.length] = obj;
                        }
                    }
                    if (subrow.length > 1) {
                        var directionModifier = 1;
                        if (layout.orientation === 'BottomToTop'
                            || layout.orientation === 'RightToLeft') {
                            directionModifier = -1;
                        }
                        var startCoord = row.key - (directionModifier * avaibaleSpace / 2.0);
                        var diff = avaibaleSpace / subrow.length;
                        for (var i = 0; i < subrow.length; i++) {
                            var newcoord = startCoord + (i * diff * directionModifier);
                            for (var p = 0; p < subrow[parseInt(i.toString(), 10)].length; p++) {
                                var obstacleSegment = subrow[parseInt(i.toString(), 10)][parseInt(p.toString(), 10)];
                                obstacleSegment.coord = newcoord;
                                Eif (!this.containsValue(modifiedgrap, obstacleSegment.coord)) {
                                    modifiedgrap.push({ key: obstacleSegment.coord, value: [] });
                                }
                                var index_1 = void 0;
                                for (var k = 0; k < modifiedgrap.length; k++) {
                                    var keyCheck = modifiedgrap[parseInt(k.toString(), 10)].key;
                                    if (keyCheck === obstacleSegment.coord) {
                                        index_1 = k;
                                        break;
                                    }
                                }
                                modifiedgrap[parseInt(index_1.toString(), 10)].value.push(obstacleSegment);
                            }
                        }
                    }
                }
            }
            for (var m = 0; m < connectorObstacles.length; m++) {
                var connectorObstacle = connectorObstacles[parseInt(m.toString(), 10)];
                var pts = [];
                for (var i = 0; i < connectorObstacle.segments.length; i++) {
                    if (i === 0) {
                        pts.push(this.getObstacleStartPoint(connectorObstacle.segments[parseInt(i.toString(), 10)]));
                    }
                    else if (isHorizontal) {
                        if (connectorObstacle.segments[parseInt(i.toString(), 10)].orientation === 'vertical') {
                            pts[pts.length - 1] = this.getObstacleStartPoint(connectorObstacle.segments[parseInt(i.toString(), 10)]);
                        }
                    }
                    else Eif (!isHorizontal) {
                        if (connectorObstacle.segments[parseInt(i.toString(), 10)].orientation === 'horizontal') {
                            pts[pts.length - 1] = this.getObstacleStartPoint(connectorObstacle.segments[parseInt(i.toString(), 10)]);
                        }
                    }
                    pts.push(this.getObstacleEndPoint(connectorObstacle.segments[parseInt(i.toString(), 10)]));
                }
                connectorObstacle.wrapper["" + obstacleCollection] = [];
                for (var j = 0; j < pts.length; j++) {
                    var point = pts[parseInt(j.toString(), 10)];
                    Eif (j === 0 || (j > 0 && !(point_1.Point.equals(point, pts[j - 1])))) {
                        connectorObstacle.wrapper["" + obstacleCollection].push(this.getPointvalue(point.x, point.y));
                    }
                }
                if (connectorObstacle.wrapper.type === 'Orthogonal') {
                    this.resetConnectorPoints(connectorObstacle.wrapper, diagram);
                }
            }
        };
        LineDistribution.prototype.sortConnectors = function (graph, diagram) {
            for (var i = 0; i < graph.length; i++) {
                for (var j = 0; j < graph[parseInt(i.toString(), 10)].value.length; j++) {
                    if (graph[parseInt(i.toString(), 10)].value.length > 1) {
                        if (diagram.layout.orientation === 'LeftToRight' || diagram.layout.orientation === 'RightToLeft') {
                            graph[parseInt(i.toString(), 10)].value.sort(function (a, b) {
                                var connectorA = diagram.nameTable["" + a.id];
                                var connectorB = diagram.nameTable["" + b.id];
                                return connectorA.targetPoint.y - connectorB.targetPoint.y;
                            });
                        }
                        else Eif (diagram.layout.orientation === 'TopToBottom' || diagram.layout.orientation === 'BottomToTop') {
                            graph[parseInt(i.toString(), 10)].value.sort(function (a, b) {
                                var connectorA = diagram.nameTable["" + a.id];
                                var connectorB = diagram.nameTable["" + b.id];
                                return connectorA.targetPoint.x - connectorB.targetPoint.x;
                            });
                        }
                    }
                }
            }
        };
        LineDistribution.prototype.resetConnectorPoints = function (edge, diagram) {
            var obstacleCollection = 'obstaclePointCollection';
            Eif (edge.segments[0].points
                && edge.segments[0].points.length > 0 && edge["" + obstacleCollection]) {
                var connector = edge;
                connector.sourcePoint = edge["" + obstacleCollection][0];
                connector.targetPoint = edge["" + obstacleCollection][edge["" + obstacleCollection].length - 1];
                var segments = void 0;
                segments = [];
                for (var i = 0; i < edge["" + obstacleCollection].length - 1; i++) {
                    var point1 = edge["" + obstacleCollection][parseInt(i.toString(), 10)];
                    var point2 = edge["" + obstacleCollection][i + 1];
                    var length_1 = diagram_util_1.findDistance(point1, point2);
                    var direction = diagram_util_1.getConnectorDirection(point1, point2);
                    if (i === edge["" + obstacleCollection].length - 2) {
                        Eif ((diagram.layout.orientation === 'RightToLeft' && direction === 'Left')
                            || (diagram.layout.orientation === 'LeftToRight' && direction === 'Right')
                            || (diagram.layout.orientation === 'TopToBottom' && direction === 'Bottom')
                            || (diagram.layout.orientation === 'BottomToTop' && direction === 'Top')) {
                            length_1 = length_1 / 2;
                        }
                    }
                    var tempSegment = new connector_1.OrthogonalSegment(edge, 'segments', { type: 'Orthogonal' }, true);
                    tempSegment.length = length_1;
                    tempSegment.direction = direction;
                    segments.push(tempSegment);
                }
                connector.segments = segments;
                connector.type = 'Orthogonal';
                diagram.connectorPropertyChange(connector, {}, {
                    type: 'Orthogonal',
                    segments: connector.segments
                });
            }
        };
        LineDistribution.prototype.getObstacleEndPoint = function (segment) {
            if (segment.orientation === 'horizontal') {
                if (segment.direction === 'Left') {
                    return this.getPointvalue(segment.start, segment.coord);
                }
                return this.getPointvalue(segment.end, segment.coord);
            }
            if (segment.direction === 'Top') {
                return this.getPointvalue(segment.coord, segment.start);
            }
            return this.getPointvalue(segment.coord, segment.end);
        };
        LineDistribution.prototype.getObstacleStartPoint = function (segment) {
            if (segment.orientation === 'horizontal') {
                if (segment.direction === 'Left') {
                    return this.getPointvalue(segment.end, segment.coord);
                }
                return this.getPointvalue(segment.start, segment.coord);
            }
            if (segment.direction === 'Top') {
                return this.getPointvalue(segment.coord, segment.end);
            }
            return this.getPointvalue(segment.coord, segment.start);
        };
        LineDistribution.prototype.updateSegmentRow = function (obstacleSegments, segmentRow) {
            var k = 0;
            if (!(segmentRow.length > 0)) {
                segmentRow[0] = [];
            }
            for (var i = 0; i < obstacleSegments.length; i++) {
                var obstacleSegment = obstacleSegments[parseInt(i.toString(), 10)];
                while (k < segmentRow.length) {
                    if (k === segmentRow.length - 1) {
                        segmentRow[k + 1] = [];
                    }
                    if (!(segmentRow[parseInt(k.toString(), 10)].length > 0)
                        || segmentRow[parseInt(k.toString(), 10)][segmentRow[parseInt(k.toString(), 10)].length - 1].end
                            < obstacleSegment.start) {
                        segmentRow[parseInt(k.toString(), 10)].push(obstacleSegment);
                        break;
                    }
                    k++;
                }
            }
            return segmentRow;
        };
        LineDistribution.prototype.portOffsetCalculation = function (port, length, direction, i) {
            if (direction === 'Top') {
                port.offset = { x: (i + 1) * (1.0 / (length + 1)), y: 0 };
            }
            if (direction === 'Bottom') {
                port.offset = { x: (i + 1) * (1.0 / (length + 1)), y: 1 };
            }
            if (direction === 'Left') {
                port.offset = { x: 0, y: (i + 1) * (1.0 / (length + 1)) };
            }
            if (direction === 'Right') {
                port.offset = { x: 1, y: (i + 1) * (1.0 / (length + 1)) };
            }
        };
        LineDistribution.prototype.addDynamicPortandDistrrbuteLine = function (layout, node, sourceDirection, targetDirection, diagram) {
            if ((node.ports && node.ports.length > 0)) {
                var port = node.ports;
                diagram.removePorts(node, port);
            }
            var existingPorts = node.ports;
            var outConnectors = node.outEdges;
            var inConnectors = node.inEdges;
            this.initPort(outConnectors, diagram, node, sourceDirection, false);
            this.initPort(inConnectors, diagram, node, targetDirection, true);
        };
        LineDistribution.prototype.initPort = function (connectors, diagram, node, targetDirection, inConnectors) {
            var obstacleCollection = 'obstaclePointCollection';
            var objects = connectors;
            if (connectors.length > 1 && inConnectors) {
                connectors = this.sortObjects(objects, inConnectors, diagram);
            }
            for (var i = 0; i <= connectors.length - 1; i++) {
                var internalConnector = diagram.nameTable[connectors[parseInt(i.toString(), 10)]];
                internalConnector["" + obstacleCollection] = [];
                var newPort = diagram_util_1.findPort(node, inConnectors ? internalConnector.targetPortID : internalConnector.sourcePortID);
                var direction = targetDirection;
                Eif (newPort === undefined) {
                    newPort = new port_1.PointPort(node, 'ports', '', true);
                    newPort.id = base_util_1.randomId() + '_LineDistribution';
                    if (inConnectors) {
                        internalConnector.targetPortID = newPort.id;
                    }
                    else {
                        internalConnector.sourcePortID = newPort.id;
                    }
                }
                this.portOffsetCalculation(newPort, connectors.length, direction, i);
                node.ports.push(newPort);
                var portWrapper = node.initPortWrapper(node.ports[node.ports.length - 1]);
                node.wrapper.children.push(portWrapper);
                node.wrapper.measure(new size_1.Size(node.width, node.height), node.id);
                node.wrapper.arrange(node.wrapper.desiredSize);
                diagram.connectorPropertyChange(internalConnector, inConnectors ? { targetPortID: '' } : { sourcePortID: '' }, inConnectors ? { targetPortID: newPort.id } : { sourcePortID: newPort.id });
            }
        };
        LineDistribution.prototype.sortObjects = function (objects, inConnectors, diagram) {
            var temp;
            for (var i = 0; i < objects.length; i++) {
                for (var j = i + 1; j < objects.length; j++) {
                    var internalConnector = diagram.nameTable[objects[parseInt(i.toString(), 10)]];
                    var internalConnector2 = diagram.nameTable[objects[parseInt(j.toString(), 10)]];
                    Eif (inConnectors) {
                        var childNode = diagram.nameTable[internalConnector.sourceID];
                        var childNode2 = diagram.nameTable[internalConnector2.sourceID];
                        if (diagram.layout.orientation === 'LeftToRight' || diagram.layout.orientation === 'RightToLeft') {
                            if (childNode.offsetY > childNode2.offsetY) {
                                temp = objects[parseInt(i.toString(), 10)];
                                objects[parseInt(i.toString(), 10)] = objects[parseInt(j.toString(), 10)];
                                objects[parseInt(j.toString(), 10)] = temp;
                            }
                        }
                        else {
                            Iif (childNode.offsetX > childNode2.offsetX) {
                                temp = objects[parseInt(i.toString(), 10)];
                                objects[parseInt(i.toString(), 10)] = objects[parseInt(j.toString(), 10)];
                                objects[parseInt(j.toString(), 10)] = temp;
                            }
                        }
                    }
                }
            }
            return objects;
        };
        LineDistribution.prototype.resetConnectorSegments = function (connector) {
            var segements = connector.segments;
            for (var i = segements.length; i > 0; i--) {
                segements.splice(i - 1, 1);
            }
        };
        LineDistribution.prototype.resetRoutingSegments = function (connector, diagram, points) {
            if (connector['levelSkip']) {
                var sourceLevel = void 0;
                var targetLevel = void 0;
                var collection = diagram.layout.ranks.reverse();
                for (var i = 0; i < collection.length; i++) {
                    for (var j = 0; j < collection[parseInt(i.toString(), 10)].length; j++) {
                        if (connector.sourceID === collection[parseInt(i.toString(), 10)][parseInt(j.toString(), 10)].id) {
                            sourceLevel = i;
                        }
                        if (connector.targetID === collection[parseInt(i.toString(), 10)][parseInt(j.toString(), 10)].id) {
                            targetLevel = i;
                        }
                    }
                }
                var overlappCollection = [];
                if (sourceLevel < targetLevel) {
                    for (var i = 0; i < collection.length; i++) {
                        if (i > sourceLevel && i < targetLevel) {
                            overlappCollection.push(collection[parseInt(i.toString(), 10)]);
                        }
                    }
                }
                else {
                    for (var i = 0; i < collection.length; i++) {
                        if (i < sourceLevel && i > targetLevel) {
                            overlappCollection.push(collection[parseInt(i.toString(), 10)]);
                        }
                    }
                }
                var overLapNodesCollection = [];
                for (var i = 0; i < overlappCollection.length; i++) {
                    for (var j = 0; j < overlappCollection[parseInt(i.toString(), 10)].length; j++) {
                        if (overlappCollection[parseInt(i.toString(), 10)][parseInt(j.toString(), 10)].edges === undefined) {
                            var node = diagram.nameTable[overlappCollection[parseInt(i.toString(), 10)][parseInt(j.toString(), 10)].id];
                            overLapNodesCollection.push(node);
                        }
                    }
                }
                var isInsideBounds = false;
                if (!diagram.routingConnectors) {
                    diagram.routingConnectors = [];
                }
                overlapping: for (var count = 0; count < overLapNodesCollection.length; count++) {
                    var bounds = overLapNodesCollection[parseInt(count.toString(), 10)].wrapper.bounds;
                    for (var i = 0; i < connector.segments.length; i++) {
                        var points_1 = connector.segments[parseInt(i.toString(), 10)].points;
                        for (var j = 0; j < points_1.length; j++) {
                            var lineStart = points_1[parseInt(j.toString(), 10)];
                            var lineEnd = points_1[j + 1];
                            if (lineEnd) {
                                var connectorPoints = this.pointsAlongLine(lineStart, lineEnd);
                                isInsideBounds = this.pointInsideBounds(connectorPoints, bounds);
                                if (isInsideBounds) {
                                    diagram.routingConnectors.push(connector);
                                    break overlapping;
                                }
                            }
                        }
                    }
                }
            }
        };
        LineDistribution.prototype.pointsAlongLine = function (start, end) {
            var granularity = 1;
            var dx = end.x - start.x;
            var dy = end.y - start.y;
            var length = Math.sqrt(dx * dx + dy * dy);
            var stepX = (dx / length) * granularity;
            var stepY = (dy / length) * granularity;
            var points = [];
            for (var i = 0; i <= length; i += granularity) {
                points.push({ x: start.x + stepX * i, y: start.y + stepY * i });
            }
            return points;
        };
        LineDistribution.prototype.pointInsideBounds = function (points, bounds) {
            var padding = 10;
            for (var _i = 0, points_2 = points; _i < points_2.length; _i++) {
                var point = points_2[_i];
                if (bounds.right > point.x &&
                    bounds.left < point.x &&
                    bounds.top < point.y &&
                    bounds.bottom > point.y) {
                    return true;
                }
            }
            return false;
        };
        LineDistribution.prototype.getPointvalue = function (x, y) {
            return { 'x': Number(x) || 0, 'y': Number(y) || 0 };
        };
        LineDistribution.prototype.containsValue = function (list, keyValue) {
            for (var i = 0; i < list.length; i++) {
                Iif (list[parseInt(i.toString(), 10)] === keyValue) {
                    return true;
                }
            }
            return false;
        };
        return LineDistribution;
    }());
    exports.LineDistribution = LineDistribution;
});