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

99.58% Statements 237/238
98.86% Branches 174/176
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   1483× 1483× 1483× 1483× 1483× 1483× 1483× 1483×   3253× 3176× 3176× 3176× 3176× 3176× 3176×       3237×   61× 61× 13×   48× 19×   29× 10×   19× 19×   61×     3176× 824× 824× 824× 824×   3176×     32644× 32644×   3176× 40553× 3176×         55067× 55067× 55067×     55061× 24229× 24229× 1812× 1812×       30832×   55061×             17061× 17061× 16098× 2951× 2951× 2951×   13147× 1032× 1032× 1032×     17061× 15992× 3275× 3275× 3275×   12717× 826× 826× 826×     17061× 16062× 5516× 5516× 5516×   10546× 327× 327× 327×     17061× 16176× 11470× 11470× 11470×   4706× 350× 350× 350×     17061×   38000× 38000×   55061×   41286×     13775×     19274× 19274× 19274×   17465× 17465× 17465× 20941× 20941× 143711× 143711× 32438×       17465×   1441× 1441× 63× 19×   63× 20×   63× 53×   63× 57×       1378× 100×   1378× 108×   1378× 144×   1378× 150×     1441×   119× 119× 119× 119×   207× 207× 207× 207×   128× 128× 128× 128×   197× 197× 197× 197×     816× 101× 48×   101× 53×       715× 86×   715× 95×       816× 5347× 5347× 2021× 2021×         952× 191× 109×   191× 82×       952× 136×   952× 78×       952× 7212× 7212× 2303× 2303×         1675× 288× 230×   288× 154×       1387× 56×   1387× 75×       1675× 20744× 20744× 6904× 6904×         1457× 265× 229×   265× 137×       1192× 62×   1192× 84×       1457× 15987× 15987× 3884× 3884×       125711× 125711× 87711× 87711× 87711× 87711× 87711×     38000× 38000× 38000× 38000×     35381× 2243×   33138× 33138× 33138×          
define(["require", "exports", "../../primitives/rect", "./quad"], function (require, exports, rect_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)) {
                        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;
});