all files / spreadsheet/actions/ scroll.js

81.11% Statements 219/270
70.32% Branches 109/155
78.57% Functions 22/28
81.41% Lines 219/269
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   881× 881× 881× 881× 881×   300×   293× 293× 293× 293× 293× 293× 293× 290× 290× 290×     293× 173× 173× 173× 173× 169× 169× 169×   173×     173× 173× 173× 173×   173× 173×   293× 79×       79× 79× 79× 79×     79× 79×   70× 70× 70×         79×   79× 79×   293× 293× 293× 293×   293×   243×                           252× 252× 252× 252× 32× 32× 32× 32×   252× 173×     79×     79× 79× 79× 79× 79× 79× 79× 7225× 5711× 5711× 55×   5656×   5653×     1514× 1514× 14×   1500×   1493×                   1493×         173× 173× 173× 173× 173× 173× 1839× 1364× 1364× 157×   1207×   1202×     475× 475×   467×   464×                   464×         1040× 53× 53× 53×   1040×   1040× 1040× 12×     53×     53×                       173×           1040× 1040× 1040× 1040× 1040× 1040×   1185× 1185× 1185× 1185×   1178× 1178× 1178× 1178× 1178×   1178× 1178×   1178×     119× 119×                                                                         881× 881× 881× 881× 881× 881× 881× 881× 29× 29×     881× 881× 881× 744× 744× 741×     881×   881× 881× 881× 881× 881× 881× 881× 881× 29× 29×          
define(["require", "exports", "@syncfusion/ej2-base", "../index", "../common/index", "../common/index", "../common/index", "../../workbook/index"], function (require, exports, ej2_base_1, index_1, index_2, index_3, index_4, index_5) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Scroll = (function () {
        function Scroll(parent) {
            this.clientX = 0;
            this.isKeyScroll = true;
            this.parent = parent;
            this.addEventListener();
            this.initProps();
        }
        Scroll.prototype.onContentScroll = function (e) {
            if (!this.parent) {
                return;
            }
            var target = this.parent.getMainContent().parentElement;
            var scrollLeft = e.scrollLeft;
            var top = e.scrollTop || target.scrollTop;
            var left = scrollLeft && this.parent.enableRtl ? this.initScrollValue - scrollLeft : scrollLeft;
            var scrollArgs;
            var prevSize;
            if (this.parent.allowAutoFill) {
                var elem = document.querySelector('#' + this.parent.element.id + '_autofilloptionbtn-popup');
                var DDBElem = document.querySelector('#' + this.parent.element.id + '_autofilloptionbtn');
                if (elem) {
                    var DDBObj = ej2_base_1.getComponent(DDBElem, 'dropdown-btn');
                    DDBObj.toggle();
                }
            }
            if (!ej2_base_1.isNullOrUndefined(scrollLeft) && this.prevScroll.scrollLeft !== left) {
                var scrollRight = left > this.prevScroll.scrollLeft;
                prevSize = this.offset.left.size;
                this.offset.left = this.getColOffset(left, scrollRight, e.skipHidden);
                if (!e.preventScroll) {
                    this.parent.getColumnHeaderContent().scrollLeft = scrollLeft;
                    this.parent.getMainContent().scrollLeft = scrollLeft;
                    e.scrollLeft = scrollLeft;
                }
                scrollArgs = {
                    cur: this.offset.left, prev: { idx: this.leftIndex, size: prevSize }, increase: scrollRight, preventScroll: e.preventScroll
                };
                this.updateTopLeftCell(scrollRight, true);
                this.parent.notify(index_2.onHorizontalScroll, scrollArgs);
                this.updateNoteContainer();
                if (!this.parent.scrollSettings.enableVirtualization && scrollRight && !this.parent.scrollSettings.isFinite) {
                    this.updateNonVirtualCols();
                }
                this.leftIndex = scrollArgs.prev.idx;
                this.prevScroll.scrollLeft = left;
            }
            if (Math.round(this.prevScroll.scrollTop) !== Math.round(top)) {
                Iif (e.skipRowVirualScroll) {
                    this.prevScroll.scrollTop = 0;
                    this.offset.top = { idx: 0, size: 0 };
                }
                var scrollDown = top > this.prevScroll.scrollTop;
                prevSize = this.offset.top.size;
                this.offset.top = this.getRowOffset(top, scrollDown);
                scrollArgs = {
                    cur: this.offset.top, prev: { idx: this.topIndex, size: prevSize }, increase: scrollDown, preventScroll: e.preventScroll
                };
                this.updateTopLeftCell(scrollDown);
                if (e.preventScroll && this.offset.top.idx <= this.parent.getThreshold('row')) {
                    this.offset.top = { idx: 0, size: 0 };
                }
                else Eif (!e.skipRowVirualScroll) {
                    this.parent.notify(index_2.onVerticalScroll, scrollArgs);
                    this.updateNoteContainer();
                }
                else {
                    scrollArgs.prev.idx = scrollArgs.cur.idx;
                }
                if (!this.parent.scrollSettings.enableVirtualization && scrollDown && !this.parent.scrollSettings.isFinite) {
                    this.updateNonVirtualRows();
                }
                this.topIndex = scrollArgs.prev.idx;
                this.prevScroll.scrollTop = top;
            }
            var isEdit = false;
            var args = { isEdit: isEdit };
            this.parent.notify(index_1.isFormulaBarEdit, args);
            if (args.isEdit) {
                var textArea = this.parent.element.querySelector('.e-formula-bar');
                textArea.focus();
            }
            this.isKeyScroll = true;
        };
        Scroll.prototype.updateNoteContainer = function () {
            Iif (document.getElementsByClassName('e-addNoteContainer') && document.getElementsByClassName('e-addNoteContainer').length > 0) {
                this.parent.notify(index_2.updateNoteContainer, null);
            }
        };
        Scroll.prototype.updateScrollValue = function (args) {
            if (args.scrollLeft !== undefined) {
                this.prevScroll.scrollLeft = args.scrollLeft + (this.prevScroll.scrollLeft - this.offset.left.size);
                this.offset.left.size = args.scrollLeft;
            }
            if (args.scrollTop !== undefined) {
                this.prevScroll.scrollTop = args.scrollTop + (this.prevScroll.scrollTop - this.offset.top.size);
                this.offset.top.size = args.scrollTop;
            }
        };
        Scroll.prototype.updateNonVirtualRows = function () {
            var sheet = this.parent.getActiveSheet();
            var threshold = this.parent.getThreshold('row');
            Eif (this.offset.top.idx > sheet.rowCount - (this.parent.viewport.rowCount + threshold)) {
                this.parent.renderModule.refreshUI({ rowIndex: sheet.rowCount, colIndex: 0, direction: 'first', refresh: 'RowPart' }, index_5.getCellAddress(sheet.rowCount, 0) + ":" + index_5.getCellAddress(sheet.rowCount + threshold - 1, sheet.colCount - 1));
                this.parent.setSheetPropertyOnMute(sheet, 'rowCount', sheet.rowCount + threshold);
                this.parent.viewport.bottomIndex = sheet.rowCount - 1;
            }
        };
        Scroll.prototype.updateNonVirtualCols = function () {
            var sheet = this.parent.getActiveSheet();
            var threshold = this.parent.getThreshold('col');
            Iif (this.offset.left.idx > sheet.colCount - (this.parent.viewport.colCount + threshold)) {
                this.parent.renderModule.refreshUI({ rowIndex: 0, colIndex: sheet.colCount, direction: 'first', refresh: 'ColumnPart' }, index_5.getCellAddress(0, sheet.colCount) + ":" + index_5.getCellAddress(sheet.rowCount - 1, sheet.colCount + threshold - 1));
                this.parent.setSheetPropertyOnMute(sheet, 'colCount', sheet.colCount + threshold);
                this.parent.viewport.rightIndex = sheet.colCount - 1;
            }
        };
        Scroll.prototype.updateTopLeftCell = function (increase, isLeft) {
            var sheet = this.parent.getActiveSheet();
            var top = this.offset.top.idx;
            var left = this.offset.left.idx;
            if (!increase) {
                var frozenRow = this.parent.frozenRowCount(sheet);
                top = index_5.skipHiddenIdx(sheet, top + frozenRow, true) - frozenRow;
                var frozenCol = this.parent.frozenColCount(sheet);
                left = index_5.skipHiddenIdx(sheet, left + frozenCol, true, 'columns') - frozenCol;
            }
            if (isLeft) {
                this.parent.updateTopLeftCell(null, left, 'row');
            }
            else {
                this.parent.updateTopLeftCell(top, null, 'col');
            }
        };
        Scroll.prototype.getRowOffset = function (scrollTop, scrollDown) {
            var temp = this.offset.top.size;
            var sheet = this.parent.getActiveSheet();
            var i = scrollDown ? this.offset.top.idx + 1 : (this.offset.top.idx ? this.offset.top.idx - 1 : 0);
            var frozenRow = this.parent.frozenRowCount(sheet);
            var count = this.parent.scrollSettings.isFinite ? sheet.rowCount : Infinity;
            scrollTop = Math.round(scrollTop);
            while (i < count) {
                if (scrollDown) {
                    temp += index_5.getRowHeight(sheet, i - 1 + frozenRow, true);
                    if (Math.abs(Math.round(temp) - scrollTop) <= 1) {
                        return { idx: index_5.skipHiddenIdx(sheet, i + frozenRow, true) - frozenRow, size: temp };
                    }
                    if (Math.round(temp) > scrollTop) {
                        return { idx: i - 1, size: temp - index_5.getRowHeight(sheet, i - 1 + frozenRow, true) };
                    }
                    i++;
                }
                else {
                    temp -= index_5.getRowHeight(sheet, i + frozenRow, true);
                    if (temp <= 0) {
                        return { idx: 0, size: 0 };
                    }
                    if (Math.abs(Math.round(temp) - scrollTop) <= 1) {
                        return { idx: i, size: temp };
                    }
                    Iif (Math.round(temp) < scrollTop) {
                        temp += index_5.getRowHeight(sheet, i + frozenRow, true);
                        if (Math.round(temp) > scrollTop) {
                            return { idx: i, size: temp - index_5.getRowHeight(sheet, i + frozenRow, true) < 0 ? 0 :
                                    temp - index_5.getRowHeight(sheet, i + frozenRow, true) };
                        }
                        else {
                            return { idx: index_5.skipHiddenIdx(sheet, i + 1 + frozenRow, true) - frozenRow, size: temp };
                        }
                    }
                    i--;
                }
            }
            return { idx: this.offset.top.idx, size: this.offset.top.size };
        };
        Scroll.prototype.getColOffset = function (scrollLeft, increase, skipHidden) {
            var temp = this.offset.left.size;
            var sheet = this.parent.getActiveSheet();
            var i = increase ? this.offset.left.idx + 1 : (this.offset.left.idx ? this.offset.left.idx - 1 : 0);
            var frozenCol = this.parent.frozenColCount(sheet);
            var count = this.parent.scrollSettings.isFinite ? sheet.colCount : Infinity;
            while (i < count) {
                if (increase) {
                    temp += index_5.getColumnWidth(sheet, i - 1 + frozenCol, skipHidden, true);
                    if (Math.abs(Math.round(temp) - scrollLeft) <= 1) {
                        return { idx: index_5.skipHiddenIdx(sheet, i + frozenCol, true, 'columns') - frozenCol, size: temp };
                    }
                    if (Math.round(temp) > scrollLeft) {
                        return { idx: i - 1, size: temp - index_5.getColumnWidth(sheet, i - 1 + frozenCol, skipHidden, true) };
                    }
                    i++;
                }
                else {
                    temp -= index_5.getColumnWidth(sheet, i + frozenCol, skipHidden, true);
                    if (temp <= 0) {
                        return { idx: 0, size: 0 };
                    }
                    if (Math.abs(Math.round(temp) - scrollLeft) <= 1) {
                        return { idx: i, size: temp };
                    }
                    Iif (Math.round(temp) < scrollLeft) {
                        temp += index_5.getColumnWidth(sheet, i + frozenCol, skipHidden, true);
                        if (Math.round(temp) > scrollLeft) {
                            temp = temp - index_5.getColumnWidth(sheet, i + frozenCol, skipHidden, true);
                            return { idx: i, size: temp < 0 ? 0 : temp };
                        }
                        else {
                            return { idx: index_5.skipHiddenIdx(sheet, i + 1 + frozenCol, true, 'columns') - frozenCol, size: temp };
                        }
                    }
                    i--;
                }
            }
            return { idx: this.offset.left.idx, size: this.offset.left.size };
        };
        Scroll.prototype.contentLoaded = function (args) {
            if (!this.parent.scrollSettings.enableVirtualization) {
                var scrollTrack = this.parent.createElement('div', { className: 'e-virtualtrack' });
                this.updateNonVirualScrollWidth({ scrollTrack: scrollTrack });
                this.parent.getScrollElement().appendChild(scrollTrack);
            }
            if (args.left) {
                this.parent.getScrollElement().scrollLeft = args.left;
            }
            this.setScrollEvent();
            if (this.parent.enableRtl) {
                this.initScrollValue = this.parent.getScrollElement().scrollLeft;
            }
        };
        Scroll.prototype.updateNonVirualScrollWidth = function (args) {
            Iif (!args.scrollTrack) {
                args.scrollTrack = this.parent.getScrollElement().getElementsByClassName('e-virtualtrack')[0];
            }
            args.scrollTrack.style.width = Math.abs(this.parent.getContentTable().getBoundingClientRect().width +
                (this.parent.scrollSettings.isFinite ? this.parent.sheetModule.getScrollSize() : 0)) + "px";
        };
        Scroll.prototype.onHeaderWheel = function (e) {
            e.preventDefault();
            this.parent.getMainContent().parentElement.scrollTop += e.deltaY;
            this.parent.getScrollElement().scrollLeft += e.deltaX;
        };
        Scroll.prototype.onContentWheel = function (e) {
            if (e.deltaX !== 0) {
                e.preventDefault();
                this.parent.getScrollElement().scrollLeft += e.deltaX;
            }
        };
        Scroll.prototype.scrollHandler = function (e) {
            this.onContentScroll({ scrollLeft: e.target.scrollLeft });
        };
        Scroll.prototype.updateScroll = function (args) {
            if (ej2_base_1.isNullOrUndefined(args.left)) {
                this.parent.sheetModule.contentPanel.scrollTop = args.top;
            }
            else {
                this.parent.getScrollElement().scrollLeft = args.left;
            }
        };
        Scroll.prototype.setScrollEvent = function () {
            ej2_base_1.EventHandler.add(this.parent.sheetModule.contentPanel, 'scroll', this.onContentScroll, this);
            ej2_base_1.EventHandler.add(this.parent.getColumnHeaderContent(), 'wheel', this.onHeaderWheel, this);
            ej2_base_1.EventHandler.add(this.parent.getSelectAllContent(), 'wheel', this.onHeaderWheel, this);
            ej2_base_1.EventHandler.add(this.parent.getMainContent(), 'wheel', this.onContentWheel, this);
            ej2_base_1.EventHandler.add(this.parent.getRowHeaderContent(), 'wheel', this.onContentWheel, this);
            ej2_base_1.EventHandler.add(this.parent.getScrollElement(), 'scroll', this.scrollHandler, this);
        };
        Scroll.prototype.initProps = function () {
            this.topIndex = 0;
            this.leftIndex = 0;
            this.prevScroll = { scrollLeft: 0, scrollTop: 0 };
            this.offset = { left: { idx: 0, size: 0 }, top: { idx: 0, size: 0 } };
        };
        Scroll.prototype.setPadding = function (isRtlChange) {
            this.parent.sheetModule.contentPanel.style.overflowY = 'scroll';
            var scrollWidth = index_2.getScrollBarWidth();
            Eif (scrollWidth > 0) {
                var colHeader = this.parent.getColumnHeaderContent();
                var cssProps = this.parent.enableRtl ? { margin: 'marginLeft', border: 'borderLeftWidth' }
                    : { margin: 'marginRight', border: 'borderRightWidth' };
                colHeader.parentElement.style[cssProps.margin] = scrollWidth + 'px';
                colHeader.style[cssProps.border] = '1px';
            }
            if (isRtlChange) {
                this.initScrollValue = 0;
            }
        };
        Scroll.prototype.setClientX = function (e) {
            Eif (e.type === 'mousedown' || e.pointerType === 'mouse') {
                return;
            }
            var args = { touchSelectionStarted: false };
            this.parent.notify(index_3.selectionStatus, args);
            if (args.touchSelectionStarted || args.isOverlayClicked) {
                return;
            }
            this.clientX = this.getPointX(e);
            var sheetContent = document.getElementById(this.parent.element.id + '_sheet');
            ej2_base_1.EventHandler.add(sheetContent, ej2_base_1.Browser.isPointer ? 'pointermove' : 'touchmove', this.onTouchScroll, this);
            ej2_base_1.EventHandler.add(sheetContent, ej2_base_1.Browser.isPointer ? 'pointerup' : 'touchend', this.pointerUpHandler, this);
        };
        Scroll.prototype.getPointX = function (e) {
            var clientX = 0;
            if (e.touches && e.touches.length) {
                clientX = e.touches[0].clientX;
            }
            else {
                clientX = e.clientX;
            }
            return clientX;
        };
        Scroll.prototype.onTouchScroll = function (e) {
            if (e.pointerType === 'mouse') {
                return;
            }
            var clientX = this.getPointX(e);
            var diff = this.clientX - clientX;
            var scroller = this.parent.element.getElementsByClassName('e-scroller')[0];
            if ((diff > 10 || diff < -10) && scroller.scrollLeft + diff >= 0) {
                e.preventDefault();
                this.clientX = clientX;
                index_1.getUpdateUsingRaf(function () { scroller.scrollLeft += diff; });
            }
        };
        Scroll.prototype.pointerUpHandler = function () {
            var sheetContent = document.getElementById(this.parent.element.id + '_sheet');
            ej2_base_1.EventHandler.remove(sheetContent, ej2_base_1.Browser.isPointer ? 'pointermove' : 'touchmove', this.onTouchScroll);
            ej2_base_1.EventHandler.remove(sheetContent, ej2_base_1.Browser.isPointer ? 'pointerup' : 'touchend', this.pointerUpHandler);
        };
        Scroll.prototype.addEventListener = function () {
            this.parent.on(index_2.contentLoaded, this.contentLoaded, this);
            this.parent.on(index_3.onContentScroll, this.onContentScroll, this);
            this.parent.on(index_3.updateScroll, this.updateScroll, this);
            this.parent.on(index_3.deInitProperties, this.initProps, this);
            this.parent.on(index_2.spreadsheetDestroyed, this.destroy, this);
            this.parent.on(index_1.mouseDown, this.setClientX, this);
            this.parent.on(index_4.updateScrollValue, this.updateScrollValue, this);
            if (!this.parent.scrollSettings.enableVirtualization) {
                this.parent.on(index_4.virtualContentLoaded, this.updateNonVirualScrollWidth, this);
                this.parent.on(index_1.colWidthChanged, this.updateNonVirualScrollWidth, this);
            }
        };
        Scroll.prototype.destroy = function () {
            this.removeEventListener();
            var ddbEle = document.querySelector('#' + this.parent.element.id + '_autofilloptionbtn');
            if (ddbEle) {
                var ddbObj = ej2_base_1.getComponent(ddbEle, 'dropdown-btn');
                if (ddbObj) {
                    ddbObj.destroy();
                }
            }
            this.parent = null;
        };
        Scroll.prototype.removeEventListener = function () {
            this.parent.off(index_2.contentLoaded, this.contentLoaded);
            this.parent.off(index_3.onContentScroll, this.onContentScroll);
            this.parent.off(index_3.updateScroll, this.updateScroll);
            this.parent.off(index_3.deInitProperties, this.initProps);
            this.parent.off(index_2.spreadsheetDestroyed, this.destroy);
            this.parent.off(index_1.mouseDown, this.setClientX);
            this.parent.off(index_4.updateScrollValue, this.updateScrollValue);
            if (!this.parent.scrollSettings.enableVirtualization) {
                this.parent.off(index_4.virtualContentLoaded, this.updateNonVirualScrollWidth);
                this.parent.off(index_1.colWidthChanged, this.updateNonVirualScrollWidth);
            }
        };
        return Scroll;
    }());
    exports.Scroll = Scroll;
});