all files / grid/renderer/ virtual-content-renderer.js

95.59% Statements 282/295
90.73% Branches 137/151
96.67% Functions 58/60
95.42% Lines 271/284
9 statements, 5 functions, 9 branches Ignored     
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        10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10×   11× 11× 11× 11× 11× 11× 11× 11× 11×       11×     12×             14×       21×     11× 11×   11×           16×   16× 16× 16× 16× 16× 16×   16× 16× 16×   16×   16× 16× 15× 15× 15× 15×             15× 15×       15× 14× 14×   15× 15× 100× 13×   15× 15×   19×   19× 19×     11× 11× 11× 11× 11× 18× 18× 18× 11× 11×   18×   11×   20×   20× 20× 20× 20×   20×   20×   20×     20×   52×   10× 10×       10× 10× 10× 40× 10× 10× 10×   10×   2159×   2042×     79×   41×   12×   57×       20×   21× 21× 21×           17× 17× 17× 17× 17× 17× 17× 2043×   2043× 2043× 2043×   2043× 2043×   17× 17×                         15× 15× 15×   15× 15× 15×   37×   24×   26× 26×        
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-data", "../base/constant", "../base/enum", "./content-renderer", "./header-renderer", "../services/intersection-observer", "../services/virtual-row-model-generator"], function (require, exports, ej2_base_1, ej2_base_2, ej2_data_1, constant_1, enum_1, content_renderer_1, header_renderer_1, intersection_observer_1, virtual_row_model_generator_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var VirtualContentRenderer = (function (_super) {
        __extends(VirtualContentRenderer, _super);
        function VirtualContentRenderer(parent, locator) {
            var _this = _super.call(this, parent, locator) || this;
            _this.prevHeight = 0;
            _this.currentInfo = {};
            _this.preventEvent = false;
            _this.actions = ['filtering', 'searching', 'grouping', 'ungrouping'];
            _this.offsets = {};
            _this.tmpOffsets = {};
            _this.virtualEle = new VirtualElementHandler();
            _this.offsetKeys = [];
            _this.isFocused = false;
            _this.locator = locator;
            _this.eventListener('on');
            _this.vgenerator = _this.generator;
            return _this;
        }
        VirtualContentRenderer.prototype.renderTable = function () {
            this.header = this.locator.getService('rendererFactory').getRenderer(enum_1.RenderType.Header);
            _super.prototype.renderTable.call(this);
            this.virtualEle.table = this.getTable();
            this.virtualEle.content = this.content = this.getPanel().firstChild;
            this.virtualEle.renderWrapper(this.parent.height);
            this.virtualEle.renderPlaceHolder();
            this.virtualEle.wrapper.style.position = 'absolute';
            var debounceEvent = (this.parent.dataSource instanceof ej2_data_1.DataManager && !this.parent.dataSource.dataSource.offline);
            var opt = {
                container: this.content, pageHeight: this.getBlockHeight() * 2, debounceEvent: debounceEvent,
                axes: this.parent.enableColumnVirtualization ? ['X', 'Y'] : ['Y']
            };
            this.observer = new intersection_observer_1.InterSectionObserver(this.virtualEle.wrapper, opt);
        };
        VirtualContentRenderer.prototype.renderEmpty = function (tbody) {
            this.getTable().appendChild(tbody);
            this.virtualEle.adjustTable(0, 0);
        };
        VirtualContentRenderer.prototype.scrollListener = function (scrollArgs) {
            if (this.preventEvent || this.parent.isDestroyed) {
                this.preventEvent = false;
                return;
            }
            this.isFocused = this.content === ej2_base_1.closest(document.activeElement, '.e-content') || this.content === document.activeElement;
            var info = scrollArgs.sentinel;
            var viewInfo = this.currentInfo = this.getInfoFromView(scrollArgs.direction, info, scrollArgs.offset);
            Iif (this.prevInfo && ((info.axis === 'Y' && this.prevInfo.blockIndexes.toString() === viewInfo.blockIndexes.toString())
                || (info.axis === 'X' && this.prevInfo.columnIndexes.toString() === viewInfo.columnIndexes.toString()))) {
                return;
            }
            this.parent.setColumnIndexesInView(this.parent.enableColumnVirtualization ? viewInfo.columnIndexes : []);
            this.parent.pageSettings.currentPage = viewInfo.loadNext && !viewInfo.loadSelf ? viewInfo.nextInfo.page : viewInfo.page;
            this.parent.notify(viewInfo.event, { requestType: 'virtualscroll', virtualInfo: viewInfo, focusElement: scrollArgs.focusElement });
        };
        VirtualContentRenderer.prototype.block = function (blk) {
            return this.vgenerator.isBlockAvailable(blk);
        };
        VirtualContentRenderer.prototype.getInfoFromView = function (direction, info, e) {
            var tempBlocks = [];
            var infoType = { direction: direction, sentinelInfo: info, offsets: e };
            infoType.page = this.getPageFromTop(e.top, infoType);
            infoType.blockIndexes = tempBlocks = this.vgenerator.getBlockIndexes(infoType.page);
            infoType.loadSelf = !this.vgenerator.isBlockAvailable(tempBlocks[infoType.block]);
            var blocks = this.ensureBlocks(infoType);
            infoType.blockIndexes = blocks;
            infoType.loadNext = !blocks.filter(function (val) { return tempBlocks.indexOf(val) === -1; })
                .every(this.block.bind(this));
            infoType.event = (infoType.loadNext || infoType.loadSelf) ? constant_1.modelChanged : constant_1.refreshVirtualBlock;
            infoType.nextInfo = infoType.loadNext ? { page: Math.max(1, infoType.page + (direction === 'down' ? 1 : -1)) } : {};
            infoType.columnIndexes = info.axis === 'X' ? this.vgenerator.getColumnIndexes() : this.parent.getColumnIndexesInView();
            if (this.parent.enableColumnVirtualization && info.axis === 'X') {
                infoType.event = constant_1.refreshVirtualBlock;
            }
            return infoType;
        };
        VirtualContentRenderer.prototype.ensureBlocks = function (info) {
            var _this = this;
            var index = info.blockIndexes[info.block];
            var mIdx;
            var old = index;
            var max = Math.max;
            var indexes = info.direction === 'down' ? [max(index, 1), ++index, ++index] : [max(index - 1, 1), index, index + 1];
            indexes = indexes.filter(function (val, ind) { return indexes.indexOf(val) === ind; });
            if (this.prevInfo.blockIndexes.toString() === indexes.toString()) {
                return indexes;
            }
            if (info.loadSelf || (info.direction === 'down' && this.isEndBlock(old))) {
                indexes = this.vgenerator.getBlockIndexes(info.page);
            }
            indexes.some(function (val, ind) {
                var result = val === _this.getTotalBlocks();
                if (result) {
                    mIdx = ind;
                }
                return result;
            });
            if (mIdx !== undefined) {
                indexes = indexes.slice(0, mIdx + 1);
                Iif (info.block === 0 && indexes.length === 1 && this.vgenerator.isBlockAvailable(indexes[0] - 1)) {
                    indexes = [indexes[0] - 1, indexes[0]];
                }
            }
            return indexes;
        };
        VirtualContentRenderer.prototype.appendContent = function (target, newChild, e) {
            var info = e.virtualInfo.sentinelInfo && e.virtualInfo.sentinelInfo.axis === 'Y' && this.currentInfo.page &&
                this.currentInfo.page !== e.virtualInfo.page ? this.currentInfo : e.virtualInfo;
            this.prevInfo = this.prevInfo || e.virtualInfo;
            var cBlock = (info.columnIndexes[0]) - 1;
            var cOffset = this.getColumnOffset(cBlock);
            var width;
            var blocks = info.blockIndexes;
            if (this.parent.groupSettings.columns.length) {
                this.refreshOffsets();
            }
            var translate = this.getTranslateY(this.content.scrollTop, this.content.getBoundingClientRect().height, info);
            this.virtualEle.adjustTable(cOffset, translate);
            if (this.parent.enableColumnVirtualization) {
                this.header.virtualEle.adjustTable(cOffset, 0);
            }
            if (this.parent.enableColumnVirtualization) {
                var cIndex = info.columnIndexes;
                width = this.getColumnOffset(cIndex[cIndex.length - 1]) - this.getColumnOffset(cIndex[0] - 1) + '';
                this.header.virtualEle.setWrapperWidth(width);
            }
            this.virtualEle.setWrapperWidth(width, this.parent.enableColumnVirtualization);
            ej2_base_1.remove(target);
            target = ej2_base_1.createElement('tbody');
            target.appendChild(newChild);
            this.getTable().appendChild(target);
            if (this.parent.groupSettings.columns.length) {
                Iif (info.direction === 'up') {
                    var blk = this.offsets[this.getTotalBlocks()] - this.prevHeight;
                    this.preventEvent = true;
                    var sTop = this.content.scrollTop;
                    this.content.scrollTop = sTop + blk;
                }
                this.setVirtualHeight();
                this.observer.setPageHeight(this.getOffset(blocks[blocks.length - 1]) - this.getOffset(blocks[0] - 1));
            }
            this.prevInfo = info;
            Iif (this.isFocused) {
                this.content.focus();
            }
        };
        VirtualContentRenderer.prototype.onDataReady = function (e) {
            if (!ej2_base_2.isNullOrUndefined(e.count)) {
                this.count = e.count;
                this.maxPage = Math.ceil(e.count / this.parent.pageSettings.pageSize);
            }
            this.vgenerator.checkAndResetCache(e.requestType);
            if (['refresh', 'filtering', 'searching', 'grouping', 'ungrouping', 'reorder', undefined]
                .some(function (value) { return e.requestType === value; })) {
                this.refreshOffsets();
            }
            this.setVirtualHeight();
            this.resetScrollPosition(e.requestType);
        };
        VirtualContentRenderer.prototype.setVirtualHeight = function () {
            var width = this.parent.enableColumnVirtualization ?
                this.getColumnOffset(this.parent.columns.length + this.parent.groupSettings.columns.length - 1) + 'px' : '100%';
            this.virtualEle.setVirtualHeight(this.offsets[this.getTotalBlocks()], width);
            if (this.parent.enableColumnVirtualization) {
                this.header.virtualEle.setVirtualHeight(1, width);
            }
        };
        VirtualContentRenderer.prototype.getPageFromTop = function (sTop, info) {
            var _this = this;
            var total = this.getTotalBlocks();
            var page = 0;
            var extra = this.offsets[total] - this.prevHeight;
            this.offsetKeys.some(function (offset) {
                var iOffset = Number(offset);
                var border = sTop < _this.offsets[offset] || (iOffset === total && sTop > _this.offsets[offset]);
                if (border) {
                    info.block = iOffset % 2 === 0 ? 1 : 0;
                    page = Math.max(1, Math.min(_this.vgenerator.getPage(iOffset), _this.maxPage));
                }
                return border;
            });
            return page;
        };
        VirtualContentRenderer.prototype.getTranslateY = function (sTop, cHeight, info, isOnenter) {
            if (info === undefined) {
                info = { page: this.getPageFromTop(sTop, {}) };
                info.blockIndexes = this.vgenerator.getBlockIndexes(info.page);
            }
            var block = (info.blockIndexes[0] || 1) - 1;
            var translate = this.getOffset(block);
            var endTranslate = this.getOffset(info.blockIndexes[info.blockIndexes.length - 1]);
            if (isOnenter) {
                info = this.prevInfo;
            }
            var result = translate > sTop ?
                this.getOffset(block - 1) : endTranslate < (sTop + cHeight) ? this.getOffset(block + 1) : translate;
            var blockHeight = this.offsets[info.blockIndexes[info.blockIndexes.length - 1]] -
                this.tmpOffsets[info.blockIndexes[0]];
            Iif (result + blockHeight > this.offsets[this.getTotalBlocks()]) {
                result -= (result + blockHeight) - this.offsets[this.getTotalBlocks()];
            }
            return result;
        };
        VirtualContentRenderer.prototype.getOffset = function (block) {
            return Math.min(this.offsets[block] | 0, this.offsets[this.maxBlock] | 0);
        };
        VirtualContentRenderer.prototype.onEntered = function () {
            var _this = this;
            return function (element, current, direction, e) {
                var xAxis = current.axis === 'X';
                var top = _this.prevInfo.offsets ? _this.prevInfo.offsets.top : null;
                var height = _this.content.getBoundingClientRect().height;
                var x = _this.getColumnOffset(xAxis ? _this.vgenerator.getColumnIndexes()[0] - 1 : _this.prevInfo.columnIndexes[0] - 1);
                var y = _this.getTranslateY(e.top, height, xAxis && top === e.top ? _this.prevInfo : undefined, true);
                _this.virtualEle.adjustTable(x, Math.min(y, _this.offsets[_this.maxBlock]));
                if (_this.parent.enableColumnVirtualization) {
                    _this.header.virtualEle.adjustTable(x, 0);
                }
            };
        };
        VirtualContentRenderer.prototype.eventListener = function (action) {
            var _this = this;
            this.parent[action](constant_1.dataReady, this.onDataReady, this);
            this.parent[action](constant_1.refreshVirtualBlock, this.refreshContentRows, this);
            this.actions.forEach(function (event) { return _this.parent[action](event + "-begin", _this.onActionBegin, _this); });
            var fn = function () {
                _this.observer.observe(function (scrollArgs) { return _this.scrollListener(scrollArgs); }, _this.onEntered());
                _this.parent.off(constant_1.contentReady, fn);
            };
            this.parent.on(constant_1.contentReady, fn, this);
        };
        VirtualContentRenderer.prototype.getBlockSize = function () {
            return this.parent.pageSettings.pageSize >> 1;
        };
        VirtualContentRenderer.prototype.getBlockHeight = function () {
            return this.getBlockSize() * this.parent.getRowHeight();
        };
        VirtualContentRenderer.prototype.isEndBlock = function (index) {
            var totalBlocks = this.getTotalBlocks();
            return index >= totalBlocks || index === totalBlocks - 1;
        };
        VirtualContentRenderer.prototype.getTotalBlocks = function () {
            return Math.ceil(this.count / this.getBlockSize());
        };
        VirtualContentRenderer.prototype.getColumnOffset = function (block) {
            return this.vgenerator.cOffsets[block] | 0;
        };
        VirtualContentRenderer.prototype.getModelGenerator = function () {
            return new virtual_row_model_generator_1.VirtualRowModelGenerator(this.parent);
        };
        VirtualContentRenderer.prototype.resetScrollPosition = function (action) {
            if (this.actions.some(function (value) { return value === action; })) {
                this.preventEvent = this.content.scrollTop !== 0;
                this.content.scrollTop = 0;
            }
        };
        VirtualContentRenderer.prototype.onActionBegin = function (e) {
            this.parent.setProperties({ pageSettings: { currentPage: 1 } }, true);
        };
        VirtualContentRenderer.prototype.getRows = function () {
            return this.vgenerator.getRows();
        };
        VirtualContentRenderer.prototype.getRowByIndex = function (index) {
            var prev = this.prevInfo.blockIndexes;
            var startIdx = (prev[0] - 1) * this.getBlockSize();
            return this.parent.getDataRows()[index - startIdx];
        };
        VirtualContentRenderer.prototype.getVirtualRowIndex = function (index) {
            var prev = this.prevInfo.blockIndexes;
            var startIdx = (prev[0] - 1) * this.getBlockSize();
            return startIdx + index;
        };
        VirtualContentRenderer.prototype.refreshOffsets = function () {
            var _this = this;
            var row = 0;
            var bSize = this.getBlockSize();
            var total = this.getTotalBlocks();
            this.prevHeight = this.offsets[total];
            this.maxBlock = total % 2 === 0 ? total - 2 : total - 1;
            this.offsets = {};
            Array.apply(null, Array(total)).map(function () { return ++row; })
                .forEach(function (block) {
                var tmp = (_this.vgenerator.cache[block] || []).length;
                var rem = _this.count % bSize;
                var size = block in _this.vgenerator.cache ?
                    tmp * _this.parent.getRowHeight() : rem && block === total ? rem * _this.parent.getRowHeight() : _this.getBlockHeight();
                _this.offsets[block] = (_this.offsets[block - 1] | 0) + size;
                _this.tmpOffsets[block] = _this.offsets[block - 1] | 0;
            });
            this.offsetKeys = Object.keys(this.offsets);
            if (this.parent.enableColumnVirtualization) {
                this.vgenerator.refreshColOffsets();
            }
        };
        VirtualContentRenderer.prototype.refreshVirtualElement = function () {
            this.vgenerator.refreshColOffsets();
            this.setVirtualHeight();
        };
        return VirtualContentRenderer;
    }(content_renderer_1.ContentRender));
    exports.VirtualContentRenderer = VirtualContentRenderer;
    var VirtualHeaderRenderer = (function (_super) {
        __extends(VirtualHeaderRenderer, _super);
        function VirtualHeaderRenderer(parent, locator) {
            var _this = _super.call(this, parent, locator) || this;
            _this.virtualEle = new VirtualElementHandler();
            _this.gen = new virtual_row_model_generator_1.VirtualRowModelGenerator(_this.parent);
            _this.parent.on(constant_1.refreshVirtualBlock, function (e) { return e.virtualInfo.sentinelInfo.axis === 'X' ? _this.refreshUI() : null; }, _this);
            return _this;
        }
        VirtualHeaderRenderer.prototype.renderTable = function () {
            this.gen.refreshColOffsets();
            this.parent.setColumnIndexesInView(this.gen.getColumnIndexes(this.getPanel().firstChild));
            _super.prototype.renderTable.call(this);
            this.virtualEle.table = this.getTable();
            this.virtualEle.content = this.getPanel().firstChild;
            this.virtualEle.content.style.position = 'relative';
            this.virtualEle.renderWrapper();
            this.virtualEle.renderPlaceHolder('absolute');
        };
        VirtualHeaderRenderer.prototype.appendContent = function (table) {
            this.virtualEle.wrapper.appendChild(table);
        };
        VirtualHeaderRenderer.prototype.refreshUI = function () {
            this.gen.refreshColOffsets();
            this.parent.setColumnIndexesInView(this.gen.getColumnIndexes(this.getPanel().firstChild));
            _super.prototype.refreshUI.call(this);
        };
        return VirtualHeaderRenderer;
    }(header_renderer_1.HeaderRender));
    exports.VirtualHeaderRenderer = VirtualHeaderRenderer;
    var VirtualElementHandler = (function () {
        function VirtualElementHandler() {
        }
        VirtualElementHandler.prototype.renderWrapper = function (height) {
            this.wrapper = ej2_base_1.createElement('div', { className: 'e-virtualtable', styles: "min-height:" + ej2_base_1.formatUnit(height) });
            this.wrapper.appendChild(this.table);
            this.content.appendChild(this.wrapper);
        };
        VirtualElementHandler.prototype.renderPlaceHolder = function (position) {
            if (position === void 0) { position = 'relative'; }
            this.placeholder = ej2_base_1.createElement('div', { className: 'e-virtualtrack', styles: "position:" + position });
            this.content.appendChild(this.placeholder);
        };
        VirtualElementHandler.prototype.adjustTable = function (xValue, yValue) {
            this.wrapper.style.transform = "translate(" + xValue + "px, " + yValue + "px)";
        };
        VirtualElementHandler.prototype.setWrapperWidth = function (width, full) {
            this.wrapper.style.width = width ? width + "px" : full ? '100%' : '';
        };
        VirtualElementHandler.prototype.setVirtualHeight = function (height, width) {
            this.placeholder.style.height = height + "px";
            this.placeholder.style.width = width;
        };
        return VirtualElementHandler;
    }());
    exports.VirtualElementHandler = VirtualElementHandler;
});