all files / diagram/interaction/spatial-search/ spatial-search.js

99.58% Statements 237/238
98.88% Branches 176/178
100% Functions 23/23
99.58% Lines 237/238
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   1654× 1654× 1654× 1654× 1654× 1654× 1654× 1654×   3766× 3688× 3688× 3688× 3688× 3688× 3688×       3768×   80× 80× 27×   53× 21×   32× 11×   21× 21×   80×     3688× 880× 880× 880× 880×   3688×     35085× 35085×   3688× 56063× 3688×         450201× 450201× 450201×   12×   450189× 417363× 417363× 2259× 2259×       32826×   450189×             28306× 28306× 24335× 3498× 3498× 3498×   20837× 1329× 1329× 1329×     28306× 23759× 3965× 3965× 3965×   19794× 1110× 1110× 1110×     28306× 24500× 6902× 6902× 6902×   17598× 392× 392× 392×     28306× 23999× 12161× 12161× 12161×   11838× 420× 420× 420×     28306×   421883× 421883×   450189×   435225×     14964×     21886× 21886× 21886×   19823× 19823× 19823× 24692× 24692× 197444× 197444× 39510×       19823×   1507× 1507× 68× 23×   68× 24×   68× 56×   68× 60×       1439× 103×   1439× 112×   1439× 146×   1439× 170×     1507×   126× 126× 126× 126×   230× 230× 230× 230×   136× 136× 136× 136×   202× 202× 202× 202×     969× 142× 55×   142× 87×       827× 96×   827× 109×       969× 6884× 6884× 2626× 2626×         1084× 223× 130×   223× 98×       1084× 147×   1084× 87×       1084× 8108× 8108× 2433× 2433×         2208× 420× 350×   420× 187×       1788× 91×   1788× 115×       2208× 27166× 27166× 9332× 9332×         1981× 412× 365×   412× 186×       1569× 69×   1569× 125×       1981× 21893× 21893× 5807× 5807×       907169× 907169× 485286× 485286× 485286× 485286× 485286×     421883× 421883× 421883× 421883×     44529× 2376×   42153× 42153× 42153×          
define(["require", "exports", "../../primitives/rect", "../../utility/diagram-util", "./quad"], function (require, exports, rect_1, diagram_util_1, quad_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SpatialSearch = (function () {
        function SpatialSearch(objectTable) {
            this.quadSize = 500;
            this.objectTable = objectTable;
            this.parentQuad = new quad_1.Quad(0, 0, this.quadSize * 2, this.quadSize * 2, this);
            this.pageLeft = Number.MAX_VALUE;
            this.pageRight = -Number.MAX_VALUE;
            this.pageTop = Number.MAX_VALUE;
            this.pageBottom = -Number.MAX_VALUE;
            this.quadTable = {};
        }
        SpatialSearch.prototype.removeFromAQuad = function (node) {
            if (this.quadTable[node.id]) {
                var quad = this.quadTable[node.id];
                var index = this.objectIndex(quad.objects, node);
                Eif (index !== -1) {
                    quad.objects.splice(index, 1);
                    this.update(quad);
                    delete this.quadTable[node.id];
                }
            }
        };
        SpatialSearch.prototype.update = function (quad) {
            if (quad.parent && quad.objects.length === 0 && quad.first == null && quad.second == null
                && quad.third == null && quad.fourth == null) {
                var parent_1 = quad.parent;
                if (parent_1.first === quad) {
                    parent_1.first = null;
                }
                else if (parent_1.second === quad) {
                    parent_1.second = null;
                }
                else if (parent_1.third === quad) {
                    parent_1.third = null;
                }
                else Eif (parent_1.fourth === quad) {
                    parent_1.fourth = null;
                }
                this.update(quad.parent);
            }
            else {
                if (quad === this.parentQuad && !quad.first && !quad.second && !quad.third && !quad.fourth) {
                    quad.left = 0;
                    quad.width = 1000;
                    quad.top = 0;
                    quad.height = 1000;
                }
                return;
            }
        };
        SpatialSearch.prototype.addIntoAQuad = function (node) {
            var quad = this.parentQuad.addIntoAQuad(node);
            this.quadTable[node.id] = quad;
        };
        SpatialSearch.prototype.objectIndex = function (objects, node) {
            for (var i = 0; i < objects.length; i++) {
                if ((objects[parseInt(i.toString(), 10)]).id === node.id) {
                    return i;
                }
            }
            return -1;
        };
        SpatialSearch.prototype.updateQuad = function (node) {
            this.setCurrentNode(node);
            var nodBounds = node.outerBounds;
            if (!(!isNaN(nodBounds.x) && !isNaN(nodBounds.y) &&
                !isNaN(nodBounds.width) && !isNaN(nodBounds.height))) {
                return false;
            }
            if (this.quadTable[node.id]) {
                var quad = this.quadTable[node.id];
                if (!quad.isContained()) {
                    this.removeFromAQuad(node);
                    this.addIntoAQuad(node);
                }
            }
            else {
                this.addIntoAQuad(node);
            }
            if (this.isWithinPageBounds(nodBounds) &&
                this.leftElement !== node &&
                this.topElement !== node &&
                this.rightElement !== node &&
                this.bottomElement !== node) {
            }
            else {
                var modified = false;
                if (this.pageLeft !== this.childLeft || node !== this.leftElement) {
                    if (this.pageLeft >= this.childLeft) {
                        this.pageLeft = this.childLeft;
                        this.leftElement = node;
                        modified = true;
                    }
                    else if (node === this.leftElement) {
                        this.pageLeft = Number.MAX_VALUE;
                        this.findLeft(this.parentQuad);
                        modified = true;
                    }
                }
                if (this.pageTop !== this.childTop || node !== this.topElement) {
                    if (this.pageTop >= this.childTop) {
                        this.pageTop = this.childTop;
                        this.topElement = node;
                        modified = true;
                    }
                    else if (node === this.topElement) {
                        this.pageTop = Number.MAX_VALUE;
                        this.findTop(this.parentQuad);
                        modified = true;
                    }
                }
                if (this.pageBottom !== this.childBottom || node !== this.bottomElement) {
                    if (this.pageBottom <= this.childBottom) {
                        modified = true;
                        this.pageBottom = this.childBottom;
                        this.bottomElement = node;
                    }
                    else if (node === this.bottomElement) {
                        this.pageBottom = -Number.MAX_VALUE;
                        this.findBottom(this.parentQuad);
                        modified = true;
                    }
                }
                if (this.pageRight !== this.childRight || node !== this.rightElement) {
                    if (this.pageRight <= this.childRight) {
                        this.pageRight = this.childRight;
                        this.rightElement = node;
                        modified = true;
                    }
                    else if (node === this.rightElement) {
                        this.pageRight = -Number.MAX_VALUE;
                        this.findRight(this.parentQuad);
                        modified = true;
                    }
                }
                return modified;
            }
            this.setCurrentNode(null);
            return false;
        };
        SpatialSearch.prototype.isWithinPageBounds = function (node) {
            if (node.left >= this.pageLeft && node.right <= this.pageRight && node.top >= this.pageTop
                && node.bottom <= this.pageBottom) {
                return true;
            }
            else {
                return false;
            }
        };
        SpatialSearch.prototype.findQuads = function (region) {
            var quads = [];
            this.parentQuad.findQuads(region, quads);
            return quads;
        };
        SpatialSearch.prototype.findObjects = function (region) {
            var quads = this.findQuads(region);
            var objects = [];
            for (var _i = 0, quads_1 = quads; _i < quads_1.length; _i++) {
                var quad = quads_1[_i];
                for (var _a = 0, _b = quad.objects; _a < _b.length; _a++) {
                    var obj = _b[_a];
                    if (obj.outerBounds.intersects(region) || diagram_util_1.isLabelFlipped(obj)) {
                        objects.push(this.objectTable[obj.id]);
                    }
                }
            }
            return objects;
        };
        SpatialSearch.prototype.updateBounds = function (node, isSwimLane) {
            var modified = false;
            if (isSwimLane) {
                if (this.topElement && node.id === this.topElement.id) {
                    modified = this.updateTop();
                }
                if (this.leftElement && node.id === this.leftElement.id) {
                    modified = this.updateLeft();
                }
                if (this.rightElement && node.id === this.rightElement.id) {
                    modified = this.updateRight();
                }
                if (this.bottomElement && node.id === this.bottomElement.id) {
                    modified = this.updateBottom();
                }
            }
            else {
                if (node === this.topElement) {
                    modified = this.updateTop();
                }
                if (node === this.leftElement) {
                    modified = this.updateLeft();
                }
                if (node === this.rightElement) {
                    modified = this.updateRight();
                }
                if (node === this.bottomElement) {
                    modified = this.updateBottom();
                }
            }
            return modified;
        };
        SpatialSearch.prototype.updateTop = function () {
            this.pageTop = Number.MAX_VALUE;
            this.topElement = null;
            this.findTop(this.parentQuad);
            return true;
        };
        SpatialSearch.prototype.updateBottom = function () {
            this.pageBottom = -Number.MAX_VALUE;
            this.bottomElement = null;
            this.findBottom(this.parentQuad);
            return true;
        };
        SpatialSearch.prototype.updateLeft = function () {
            this.pageLeft = Number.MAX_VALUE;
            this.leftElement = null;
            this.findLeft(this.parentQuad);
            return true;
        };
        SpatialSearch.prototype.updateRight = function () {
            this.pageRight = -Number.MAX_VALUE;
            this.rightElement = null;
            this.findRight(this.parentQuad);
            return true;
        };
        SpatialSearch.prototype.findBottom = function (quad) {
            {
                if (quad.third || quad.fourth) {
                    if (quad.third) {
                        this.findBottom(quad.third);
                    }
                    if (quad.fourth) {
                        this.findBottom(quad.fourth);
                    }
                }
                else {
                    if (quad.second) {
                        this.findBottom(quad.second);
                    }
                    if (quad.first) {
                        this.findBottom(quad.first);
                    }
                }
            }
            for (var _i = 0, _a = quad.objects; _i < _a.length; _i++) {
                var node = _a[_i];
                if (this.pageBottom <= node.outerBounds.bottom) {
                    this.pageBottom = node.outerBounds.bottom;
                    this.bottomElement = node;
                }
            }
        };
        SpatialSearch.prototype.findRight = function (quad) {
            {
                if (quad.second || quad.fourth) {
                    if (quad.second) {
                        this.findRight(quad.second);
                    }
                    if (quad.fourth) {
                        this.findRight(quad.fourth);
                    }
                }
                {
                    if (quad.first) {
                        this.findRight(quad.first);
                    }
                    if (quad.third) {
                        this.findRight(quad.third);
                    }
                }
            }
            for (var _i = 0, _a = quad.objects; _i < _a.length; _i++) {
                var node = _a[_i];
                if (this.pageRight <= node.outerBounds.right) {
                    this.pageRight = node.outerBounds.right;
                    this.rightElement = node;
                }
            }
        };
        SpatialSearch.prototype.findLeft = function (quad) {
            {
                if (quad.first || quad.third) {
                    if (quad.first) {
                        this.findLeft(quad.first);
                    }
                    if (quad.third) {
                        this.findLeft(quad.third);
                    }
                }
                else {
                    if (quad.second) {
                        this.findLeft(quad.second);
                    }
                    if (quad.fourth) {
                        this.findLeft(quad.fourth);
                    }
                }
            }
            for (var _i = 0, _a = quad.objects; _i < _a.length; _i++) {
                var node = _a[_i];
                if (this.pageLeft >= node.outerBounds.left) {
                    this.pageLeft = node.outerBounds.left;
                    this.leftElement = node;
                }
            }
        };
        SpatialSearch.prototype.findTop = function (quad) {
            {
                if (quad.first || quad.second) {
                    if (quad.first) {
                        this.findTop(quad.first);
                    }
                    if (quad.second) {
                        this.findTop(quad.second);
                    }
                }
                else {
                    if (quad.third) {
                        this.findTop(quad.third);
                    }
                    if (quad.fourth) {
                        this.findTop(quad.fourth);
                    }
                }
            }
            for (var _i = 0, _a = quad.objects; _i < _a.length; _i++) {
                var node = _a[_i];
                if (this.pageTop >= node.outerBounds.top) {
                    this.pageTop = node.outerBounds.top;
                    this.topElement = node;
                }
            }
        };
        SpatialSearch.prototype.setCurrentNode = function (node) {
            this.childNode = node;
            if (node) {
                var r = node.outerBounds;
                this.childLeft = Number(r.left);
                this.childTop = Number(r.top);
                this.childRight = Number(r.right);
                this.childBottom = Number(r.bottom);
            }
            else {
                this.childLeft = Number.MAX_VALUE;
                this.childTop = Number.MAX_VALUE;
                this.childRight = -Number.MAX_VALUE;
                this.childBottom = -Number.MAX_VALUE;
            }
        };
        SpatialSearch.prototype.getPageBounds = function (originX, originY) {
            if (this.pageLeft === Number.MAX_VALUE) {
                return new rect_1.Rect(0, 0, 0, 0);
            }
            var left = originX !== undefined ? Math.min(this.pageLeft, 0) : this.pageLeft;
            var top = originY !== undefined ? Math.min(this.pageTop, 0) : this.pageTop;
            return new rect_1.Rect(Math.round(left), Math.round(top), Math.round(this.pageRight - left), Math.round(this.pageBottom - top));
        };
        SpatialSearch.prototype.getQuad = function (node) {
            return this.quadTable[node.id];
        };
        return SpatialSearch;
    }());
    exports.SpatialSearch = SpatialSearch;
});