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   1538× 1538× 1538× 1538× 1538× 1538× 1538× 1538×   3396× 3317× 3317× 3317× 3317× 3317× 3317×       3378×   61× 61× 13×   48× 19×   29× 10×   19× 19×   61×     3317× 856× 856× 856× 856×   3317×     33569× 33569×   3317× 44393× 3317×         58043× 58043× 58043×     58034× 26387× 26387× 1922× 1922×       31647×   58034×             17710× 17710× 16695× 3085× 3085× 3085×   13610× 1092× 1092× 1092×     17710× 16585× 3439× 3439× 3439×   13146× 867× 867× 867×     17710× 16663× 5851× 5851× 5851×   10812× 348× 348× 348×     17710× 16767× 11717× 11717× 11717×   5050× 376× 376× 376×     17710×   40324× 40324×   58034×   43847×     14187×     20557× 20557× 20557×   18640× 18640× 18640× 23140× 23140× 178564× 178564× 36421×       18640×   1474× 1474× 68× 23×   68× 24×   68× 56×   68× 60×       1406× 102×   1406× 110×   1406× 144×   1406× 166×     1474×   125× 125× 125× 125×   226× 226× 226× 226×   134× 134× 134× 134×   200× 200× 200× 200×     900× 125× 52×   125× 73×       775× 94×   775× 107×       900× 6210× 6210× 2406× 2406×         995× 201× 120×   201× 84×       995× 135×   995× 80×       995× 7566× 7566× 2286× 2286×         1781× 319× 255×   319× 161×       1462× 63×   1462× 76×       1781× 22300× 22300× 7532× 7532×         1579× 307× 266×   307× 165×       1272× 69×   1272× 87×       1579× 16941× 16941× 3983× 3983×       131936× 131936× 91612× 91612× 91612× 91612× 91612×     40324× 40324× 40324× 40324×     37431× 2305×   35126× 35126× 35126×          
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;
});