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

93.66% Statements 606/647
90.03% Branches 569/632
100% Functions 33/33
93.65% Lines 605/646
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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979   117× 117× 117× 117× 117× 117× 117×   26× 26× 26×         304× 304×   304× 304× 304× 304× 304× 304× 304×   38× 38× 38× 38× 38× 38× 38× 38× 38× 38× 38× 1280× 77359× 1857×   77359×       77359× 77359×   1280× 1280×     304× 304×   304× 304× 304× 3090× 3090× 3040×     304×   304× 304× 304× 304× 7995× 384354× 384354× 384354× 384354× 384354× 384354× 384354× 384354× 4518975× 4518975×   4518975× 77079× 77079× 77079× 170×       384354×   7995× 7995×     4518975×     614× 614× 614× 56×   56× 56× 56× 14×   12× 11×               42×       558× 558×               614×   199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 199× 5586× 278890×   278890×   278890×     199× 199× 199× 9512× 278890× 278890× 278890×   199×     278890×   199×     278890× 220623× 220623× 185×   220623× 185×   220623× 185×   220623× 185×     278890× 220623× 220623× 185×   220623× 185×   220623× 185×   220623× 185×         199× 199× 199× 199× 199× 199× 199× 185×   199× 6019× 6019× 6019× 39094× 39094× 39094× 202× 202× 202×   202× 202×     197× 197×         5822× 5822×           4095× 4095× 4095× 2024× 2157× 2157× 2423×       2157× 2024×     133×         2071× 2016×     4095×   398× 398× 398× 370× 2943× 2943×   2915×     370× 1152× 1152×   768×         28×     185× 185×                                               185× 185×                                     185× 24× 48× 48× 48× 48× 48× 48× 44× 44×     48× 48× 48× 48× 48×   48× 44×   48× 44×   48× 48× 92×       48× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10×         44×   170× 130× 30×   100× 20×   80× 60×   20× 20×       40×       40× 40×   40× 40× 140× 40×   100× 30×   70× 70×         140× 110×       202× 202× 202× 1627× 1627×   202× 202×   202× 117× 116× 116×           85× 34× 34×     51× 51×         202× 202×   202×     202× 202× 1223× 1223× 1223× 1223× 358×     202× 202×   1223×   202× 101×   101× 101× 63× 63× 63× 63× 63× 34×   24× 24× 24× 24× 24× 24× 225×     216×   216× 128×     88×     24× 15× 15× 15×       29× 29×   15× 15× 15× 15× 15× 15× 68× 12× 12×     56×   56× 20×     36×     15×       45×   101×   202× 202× 202× 202× 202× 202× 202× 202× 202× 202× 202× 202× 202× 202× 202× 14×   202× 726× 726× 726× 726× 726× 726× 524× 253×     271×     726×     726×     202× 202× 524× 524× 524× 253× 74×     253× 87×     253× 85× 85× 85× 85× 85× 85×   253× 99×         99×       99× 99×       271× 114×     271× 101×     271× 117× 117× 117× 117× 117× 117×   271× 103×       103×         103× 103×       202× 524× 524× 524× 253× 114× 114×     139× 139×       271× 25× 25×     246× 246×     524× 524×     202× 202× 202× 10×   202×             202×     197× 197× 35×   197×       5822× 5822× 5822× 5822× 10017× 10017× 9635× 9635× 9635× 8156× 4720×     9635× 9635×   9487× 9487× 9487× 9487× 9487×       5822× 4549× 9487× 9487× 9475×       5822×   5822× 5822× 200025× 200025× 39179355× 245219× 245219× 245219× 245219×       5822×   9635× 9635×   9475×   6192× 6192× 6192× 5623× 5623× 5623× 2700×   1794×   2700×   1351×   2700×   2225×     2923× 2923×   1051×   2923×   1262×   2923×   1588×         569× 569× 569× 569×   6192×   2478× 2478× 2478× 7822×   7822×   7822× 7822×   2478× 2150×   2478×   5344× 5344×               20× 20× 20× 35×   35×   35× 35×   20× 10×     10× 10×     25× 25× 10×     15×             20×   192553× 190180× 190180×       179248× 173031×       19522×   44979× 44979× 44979× 44979× 44979× 326358×   315×     44664× 2774×   41890× 41890× 7371×   34519× 34519× 32275× 32275×   1183×     33336× 2244× 2244×   766×   1478×   342×     32228×     46813×        
define(["require", "exports", "../primitives/rect", "../utility/connector", "../utility/constraints-util"], function (require, exports, rect_1, connector_1, constraints_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var LineRouting = (function () {
        function LineRouting() {
            this.size = 20;
            this.intermediatePoints = [];
            this.gridCollection = [];
            this.startArray = [];
            this.targetGridCollection = [];
            this.sourceGridCollection = [];
            this.considerWalkable = [];
        }
        LineRouting.prototype.lineRouting = function (diagram) {
            var length = diagram.connectors.length;
            this.renderVirtualRegion(diagram);
            Eif (length > 0) {
                for (var k = 0; k < length; k++) {
                    var connector = diagram.connectors[parseInt(k.toString(), 10)];
                    Eif (connector.type === 'Orthogonal' && connector.visible) {
                        this.refreshConnectorSegments(diagram, connector, true);
                    }
                }
            }
        };
        LineRouting.prototype.renderVirtualRegion = function (diagram, isUpdate) {
            var extraBounds = this.size;
            if (diagram.spatialSearch['pageTop'] < 0 || diagram.spatialSearch['pageLeft'] < 0) {
                extraBounds = this.size + (this.size / 2);
            }
            var right = diagram.spatialSearch['pageRight'] + extraBounds;
            var bottom = diagram.spatialSearch['pageBottom'] + extraBounds;
            var left = diagram.spatialSearch['pageLeft'] - extraBounds;
            var top = diagram.spatialSearch['pageTop'] - extraBounds;
            left = left < 0 ? left - 20 : 0;
            top = top < 0 ? top - 20 : 0;
            if ((isUpdate && (this.width !== (right - left) || this.height !== (bottom - top) ||
                this.diagramStartX !== left || this.diagramStartY !== top)) || isUpdate === undefined) {
                this.width = right - left;
                this.height = bottom - top;
                this.diagramStartX = left;
                this.diagramStartY = top;
                this.gridCollection = [];
                this.noOfRows = this.width / this.size;
                this.noOfCols = this.height / this.size;
                var size = this.size;
                var x = this.diagramStartX < 0 ? this.diagramStartX : 0;
                var y = this.diagramStartY < 0 ? this.diagramStartY : 0;
                for (var i = 0; i < this.noOfCols; i++) {
                    for (var j = 0; j < this.noOfRows; j++) {
                        if (i === 0) {
                            this.gridCollection.push([0]);
                        }
                        var grid = {
                            x: x, y: y, width: size, height: size, gridX: j,
                            gridY: i, walkable: true, tested: undefined, nodeId: []
                        };
                        this.gridCollection[parseInt(j.toString(), 10)][parseInt(i.toString(), 10)] = grid;
                        x += size;
                    }
                    x = this.diagramStartX < 0 ? this.diagramStartX : 0;
                    y += size;
                }
            }
            var nodes = this.findNodes(diagram.nodes);
            this.updateNodesInVirtualRegion(nodes);
        };
        LineRouting.prototype.findNodes = function (nodes) {
            var objects = [];
            var node;
            for (var i = 0; i < nodes.length; i++) {
                node = nodes[parseInt(i.toString(), 10)];
                if (node.shape.type !== 'SwimLane' && !node.isLane && !node.isPhase && !node.isHeader && node.visible) {
                    objects.push(node);
                }
            }
            return objects;
        };
        LineRouting.prototype.updateNodesInVirtualRegion = function (diagramNodes) {
            var size = this.size;
            var x = this.diagramStartX < 0 ? this.diagramStartX : 0;
            var y = this.diagramStartY < 0 ? this.diagramStartY : 0;
            for (var i = 0; i < this.noOfCols; i++) {
                for (var j = 0; j < this.noOfRows; j++) {
                    var grid = this.gridCollection[parseInt(j.toString(), 10)][parseInt(i.toString(), 10)];
                    var rectangle = new rect_1.Rect(x, y, this.size, this.size);
                    var isContains = void 0;
                    var k = void 0;
                    grid.walkable = true;
                    grid.tested = undefined;
                    grid.nodeId = [];
                    for (k = 0; k < diagramNodes.length; k++) {
                        Eif (diagramNodes[parseInt(k.toString(), 10)].wrapper.bounds) {
                            isContains = this.intersectRect(rectangle, diagramNodes[parseInt(k.toString(), 10)].wrapper.bounds);
                        }
                        if (isContains) {
                            grid.nodeId.push(diagramNodes[parseInt(k.toString(), 10)].id);
                            grid.walkable = false;
                            if (diagramNodes[parseInt(k.toString(), 10)].parentId !== '') {
                                grid.parentNodeId = diagramNodes[parseInt(k.toString(), 10)].parentId;
                            }
                        }
                    }
                    x += size;
                }
                x = this.diagramStartX < 0 ? this.diagramStartX : 0;
                y += size;
            }
        };
        LineRouting.prototype.intersectRect = function (r1, r2) {
            return !(r2.left >= r1.right || r2.right <= r1.left ||
                r2.top >= r1.bottom || r2.bottom <= r1.top);
        };
        LineRouting.prototype.findEndPoint = function (connector, isSource, isPortBounds) {
            var endPoint;
            var portDirection;
            if ((isSource && connector.sourcePortID !== '' && connector.sourcePortWrapper) || (!isSource && connector.targetPortID !== '' && connector.targetPortWrapper)) {
                endPoint = (isSource) ? { x: connector.sourcePortWrapper.offsetX, y: connector.sourcePortWrapper.offsetY } :
                    { x: connector.targetPortWrapper.offsetX, y: connector.targetPortWrapper.offsetY };
                portDirection = connector_1.getPortDirection(endPoint, undefined, (isSource) ? connector.sourceWrapper.bounds : connector.targetWrapper.bounds, false);
                var bounds = (isSource) ? connector.sourcePortWrapper.bounds : connector.targetPortWrapper.bounds;
                if (isPortBounds) {
                    if (portDirection === 'Top') {
                        endPoint = { x: bounds.topCenter.x, y: bounds.topCenter.y };
                    }
                    else if (portDirection === 'Left') {
                        endPoint = { x: bounds.middleLeft.x, y: bounds.middleLeft.y };
                    }
                    else Eif (portDirection === 'Right') {
                        endPoint = { x: bounds.middleRight.x, y: bounds.middleRight.y };
                    }
                    else {
                        endPoint = { x: bounds.bottomCenter.x, y: bounds.bottomCenter.y };
                    }
                }
                else {
                    endPoint = { x: bounds.center.x, y: bounds.center.y };
                }
            }
            else {
                Eif ((isSource && this.startNode) || (!isSource && this.targetNode)) {
                    endPoint = (isSource) ? { x: this.startNode.wrapper.offsetX, y: this.startNode.wrapper.offsetY } :
                        { x: this.targetNode.wrapper.offsetX, y: this.targetNode.wrapper.offsetY };
                }
                else {
                    endPoint = (isSource) ? { x: connector.sourcePoint.x, y: connector.sourcePoint.y } :
                        { x: connector.targetPoint.x, y: connector.targetPoint.y };
                }
            }
            return endPoint;
        };
        LineRouting.prototype.refreshConnectorSegments = function (diagram, connector, isUpdate, isEnableRouting) {
            var sourceId = connector.sourceID;
            var targetId = connector.targetID;
            var sourcePortID = connector.sourcePortID;
            var targetPortID = connector.targetPortID;
            var startPoint;
            var targetPoint;
            var sourcePortDirection;
            var targetPortDirection;
            var grid;
            var sourceTop;
            var sourceBottom;
            var isBreak;
            var sourceLeft;
            var sourceRight;
            var targetRight;
            var targetTop;
            var targetBottom;
            var targetLeft;
            Eif (constraints_util_1.canEnableRouting(connector, diagram) || isEnableRouting) {
                this.startNode = diagram.nameTable["" + sourceId];
                this.targetNode = diagram.nameTable["" + targetId];
                this.intermediatePoints = [];
                this.startArray = [];
                this.targetGridCollection = [];
                this.sourceGridCollection = [];
                this.startGrid = undefined;
                this.targetGrid = undefined;
                for (var i = 0; i < this.noOfCols; i++) {
                    for (var j = 0; j < this.noOfRows; j++) {
                        this.gridCollection[parseInt(j.toString(), 10)][parseInt(i.toString(), 10)].tested
                            = this.gridCollection[parseInt(j.toString(), 10)][parseInt(i.toString(), 10)].parent = undefined;
                        this.gridCollection[parseInt(j.toString(), 10)][parseInt(i.toString(), 10)].previousDistance
                            = this.gridCollection[parseInt(j.toString(), 10)][parseInt(i.toString(), 10)].afterDistance = undefined;
                        this.gridCollection[parseInt(j.toString(), 10)][parseInt(i.toString(), 10)].totalDistance = undefined;
                    }
                }
                startPoint = this.findEndPoint(connector, true);
                targetPoint = this.findEndPoint(connector, false);
                for (var i = 0; i < this.noOfRows; i++) {
                    for (var j = 0; j < this.noOfCols; j++) {
                        grid = this.gridCollection[parseInt(i.toString(), 10)][parseInt(j.toString(), 10)];
                        var rectangle = new rect_1.Rect(grid.x, grid.y, grid.width, grid.height);
                        if (rectangle.containsPoint(startPoint) && !this.startGrid &&
                            (grid.nodeId.indexOf(sourceId) !== -1 || sourceId === '')) {
                            this.startGrid = (sourcePortID && this.startGrid &&
                                (sourcePortDirection === 'Left' || sourcePortDirection === 'Top')) ? this.startGrid : grid;
                        }
                        if (rectangle.containsPoint(targetPoint) && !this.targetGrid &&
                            (grid.nodeId.indexOf(targetId) !== -1 || targetId === '')) {
                            this.targetGrid = (targetPortID && this.targetGrid &&
                                (targetPortDirection === 'Left' || targetPortDirection === 'Top')) ? this.targetGrid : grid;
                        }
                        if (!sourcePortID && this.startNode) {
                            var bounds = this.startNode.wrapper.bounds;
                            if (rectangle.containsPoint(bounds.topCenter) && !sourceTop) {
                                sourceTop = grid;
                            }
                            if (rectangle.containsPoint(bounds.middleLeft) && !sourceLeft) {
                                sourceLeft = grid;
                            }
                            if (rectangle.containsPoint(bounds.middleRight) && !sourceRight) {
                                sourceRight = grid;
                            }
                            if (rectangle.containsPoint(bounds.bottomCenter) && !sourceBottom) {
                                sourceBottom = grid;
                            }
                        }
                        if (!targetPortID && this.targetNode) {
                            var bounds = this.targetNode.wrapper.bounds;
                            if (rectangle.containsPoint(bounds.topCenter) && !targetTop) {
                                targetTop = grid;
                            }
                            if (rectangle.containsPoint(bounds.middleLeft) && !targetLeft) {
                                targetLeft = grid;
                            }
                            if (rectangle.containsPoint(bounds.middleRight) && !targetRight) {
                                targetRight = grid;
                            }
                            if (rectangle.containsPoint({ x: bounds.bottomCenter.x, y: bounds.bottomCenter.y }) && !targetBottom) {
                                targetBottom = grid;
                            }
                        }
                    }
                }
                this.findEdgeBoundary(sourcePortID, sourceLeft, sourceRight, sourceTop, sourceBottom, true);
                this.findEdgeBoundary(targetPortID, targetLeft, targetRight, targetTop, targetBottom, false);
                this.startGrid.totalDistance = 0;
                this.startGrid.previousDistance = 0;
                this.intermediatePoints.push({ x: this.startGrid.gridX, y: this.startGrid.gridY });
                this.startArray.push(this.startGrid);
                if (connector && targetLeft && targetRight && targetTop && targetBottom) {
                    this.checkObstacles(connector, diagram, targetLeft, targetRight, targetTop, targetBottom);
                }
                renderPathElement: while (this.startArray.length > 0) {
                    var startGridNode = this.startArray.pop();
                    Eif (startGridNode) {
                        for (var i = 0; i < this.targetGridCollection.length; i++) {
                            var target = this.targetGridCollection[parseInt(i.toString(), 10)];
                            Eif (startGridNode) {
                                if (startGridNode.gridX === target.gridX && startGridNode.gridY === target.gridY) {
                                    this.getIntermediatePoints(startGridNode);
                                    this.optimizeIntermediatePoints();
                                    if (startGridNode.nodeId && startGridNode.nodeId.length > 1) {
                                        connector.segments = [];
                                    }
                                    isBreak = this.updateConnectorSegments(diagram, this.intermediatePoints, this.gridCollection, connector, isUpdate);
                                    if (!isBreak) {
                                        this.targetGridCollection.splice(this.targetGridCollection.indexOf(target), 1);
                                        startGridNode = this.startArray.pop();
                                    }
                                    else {
                                        this.considerWalkable = [];
                                        break renderPathElement;
                                    }
                                }
                            }
                        }
                        Eif (startGridNode) {
                            this.findPath(startGridNode);
                        }
                    }
                }
            }
        };
        LineRouting.prototype.checkChildNodes = function (grid, isSource) {
            var check = false;
            var reject = false;
            if (grid.nodeId.length >= 1 && !isSource) {
                for (var i = 0; i < grid.nodeId.length; i++) {
                    var id = grid.nodeId[parseInt(i.toString(), 10)];
                    for (var j = 0; j < grid.nodeId.length; j++) {
                        Iif (this.targetNode.parentId === grid.nodeId[parseInt(j.toString(), 10)]) {
                            reject = true;
                        }
                    }
                    if (!reject && this.targetNode.id === id) {
                        check = true;
                    }
                    else {
                        check = false;
                    }
                }
            }
            else {
                if (grid.nodeId.length === 1) {
                    check = true;
                }
            }
            return check;
        };
        LineRouting.prototype.findEdgeBoundary = function (portID, left, right, top, bottom, isSource) {
            var grid;
            var collection = (isSource) ? this.sourceGridCollection : this.targetGridCollection;
            if (!portID && ((isSource) ? this.startNode : this.targetNode)) {
                for (var i = left.gridX; i <= right.gridX; i++) {
                    grid = this.gridCollection[parseInt(i.toString(), 10)][left.gridY];
                    if ((this.checkChildNodes(grid, isSource) && (i === left.gridX || i === right.gridX)) ||
                        (i !== left.gridX && i !== right.gridX)) {
                        collection.push(grid);
                    }
                }
                for (var i = top.gridY; i <= bottom.gridY; i++) {
                    grid = this.gridCollection[top.gridX][parseInt(i.toString(), 10)];
                    if (((this.checkChildNodes(grid, isSource) && (i === top.gridY || i === bottom.gridY)) ||
                        (i !== top.gridY && i !== bottom.gridY)) && collection.indexOf(grid) === -1) {
                        collection.push(grid);
                    }
                }
            }
            else {
                collection.push((isSource) ? this.startGrid : this.targetGrid);
            }
        };
        LineRouting.prototype.checkObstacles = function (connector, diagram, targetLeft, targetRight, targetTop, targetBottom) {
            var neigbours = this.findNearestNeigbours(this.startGrid, this.gridCollection, true);
            Iif (neigbours.length === 0) {
                if (connector.sourcePortID !== '') {
                    var endPoint = { x: connector.sourcePortWrapper.offsetX, y: connector.sourcePortWrapper.offsetY };
                    var portDirection = connector_1.getPortDirection(endPoint, undefined, connector.sourceWrapper.bounds, false);
                    if (portDirection === 'Top') {
                        this.resetGridColl(this.startGrid, 'top', true);
                    }
                    else if (portDirection === 'Right') {
                        this.resetGridColl(this.startGrid, 'right', true);
                    }
                    else if (portDirection === 'Bottom') {
                        this.resetGridColl(this.startGrid, 'bottom', true);
                    }
                    else {
                        this.resetGridColl(this.startGrid, 'left', true);
                    }
                }
                else {
                    this.resetGridColl(this.startGrid, 'top', true);
                    this.resetGridColl(this.startGrid, 'right', true);
                    this.resetGridColl(this.startGrid, 'bottom', true);
                    this.resetGridColl(this.startGrid, 'left', true);
                }
            }
            neigbours = this.findNearestNeigbours(this.targetGrid, this.gridCollection, false);
            if (neigbours.length === 0) {
                Iif (connector.targetPortID !== '') {
                    var endPoint = { x: connector.targetPortWrapper.offsetX, y: connector.targetPortWrapper.offsetY };
                    var portDirection = connector_1.getPortDirection(endPoint, undefined, connector.targetWrapper.bounds, false);
                    if (portDirection === 'Top') {
                        this.resetGridColl(this.targetGrid, 'top', true);
                    }
                    else if (portDirection === 'Right') {
                        this.resetGridColl(this.targetGrid, 'right', true);
                    }
                    else if (portDirection === 'Bottom') {
                        this.resetGridColl(this.targetGrid, 'bottom', true);
                    }
                    else {
                        this.resetGridColl(this.targetGrid, 'left', true);
                    }
                }
                else {
                    this.resetGridColl(this.targetGrid, 'top', false);
                    this.resetGridColl(this.targetGrid, 'right', false);
                    this.resetGridColl(this.targetGrid, 'bottom', false);
                    this.resetGridColl(this.targetGrid, 'left', false);
                }
            }
            if (this.targetGridCollection.length > 1 && this.targetGridCollection[0].nodeId.length > 1) {
                for (var i = 0; i <= 1; i++) {
                    var gridX = this.targetGridCollection[parseInt(i.toString(), 10)].gridX;
                    var gridY = this.targetGridCollection[parseInt(i.toString(), 10)].gridY;
                    var gridNodes = this.targetGridCollection[parseInt(i.toString(), 10)].nodeId;
                    var targetNode = void 0;
                    for (var k = 0; k < gridNodes.length; k++) {
                        if (this.targetNode.id !== gridNodes[parseInt(k.toString(), 10)]) {
                            targetNode = gridNodes[parseInt(k.toString(), 10)];
                            break;
                        }
                    }
                    var targetNodewrapper = void 0;
                    var overLapNode = void 0;
                    var contains = void 0;
                    Eif (diagram.nameTable[this.targetNode.id]) {
                        targetNodewrapper = diagram.nameTable[this.targetNode.id].wrapper;
                    }
                    if (diagram.nameTable["" + targetNode]) {
                        overLapNode = diagram.nameTable["" + targetNode].wrapper;
                    }
                    if (targetNodewrapper && overLapNode) {
                        contains = this.contains(overLapNode.bounds, targetNodewrapper.bounds);
                    }
                    var reject = void 0;
                    for (var j = 0; j < gridNodes.length; j++) {
                        Iif (this.targetNode.parentId === gridNodes[parseInt(j.toString(), 10)]) {
                            reject = true;
                        }
                    }
                    if (!this.gridCollection[parseInt(gridX.toString(), 10)][parseInt(gridY.toString(), 10)].walkable && contains && !reject) {
                        var grid = void 0;
                        var diff = void 0;
                        grid = this.getEndvalue(targetLeft, 'left');
                        diff = targetLeft.gridX - grid.gridX;
                        this.changeValue(targetLeft, diff, 'left');
                        grid = this.getEndvalue(targetRight, 'right');
                        diff = grid.gridX - targetRight.gridX;
                        this.changeValue(targetRight, diff, 'right');
                        grid = this.getEndvalue(targetTop, 'top');
                        diff = targetTop.gridY - grid.gridY;
                        this.changeValue(targetTop, diff, 'top');
                        grid = this.getEndvalue(targetBottom, 'bottom');
                        diff = targetBottom.gridY - grid.gridY;
                        this.changeValue(targetBottom, diff, 'top');
                    }
                }
            }
        };
        LineRouting.prototype.contains = function (rect1, rect2) {
            return rect1.left <= rect2.left && rect1.right >= rect2.right && rect1.top <= rect2.top && rect1.bottom >= rect2.bottom;
        };
        LineRouting.prototype.getEndvalue = function (target, direction) {
            if (!this.gridCollection[target.gridX][target.gridY].walkable) {
                if (direction === 'left') {
                    return this.getEndvalue(this.gridCollection[target.gridX - 1][target.gridY], direction);
                }
                if (direction === 'right') {
                    return this.getEndvalue(this.gridCollection[target.gridX + 1][target.gridY], direction);
                }
                if (direction === 'top') {
                    return this.getEndvalue(this.gridCollection[target.gridX][target.gridY - 1], direction);
                }
                Eif (direction === 'bottom') {
                    return this.getEndvalue(this.gridCollection[target.gridX][target.gridY + 1], direction);
                }
            }
            else {
                return target;
            }
            return target;
        };
        LineRouting.prototype.changeValue = function (targetLeft, diff, direction) {
            Eif (!targetLeft.walkable) {
                this.considerWalkable.push(targetLeft);
            }
            var grid;
            for (var i = 0; i <= diff; i++) {
                if (direction === 'left') {
                    grid = this.gridCollection[targetLeft.gridX - i][targetLeft.gridY];
                }
                else if (direction === 'right') {
                    grid = this.gridCollection[targetLeft.gridX + i][targetLeft.gridY];
                }
                else Eif (direction === 'top') {
                    grid = this.gridCollection[targetLeft.gridX][targetLeft.gridY - i];
                }
                else if (direction === 'bottom') {
                    grid = this.gridCollection[targetLeft.gridX][targetLeft.gridY + i];
                }
                if (!grid.walkable) {
                    this.considerWalkable.push(grid);
                }
            }
        };
        LineRouting.prototype.getIntermediatePoints = function (target) {
            var distance;
            this.intermediatePoints = [];
            while (target) {
                this.intermediatePoints.push({ x: target.gridX, y: target.gridY });
                target = target.parent;
            }
            this.intermediatePoints.reverse();
            Eif (this.intermediatePoints.length >= 1 && this.intermediatePoints[0] !== undefined
                && this.intermediatePoints[1] !== undefined) {
                if (this.intermediatePoints[0].x === this.intermediatePoints[1].x) {
                    if (this.intermediatePoints[0].y < this.intermediatePoints[1].y) {
                        distance = this.neigbour(this.startGrid, 'bottom', undefined, true);
                        this.intermediatePoints[0].y += distance - 1;
                    }
                    else {
                        distance = this.neigbour(this.startGrid, 'top', undefined, true);
                        this.intermediatePoints[0].y -= distance - 1;
                    }
                }
                else {
                    if (this.intermediatePoints[0].x < this.intermediatePoints[1].x) {
                        distance = this.neigbour(this.startGrid, 'right', undefined, true);
                        this.intermediatePoints[0].x += distance - 1;
                    }
                    else {
                        distance = this.neigbour(this.startGrid, 'left', undefined, true);
                        this.intermediatePoints[0].x -= distance - 1;
                    }
                }
            }
        };
        LineRouting.prototype.optimizeIntermediatePoints = function () {
            this.intermediatePoints = this.removePointsInSameLine(this.intermediatePoints);
            this.intermediatePoints = this.getValidPoints(this.intermediatePoints);
        };
        LineRouting.prototype.removePointsInSameLine = function (points) {
            Iif (points.length < 3) {
                return points;
            }
            var result = [points[0]];
            for (var i = 1; i < points.length - 1; i++) {
                var prevPoint = result[result.length - 1];
                var currentPoint = points[parseInt(i.toString(), 10)];
                var nextPoint = points[i + 1];
                if (!this.arePointsInSameLine(prevPoint, currentPoint, nextPoint)) {
                    result.push(currentPoint);
                }
            }
            result.push(points[points.length - 1]);
            return result;
        };
        LineRouting.prototype.arePointsInSameLine = function (point1, point2, point3) {
            return (point2.x - point1.x) * (point3.y - point1.y) === (point3.x - point1.x) * (point2.y - point1.y);
        };
        LineRouting.prototype.getValidPoints = function (points) {
            if (points.length < 4) {
                return points;
            }
            var i = 1;
            while (i < points.length - 3) {
                var lineStart1 = points[parseInt(i.toString(), 10)];
                var lineEnd1 = points[i + 1];
                var lineStart2 = points[i + 2];
                var lineEnd2 = points[i + 3];
                if (lineStart1.x === lineEnd1.x) {
                    if ((lineEnd1.y < lineStart1.y && lineEnd2.y < lineStart2.y)
                        || (lineEnd1.y > lineStart1.y && lineEnd2.y > lineStart2.y)) {
                        var dx = lineStart1.x < lineStart2.x ? 1 : -1;
                        var dy = lineEnd1.y < lineStart1.y ? -1 : 1;
                        var neigbourGridX = lineStart1.x + dx;
                        var neigbourGridY = lineStart1.y;
                        var isValid = false;
                        while (neigbourGridX !== lineEnd2.x || neigbourGridY !== lineEnd2.y) {
                            if (!this.isWalkable(neigbourGridX, neigbourGridY)) {
                                isValid = false;
                                break;
                            }
                            else {
                                isValid = true;
                            }
                            if (neigbourGridX !== lineStart2.x) {
                                neigbourGridX += dx;
                            }
                            else {
                                neigbourGridY += dy;
                            }
                        }
                        if (isValid) {
                            lineStart1.x = lineStart2.x;
                            points.splice(i + 1, 2);
                            continue;
                        }
                    }
                }
                else Eif (lineStart1.y === lineEnd1.y) {
                    if ((lineEnd1.x < lineStart1.x && lineEnd2.x < lineStart2.x)
                        || (lineEnd1.x > lineStart1.x && lineEnd2.x > lineStart2.x)) {
                        var dy1 = lineStart1.y < lineStart2.y ? 1 : -1;
                        var dx1 = lineEnd1.x < lineStart1.x ? -1 : 1;
                        var neigbourGridY1 = lineStart1.y + dy1;
                        var neigbourGridX1 = lineStart1.x;
                        var isValid1 = false;
                        while (neigbourGridX1 !== lineEnd2.x || neigbourGridY1 !== lineEnd2.y) {
                            if (!this.isWalkable(neigbourGridX1, neigbourGridY1)) {
                                isValid1 = false;
                                break;
                            }
                            else {
                                isValid1 = true;
                            }
                            if (neigbourGridY1 !== lineStart2.y) {
                                neigbourGridY1 += dy1;
                            }
                            else {
                                neigbourGridX1 += dx1;
                            }
                        }
                        if (isValid1) {
                            lineStart1.y = lineStart2.y;
                            points.splice(i + 1, 2);
                            continue;
                        }
                    }
                }
                i++;
            }
            return points;
        };
        LineRouting.prototype.updateConnectorSegments = function (diagram, intermediatePoints, gridCollection, connector, isUpdate) {
            var segments = [];
            var seg;
            var targetPoint;
            var pointX;
            var pointY;
            var node;
            var points = [];
            var direction;
            var length;
            var currentdirection;
            var prevDirection;
            var targetWrapper = connector.targetWrapper;
            var sourceWrapper = connector.sourceWrapper;
            var sourcePoint = this.findEndPoint(connector, true);
            if (connector.targetPortID !== '' || !connector.targetWrapper) {
                targetPoint = this.findEndPoint(connector, false, true);
            }
            for (var i = 0; i < intermediatePoints.length; i++) {
                node = gridCollection[intermediatePoints[parseInt(i.toString(), 10)].x][intermediatePoints[parseInt(i.toString(), 10)].y];
                Eif (node) {
                    pointX = node.x + node.width / 2;
                    pointY = node.y + node.height / 2;
                    points.push({ x: pointX, y: pointY });
                    if (i >= 1 && points.length > 1) {
                        if (points[points.length - 2].x !== points[points.length - 1].x) {
                            currentdirection = (points[points.length - 2].x > points[points.length - 1].x) ? 'Left' : 'Right';
                        }
                        else {
                            currentdirection = (points[points.length - 2].y > points[points.length - 1].y) ? 'Top' : 'Bottom';
                        }
                    }
                    Iif (i >= 2 && prevDirection === currentdirection && points.length > 1) {
                        points.splice(points.length - 2, 1);
                    }
                    prevDirection = currentdirection;
                }
            }
            Eif (points && points.length > 1) {
                for (var j = 0; j < points.length - 1; j++) {
                    var currentPoint = points[parseInt(j.toString(), 10)];
                    var nextPoint = points[j + 1];
                    if (currentPoint.x !== nextPoint.x) {
                        if (j === 0 && connector.sourcePortID === '' && sourceWrapper) {
                            sourcePoint = (currentPoint.x > nextPoint.x)
                                ? sourceWrapper.bounds.middleLeft : sourceWrapper.bounds.middleRight;
                        }
                        if (j === points.length - 2 && connector.targetPortID === '' && targetWrapper) {
                            targetPoint = (currentPoint.x > nextPoint.x)
                                ? targetWrapper.bounds.middleRight : targetWrapper.bounds.middleLeft;
                        }
                        if (j === 0 && sourcePoint) {
                            currentPoint.x = sourcePoint.x;
                            currentPoint.y = nextPoint.y = sourcePoint.y;
                            var newDirection = currentPoint.x > nextPoint.x ? 'Left' : 'Right';
                            var refPoint = connector_1.findPoint(sourceWrapper.bounds, connector_1.getOppositeDirection(newDirection));
                            sourcePoint = connector_1.getIntersection(connector, sourceWrapper, sourcePoint, refPoint, false);
                            currentPoint.x = sourcePoint.x;
                        }
                        if (j === points.length - 2 && targetPoint) {
                            Iif (j > 0 && connector.targetDecorator &&
                                ((targetPoint.x - nextPoint.x) < 0) &&
                                (Math.abs(targetPoint.x - currentPoint.x) < connector.targetDecorator.width + 1)) {
                                currentPoint.x = points[j - 1].x -= this.size / 2;
                            }
                            if (j > 0 && connector.targetDecorator &&
                                ((targetPoint.x - nextPoint.x) > 0) &&
                                (Math.abs(targetPoint.x - currentPoint.x) < connector.targetDecorator.width + 1)) {
                                currentPoint.x = points[j - 1].x += this.size / 2;
                            }
                            nextPoint.x = targetPoint.x;
                            currentPoint.y = nextPoint.y = targetPoint.y;
                        }
                    }
                    else {
                        if (j === 0 && connector.sourcePortID === '' && sourceWrapper) {
                            sourcePoint = (currentPoint.y > nextPoint.y)
                                ? sourceWrapper.bounds.topCenter : sourceWrapper.bounds.bottomCenter;
                        }
                        if (j === points.length - 2 && connector.targetPortID === '' && targetWrapper) {
                            targetPoint = (currentPoint.y > nextPoint.y)
                                ? targetWrapper.bounds.bottomCenter : targetWrapper.bounds.topCenter;
                        }
                        if (j === 0 && sourcePoint) {
                            currentPoint.y = sourcePoint.y;
                            currentPoint.x = nextPoint.x = sourcePoint.x;
                            var newDirection1 = currentPoint.y > nextPoint.y ? 'Top' : 'Bottom';
                            var refPoint = connector_1.findPoint(sourceWrapper.bounds, connector_1.getOppositeDirection(newDirection1));
                            sourcePoint = connector_1.getIntersection(connector, sourceWrapper, sourcePoint, refPoint, false);
                            currentPoint.y = sourcePoint.y;
                        }
                        if (j === points.length - 2 && targetPoint) {
                            if (j > 0 && connector.targetDecorator &&
                                ((targetPoint.y - nextPoint.y) < 0) &&
                                (Math.abs(targetPoint.y - currentPoint.y) < connector.targetDecorator.height + 1)) {
                                currentPoint.y = points[j - 1].y -= this.size / 2;
                            }
                            Iif (j > 0 && connector.targetDecorator &&
                                ((targetPoint.y - nextPoint.y) > 0) &&
                                (Math.abs(targetPoint.y - currentPoint.y) < connector.targetDecorator.height + 1)) {
                                currentPoint.y = points[j - 1].y += this.size / 2;
                            }
                            nextPoint.y = targetPoint.y;
                            currentPoint.x = nextPoint.x = targetPoint.x;
                        }
                    }
                }
                for (var j = 0; j < points.length - 1; j++) {
                    var currentPoint = points[parseInt(j.toString(), 10)];
                    var nextPoint = points[j + 1];
                    if (currentPoint.x !== nextPoint.x) {
                        if (currentPoint.x > nextPoint.x) {
                            direction = 'Left';
                            length = currentPoint.x - nextPoint.x;
                        }
                        else {
                            direction = 'Right';
                            length = nextPoint.x - currentPoint.x;
                        }
                    }
                    else {
                        if (currentPoint.y > nextPoint.y) {
                            direction = 'Top';
                            length = currentPoint.y - nextPoint.y;
                        }
                        else {
                            direction = 'Bottom';
                            length = nextPoint.y - currentPoint.y;
                        }
                    }
                    seg = { type: 'Orthogonal', length: length, direction: direction };
                    segments.push(seg);
                }
            }
            Eif (segments && segments.length > 0) {
                var lastSeg = segments[segments.length - 1];
                if (segments.length === 1) {
                    lastSeg.length -= 20;
                }
                Iif (lastSeg.length < 10 && segments.length === 2) {
                    segments.pop();
                    if (segments.length > 0) {
                        segments[0].length -= 20;
                        lastSeg = segments[0];
                    }
                }
                if (connector.targetDecorator &&
                    ((lastSeg.direction === 'Top' || lastSeg.direction === 'Bottom') && lastSeg.length > connector.targetDecorator.height + 1) ||
                    ((lastSeg.direction === 'Right' || lastSeg.direction === 'Left') && lastSeg.length > connector.targetDecorator.width + 1)) {
                    connector.segments = segments;
                    if (isUpdate) {
                        diagram.connectorPropertyChange(connector, {}, { type: 'Orthogonal', segments: segments });
                    }
                    return true;
                }
            }
            return false;
        };
        LineRouting.prototype.findPath = function (startGrid) {
            var intermediatePoint;
            var collection = [];
            var neigbours = this.findNearestNeigbours(startGrid, this.gridCollection, true);
            for (var i = 0; i < neigbours.length; i++) {
                intermediatePoint = this.findIntermediatePoints(neigbours[parseInt(i.toString(), 10)].gridX, neigbours[parseInt(i.toString(), 10)].gridY, startGrid.gridX, startGrid.gridY, this.targetGrid.gridX, this.targetGrid.gridY);
                if (intermediatePoint !== null) {
                    var grid = this.gridCollection[intermediatePoint.x][intermediatePoint.y];
                    var h = this.octile(Math.abs(intermediatePoint.x - startGrid.gridX), Math.abs(intermediatePoint.y - startGrid.gridY));
                    if (startGrid.parent && startGrid.parent.parent) {
                        if (grid.gridX !== startGrid.parent.gridX && grid.gridY !== startGrid.parent.gridY) {
                            h += 0.1;
                        }
                    }
                    var l = startGrid.previousDistance + h;
                    if ((!grid.previousDistance || grid.previousDistance > l) &&
                        (!(intermediatePoint.x === startGrid.gridX && intermediatePoint.y === startGrid.gridY))) {
                        collection.push(intermediatePoint);
                        grid.previousDistance = l;
                        grid.afterDistance = grid.afterDistance || this.manhattan(Math.abs(intermediatePoint.x - this.targetGrid.gridX), Math.abs(intermediatePoint.y - this.targetGrid.gridY));
                        grid.totalDistance = grid.previousDistance + grid.afterDistance;
                        grid.parent = startGrid;
                    }
                }
            }
            if (collection.length > 0) {
                for (var i = 0; i < collection.length; i++) {
                    var grid = this.gridCollection[collection[parseInt(i.toString(), 10)].x][collection[parseInt(i.toString(), 10)].y];
                    if (this.startArray.indexOf(grid) === -1) {
                        this.startArray.push(grid);
                    }
                }
            }
            this.sorting(this.startArray);
        };
        LineRouting.prototype.sorting = function (array) {
            var done = false;
            while (!done) {
                done = true;
                for (var i = 1; i < array.length; i += 1) {
                    if (array[i - 1].totalDistance < array[parseInt(i.toString(), 10)].totalDistance) {
                        done = false;
                        var tmp = array[i - 1];
                        array[i - 1] = array[parseInt(i.toString(), 10)];
                        array[parseInt(i.toString(), 10)] = tmp;
                    }
                }
            }
            return array;
        };
        LineRouting.prototype.octile = function (t, e) {
            var r = Math.SQRT2 - 1;
            return e > t ? r * t + e : r * e + t;
        };
        LineRouting.prototype.manhattan = function (t, e) {
            return t + e;
        };
        LineRouting.prototype.findNearestNeigbours = function (startGrid, gridCollection, isSource) {
            var neigbours = [];
            var parent = startGrid.parent;
            if (parent) {
                var dx = (startGrid.gridX - parent.gridX) / Math.max(Math.abs(startGrid.gridX - parent.gridX), 1);
                var dy = (startGrid.gridY - parent.gridY) / Math.max(Math.abs(startGrid.gridY - parent.gridY), 1);
                if (dx !== 0) {
                    if (this.isWalkable(startGrid.gridX, startGrid.gridY - 1, true) &&
                        this.sourceGridCollection.indexOf(gridCollection[startGrid.gridX][startGrid.gridY - 1]) === -1) {
                        neigbours.push(gridCollection[startGrid.gridX][startGrid.gridY - 1]);
                    }
                    if (this.isWalkable(startGrid.gridX, startGrid.gridY + 1, true) &&
                        this.sourceGridCollection.indexOf(gridCollection[startGrid.gridX][startGrid.gridY + 1])) {
                        neigbours.push(gridCollection[startGrid.gridX][startGrid.gridY + 1]);
                    }
                    if (this.isWalkable(startGrid.gridX + dx, startGrid.gridY, true) &&
                        this.sourceGridCollection.indexOf(gridCollection[startGrid.gridX + dx][startGrid.gridY]) === -1) {
                        neigbours.push(gridCollection[startGrid.gridX + dx][startGrid.gridY]);
                    }
                }
                else Eif (dy !== 0) {
                    if (this.isWalkable(startGrid.gridX - 1, startGrid.gridY, true) &&
                        this.sourceGridCollection.indexOf(gridCollection[startGrid.gridX - 1][startGrid.gridY]) === -1) {
                        neigbours.push(gridCollection[startGrid.gridX - 1][startGrid.gridY]);
                    }
                    if (this.isWalkable(startGrid.gridX + 1, startGrid.gridY, true) &&
                        this.sourceGridCollection.indexOf(gridCollection[startGrid.gridX + 1][startGrid.gridY]) === -1) {
                        neigbours.push(gridCollection[startGrid.gridX + 1][startGrid.gridY]);
                    }
                    if (this.isWalkable(startGrid.gridX, startGrid.gridY + dy, true) &&
                        this.sourceGridCollection.indexOf(gridCollection[startGrid.gridX][startGrid.gridY + dy]) === -1) {
                        neigbours.push(gridCollection[startGrid.gridX][startGrid.gridY + dy]);
                    }
                }
            }
            else {
                this.neigbour(startGrid, 'top', neigbours, isSource);
                this.neigbour(startGrid, 'right', neigbours, isSource);
                this.neigbour(startGrid, 'bottom', neigbours, isSource);
                this.neigbour(startGrid, 'left', neigbours, isSource);
            }
            return neigbours;
        };
        LineRouting.prototype.neigbour = function (startGrid, direction, neigbours, isSource) {
            var i = 1;
            var nearGrid;
            while (i > 0) {
                var x = (direction === 'top' || direction === 'bottom') ?
                    (startGrid.gridX) : ((direction === 'left') ? startGrid.gridX - i : startGrid.gridX + i);
                var y = (direction === 'right' || direction === 'left') ?
                    (startGrid.gridY) : ((direction === 'top') ? startGrid.gridY - i : startGrid.gridY + i);
                nearGrid = this.gridCollection[parseInt(x.toString(), 10)][parseInt(y.toString(), 10)];
                if (nearGrid && ((isSource && this.sourceGridCollection.indexOf(nearGrid) === -1)
                    || (!isSource && this.targetGridCollection.indexOf(nearGrid) === -1))) {
                    if (neigbours && this.isWalkable(x, y)) {
                        neigbours.push(nearGrid);
                    }
                    return i;
                }
                Eif (x > 0 && y > 0) {
                    i++;
                }
                else {
                    break;
                }
            }
            return null;
        };
        LineRouting.prototype.resetGridColl = function (grid, direction, isSource) {
            var i = 1;
            var nearGrid;
            while (i > 0) {
                var x = (direction === 'top' || direction === 'bottom') ?
                    (grid.gridX) : ((direction === 'left') ? grid.gridX - i : grid.gridX + i);
                var y = (direction === 'right' || direction === 'left') ?
                    (grid.gridY) : ((direction === 'top') ? grid.gridY - i : grid.gridY + i);
                nearGrid = this.gridCollection[parseInt(x.toString(), 10)][parseInt(y.toString(), 10)];
                if (nearGrid && ((isSource && this.sourceGridCollection.indexOf(nearGrid) === -1) ||
                    (!isSource && this.targetGridCollection.indexOf(nearGrid) === -1))) {
                    if (this.isWalkable(x, y)) {
                        break;
                    }
                    else {
                        var grid_1 = this.gridCollection[parseInt(x.toString(), 10)][parseInt(y.toString(), 10)];
                        this.considerWalkable.push(grid_1);
                    }
                }
                Eif (x > 0 && y > 0) {
                    if (direction === 'top' || direction === 'left') {
                        i--;
                    }
                    else {
                        i++;
                    }
                }
                else {
                    break;
                }
            }
            return null;
        };
        LineRouting.prototype.isWalkable = function (x, y, isparent) {
            if (x >= 0 && x < this.noOfRows && y >= 0 && y < this.noOfCols) {
                var grid = this.gridCollection[parseInt(x.toString(), 10)][parseInt(y.toString(), 10)];
                if (grid && (grid.walkable || ((grid.nodeId.length === 1 || (grid.nodeId.length === 2 && grid.parentNodeId
                    || (this.considerWalkable.indexOf(grid) !== -1))) &&
                    (this.sourceGridCollection.indexOf(grid) !== -1 || this.targetGridCollection.indexOf(grid) !== -1 ||
                        this.considerWalkable.indexOf(grid) !== -1)))) {
                    if ((isparent && !grid.parent) || !isparent) {
                        return true;
                    }
                }
            }
            return false;
        };
        LineRouting.prototype.findIntermediatePoints = function (neigbourGridX, neigbourGridY, startGridX, startGridY, endGridX, endGridY) {
            var dx = neigbourGridX - startGridX;
            var dy = neigbourGridY - startGridY;
            var gridX = neigbourGridX;
            var gridY = neigbourGridY;
            for (var i = 0; i < this.targetGridCollection.length; i++) {
                if (neigbourGridX === this.targetGridCollection[parseInt(i.toString(), 10)].gridX
                    && neigbourGridY === this.targetGridCollection[parseInt(i.toString(), 10)].gridY) {
                    return { x: neigbourGridX, y: neigbourGridY };
                }
            }
            if (!this.isWalkable(neigbourGridX, neigbourGridY)) {
                return null;
            }
            var neigbourGrid = this.gridCollection[parseInt(neigbourGridX.toString(), 10)][parseInt(neigbourGridY.toString(), 10)];
            if (neigbourGrid.tested) {
                return { x: neigbourGridX, y: neigbourGridY };
            }
            neigbourGrid.tested = true;
            if (dx !== 0) {
                dx = (dx > 0) ? 1 : -1;
                if ((this.isWalkable(gridX, gridY - 1) && !this.isWalkable(gridX - dx, gridY - 1)) ||
                    (this.isWalkable(gridX, gridY + 1) && !this.isWalkable(gridX - dx, gridY + 1))) {
                    return { x: neigbourGridX, y: neigbourGridY };
                }
            }
            if (dy !== 0) {
                dy = (dy > 0) ? 1 : -1;
                if ((this.isWalkable(gridX - 1, gridY) && !this.isWalkable(gridX - 1, gridY - dy)) ||
                    (this.isWalkable(gridX + 1, gridY) && !this.isWalkable(gridX + 1, gridY - dy))) {
                    return { x: neigbourGridX, y: neigbourGridY };
                }
                if (this.findIntermediatePoints(gridX + 1, gridY, gridX, gridY, endGridX, endGridY) ||
                    this.findIntermediatePoints(gridX - 1, gridY, gridX, gridY, endGridX, endGridY)) {
                    return { x: neigbourGridX, y: neigbourGridY };
                }
            }
            return this.findIntermediatePoints(gridX + dx, gridY + dy, gridX, gridY, endGridX, endGridY);
        };
        LineRouting.prototype.destroy = function () {
        };
        LineRouting.prototype.getModuleName = function () {
            return 'LineRouting';
        };
        return LineRouting;
    }());
    exports.LineRouting = LineRouting;
});