all files / spreadsheet/actions/ keyboard-navigation.js

52.73% Statements 135/256
47.16% Branches 133/282
92.86% Functions 13/14
52.73% Lines 135/256
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   122× 122×   122×   122×     52× 52×   40× 40× 40× 40× 40× 40× 40×   40×   40×     40×                   39×                           39×                       39×         37× 11× 11×   11× 11× 11×     26×                                                 40× 14× 11×                       14×                       14× 14× 14× 14×   13× 13×   14× 12×     12×           52×                                         102× 102×                                                                                                                                                   17×   15× 15× 15× 15× 15× 15× 15× 15× 15× 15× 15× 15× 15×                             15×                             15×       15×       15× 15×       15×         15× 15× 15× 15× 334× 334× 15×       15× 15× 15×   15× 176× 176× 15×       18525×   122× 122×        
define(["require", "exports", "../common/index", "../../workbook/index", "../../workbook/index", "@syncfusion/ej2-base"], function (require, exports, index_1, index_2, index_3, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var KeyboardNavigation = (function () {
        function KeyboardNavigation(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        KeyboardNavigation.prototype.addEventListener = function () {
            this.parent.on(index_1.keyDown, this.keyDownHandler, this);
        };
        KeyboardNavigation.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_1.keyDown, this.keyDownHandler);
            }
        };
        KeyboardNavigation.prototype.keyDownHandler = function (e) {
            var _this = this;
            if (!this.parent.isEdit && (document.activeElement.classList.contains('e-spreadsheet') ||
                ej2_base_1.closest(document.activeElement, '.e-sheet')) && !ej2_base_1.closest(e.target, '.e-name-box')) {
                var isNavigate = void 0;
                var scrollIdxes = void 0;
                var isRtl = this.parent.enableRtl;
                var sheet_1 = this.parent.getActiveSheet();
                var filterArgs = { e: e, isFilterCell: false };
                var actIdxes_1 = index_2.getCellIndexes(sheet_1.activeCell);
                if ([9, 37, 38, 39, 40, 33, 34].indexOf(e.keyCode) > -1) {
                    e.preventDefault();
                }
                if (e.shiftKey) {
                    this.shiftSelection(e);
                }
                Iif (!e.shiftKey && e.altKey && (e.keyCode === 38 || e.keyCode === 40)) {
                    this.parent.notify(index_1.filterCellKeyDown, filterArgs);
                }
                if ((!e.shiftKey && ((!isRtl && e.keyCode === 37) || (isRtl && e.keyCode === 39)))
                    || (e.shiftKey && e.keyCode === 9)) {
                    Eif (actIdxes_1[1] > 0) {
                        actIdxes_1[1] -= 1;
                        isNavigate = true;
                    }
                    else {
                        var content = this.parent.getMainContent();
                        if (actIdxes_1[1] === 0 && content.scrollLeft && !isRtl) {
                            content.scrollLeft = 0;
                        }
                    }
                }
                else Iif (e.shiftKey && e.keyCode === 13) {
                    if (!this.parent.element.querySelector('.e-find-toolbar')) {
                        if (actIdxes_1[0] > 0) {
                            actIdxes_1[0] -= 1;
                            isNavigate = true;
                        }
                        else {
                            var content = this.parent.getMainContent().parentElement;
                            if (actIdxes_1[0] === 0 && content.scrollTop) {
                                content.scrollTop = 0;
                            }
                        }
                    }
                }
                else Iif (!filterArgs.isFilterCell && !e.shiftKey && e.keyCode === 38) {
                    if (actIdxes_1[0] > 0) {
                        actIdxes_1[0] -= 1;
                        isNavigate = true;
                    }
                    else {
                        var content = this.parent.getMainContent().parentElement;
                        if (actIdxes_1[0] === 0 && content.scrollTop) {
                            content.scrollTop = 0;
                        }
                    }
                }
                else if ((!e.shiftKey && ((!isRtl && e.keyCode === 39) || (isRtl && e.keyCode === 37))) || e.keyCode === 9) {
                    var cell = index_2.getCell(actIdxes_1[0], actIdxes_1[1], sheet_1);
                    Iif (cell && cell.colSpan > 1) {
                        actIdxes_1[1] += (cell.colSpan - 1);
                    }
                    Eif (actIdxes_1[1] < sheet_1.colCount - 1) {
                        actIdxes_1[1] += 1;
                        isNavigate = true;
                    }
                }
                else if ((!filterArgs.isFilterCell && !e.shiftKey && e.keyCode === 40) || e.keyCode === 13) {
                    var cell = index_2.getCell(actIdxes_1[0], actIdxes_1[1], sheet_1);
                    if (cell && cell.rowSpan > 1) {
                        actIdxes_1[0] += (cell.rowSpan - 1);
                    }
                    Eif (actIdxes_1[0] < sheet_1.rowCount - 1) {
                        actIdxes_1[0] += 1;
                        isNavigate = true;
                    }
                }
                else Iif (!e.shiftKey && (e.keyCode === 34 || e.keyCode === 33) && (!this.parent.scrollModule ||
                    this.parent.scrollModule.isKeyScroll)) {
                    var mainPanel = this.parent.element.querySelector('.e-main-panel');
                    var diff = 0;
                    if (e.keyCode === 34) {
                        diff = mainPanel.getBoundingClientRect().height + mainPanel.scrollTop;
                    }
                    else {
                        diff = mainPanel.scrollTop - mainPanel.getBoundingClientRect().height;
                        if (diff < 0) {
                            return;
                        }
                    }
                    var aRowIdx = index_2.getCellIndexes(this.parent.getActiveSheet().activeCell)[0];
                    var topRow_1 = index_2.getCellIndexes(this.parent.getActiveSheet().paneTopLeftCell)[0];
                    var selectDiff_1 = aRowIdx - topRow_1;
                    if (this.parent.scrollModule) {
                        this.parent.scrollModule.isKeyScroll = false;
                    }
                    mainPanel.scrollTop = diff;
                    index_1.getUpdateUsingRaf(function () {
                        topRow_1 = index_2.getCellIndexes(_this.parent.getActiveSheet().paneTopLeftCell)[0];
                        _this.parent.notify(index_1.cellNavigate, { range: [topRow_1 + selectDiff_1, actIdxes_1[1]], preventAnimation: true });
                    });
                }
                if (isNavigate && (!this.parent.scrollModule || this.parent.scrollModule.isKeyScroll)) {
                    if (e.keyCode === 40 || e.keyCode === 38 || e.keyCode === 13) {
                        while (index_3.isHiddenRow(sheet_1, actIdxes_1[0])) {
                            if (e.keyCode === 40 || (!e.shiftKey && e.keyCode === 13)) {
                                actIdxes_1[0] = actIdxes_1[0] + 1;
                            }
                            if (e.keyCode === 38 || (e.shiftKey && e.keyCode === 13)) {
                                actIdxes_1[0] = actIdxes_1[0] - 1;
                                if (actIdxes_1[0] < 0) {
                                    return;
                                }
                            }
                        }
                    }
                    if (e.keyCode === 37 || e.keyCode === 39 || e.keyCode === 9) {
                        while (index_2.isHiddenCol(sheet_1, actIdxes_1[1])) {
                            if (e.keyCode === 39 || (!e.shiftKey && e.keyCode === 9)) {
                                actIdxes_1[1] = actIdxes_1[1] + 1;
                            }
                            if (e.keyCode === 37 || (e.shiftKey && e.keyCode === 9)) {
                                actIdxes_1[1] = actIdxes_1[1] - 1;
                                if (actIdxes_1[1] < 0) {
                                    return;
                                }
                            }
                        }
                    }
                    this.scrollNavigation(scrollIdxes || actIdxes_1, scrollIdxes ? true : false);
                    var range_1 = index_2.getRangeAddress(actIdxes_1);
                    var navigateFn = function (preventAnimation) {
                        if (range_1 === sheet_1.selectedRange) {
                            return;
                        }
                        _this.parent.setSheetPropertyOnMute(sheet_1, 'activeCell', range_1);
                        _this.parent.notify(index_1.cellNavigate, { range: actIdxes_1, preventAnimation: preventAnimation });
                    };
                    if (this.parent.scrollModule && this.parent.scrollModule.isKeyScroll) {
                        Iif (range_1 === sheet_1.selectedRange) {
                            return;
                        }
                        index_1.getUpdateUsingRaf(navigateFn.bind(this, true));
                    }
                    else {
                        navigateFn();
                    }
                }
            }
            if (e.target.classList.contains('e-sheet-rename')) {
                Iif (e.keyCode === 32) {
                    e.stopPropagation();
                }
                else Eif (e.keyCode === 13 || e.keyCode === 27) {
                    this.parent.notify(index_1.renameSheet, e);
                }
            }
        };
        KeyboardNavigation.prototype.shiftSelection = function (e) {
            var sheet = this.parent.getActiveSheet();
            var selectedRange = index_3.getRangeIndexes(sheet.selectedRange);
            var swapRange = index_3.getSwapRange(selectedRange);
            var noHidden = true;
            Iif (e.keyCode === 38) {
                for (var i = swapRange[1]; i <= swapRange[3]; i++) {
                    var cell = index_2.getCell(selectedRange[2], i, this.parent.getActiveSheet());
                    if (!ej2_base_1.isNullOrUndefined(cell) && cell.rowSpan && cell.rowSpan < 0) {
                        selectedRange[2] = selectedRange[2] - (Math.abs(cell.rowSpan) + 1);
                        noHidden = false;
                        break;
                    }
                }
                if (noHidden) {
                    selectedRange[2] = selectedRange[2] - 1;
                }
                if (selectedRange[2] < 0) {
                    selectedRange[2] = 0;
                }
            }
            if (e.keyCode === 40) {
                for (var i = swapRange[1]; i <= swapRange[3]; i++) {
                    var cell = index_2.getCell(selectedRange[2], i, this.parent.getActiveSheet());
                    Iif (!ej2_base_1.isNullOrUndefined(cell) && cell.rowSpan && cell.rowSpan > 0) {
                        selectedRange[2] = selectedRange[2] + Math.abs(cell.rowSpan);
                        noHidden = false;
                        break;
                    }
                }
                Eif (noHidden) {
                    selectedRange[2] = selectedRange[2] + 1;
                }
            }
            if (e.keyCode === 39) {
                for (var i = swapRange[0]; i <= swapRange[2]; i++) {
                    var cell = index_2.getCell(i, selectedRange[3], this.parent.getActiveSheet());
                    Iif (!ej2_base_1.isNullOrUndefined(cell) && cell.colSpan && cell.colSpan > 0) {
                        selectedRange[3] = selectedRange[3] + Math.abs(cell.colSpan);
                        noHidden = false;
                        break;
                    }
                }
                Eif (noHidden) {
                    selectedRange[3] = selectedRange[3] + 1;
                }
            }
            Iif (e.keyCode === 37) {
                for (var i = swapRange[0]; i <= swapRange[2]; i++) {
                    var cell = index_2.getCell(i, selectedRange[3], this.parent.getActiveSheet());
                    if (!ej2_base_1.isNullOrUndefined(cell) && cell.colSpan && cell.colSpan < 0) {
                        selectedRange[3] = selectedRange[3] - (Math.abs(cell.colSpan) + 1);
                        noHidden = false;
                        break;
                    }
                }
                if (noHidden) {
                    selectedRange[3] = selectedRange[3] - 1;
                }
                if (selectedRange[3] < 0) {
                    selectedRange[3] = 0;
                }
            }
            Iif (e.shiftKey && e.ctrlKey && !this.parent.scrollSettings.enableVirtualization) {
                var usedRange = [sheet.usedRange.rowIndex, sheet.usedRange.colIndex];
                if (e.keyCode === 37) {
                    if (selectedRange[3] <= usedRange[1]) {
                        selectedRange[3] = 0;
                    }
                    else {
                        selectedRange[3] = usedRange[1];
                    }
                }
                if (e.keyCode === 38) {
                    if (selectedRange[2] <= usedRange[0]) {
                        selectedRange[2] = 0;
                    }
                    else {
                        selectedRange[2] = usedRange[0];
                    }
                }
                if (e.keyCode === 39) {
                    if (selectedRange[3] <= usedRange[1]) {
                        selectedRange[3] = usedRange[1];
                    }
                    else {
                        selectedRange[3] = this.parent.getActiveSheet().colCount;
                    }
                }
                if (e.keyCode === 40) {
                    if (selectedRange[2] <= usedRange[0]) {
                        selectedRange[2] = usedRange[0];
                    }
                    else {
                        selectedRange[2] = this.parent.getActiveSheet().rowCount;
                    }
                }
            }
            if (e.keyCode === 37 || e.keyCode === 39 || e.keyCode === 38 || e.keyCode === 40) {
                var activeIdxes = index_2.getCellIndexes(sheet.activeCell);
                while (index_3.isHiddenRow(this.parent.getActiveSheet(), selectedRange[2])) {
                    if (e.keyCode === 40) {
                        selectedRange[2] = selectedRange[2] + 1;
                    }
                    if (e.keyCode === 38) {
                        selectedRange[2] = selectedRange[2] - 1;
                    }
                }
                this.parent.selectRange(index_2.getRangeAddress(selectedRange));
                this.scrollNavigation([index_3.isColumnSelected(sheet, selectedRange) ? activeIdxes[0] : selectedRange[2],
                    index_3.isRowSelected(sheet, selectedRange) ? activeIdxes[1] : selectedRange[3]], false);
            }
        };
        KeyboardNavigation.prototype.scrollNavigation = function (actIdxes, isScroll) {
            if (!this.parent.allowScrolling) {
                return;
            }
            var x = this.parent.enableRtl ? -1 : 1;
            var cont = this.parent.getMainContent().parentElement;
            var hCont = this.parent.getScrollElement();
            var sheet = this.parent.getActiveSheet();
            var selectedRange = index_3.getSwapRange(index_3.getRangeIndexes(sheet.selectedRange));
            var topLeftIdxes = index_2.getCellIndexes(sheet.topLeftCell);
            var frozenRow = this.parent.frozenRowCount(sheet);
            var frozenCol = this.parent.frozenColCount(sheet);
            var paneTopLeftIdxes = index_2.getCellIndexes(sheet.paneTopLeftCell);
            var topIdx = index_1.skipHiddenIdx(sheet, actIdxes[0] < frozenRow ? topLeftIdxes[0] : paneTopLeftIdxes[0], true);
            var leftIdx = actIdxes[1] < frozenCol ? topLeftIdxes[1] : paneTopLeftIdxes[1];
            var offsetTopSize = this.parent.scrollModule.offset.top.size;
            Iif (cont.scrollTop) {
                if (frozenRow && actIdxes[0] !== selectedRange[2]) {
                    if (actIdxes[0] === frozenRow) {
                        cont.scrollTop = 0;
                        return;
                    }
                    if (actIdxes[0] === frozenRow - 1) {
                        cont.scrollTop = 0;
                    }
                }
                else if (actIdxes[0] === index_1.skipHiddenIdx(sheet, 0, true)) {
                    cont.scrollTop = 0;
                    return;
                }
            }
            Iif (hCont.scrollLeft) {
                if (frozenCol && actIdxes[1] !== selectedRange[3]) {
                    if (actIdxes[1] === frozenCol) {
                        hCont.scrollLeft = 0;
                        return;
                    }
                    if (actIdxes[1] === frozenCol - 1) {
                        hCont.scrollLeft = 0;
                    }
                }
                else if (actIdxes[1] === index_1.skipHiddenIdx(sheet, 0, true, 'columns')) {
                    hCont.scrollLeft = 0;
                    return;
                }
            }
            Iif (this.getBottomIdx(topIdx) <= actIdxes[0] || isScroll) {
                cont.scrollTop = offsetTopSize + index_2.getRowHeight(sheet, index_1.skipHiddenIdx(sheet, paneTopLeftIdxes[0], true), true);
                this.parent.scrollModule.isKeyScroll = false;
            }
            else Iif (topIdx > actIdxes[0]) {
                cont.scrollTop = offsetTopSize - Math.ceil(index_2.getRowHeight(sheet, actIdxes[0], true));
                this.parent.scrollModule.isKeyScroll = false;
            }
            var scrollLeftIdx = this.getRightIdx(leftIdx);
            Iif (scrollLeftIdx <= actIdxes[1] || isScroll) {
                hCont.scrollLeft += index_2.getColumnWidth(sheet, scrollLeftIdx, null, true) * x;
                this.parent.scrollModule.isKeyScroll = false;
            }
            else Iif (leftIdx > actIdxes[1]) {
                hCont.scrollLeft -= index_2.getColumnWidth(sheet, actIdxes[1], null, true) * x;
                this.parent.scrollModule.isKeyScroll = false;
            }
        };
        KeyboardNavigation.prototype.getBottomIdx = function (top) {
            var hgt = 0;
            var sheet = this.parent.getActiveSheet();
            var viewPortHeight = (sheet.frozenRows ? this.parent.viewport.height - this.parent.sheetModule.getColHeaderHeight(sheet, true) : this.parent.viewport.height) - 17 || 20;
            for (var i = top;; i++) {
                hgt += index_2.getRowHeight(sheet, i, true);
                if (hgt >= viewPortHeight) {
                    return i;
                }
            }
        };
        KeyboardNavigation.prototype.getRightIdx = function (left) {
            var width = 0;
            var sheet = this.parent.getActiveSheet();
            var contWidth = this.parent.getMainContent().parentElement.offsetWidth -
                this.parent.sheetModule.getRowHeaderWidth(sheet) - this.parent.sheetModule.getScrollSize();
            for (var i = left;; i++) {
                width += index_2.getColumnWidth(sheet, i, null, true);
                if (width >= contWidth) {
                    return i;
                }
            }
        };
        KeyboardNavigation.prototype.getModuleName = function () {
            return 'keyboardNavigation';
        };
        KeyboardNavigation.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        return KeyboardNavigation;
    }());
    exports.KeyboardNavigation = KeyboardNavigation;
});