all files / spreadsheet/actions/ scroll.js

68.75% Statements 176/256
51.11% Branches 69/135
65.38% Functions 17/26
68.9% Lines 175/254
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   119× 119× 119× 119× 119×   14× 13× 13× 13× 13× 13× 13× 13× 13× 13×         13×             13×                             13× 13× 13× 13×       13×                                   13× 13× 13× 13×   13×   372× 251× 251×   246×     246×     121×     121× 121×     121×   119×                   119×         136× 89× 89×   85×     85×     47×     47× 47×   45×                 45×         144×   144×     144× 144×                                             144× 144× 144× 144× 144× 144× 144× 144×                     144× 144× 144× 144×   144× 144× 144× 144× 144×   144× 144×     10× 10×                                                                         119× 119× 119× 119× 119× 119× 119× 119×     119× 119×   119× 119× 119× 119× 119× 119× 119× 119×          
define(["require", "exports", "@syncfusion/ej2-base", "../index", "../common/index", "../common/index", "../common/index", "../../workbook/index", "@syncfusion/ej2-splitbuttons"], function (require, exports, ej2_base_1, index_1, index_2, index_3, index_4, index_5, ej2_splitbuttons_1) {
    "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) {
            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;
            Eif (this.parent.allowAutoFill) {
                var elem = document.querySelector('#' + this.parent.element.id + '_autofilloptionbtn-popup');
                var DDBElem = document.querySelector('#' + this.parent.element.id + '_autofilloptionbtn');
                Iif (elem) {
                    var DDBObj = ej2_base_1.getComponent(DDBElem, ej2_splitbuttons_1.DropDownButton);
                    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);
                Eif (!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);
                this.parent.notify(index_2.onHorizontalScroll, scrollArgs);
                Iif (!this.parent.scrollSettings.enableVirtualization && scrollRight && !this.parent.scrollSettings.isFinite) {
                    this.updateNonVirtualCols();
                }
                this.leftIndex = scrollArgs.prev.idx;
                this.prevScroll.scrollLeft = left;
            }
            if (this.prevScroll.scrollTop !== 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);
                Iif (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);
                }
                else {
                    scrollArgs.prev.idx = scrollArgs.cur.idx;
                }
                Iif (!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);
            Iif (args.isEdit) {
                var textArea = this.parent.element.querySelector('.e-formula-bar');
                textArea.focus();
            }
            this.isKeyScroll = true;
        };
        Scroll.prototype.updateNonVirtualRows = function () {
            var sheet = this.parent.getActiveSheet();
            var threshold = this.parent.getThreshold('row');
            if (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');
            if (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) {
            var sheet = this.parent.getActiveSheet();
            var top = this.offset.top.idx;
            var left = this.offset.left.idx;
            if (!increase) {
                top = index_3.skipHiddenIdx(sheet, top, true);
                left = index_3.skipHiddenIdx(sheet, left, true, 'columns');
            }
            this.parent.updateTopLeftCell(top, left);
        };
        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: i, size: temp };
                    }
                    Iif (Math.round(temp) > scrollTop) {
                        return { idx: i - 1, size: temp - index_5.getRowHeight(sheet, i - 1 + frozenRow, true) };
                    }
                    i++;
                }
                else {
                    Iif (temp === 0) {
                        return { idx: 0, size: 0 };
                    }
                    temp -= index_5.getRowHeight(sheet, i + frozenRow, true);
                    Iif (temp < 0) {
                        temp = 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_3.skipHiddenIdx(sheet, i + 1, true), 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 - 1;
            var frozenCol = this.parent.frozenColCount(sheet);
            var count = this.parent.scrollSettings.isFinite ? sheet.colCount : Infinity;
            scrollLeft = Math.round(scrollLeft);
            while (i < count) {
                if (increase) {
                    temp += index_5.getColumnWidth(sheet, i - 1 + frozenCol, skipHidden, true);
                    if (Math.round(temp) === scrollLeft) {
                        return { idx: i, size: temp };
                    }
                    Iif (Math.round(temp) > scrollLeft) {
                        return { idx: i - 1, size: temp - index_5.getColumnWidth(sheet, i - 1 + frozenCol, skipHidden, true) };
                    }
                    i++;
                }
                else {
                    Iif (temp === 0) {
                        return { idx: 0, size: 0 };
                    }
                    temp -= index_5.getColumnWidth(sheet, i + frozenCol, skipHidden, true);
                    if (Math.round(temp) === scrollLeft) {
                        return { idx: i, size: temp };
                    }
                    Iif (Math.round(temp) < scrollLeft) {
                        temp += index_5.getColumnWidth(sheet, i + frozenCol, skipHidden, true);
                        if (Math.round(temp) > scrollLeft) {
                            return { idx: i, size: temp - index_5.getColumnWidth(sheet, i + frozenCol, skipHidden, true) };
                        }
                        else {
                            return { idx: i + 1, 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);
            }
            Iif (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) + '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 (args) {
            Eif (args === void 0) { args = { set: true }; }
            Eif (args.set) {
                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);
            }
            else {
                ej2_base_1.EventHandler.remove(this.parent.sheetModule.contentPanel, 'scroll', this.onContentScroll);
                ej2_base_1.EventHandler.remove(this.parent.getColumnHeaderContent(), 'wheel', this.onHeaderWheel);
                ej2_base_1.EventHandler.remove(this.parent.getSelectAllContent(), 'wheel', this.onHeaderWheel);
                ej2_base_1.EventHandler.remove(this.parent.getMainContent(), 'wheel', this.onContentWheel);
                ej2_base_1.EventHandler.remove(this.parent.getRowHeaderContent(), 'wheel', this.onContentWheel);
                ej2_base_1.EventHandler.remove(this.parent.getScrollElement(), 'scroll', this.scrollHandler);
            }
        };
        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 () {
            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';
            }
        };
        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) {
                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_3.setScrollEvent, this.setScrollEvent, this);
            this.parent.on(index_1.mouseDown, this.setClientX, 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();
            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_3.setScrollEvent, this.setScrollEvent);
            this.parent.off(index_1.mouseDown, this.setClientX);
            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;
});