all files / list-view/ virtualization.js

97.51% Statements 509/522
92.72% Branches 331/357
100% Functions 49/49
97.51% Lines 509/522
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   22× 22×   534×     26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26× 26×   26×   50× 26× 11× 11×     15×       24× 11× 11×     13×       53× 53× 53× 23×     30×     28×     53×   53×   27×   26× 26× 26× 1311×   1311×       26×           79×       79× 79× 79× 79× 79× 79× 48×       31×   79× 79× 79× 79× 79×   79× 50× 50× 50× 23× 23×     17×       27× 27×     23×       79× 79×   79×   10× 10× 10× 250× 250×   10× 10×   40× 17× 119× 119× 119×       23× 87× 87× 87× 87×       425× 425×   33× 33×       392×   392×     425× 25× 23×   25× 24× 24× 24× 24×     24×     425× 82×   82× 82×       75×                 501× 501×   501× 501×     501× 225× 225× 225× 225× 214× 214×   225×           276× 276× 276×       501× 146× 13× 12× 12× 12×       133× 13× 13× 13×         50× 50× 50× 50× 49× 2101×     44× 44× 44×     2057×       50×   14× 14× 10× 10× 10×                                 104× 104×           104×                               10× 10× 10×                                                                                                   30× 30× 30× 30× 29× 29×           27×     30×   30× 30× 29× 29×   30×                   104× 99×             26× 26× 26×   26×     26×   26×         18× 18× 18× 10×         18× 18× 18× 18× 17× 17× 13×           26× 26×   31× 31× 31× 31× 31×   31× 26×     24×   24×   24×     21×   24×   23×     15× 15× 15× 15×       31× 31×   57× 57× 171× 34× 17×     17×         22× 31× 31× 31× 31×     29×             24× 24× 24× 24×                             152× 135×   135× 124× 124×   124× 124×   124× 124× 124×     124× 124×   124× 124× 124× 124×         124× 124×         124× 124×   124× 124×                 501× 501× 76× 76×   76×     425×   501× 501× 251×     76× 76×     76× 76×   76× 76×         28×   18× 18× 18×        
define(["require", "exports", "./list-view", "@syncfusion/ej2-base", "../common/list-base", "@syncfusion/ej2-data"], function (require, exports, list_view_1, ej2_base_1, list_base_1, ej2_data_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var listElementCount = 1.5;
    var windowElementCount = 3;
    var Virtualization = (function () {
        function Virtualization(instance) {
            this.elementDifference = 0;
            this.listViewInstance = instance;
        }
        Virtualization.prototype.isNgTemplate = function () {
            return !ej2_base_1.isNullOrUndefined(this.listViewInstance.templateRef) && typeof this.listViewInstance.templateRef !== 'string';
        };
        Virtualization.prototype.isVueFunctionTemplate = function () {
            return this.listViewInstance.isVue && typeof this.listViewInstance.template === 'function';
        };
        Virtualization.prototype.uiVirtualization = function () {
            this.wireScrollEvent(false);
            var isRendered = this.listViewInstance.isRendered;
            var firstIndex = isRendered && !ej2_base_1.isNullOrUndefined(this.uiFirstIndex) ? this.uiFirstIndex : 0;
            var curViewDS = this.listViewInstance.curViewDS;
            var firstDs = curViewDS.slice(firstIndex, firstIndex + 1);
            this.listViewInstance.ulElement = this.listViewInstance.curUL = list_base_1.ListBase.createList(this.listViewInstance.createElement, firstDs, this.listViewInstance.listBaseOption, null, this.listViewInstance);
            this.listViewInstance.contentContainer = this.listViewInstance.createElement('div', { className: list_view_1.classNames.container });
            this.listViewInstance.element.appendChild(this.listViewInstance.contentContainer);
            this.listViewInstance.contentContainer.appendChild(this.listViewInstance.ulElement);
            this.listItemHeight = this.listViewInstance.ulElement.firstElementChild.getBoundingClientRect().height;
            this.expectedDomItemCount = this.ValidateItemCount(10000);
            this.updateDOMItemCount();
            var lastIndex = isRendered && !ej2_base_1.isNullOrUndefined(this.uiLastIndex) ? this.uiLastIndex : this.domItemCount - 1;
            this.uiFirstIndex = firstIndex;
            this.uiLastIndex = lastIndex;
            var otherDs = curViewDS.slice(firstIndex + 1, lastIndex + 1);
            var listItems = list_base_1.ListBase.createListItemFromJson(this.listViewInstance.createElement, otherDs, this.listViewInstance.listBaseOption, null, null, this.listViewInstance);
            ej2_base_1.append(listItems, this.listViewInstance.ulElement);
            this.listViewInstance.liCollection = this.listViewInstance.curUL.querySelectorAll('li');
            this.topElement = this.listViewInstance.createElement('div');
            this.listViewInstance.ulElement.insertBefore(this.topElement, this.listViewInstance.ulElement.firstElementChild);
            this.bottomElement = this.listViewInstance.createElement('div');
            this.listViewInstance.ulElement.insertBefore(this.bottomElement, null);
            this.totalHeight = (Object.keys(curViewDS).length * this.listItemHeight) - (this.domItemCount * this.listItemHeight);
            this.topElement.style.height = isRendered ? this.topElementHeight + 'px' : '0px';
            this.bottomElement.style.height = isRendered ? (this.totalHeight - this.topElementHeight) + 'px' : this.totalHeight + 'px';
            this.topElementHeight = isRendered ? this.topElementHeight : 0;
            this.bottomElementHeight = isRendered ? (this.totalHeight - this.topElementHeight) : this.totalHeight;
            this.listDiff = isRendered ? this.listDiff : 0;
            if (isRendered) {
                this.listViewInstance.element.scrollTop = this.listViewInstance.previousScrollTop;
            }
            this.uiIndicesInitialization();
        };
        Virtualization.prototype.wireScrollEvent = function (destroy) {
            if (!destroy) {
                if (this.listViewInstance.isWindow) {
                    this.onVirtualScroll = this.onVirtualUiScroll.bind(this);
                    window.addEventListener('scroll', this.onVirtualScroll);
                }
                else {
                    ej2_base_1.EventHandler.add(this.listViewInstance.element, 'scroll', this.onVirtualUiScroll, this);
                }
            }
            else {
                if (this.listViewInstance.isWindow === true) {
                    window.removeEventListener('scroll', this.onVirtualScroll);
                    window.removeEventListener('scroll', this.updateUl);
                }
                else {
                    ej2_base_1.EventHandler.remove(this.listViewInstance.element, 'scroll', this.onVirtualUiScroll);
                }
            }
        };
        Virtualization.prototype.ValidateItemCount = function (dataSourceLength) {
            var height = parseFloat(ej2_base_1.formatUnit(this.listViewInstance.height));
            var itemCount;
            if (this.listViewInstance.isWindow) {
                itemCount = Math.round((window.innerHeight / this.listItemHeight) * windowElementCount);
            }
            else {
                if (typeof this.listViewInstance.height === 'string' && this.listViewInstance.height.indexOf('%') !== -1) {
                    itemCount = Math.round((this.listViewInstance.element.getBoundingClientRect().height / this.listItemHeight) * listElementCount);
                }
                else {
                    itemCount = Math.round((height / this.listItemHeight) * listElementCount);
                }
            }
            if (itemCount > dataSourceLength) {
                itemCount = dataSourceLength;
            }
            return itemCount;
        };
        Virtualization.prototype.updateDOMItemCount = function () {
            this.domItemCount = this.ValidateItemCount(Object.keys(this.listViewInstance.curViewDS).length);
        };
        Virtualization.prototype.uiIndicesInitialization = function () {
            this.uiIndices = { 'activeIndices': [], 'disabledItemIndices': [], 'hiddenItemIndices': [] };
            var data = this.listViewInstance.curViewDS;
            for (var i = 0; i < data.length; i++) {
                if (this.listViewInstance.showCheckBox && data[i][this.listViewInstance.fields.isChecked]) {
                    this.uiIndices.activeIndices.push(i);
                }
                if (!ej2_base_1.isNullOrUndefined(data[parseInt(i.toString(), 10)][this.listViewInstance.fields.enabled]) &&
                    !data[i][this.listViewInstance.fields.enabled]) {
                    (this.uiIndices.disabledItemIndices.push(i));
                }
            }
            if (this.isNgTemplate()) {
                var items = this.listViewInstance.element.querySelectorAll('.' + list_view_1.classNames.listItem);
                for (var index = 0; index < items.length; index++) {
                    items[index].context = this.listViewInstance.viewContainerRef.get(index).context;
                }
            }
        };
        Virtualization.prototype.refreshItemHeight = function () {
            Eif (this.listViewInstance.curViewDS.length) {
                var curViewDS = this.listViewInstance.curViewDS;
                this.listItemHeight = this.topElement.nextSibling.getBoundingClientRect().height;
                this.totalHeight = (Object.keys(curViewDS).length * this.listItemHeight) - (this.domItemCount * this.listItemHeight);
                this.bottomElementHeight = this.totalHeight;
                this.bottomElement.style.height = this.totalHeight + 'px';
            }
        };
        Virtualization.prototype.getscrollerHeight = function (startingHeight) {
            return this.listViewInstance.isWindow ? (((pageYOffset - startingHeight) <= 0) ? 0 :
                (pageYOffset - startingHeight)) : ((this.listViewInstance.element.scrollTop - startingHeight) <= 0) ? 0 :
                (this.listViewInstance.element.scrollTop - startingHeight);
        };
        Virtualization.prototype.onVirtualUiScroll = function () {
            var _a;
            var startingHeight;
            var curViewDS = this.listViewInstance.curViewDS;
            this.listItemHeight = ej2_base_1.select('.e-list-item', this.listViewInstance.element).getBoundingClientRect().height;
            this.totalHeight = (Object.keys(curViewDS).length * this.listItemHeight) - (this.domItemCount * this.listItemHeight);
            if (this.listViewInstance.isWindow) {
                startingHeight = this.listViewInstance.ulElement.getBoundingClientRect().top -
                    document.documentElement.getBoundingClientRect().top;
            }
            else {
                startingHeight = this.listViewInstance.headerEle ? this.listViewInstance.headerEle.getBoundingClientRect().height : 0;
            }
            this.scrollPosition = ej2_base_1.isNullOrUndefined(this.scrollPosition) ? 0 : this.scrollPosition;
            var scroll = this.getscrollerHeight(startingHeight);
            this.topElementHeight = this.listItemHeight * Math.floor(scroll / this.listItemHeight);
            this.bottomElementHeight = this.totalHeight - this.topElementHeight;
            _a = scroll <= this.totalHeight ?
                [this.topElementHeight, this.bottomElementHeight] : [this.totalHeight, 0], this.topElementHeight = _a[0], this.bottomElementHeight = _a[1];
            if (this.topElementHeight !== parseFloat(this.topElement.style.height)) {
                this.topElement.style.height = this.topElementHeight + 'px';
                this.bottomElement.style.height = this.bottomElementHeight + 'px';
                if (scroll > this.scrollPosition) {
                    var listDiff = Math.round(((this.topElementHeight / this.listItemHeight) - this.listDiff));
                    if (listDiff > (this.expectedDomItemCount + 5)) {
                        this.onLongScroll(listDiff, true);
                    }
                    else {
                        this.onNormalScroll(listDiff, true);
                    }
                }
                else {
                    var listDiff = Math.round((this.listDiff - (this.topElementHeight / this.listItemHeight)));
                    if (listDiff > (this.expectedDomItemCount + 5)) {
                        this.onLongScroll(listDiff, false);
                    }
                    else {
                        this.onNormalScroll(listDiff, false);
                    }
                }
            }
            this.listDiff = Math.round(this.topElementHeight / this.listItemHeight);
            if (typeof this.listViewInstance.onUIScrolled === 'function') {
                this.listViewInstance.onUIScrolled();
            }
            this.scrollPosition = scroll;
        };
        Virtualization.prototype.onLongScroll = function (listDiff, isScrollingDown) {
            var index = isScrollingDown ? (this.uiFirstIndex + listDiff) : (this.uiFirstIndex - listDiff);
            var elements = this.listViewInstance.ulElement.querySelectorAll('li');
            for (var i = 0; i < elements.length; i++) {
                this.updateUI(elements[i], index);
                index++;
            }
            this.uiLastIndex = isScrollingDown ? (this.uiLastIndex + listDiff) : (this.uiLastIndex - listDiff);
            this.uiFirstIndex = isScrollingDown ? (this.uiFirstIndex + listDiff) : (this.uiFirstIndex - listDiff);
        };
        Virtualization.prototype.onNormalScroll = function (listDiff, isScrollingDown) {
            if (isScrollingDown) {
                for (var i = 0; i < listDiff; i++) {
                    var index = ++this.uiLastIndex;
                    this.updateUI(this.topElement.nextElementSibling, index, this.bottomElement);
                    this.uiFirstIndex++;
                }
            }
            else {
                for (var i = 0; i < listDiff; i++) {
                    var index = --this.uiFirstIndex;
                    var target = this.topElement.nextSibling;
                    this.updateUI(this.bottomElement.previousElementSibling, index, target);
                    this.uiLastIndex--;
                }
            }
        };
        Virtualization.prototype.updateUiContent = function (element, index) {
            var curViewDs = this.listViewInstance.curViewDS;
            if (typeof this.listViewInstance.dataSource[0] === 'string' ||
                typeof this.listViewInstance.dataSource[0] === 'number') {
                element.dataset.uid = list_base_1.ListBase.generateId();
                element.getElementsByClassName(list_view_1.classNames.listItemText)[0].innerHTML =
                    this.listViewInstance.curViewDS[index].toString();
            }
            else {
                element.dataset.uid = (curViewDs[parseInt(index.toString(), 10)][this.listViewInstance.fields.id]) ?
                    (curViewDs[parseInt(index.toString(), 10)][this.listViewInstance.fields.id]) : list_base_1.ListBase.generateId();
                element.getElementsByClassName(list_view_1.classNames.listItemText)[0].innerHTML =
                    (curViewDs[parseInt(index.toString(), 10)][this.listViewInstance.fields.text]);
            }
            if (this.listViewInstance.showIcon) {
                if (element.querySelector('.' + list_view_1.classNames.listIcon)) {
                    ej2_base_1.detach(element.querySelector('.' + list_view_1.classNames.listIcon));
                }
                if (this.listViewInstance.curViewDS[index][this.listViewInstance.fields.iconCss]) {
                    var textContent = element.querySelector('.' + list_view_1.classNames.textContent);
                    var curViewDS = this.listViewInstance.curViewDS[index];
                    var iconCss = curViewDS[this.listViewInstance.fields.iconCss].toString();
                    var target = this.listViewInstance.createElement('div', {
                        className: list_view_1.classNames.listIcon + ' ' + iconCss
                    });
                    textContent.insertBefore(target, element.querySelector('.' + list_view_1.classNames.listItemText));
                }
            }
            if (this.listViewInstance.showCheckBox && this.listViewInstance.fields.groupBy) {
                if (!this.checkListWrapper) {
                    this.checkListWrapper = this.listViewInstance.curUL.querySelector('.' + list_view_1.classNames.checkboxWrapper).cloneNode(true);
                }
                var textContent = element.querySelector('.' + list_view_1.classNames.textContent);
                if (this.listViewInstance.curViewDS[index].isHeader) {
                    Eif (element.querySelector('.' + list_view_1.classNames.checkboxWrapper)) {
                        element.classList.remove(list_view_1.classNames.checklist);
                        textContent.classList.remove(list_view_1.classNames.checkbox);
                        ej2_base_1.detach(element.querySelector('.' + list_view_1.classNames.checkboxWrapper));
                    }
                }
                else {
                    if (!element.querySelector('.' + list_view_1.classNames.checkboxWrapper)) {
                        element.classList.add(list_view_1.classNames.checklist);
                        textContent.classList.add(list_view_1.classNames.checkbox);
                        Eif (this.listViewInstance.checkBoxPosition === 'Left') {
                            textContent.classList.add('e-checkbox-left');
                        }
                        else {
                            textContent.classList.add('e-checkbox-right');
                        }
                        textContent.append(this.checkListWrapper.cloneNode(true));
                    }
                }
            }
        };
        Virtualization.prototype.changeElementAttributes = function (element, index) {
            element.classList.remove(list_view_1.classNames.disable);
            if (this.uiIndices.disabledItemIndices.length && this.uiIndices.disabledItemIndices.indexOf(index) !== -1) {
                element.classList.add(list_view_1.classNames.disable);
            }
            element.style.display = '';
            Iif (this.uiIndices.hiddenItemIndices.length && this.uiIndices.hiddenItemIndices.indexOf(index) !== -1) {
                element.style.display = 'none';
            }
            if (this.listViewInstance.showCheckBox) {
                var checklistElement = element.querySelector('.' + list_view_1.classNames.checkboxWrapper);
                element.classList.remove(list_view_1.classNames.selected);
                element.classList.remove(list_view_1.classNames.focused);
                if (checklistElement) {
                    checklistElement.removeAttribute('aria-checked');
                    checklistElement.firstElementChild.classList.remove(list_view_1.classNames.checked);
                }
                if (this.uiIndices.activeIndices.length && this.uiIndices.activeIndices.indexOf(index) !== -1 &&
                    !this.listViewInstance.curUL.querySelector(list_view_1.classNames.selected)) {
                    element.classList.add(list_view_1.classNames.selected);
                    checklistElement.firstElementChild.classList.add(list_view_1.classNames.checked);
                    checklistElement.setAttribute('aria-checked', 'true');
                    if (this.activeIndex === index) {
                        element.classList.add(list_view_1.classNames.focused);
                    }
                }
            }
            else {
                element.classList.remove(list_view_1.classNames.selected);
                element.removeAttribute('aria-selected');
                if (!ej2_base_1.isNullOrUndefined(this.activeIndex) && this.activeIndex === index &&
                    !this.listViewInstance.curUL.querySelector(list_view_1.classNames.selected)) {
                    element.classList.add(list_view_1.classNames.selected);
                    element.setAttribute('aria-selected', 'true');
                }
            }
            if (this.listViewInstance.fields.groupBy) {
                if (this.listViewInstance.curViewDS[index].isHeader) {
                    if (element.classList.contains(list_view_1.classNames.listItem)) {
                        element.classList.remove(list_view_1.classNames.listItem);
                        element.setAttribute('role', 'group');
                        element.classList.add(list_view_1.classNames.groupListItem);
                    }
                }
                else {
                    if (element.classList.contains(list_view_1.classNames.groupListItem)) {
                        element.classList.remove(list_view_1.classNames.groupListItem);
                        element.setAttribute('role', 'listitem');
                        element.classList.add(list_view_1.classNames.listItem);
                    }
                }
            }
        };
        Virtualization.prototype.findDSAndIndexFromId = function (ds, fields) {
            var _this = this;
            var resultJSON = {};
            fields = this.listViewInstance.getElementUID(fields);
            if (!ej2_base_1.isNullOrUndefined(fields)) {
                ds.some(function (data, index) {
                    if ((fields[_this.listViewInstance.fields.id] &&
                        fields[_this.listViewInstance.fields.id]
                            === (data[_this.listViewInstance.fields.id] && data[_this.listViewInstance.fields.id]) || fields === data)) {
                        resultJSON.index = index;
                        resultJSON.data = data;
                        return true;
                    }
                    else {
                        return false;
                    }
                });
            }
            return resultJSON;
        };
        Virtualization.prototype.getSelectedItems = function () {
            var _this = this;
            if (!ej2_base_1.isNullOrUndefined(this.activeIndex) || (this.listViewInstance.showCheckBox && this.uiIndices.activeIndices.length)) {
                var dataCollection = [];
                var textCollection = [];
                if (typeof this.listViewInstance.dataSource[0] === 'string' ||
                    typeof this.listViewInstance.dataSource[0] === 'number') {
                    var curViewDS_1 = this.listViewInstance.curViewDS;
                    if (this.listViewInstance.showCheckBox) {
                        var indices = this.uiIndices.activeIndices;
                        for (var i = 0; i < indices.length; i++) {
                            dataCollection.push(curViewDS_1[indices[i]]);
                        }
                        return {
                            text: dataCollection,
                            data: dataCollection,
                            index: this.uiIndices.activeIndices.map(function (index) {
                                return _this.listViewInstance.dataSource.indexOf(curViewDS_1[index]);
                            })
                        };
                    }
                    else {
                        return {
                            text: curViewDS_1[this.activeIndex],
                            data: curViewDS_1[this.activeIndex],
                            index: this.listViewInstance.dataSource.indexOf(curViewDS_1[this.activeIndex])
                        };
                    }
                }
                else {
                    var curViewDS_2 = this.listViewInstance.curViewDS;
                    var text = this.listViewInstance.fields.text;
                    if (this.listViewInstance.showCheckBox) {
                        var indexArray = this.uiIndices.activeIndices;
                        for (var i = 0; i < indexArray.length; i++) {
                            textCollection.push(curViewDS_2[indexArray[i]]["" + text]);
                            dataCollection.push(curViewDS_2[indexArray[parseInt(i.toString(), 10)]]);
                        }
                        var dataSource_1 = this.listViewInstance.dataSource instanceof ej2_data_1.DataManager
                            ? curViewDS_2 : this.listViewInstance.dataSource;
                        return {
                            text: textCollection,
                            data: dataCollection,
                            index: this.uiIndices.activeIndices.map(function (index) {
                                return dataSource_1.indexOf(curViewDS_2[index]);
                            })
                        };
                    }
                    else {
                        var dataSource = this.listViewInstance.dataSource instanceof ej2_data_1.DataManager
                            ? curViewDS_2 : this.listViewInstance.dataSource;
                        return {
                            text: curViewDS_2[this.activeIndex][this.listViewInstance.fields.text],
                            data: curViewDS_2[this.activeIndex],
                            index: dataSource.indexOf(curViewDS_2[this.activeIndex])
                        };
                    }
                }
            }
            else {
                return undefined;
            }
        };
        Virtualization.prototype.selectItem = function (obj) {
            var _this = this;
            var resutJSON = this.findDSAndIndexFromId(this.listViewInstance.curViewDS, obj);
            if (Object.keys(resutJSON).length) {
                var isSelected = this.activeIndex === resutJSON.index;
                var isChecked_1;
                this.activeIndex = resutJSON.index;
                if (this.listViewInstance.showCheckBox) {
                    if (this.uiIndices.activeIndices.indexOf(resutJSON.index) === -1) {
                        isChecked_1 = true;
                        this.uiIndices.activeIndices.push(resutJSON.index);
                    }
                    else {
                        isChecked_1 = false;
                        this.uiIndices.activeIndices.splice(this.uiIndices.activeIndices.indexOf(resutJSON.index), 1);
                    }
                    if (this.listViewInstance.curUL.querySelector('.' + list_view_1.classNames.focused)) {
                        this.listViewInstance.curUL.querySelector('.' + list_view_1.classNames.focused).classList.remove(list_view_1.classNames.focused);
                    }
                }
                if (this.listViewInstance.getLiFromObjOrElement(obj)) {
                    if (this.listViewInstance.showCheckBox) {
                        this.listViewInstance.setCheckboxLI(this.listViewInstance.getLiFromObjOrElement(obj));
                    }
                    else {
                        this.listViewInstance.setSelectLI(this.listViewInstance.getLiFromObjOrElement(obj));
                    }
                }
                else {
                    var eventArgs_1;
                    if (typeof this.listViewInstance.dataSource[0] === 'string' ||
                        typeof this.listViewInstance.dataSource[0] === 'number') {
                        eventArgs_1 = {
                            text: this.listViewInstance.curViewDS[this.activeIndex],
                            data: this.listViewInstance.curViewDS[this.activeIndex],
                            index: this.activeIndex
                        };
                    }
                    else {
                        var curViewDS = this.listViewInstance.curViewDS;
                        eventArgs_1 = {
                            text: curViewDS[this.activeIndex][this.listViewInstance.fields.text],
                            data: curViewDS[this.activeIndex],
                            index: this.activeIndex
                        };
                    }
                    if (this.listViewInstance.showCheckBox) {
                        this.listViewInstance.trigger('select', eventArgs_1, function (observedArgs) {
                            Iif (observedArgs.cancel) {
                                if (!isChecked_1) {
                                    eventArgs_1.isChecked = isChecked_1;
                                    _this.uiIndices.activeIndices.push(resutJSON.index);
                                }
                                else {
                                    eventArgs_1.isChecked = !isChecked_1;
                                    _this.uiIndices.activeIndices.splice(_this.uiIndices.activeIndices.indexOf(resutJSON.index), 1);
                                }
                            }
                        });
                    }
                    else Eif (!isSelected) {
                        this.listViewInstance.removeSelect();
                        this.listViewInstance.trigger('select', eventArgs_1, function (observedArgs) {
                            Iif (observedArgs.cancel) {
                                _this.activeIndex = undefined;
                            }
                        });
                    }
                }
            }
            else Eif (ej2_base_1.isNullOrUndefined(obj) && !this.listViewInstance.showCheckBox) {
                this.listViewInstance.removeSelect();
                this.activeIndex = undefined;
            }
        };
        Virtualization.prototype.enableItem = function (obj) {
            var resutJSON = this.findDSAndIndexFromId(this.listViewInstance.curViewDS, obj);
            if (Object.keys(resutJSON).length) {
                this.uiIndices.disabledItemIndices.splice(this.uiIndices.disabledItemIndices.indexOf(resutJSON.index), 1);
            }
        };
        Virtualization.prototype.disableItem = function (obj) {
            var resutJSON = this.findDSAndIndexFromId(this.listViewInstance.curViewDS, obj);
            if (Object.keys(resutJSON).length && this.uiIndices.disabledItemIndices.indexOf(resutJSON.index) === -1) {
                this.uiIndices.disabledItemIndices.push(resutJSON.index);
            }
        };
        Virtualization.prototype.showItem = function (obj) {
            var resutJSON = this.findDSAndIndexFromId(this.listViewInstance.curViewDS, obj);
            if (Object.keys(resutJSON).length) {
                this.uiIndices.hiddenItemIndices.splice(this.uiIndices.hiddenItemIndices.indexOf(resutJSON.index), 1);
            }
        };
        Virtualization.prototype.hideItem = function (obj) {
            var resutJSON = this.findDSAndIndexFromId(this.listViewInstance.curViewDS, obj);
            if (Object.keys(resutJSON).length && this.uiIndices.hiddenItemIndices.indexOf(resutJSON.index) === -1) {
                this.uiIndices.hiddenItemIndices.push(resutJSON.index);
            }
        };
        Virtualization.prototype.removeItem = function (obj) {
            var dataSource;
            var curViewDS = this.listViewInstance.curViewDS;
            var resutJSON = this.findDSAndIndexFromId(curViewDS, obj);
            if (Object.keys(resutJSON).length) {
                dataSource = resutJSON.data;
                if (curViewDS[resutJSON.index - 1] &&
                    curViewDS[resutJSON.index - 1].isHeader &&
                    (curViewDS[resutJSON.index - 1])
                        .items.length === 1) {
                    this.removeUiItem(resutJSON.index - 1);
                    this.removeUiItem(resutJSON.index - 1);
                }
                else {
                    this.removeUiItem(resutJSON.index);
                }
            }
            var listDataSource = this.listViewInstance.dataSource instanceof ej2_data_1.DataManager
                ? this.listViewInstance.localData : this.listViewInstance.dataSource;
            var index = listDataSource.indexOf(dataSource);
            if (index !== -1) {
                listDataSource.splice(index, 1);
                this.listViewInstance.setViewDataSource(listDataSource);
            }
            this.listViewInstance.liCollection =
                this.listViewInstance.curUL.querySelectorAll('li');
        };
        Virtualization.prototype.setCheckboxLI = function (li, e) {
            var index = Array.prototype.indexOf.call(this.listViewInstance.curUL.querySelectorAll('li'), li) + this.uiFirstIndex;
            this.activeIndex = Array.prototype.indexOf.call(this.listViewInstance.curUL.querySelectorAll('li'), li) + this.uiFirstIndex;
            Eif (li.classList.contains(list_view_1.classNames.selected)) {
                if (this.uiIndices.activeIndices.indexOf(index) === -1) {
                    this.uiIndices.activeIndices.push(index);
                }
            }
            else {
                this.uiIndices.activeIndices.splice(this.uiIndices.activeIndices.indexOf(index), 1);
            }
        };
        Virtualization.prototype.setSelectLI = function (li, e) {
            this.activeIndex = Array.prototype.indexOf.call(this.listViewInstance.curUL.querySelectorAll('li'), li) + this.uiFirstIndex;
        };
        Virtualization.prototype.checkedItem = function (checked) {
            if (checked) {
                this.uiIndices.activeIndices = [];
                this.activeIndex = undefined;
                var data = this.listViewInstance.curViewDS;
                for (var index = 0; index < data.length; index++) {
                    if (!data[index].isHeader) {
                        this.uiIndices.activeIndices.push(index);
                    }
                }
            }
            else {
                this.activeIndex = undefined;
                this.uiIndices.activeIndices = [];
            }
        };
        Virtualization.prototype.addUiItem = function (index) {
            var curViewDs = this.listViewInstance.curViewDS;
            this.changeUiIndices(index, true);
            if (this.activeIndex && this.activeIndex >= index) {
                this.activeIndex++;
            }
            if (this.listViewInstance.showCheckBox &&
                curViewDs[index][this.listViewInstance.fields.isChecked]) {
                this.uiIndices.activeIndices.push(index);
            }
            if (!parseFloat(this.bottomElement.style.height) && !parseFloat(this.topElement.style.height)) {
                this.bottomElement.style.height = parseFloat(this.bottomElement.style.height) + this.listItemHeight + 'px';
            }
            if (parseFloat(this.bottomElement.style.height)) {
                var liItem = this.listViewInstance.curUL.lastElementChild.previousSibling;
                var target = this.listViewInstance.getLiFromObjOrElement(curViewDs[index + 1]) ||
                    this.listViewInstance.getLiFromObjOrElement(curViewDs[index + 2]);
                if (target) {
                    this.bottomElement.style.height = parseFloat(this.bottomElement.style.height) + this.listItemHeight + 'px';
                    this.updateUI(liItem, index, target);
                }
            }
            else {
                var liItem = this.listViewInstance.curUL.firstElementChild.nextSibling;
                var target = void 0;
                if ((Object.keys(this.listViewInstance.curViewDS).length - 1) === index) {
                    target = this.listViewInstance.curUL.lastElementChild;
                }
                else {
                    target = this.listViewInstance.getLiFromObjOrElement(curViewDs[index + 1]) ||
                        this.listViewInstance.getLiFromObjOrElement(curViewDs[index + 2]);
                }
                this.topElement.style.height = parseFloat(this.topElement.style.height) + this.listItemHeight + 'px';
                this.uiFirstIndex++;
                this.uiLastIndex++;
                if (target) {
                    this.updateUI(liItem, index, target);
                    if (this.listViewInstance.isWindow === true) {
                        window.scrollTo(0, (pageYOffset + this.listItemHeight));
                    }
                    else {
                        this.listViewInstance.element.scrollTop += this.listItemHeight;
                    }
                }
            }
            this.totalHeight += this.listItemHeight;
            this.listDiff = Math.round(parseFloat(this.topElement.style.height) / this.listItemHeight);
        };
        Virtualization.prototype.removeUiItem = function (index) {
            this.totalHeight -= this.listItemHeight;
            var curViewDS = this.listViewInstance.curViewDS[index];
            var liItem = this.listViewInstance.getLiFromObjOrElement(curViewDS);
            this.listViewInstance.curViewDS.splice(index, 1);
            if (this.activeIndex && this.activeIndex >= index) {
                this.activeIndex--;
            }
            if (liItem) {
                if (this.domItemCount > Object.keys(this.listViewInstance.curViewDS).length) {
                    ej2_base_1.detach(liItem);
                    this.domItemCount--;
                    this.uiLastIndex--;
                    this.totalHeight = 0;
                }
                else {
                    if (liItem.classList.contains(list_view_1.classNames.disable)) {
                        liItem.classList.remove(list_view_1.classNames.disable);
                        this.uiIndices.disabledItemIndices.splice(this.uiIndices.disabledItemIndices.indexOf(index), 1);
                    }
                    if (liItem.style.display === 'none') {
                        liItem.style.display = '';
                        this.uiIndices.hiddenItemIndices.splice(this.uiIndices.hiddenItemIndices.indexOf(index), 1);
                    }
                    if (this.listViewInstance.showCheckBox && liItem.classList.contains(list_view_1.classNames.selected)) {
                        this.listViewInstance.removeSelect();
                        this.uiIndices.activeIndices.splice(this.uiIndices.activeIndices.indexOf(index), 1);
                        var checklistElement = liItem.querySelector('.' + list_view_1.classNames.checkboxWrapper);
                        checklistElement.removeAttribute('aria-checked');
                        checklistElement.firstElementChild.classList.remove(list_view_1.classNames.checked);
                        if (liItem.classList.contains(list_view_1.classNames.focused)) {
                            liItem.classList.remove(list_view_1.classNames.focused);
                            this.activeIndex = undefined;
                        }
                    }
                    else if (liItem.classList.contains(list_view_1.classNames.selected)) {
                        this.listViewInstance.removeSelect();
                        this.activeIndex = undefined;
                    }
                    if (!parseFloat(this.bottomElement.style.height) && !parseFloat(this.topElement.style.height)) {
                        this.updateUI(liItem, this.uiLastIndex, this.bottomElement);
                    }
                    else if (parseFloat(this.bottomElement.style.height)) {
                        this.bottomElement.style.height = parseFloat(this.bottomElement.style.height) - this.listItemHeight + 'px';
                        this.updateUI(liItem, this.uiLastIndex, this.bottomElement);
                    }
                    else {
                        this.topElement.style.height = parseFloat(this.topElement.style.height) - this.listItemHeight + 'px';
                        this.updateUI(liItem, (this.uiFirstIndex - 1), this.topElement.nextSibling);
                        this.uiLastIndex--;
                        this.uiFirstIndex--;
                    }
                }
            }
            this.changeUiIndices(index, false);
            this.listDiff = Math.round(parseFloat(this.topElement.style.height) / this.listItemHeight);
        };
        Virtualization.prototype.changeUiIndices = function (index, increment) {
            var keys = Object.keys(this.uiIndices);
            for (var ind = 0; ind < keys.length; ind++) {
                this.uiIndices[keys[ind]] = this.uiIndices[keys[ind]].map(function (i) {
                    if (i >= index) {
                        return increment ? ++i : --i;
                    }
                    else {
                        return i;
                    }
                });
            }
        };
        Virtualization.prototype.addItem = function (data, fields, dataSource, index) {
            for (var i = 0; i < data.length; i++) {
                var currentItem = data[i];
                dataSource = this.listViewInstance.addItemAtIndex(index, dataSource, currentItem);
                this.listViewInstance.setViewDataSource(dataSource);
                if (!this.domItemCount) {
                    if ((this.listViewInstance.template || this.listViewInstance.groupTemplate) && !this.isNgTemplate()) {
                        this.listViewInstance.listBaseOption.template = null;
                        this.listViewInstance.listBaseOption.groupTemplate = null;
                        this.listViewInstance.listBaseOption.itemCreated = this.createUIItem.bind(this);
                    }
                    this.uiVirtualization();
                }
                else if (this.domItemCount < this.expectedDomItemCount) {
                    var ds = this.listViewInstance.findItemFromDS(dataSource, fields);
                    Eif (ds instanceof Array) {
                        Eif (this.listViewInstance.ulElement) {
                            var index_1 = this.listViewInstance.curViewDS.indexOf(currentItem);
                            this.createAndInjectNewItem(currentItem, index_1);
                            var curViewDS = this.listViewInstance.curViewDS[index_1 - 1];
                            if (curViewDS && curViewDS.isHeader && curViewDS.items.length === 1) {
                                --index_1;
                                this.createAndInjectNewItem(curViewDS, index_1);
                            }
                        }
                        this.listViewInstance.liCollection =
                            this.listViewInstance.curUL.querySelectorAll('li');
                    }
                }
                else {
                    var index_2 = this.listViewInstance.curViewDS.indexOf(currentItem);
                    this.addUiItem(index_2);
                    var curViewDS = this.listViewInstance.curViewDS[index_2 - 1];
                    if (curViewDS && curViewDS.isHeader && curViewDS.items.length === 1) {
                        this.addUiItem(index_2 - 1);
                    }
                }
            }
        };
        Virtualization.prototype.createAndInjectNewItem = function (itemData, index) {
            var target;
            var li = list_base_1.ListBase.createListItemFromJson(this.listViewInstance.createElement, [itemData], this.listViewInstance.listBaseOption, null, null, this.listViewInstance);
            if ((Object.keys(this.listViewInstance.curViewDS).length - 1) === index) {
                target = this.listViewInstance.curUL.lastElementChild;
            }
            else {
                target = this.listViewInstance.getLiFromObjOrElement(this.listViewInstance.curViewDS[index + 1]) ||
                    this.listViewInstance.getLiFromObjOrElement(this.listViewInstance.curViewDS[index + 2]);
            }
            if (this.listViewInstance.fields.groupBy
                && this.listViewInstance.curViewDS[index + 1]
                && this.listViewInstance.curViewDS[index + 1].isHeader) {
                var targetEle = this.listViewInstance.getLiFromObjOrElement(this.listViewInstance.curViewDS[index - 1]);
                Eif (targetEle) {
                    target = targetEle.nextElementSibling;
                }
            }
            this.listViewInstance.ulElement.insertBefore(li[0], target);
            this.domItemCount++;
            if (this.bottomElementHeight <= 0) {
                this.uiLastIndex++;
            }
            this.refreshItemHeight();
        };
        Virtualization.prototype.createUIItem = function (args) {
            if (!args.item.classList.contains('e-list-group-item')) {
                this.templateData = args.curData.isHeader ? args.curData.items[0] :
                    args.curData;
                if (this.listViewInstance.showCheckBox) {
                    this.listViewInstance.renderCheckbox(args);
                    Eif ((!ej2_base_1.isNullOrUndefined(this.listViewInstance.virtualCheckBox)) &&
                        (!ej2_base_1.isNullOrUndefined(this.listViewInstance.virtualCheckBox.outerHTML))) {
                        var div_1 = document.createElement('div');
                        var commonTemplate = '<div class="e-text-content" role="presentation"> ' +
                            '<span class="e-list-text"> ${' + this.listViewInstance.fields.text + '} </span></div>';
                        var templateFunction = ej2_base_1.compile(this.listViewInstance.template || commonTemplate, this.listViewInstance);
                        var nodes = templateFunction(this.templateData, this.listViewInstance);
                        Iif (this.listViewInstance.template && this.listViewInstance.isReact) {
                            this.listViewInstance.renderReactTemplates();
                        }
                        [].slice.call(nodes).forEach(function (ele) {
                            div_1.appendChild(ele);
                        });
                        Eif (div_1.children && div_1.children[0]) {
                            div_1.children[0].classList.add('e-checkbox');
                            Eif (this.listViewInstance.checkBoxPosition === 'Left') {
                                div_1.children[0].classList.add('e-checkbox-left');
                            }
                            else {
                                div_1.children[0].classList.add('e-checkbox-right');
                            }
                            Eif (this.listViewInstance.checkBoxPosition === 'Left') {
                                div_1.children[0].insertBefore(this.listViewInstance.virtualCheckBox, div_1.childNodes[0].children[0]);
                            }
                            else {
                                div_1.children[0].appendChild(this.listViewInstance.virtualCheckBox);
                            }
                            while (args.item.lastChild) {
                                args.item.removeChild(args.item.lastChild);
                            }
                            [].slice.call(div_1.children).forEach(function (ele) {
                                args.item.appendChild(ele);
                            });
                        }
                    }
                }
            }
        };
        Virtualization.prototype.reRenderUiVirtualization = function () {
            this.wireScrollEvent(true);
            if (this.listViewInstance.contentContainer) {
                ej2_base_1.detach(this.listViewInstance.contentContainer);
            }
            this.listViewInstance.preRender();
            this.domItemCount = 0;
            this.listViewInstance.header();
            this.listViewInstance.setLocalData();
        };
        Virtualization.prototype.updateUI = function (element, index, targetElement) {
            var onChange = this.isNgTemplate() ? this.onNgChange : this.onChange;
            if (this.listViewInstance.template || this.listViewInstance.groupTemplate) {
                var curViewDS = this.listViewInstance.curViewDS[index];
                element.dataset.uid = (curViewDS[this.listViewInstance.fields.id]) ?
                    (curViewDS[this.listViewInstance.fields.id]) : list_base_1.ListBase.generateId();
                onChange(curViewDS, element, this);
            }
            else {
                this.updateUiContent(element, index);
            }
            this.changeElementAttributes(element, index);
            if (targetElement) {
                this.listViewInstance.ulElement.insertBefore(element, targetElement);
            }
        };
        Virtualization.prototype.onChange = function (newData, listElement, virtualThis) {
            var liItem = list_base_1.ListBase.createListItemFromJson(virtualThis.listViewInstance.createElement, [newData], virtualThis.listViewInstance.listBaseOption, null, null, virtualThis.listViewInstance);
            Iif (virtualThis.listViewInstance.isReact) {
                virtualThis.listViewInstance.renderReactTemplates();
            }
            while (listElement.lastChild) {
                listElement.removeChild(listElement.lastChild);
            }
            [].slice.call(liItem[0].children).forEach(function (ele) {
                listElement.appendChild(ele);
            });
        };
        Virtualization.prototype.onNgChange = function (newData, listElement, virtualThis) {
            var templateCompiler = ej2_base_1.compile(virtualThis.listViewInstance.template);
            var resultElement = templateCompiler(newData);
            while (listElement.lastChild) {
                listElement.removeChild(listElement.lastChild);
            }
            listElement.appendChild(resultElement[0]);
        };
        Virtualization.prototype.getModuleName = function () {
            return 'virtualization';
        };
        Virtualization.prototype.destroy = function () {
            this.wireScrollEvent(true);
            this.topElement = null;
            this.bottomElement = null;
        };
        return Virtualization;
    }());
    exports.Virtualization = Virtualization;
});