all files / spreadsheet/actions/ auto-fill.js

96.14% Statements 374/389
85.59% Branches 196/229
100% Functions 34/34
96.12% Lines 372/387
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   883× 883× 883×   1078× 1078×             1039× 1038× 1038× 1038× 26×     1012×   1038× 1038× 275× 275×   1038×     1040× 1040× 1040× 1040×             18×   20×   1040× 1040× 1040× 1040×   18× 18× 18× 10×           18×   38× 38× 38× 38× 38× 38×   18×             18× 18× 18× 18× 18× 18× 18× 18× 18×   18× 18× 18× 18×     18× 18× 18× 18× 18× 18×     18× 18× 18×   18×   56× 213×   56× 213× 56×     56×   56×   19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729× 19729×     182× 182×   19547× 19531× 59× 59× 59× 59× 59× 59×   19472× 139× 139× 139× 139× 139× 139×   19531× 316× 17× 17×       299× 299×     316× 316× 316× 316× 304× 304×     304× 521×           19215× 19215× 19215× 19215× 19070×   19215× 19166×   19215× 19215× 19215× 19215× 19215× 19215× 37×     19178×   19215× 19215× 19215×     19215× 64× 64× 64×     63×             220× 220× 298× 298×       10625× 10625× 10672× 10672×       104× 104× 104× 104× 104× 104× 104× 104× 103×         104× 104×   158× 158× 158× 158× 158× 158× 158× 158× 158× 158× 158× 158× 158× 158× 22× 18×               136× 66×       65×       70×             64× 64×                   158× 158× 21× 21× 21× 21×     19× 13× 13× 12×               158×   64×     63× 63×     54×     54×     54× 54× 54×   63× 63× 63×         63×   63×       63× 63×   62× 62×   60×     53× 53×               18× 18× 13×                                           60× 60× 60× 60×             57× 57×       57×   57× 57×   57× 57× 57× 57×     218× 218× 218× 218× 218× 218× 218× 218× 218× 1129×   1128× 1128× 1294×   1293× 1293× 1293×   1128×   218×   218× 218× 218× 1143× 24×     194×   883× 883× 883× 883× 883× 883× 883× 883×   883× 12× 12× 12× 12× 12× 12× 12× 12×     883× 883× 763×   883× 883× 883× 883× 763×   883× 883× 883× 883× 763× 763×   883× 883×   126650×        
define(["require", "exports", "../../spreadsheet/index", "../../spreadsheet/index", "../../spreadsheet/index", "../common/index", "../../workbook/index", "../../workbook/index", "../../workbook/index", "@syncfusion/ej2-base", "@syncfusion/ej2-splitbuttons"], function (require, exports, index_1, index_2, index_3, index_4, index_5, index_6, index_7, ej2_base_1, ej2_splitbuttons_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var AutoFill = (function () {
        function AutoFill(parent) {
            this.fillOptionIndex = 0;
            this.parent = parent;
            this.addEventListener();
        }
        AutoFill.prototype.getfillItems = function () {
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            return [
                { text: l10n.getConstant('CopyCells') },
                { text: l10n.getConstant('FillSeries') },
                { text: l10n.getConstant('FillFormattingOnly') },
                { text: l10n.getConstant('FillWithoutFormatting') }
            ];
        };
        AutoFill.prototype.createAutoFillElement = function () {
            if (this.parent.allowAutoFill) {
                var element = this.parent.getMainContent();
                var ele = this.parent.createElement('div', { className: 'e-autofill' });
                if (element.lastElementChild && element.lastElementChild.classList.contains('e-ss-overlay')) {
                    element.insertBefore(ele, element.getElementsByClassName('e-ss-overlay')[0]);
                }
                else {
                    element.appendChild(ele);
                }
                this.autoFillElement = ele;
                if (this.autoFillDropDown) {
                    this.autoFillDropDown.destroy();
                    this.autoFillDropDown = null;
                }
                this.getautofillDDB({ id: this.parent.element.id + '_autofilloptionbtn', appendElem: element });
            }
        };
        AutoFill.prototype.getautofillDDB = function (args) {
            var _this = this;
            this.splitBtnElem = this.parent.createElement('button', { id: args.id, className: 'e-filloption', attrs: { 'type': 'button' } });
            this.splitBtnElem.appendChild(this.parent.createElement('span', { className: 'e-tbar-btn-text' }));
            this.autoFillDropDown = new ej2_splitbuttons_1.DropDownButton({
                cssClass: 'e-dragfill-ddb',
                iconCss: 'e-icons e-dragfill-icon',
                items: this.getfillItems(),
                createPopupOnClick: true,
                enableRtl: this.parent.enableRtl,
                select: function (args) {
                    _this.autoFillOptionClick({ type: _this.getFillType(args.item.text) });
                },
                beforeOpen: function () { return _this.autoFillClick(); }
            });
            this.autoFillDropDown.createElement = this.parent.createElement;
            this.autoFillDropDown.appendTo(this.splitBtnElem);
            args.appendElem.appendChild(this.splitBtnElem);
            return this.autoFillDropDown;
        };
        AutoFill.prototype.getFillType = function (text) {
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            var type;
            if (text === l10n.getConstant('CopyCells')) {
                type = 'CopyCells';
            }
            else if (text === l10n.getConstant('FillSeries')) {
                type = 'FillSeries';
            }
            else if (text === l10n.getConstant('FillFormattingOnly')) {
                type = 'FillFormattingOnly';
            }
            else {
                type = 'FillWithoutFormatting';
            }
            return type;
        };
        AutoFill.prototype.autoFillClick = function () {
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            var fillInfo = { fillType: 'FillSeries', disableItems: [''] };
            this.parent.notify(index_5.getFillInfo, fillInfo);
            this.autoFillDropDown.setProperties({ 'items': this.getfillItems() }, true);
            this.autoFillDropDown.removeItems(fillInfo.disableItems);
            this.refreshAutoFillOption(l10n.getConstant(fillInfo.fillType));
        };
        AutoFill.prototype.getFillRange = function (pStartCell, pEndCell, pFillCell, direction) {
            switch (direction) {
                case 'Up':
                    return [pFillCell.rowIndex, pStartCell.colIndex, pStartCell.rowIndex - 1, pEndCell.colIndex];
                case 'Right':
                    return [pStartCell.rowIndex, pEndCell.colIndex + 1, pEndCell.rowIndex, pFillCell.colIndex];
                case 'Down':
                    return [pEndCell.rowIndex + 1, pStartCell.colIndex, pFillCell.rowIndex, pEndCell.colIndex];
                case 'Left':
                    return [pStartCell.rowIndex, pFillCell.colIndex, pEndCell.rowIndex, pStartCell.colIndex - 1];
            }
        };
        AutoFill.prototype.autoFillOptionClick = function (args) {
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            var sheet = this.parent.getActiveSheet();
            var range = index_6.getSwapRange(index_6.getRangeIndexes(this.parent.selectionModule.dAutoFillCell));
            var currcell = index_6.getRangeIndexes(sheet.selectedRange);
            var minr = range[0];
            var minc = range[1];
            var maxr = range[2];
            var maxc = range[3];
            var dir = this.getDirection({ rowIndex: maxr, colIndex: maxc }, { rowIndex: currcell[2],
                colIndex: currcell[3] });
            var dataRange = [minr, minc, maxr, maxc];
            var fillRange = this.getFillRange({ rowIndex: minr, colIndex: minc }, { rowIndex: maxr, colIndex: maxc }, { rowIndex: currcell[2], colIndex: currcell[3] }, dir);
            this.refreshAutoFillOption(l10n.getConstant(args.type));
            var evtArgs = { isUndo: true, isPublic: true, preventReSelect: true,
                preventEvt: args.type === 'FillWithoutFormatting',
                setCollection: args.type === 'FillFormattingOnly' || args.type === 'FillWithoutFormatting', isFromAutoFillOption: true };
            this.parent.notify(index_1.performUndoRedo, evtArgs);
            var eventArgs = { dataRange: sheet.name + '!' + index_7.getRangeAddress(dataRange), fillRange: sheet.name + '!' + index_7.getRangeAddress(fillRange), direction: dir, fillType: args.type, isFillOptClick: true };
            this.isVerticalFill = eventArgs.direction === 'Down' || eventArgs.direction === 'Up';
            this.parent.notify(index_5.setAutoFill, eventArgs);
            this.positionAutoFillElement({ isautofill: true });
            var autoFillArgs = { dataRange: eventArgs.dataRange, fillRange: eventArgs.fillRange,
                fillType: eventArgs.fillType, direction: eventArgs.direction, selectedRange: sheet.name + '!' + index_7.getRangeAddress(currcell),
                undoArgs: evtArgs.undoArgs };
            this.parent.notify(index_1.completeAction, { eventArgs: autoFillArgs, action: 'autofill' });
            Eif (this.parent.showAggregate) {
                this.parent.notify(index_1.showAggregate, {});
            }
            this.autoFillClick();
        };
        AutoFill.prototype.refreshAutoFillOption = function (type) {
            for (var i = 0; i < this.autoFillDropDown.items.length; i++) {
                this.autoFillDropDown.items[i].iconCss = '';
            }
            for (var i = 0; i < this.autoFillDropDown.items.length; i++) {
                if (this.autoFillDropDown.items[i].text === type) {
                    this.autoFillDropDown.items[i].iconCss = 'e-icons e-selected-icon';
                }
            }
            if (['Copy Cells', 'Fill Series', 'Fill Formatting Only', 'Fill Without Formatting'].indexOf(type) < 0) {
                this.autoFillDropDown.items[this.fillOptionIndex].iconCss = '';
            }
            this.autoFillDropDown.dataBind();
        };
        AutoFill.prototype.positionAutoFillElement = function (args) {
            var top = 0;
            var left = 0;
            var sheet = this.parent.getActiveSheet();
            var indexes = index_6.getSwapRange(index_6.getRangeIndexes(sheet.selectedRange));
            var tdiff = -5;
            var ldiff = -5;
            var otdiff = 6;
            var oldiff = 6;
            var isRowSelected = (indexes[1] === 0 && indexes[3] === sheet.colCount - 1);
            var isColSelected = (indexes[0] === 0 && indexes[2] === sheet.rowCount - 1);
            var rowIdx = indexes[2];
            var colIdx = indexes[3];
            var height;
            var width;
            var pos;
            var isRtl = this.parent.enableRtl;
            var cell = this.parent.getCell(rowIdx, colIdx);
            if (index_7.isHiddenCol(sheet, indexes[3]) || index_5.isHiddenRow(sheet, indexes[2]) ||
                (cell && cell.classList.contains('e-formularef-selection')) || (sheet.isProtected && sheet.protectSettings.selectUnLockedCells
                && index_3.isLockedCells(this.parent, indexes))) {
                this.hideAutoFillElement();
                return;
            }
            if ((sheet.isProtected && (sheet.protectSettings.selectCells || sheet.protectSettings.selectUnLockedCells)) || !sheet.isProtected) {
                if (isRowSelected) {
                    tdiff = -5;
                    ldiff = -1;
                    otdiff = 6;
                    oldiff = 2;
                    rowIdx = indexes[2];
                    colIdx = indexes[1];
                }
                else if (isColSelected) {
                    ldiff = -5;
                    tdiff = 0;
                    otdiff = 1;
                    oldiff = 6;
                    rowIdx = indexes[0];
                    colIdx = indexes[3];
                }
                if (sheet.frozenColumns || sheet.frozenRows) {
                    if (isColSelected || isRowSelected) {
                        index_1.setPosition(this.parent, this.autoFillElement, indexes, 'e-autofill', args && args.preventAnimation);
                        if (this.parent.autoFillSettings.showFillOptions && args && args.isautofill) {
                            index_1.setPosition(this.parent, this.autoFillDropDown.element, indexes, 'e-filloption');
                        }
                    }
                    else {
                        index_1.setPosition(this.parent, this.autoFillElement, [rowIdx, colIdx, rowIdx, colIdx], 'e-autofill', args && args.preventAnimation);
                        if (this.parent.autoFillSettings.showFillOptions && args && args.isautofill) {
                            index_1.setPosition(this.parent, this.autoFillDropDown.element, [rowIdx, colIdx, rowIdx, colIdx], 'e-filloption');
                        }
                    }
                    Eif (this.autoFillElement) {
                        this.autoFillCell = { rowIndex: rowIdx, colIndex: colIdx };
                        var autoFillHandles = this.parent.element.querySelectorAll('.e-autofill');
                        if (autoFillHandles.length) {
                            var clientRect = autoFillHandles[0].getBoundingClientRect();
                            this.autoFillElementPosition = {
                                left: clientRect.left, top: clientRect.top
                            };
                            [].slice.call(autoFillHandles).forEach(function (autoFillElem) {
                                ej2_base_1.removeClass([autoFillElem], 'e-hide');
                            });
                        }
                    }
                }
                else {
                    pos = index_2.getCellPosition(sheet, [rowIdx, colIdx, rowIdx, colIdx], this.parent.frozenRowCount(sheet), this.parent.frozenColCount(sheet), this.parent.viewport.beforeFreezeHeight, this.parent.viewport.beforeFreezeWidth, this.parent.sheetModule.colGroupWidth);
                    height = index_6.getRowsHeight(sheet, rowIdx, rowIdx, true);
                    width = index_6.getColumnsWidth(sheet, colIdx, colIdx, true);
                    if (!isColSelected) {
                        top += height;
                    }
                    if (!isRowSelected) {
                        left += width;
                    }
                    top += Math.round(pos.top) + tdiff;
                    left += Math.round(pos.left) + ldiff;
                    Eif (this.autoFillElement) {
                        ej2_base_1.removeClass([this.autoFillElement], 'e-hide');
                        this.autoFillElement.style.top = top + 'px';
                        if (isRtl) {
                            this.autoFillElement.style.right = left + 'px';
                        }
                        else {
                            this.autoFillElement.style.left = left + 'px';
                        }
                        this.autoFillCell = { rowIndex: rowIdx, colIndex: colIdx };
                        var clientRect = this.autoFillElement.getBoundingClientRect();
                        this.autoFillElementPosition = {
                            left: clientRect.left, top: clientRect.top
                        };
                        if (this.parent.autoFillSettings.showFillOptions && args && args.isautofill) {
                            ej2_base_1.removeClass([this.autoFillDropDown.element], 'e-hide');
                            this.autoFillDropDown.element.style.top = top + otdiff + 'px';
                            if (isRtl) {
                                this.autoFillDropDown.element.style.right = left + oldiff + 'px';
                            }
                            else {
                                this.autoFillDropDown.element.style.left = left + oldiff + 'px';
                            }
                        }
                    }
                }
            }
        };
        AutoFill.prototype.hideAutoFillElement = function () {
            var elem = this.parent.element;
            [].slice.call(elem.querySelectorAll('.e-autofill')).forEach(function (optElem) {
                Eif (elem) {
                    ej2_base_1.addClass([optElem], 'e-hide');
                }
            });
        };
        AutoFill.prototype.hideAutoFillOptions = function () {
            var elem = this.parent.element;
            [].slice.call(elem.querySelectorAll('.e-filloption')).forEach(function (optElem) {
                Eif (elem) {
                    ej2_base_1.addClass([optElem], 'e-hide');
                }
            });
        };
        AutoFill.prototype.selectAutoFillRange = function (args) {
            var rowObj = { clientY: index_1.getClientY(args.e), target: args.e.target };
            var colObj = { clientX: index_1.getClientX(args.e), target: args.e.target };
            var sheet = this.parent.getActiveSheet();
            this.parent.notify(index_2.getRowIdxFromClientY, rowObj);
            this.parent.notify(index_1.getColIdxFromClientX, colObj);
            var rangeIndexes;
            var autofillRange = this.getAutoFillRange({ rowIndex: rowObj.clientY, colIndex: colObj.clientX });
            if (autofillRange && autofillRange.fillRange) {
                rangeIndexes = [autofillRange.startCell.rowIndex, autofillRange.startCell.colIndex, autofillRange.endCell.rowIndex,
                    autofillRange.endCell.colIndex];
            }
            else {
                rangeIndexes = index_6.getRangeIndexes(sheet.selectedRange);
            }
            args.indexes = rangeIndexes;
            return rangeIndexes;
        };
        AutoFill.prototype.getAutoFillRange = function (idx) {
            var sheet = this.parent.getActiveSheet();
            var aCell = this.autoFillCell;
            var range = index_6.getSwapRange(index_6.getRangeIndexes(sheet.selectedRange));
            var minr = range[0];
            var minc = range[1];
            var maxr = range[2];
            var maxc = range[3];
            var inRange = index_6.isInRange(range, [idx.rowIndex, idx.colIndex, idx.rowIndex, idx.colIndex], true);
            var minIdx = { rowIndex: minr, colIndex: minc };
            var scell = { rowIndex: range[0], colIndex: range[1] };
            var ecell = { rowIndex: range[2], colIndex: range[3] };
            var maxIdx = { rowIndex: maxr, colIndex: maxc };
            var modifiedIdx = this.modifyRangeForMerge(idx.rowIndex, idx.colIndex, aCell.rowIndex, aCell.colIndex, range);
            if (idx.rowIndex < aCell.rowIndex) {
                if ((minr - idx.rowIndex > idx.colIndex - maxc) && (minr - idx.rowIndex > minc - idx.colIndex)) {
                    return inRange ? { startCell: minIdx, endCell: { rowIndex: idx.rowIndex, colIndex: maxc } } : { startCell: maxIdx, endCell: { rowIndex: modifiedIdx.rowIndex, colIndex: minc }, fillRange: [modifiedIdx.rowIndex, minc, minr - 1, maxc], direction: 'Up' };
                }
                else if (idx.colIndex > aCell.colIndex) {
                    return { startCell: minIdx, endCell: { rowIndex: maxr, colIndex: idx.colIndex },
                        fillRange: [minr, maxc + 1, maxr, idx.colIndex], direction: 'Right' };
                }
                else if (idx.colIndex < aCell.colIndex) {
                    return inRange ? { startCell: minIdx, endCell: maxIdx } : { startCell: maxIdx, endCell: { rowIndex: minr, colIndex: idx.colIndex }, fillRange: [minr, idx.colIndex, maxr, minc - 1], direction: 'Left' };
                }
                else {
                    return { startCell: scell, endCell: ecell };
                }
            }
            else if (idx.colIndex > aCell.colIndex) {
                if ((idx.rowIndex - maxr > idx.colIndex - maxc)) {
                    return { startCell: minIdx, endCell: { rowIndex: idx.rowIndex, colIndex: maxc },
                        fillRange: [maxr + 1, minc, idx.rowIndex, maxc], direction: 'Down' };
                }
                else {
                    return { startCell: minIdx, endCell: { rowIndex: maxr, colIndex: modifiedIdx.colIndex },
                        fillRange: [minr, maxc + 1, maxr, modifiedIdx.colIndex], direction: 'Right' };
                }
            }
            else if (idx.colIndex < aCell.colIndex) {
                Iif ((idx.rowIndex - maxr > maxc - idx.colIndex) || ((idx.rowIndex - minr > maxc - idx.colIndex) && idx.rowIndex !== maxr)) {
                    return { startCell: minIdx, endCell: { rowIndex: idx.rowIndex, colIndex: maxc },
                        fillRange: [maxr + 1, minc, idx.rowIndex, maxc], direction: 'Down' };
                }
                else {
                    return inRange ? { startCell: minIdx, endCell: maxIdx } : { startCell: maxIdx, endCell: { rowIndex: minr, colIndex: modifiedIdx.colIndex }, fillRange: [minr, modifiedIdx.colIndex, maxr, minc - 1], direction: 'Left' };
                }
            }
            else Eif (idx.rowIndex > aCell.rowIndex) {
                return { startCell: minIdx, endCell: { rowIndex: modifiedIdx.rowIndex, colIndex: maxc },
                    fillRange: [maxr + 1, minc, modifiedIdx.rowIndex, maxc], direction: 'Down' };
            }
            else if (idx.rowIndex === aCell.rowIndex && idx.colIndex === aCell.colIndex) {
                return { startCell: scell, endCell: ecell };
            }
            else {
                return { startCell: scell, endCell: ecell };
            }
        };
        AutoFill.prototype.modifyRangeForMerge = function (rowIdx, colIdx, autoFillRowIdx, autoFillColIdx, selRange) {
            var modifiedIdx = { rowIndex: rowIdx, colIndex: colIdx };
            if (this.isMergedRange(selRange)) {
                var selRowCount = selRange[2] - selRange[0] + 1;
                var selColCount = selRange[3] - selRange[1] + 1;
                var remainder = void 0;
                if (rowIdx < autoFillRowIdx) {
                    remainder = (selRange[2] - rowIdx + 1) % selRowCount;
                    Eif (remainder && rowIdx - (selRowCount - remainder) >= 0) {
                        modifiedIdx.rowIndex = rowIdx - (selRowCount - remainder);
                    }
                }
                else if (colIdx > autoFillColIdx) {
                    remainder = (colIdx - selRange[1] + 1) % selColCount;
                    if (remainder) {
                        modifiedIdx.colIndex = colIdx + (selColCount - remainder);
                    }
                }
                else if (colIdx < autoFillColIdx) {
                    remainder = (selRange[3] - colIdx + 1) % selColCount;
                    Eif (remainder && colIdx - (selColCount - remainder) >= 0) {
                        modifiedIdx.colIndex = colIdx - (selColCount - remainder);
                    }
                }
                else Eif (rowIdx > autoFillRowIdx) {
                    remainder = (rowIdx - selRange[0] + 1) % selRowCount;
                    Eif (remainder) {
                        modifiedIdx.rowIndex = rowIdx + (selRowCount - remainder);
                    }
                }
            }
            return modifiedIdx;
        };
        AutoFill.prototype.performAutoFill = function (args) {
            if (args.rangeInfo || !(args.event.clientX > this.autoFillElementPosition.left &&
                args.event.clientX < this.autoFillElementPosition.left + 10) ||
                !(args.event.clientY > this.autoFillElementPosition.top && args.event.clientY < this.autoFillElementPosition.top + 10)) {
                var autofillRange = void 0;
                if (args.rangeInfo) {
                    autofillRange = args.rangeInfo;
                }
                else {
                    var rowObj = {
                        clientY: index_1.getClientY(args.event), target: args.event.target
                    };
                    var colObj = {
                        clientX: index_1.getClientX(args.event), target: args.event.target
                    };
                    this.parent.notify(index_2.getRowIdxFromClientY, rowObj);
                    this.parent.notify(index_1.getColIdxFromClientX, colObj);
                    autofillRange = this.getAutoFillRange({ rowIndex: rowObj.clientY, colIndex: colObj.clientX });
                }
                var sheet = this.parent.getActiveSheet();
                Eif (autofillRange && autofillRange.fillRange) {
                    var eventArgs = {
                        dataRange: sheet.name + '!' + args.dAutoFillCell,
                        fillRange: sheet.name + '!' + index_7.getRangeAddress(autofillRange.fillRange), direction: autofillRange.direction,
                        fillType: args.fillType || this.parent.autoFillSettings.fillType, cancel: false
                    };
                    var isReadonlyCells = index_4.isReadOnlyCells(this.parent, index_6.getRangeIndexes(args.dAutoFillCell)) ||
                        index_4.isReadOnlyCells(this.parent, autofillRange.fillRange);
                    Iif (isReadonlyCells) {
                        this.parent.notify(index_4.readonlyAlert, null);
                        return;
                    }
                    this.parent.notify(index_7.beginAction, { eventArgs: eventArgs, action: 'autofill' });
                    if (eventArgs.cancel) {
                        return;
                    }
                    var isLockedCell = index_3.isLockedCells(this.parent, autofillRange.fillRange);
                    if (sheet.isProtected && isLockedCell) {
                        this.parent.notify(index_4.editAlert, null);
                        return;
                    }
                    if (args.rangeInfo) {
                        this.performAutoFillAction(eventArgs, null, isLockedCell);
                    }
                    else {
                        this.performAutoFillAction(eventArgs, autofillRange, isLockedCell);
                        this.positionAutoFillElement({ isautofill: true });
                    }
                }
            }
            else {
                this.positionAutoFillElement({ isautofill: false });
            }
        };
        AutoFill.prototype.refreshCell = function (options) {
            this.parent.serviceLocator.getService('cell').refreshRange([options.rowIndex, options.colIndex, options.rowIndex, options.colIndex]);
        };
        AutoFill.prototype.getDirection = function (endCell, currcell, isVerticalFill) {
            isVerticalFill = ej2_base_1.isNullOrUndefined(isVerticalFill) ? this.isVerticalFill : isVerticalFill;
            if (isVerticalFill) {
                if (currcell.rowIndex < endCell.rowIndex) {
                    return 'Up';
                }
                else Eif (currcell.rowIndex > endCell.rowIndex) {
                    return 'Down';
                }
                else if (currcell.colIndex > endCell.colIndex) {
                    return 'Right';
                }
                else if (currcell.colIndex < endCell.colIndex) {
                    return 'Left';
                }
            }
            else {
                if (currcell.colIndex > endCell.colIndex) {
                    return 'Right';
                }
                else Eif (currcell.colIndex < endCell.colIndex) {
                    return 'Left';
                }
                else if (currcell.rowIndex < endCell.rowIndex) {
                    return 'Up';
                }
                else if (currcell.rowIndex > endCell.rowIndex) {
                    return 'Down';
                }
            }
            return null;
        };
        AutoFill.prototype.performAutoFillAction = function (args, autoFillRange, isLockedCell) {
            var _this = this;
            var sheet = this.parent.getActiveSheet();
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            if (this.isMergedRange(index_6.getRangeIndexes(args.fillRange))) {
                var dialogInst_1 = this.parent.serviceLocator.getService(index_1.dialog);
                dialogInst_1.show({
                    isModal: true, showCloseIcon: true, height: 180, width: 400, content: l10n.getConstant('AutoFillMergeAlertMsg'),
                    buttons: [{
                            buttonModel: { content: this.parent.serviceLocator.getService(index_1.locale).getConstant('Ok'), isPrimary: true },
                            click: function () { dialogInst_1.hide(); _this.parent.selectRange(args.dataRange); }
                        }],
                    close: function () { dialogInst_1.hide(); _this.parent.selectRange(args.dataRange); }
                }, false);
                return;
            }
            this.isVerticalFill = args.direction === 'Down' || args.direction === 'Up';
            this.parent.notify(index_5.setAutoFill, {
                dataRange: args.dataRange,
                fillRange: args.fillRange, direction: args.direction, fillType: args.fillType, isLockedCell: isLockedCell
            });
            var selRange = autoFillRange ? index_7.getRangeAddress([autoFillRange.startCell.rowIndex, autoFillRange.startCell.colIndex,
                autoFillRange.endCell.rowIndex, autoFillRange.endCell.colIndex]) : sheet.selectedRange;
            index_5.updateSelectedRange(this.parent, selRange, sheet);
            var autoFillArgs = { dataRange: args.dataRange, fillRange: args.fillRange, fillType: args.fillType, direction: args.direction,
                selectedRange: selRange };
            this.parent.notify(index_1.completeAction, { eventArgs: autoFillArgs, action: 'autofill' });
            this.parent.trigger('select', { range: this.parent.getActiveSheet().selectedRange });
            Eif (this.parent.showAggregate) {
                this.parent.notify(index_1.showAggregate, {});
            }
        };
        AutoFill.prototype.getRangeData = function (options) {
            var arr = [];
            var sheet = this.parent.getActiveSheet();
            var minr = options.range[0];
            var minc = options.range[1];
            var maxr = options.range[2];
            var maxc = options.range[3];
            var minCol = minc;
            var cell;
            while (minr <= maxr) {
                if (index_5.isHiddenRow(sheet, minr)) {
                    minr++;
                    continue;
                }
                minc = minCol;
                while (minc <= maxc) {
                    if (index_7.isHiddenCol(sheet, minc)) {
                        minc++;
                        continue;
                    }
                    cell = index_7.getCell(minr, minc, sheet);
                    arr.push(cell);
                    minc++;
                }
                minr++;
            }
            return arr;
        };
        AutoFill.prototype.isMergedRange = function (range) {
            var i = 0;
            var data = this.getRangeData({ range: range, sheetIdx: this.parent.activeSheetIndex });
            for (i = 0; i < data.length; i++) {
                if (data[i] && (data[i].rowSpan || data[i].colSpan)) {
                    return true;
                }
            }
            return false;
        };
        AutoFill.prototype.addEventListener = function () {
            this.parent.on(index_2.contentLoaded, this.createAutoFillElement, this);
            this.parent.on(index_2.positionAutoFillElement, this.positionAutoFillElement, this);
            this.parent.on(index_1.hideAutoFillOptions, this.hideAutoFillOptions, this);
            this.parent.on(index_1.hideAutoFillElement, this.hideAutoFillElement, this);
            this.parent.on(index_3.performAutoFill, this.performAutoFill, this);
            this.parent.on(index_1.selectAutoFillRange, this.selectAutoFillRange, this);
            this.parent.on(index_5.refreshCell, this.refreshCell, this);
            this.parent.on(index_5.getautofillDDB, this.getautofillDDB, this);
        };
        AutoFill.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_2.contentLoaded, this.createAutoFillElement);
                this.parent.off(index_2.positionAutoFillElement, this.positionAutoFillElement);
                this.parent.off(index_1.hideAutoFillOptions, this.hideAutoFillOptions);
                this.parent.off(index_1.hideAutoFillElement, this.hideAutoFillElement);
                this.parent.off(index_3.performAutoFill, this.performAutoFill);
                this.parent.off(index_1.selectAutoFillRange, this.selectAutoFillRange);
                this.parent.off(index_5.refreshCell, this.refreshCell);
                this.parent.off(index_5.getautofillDDB, this.getautofillDDB);
            }
        };
        AutoFill.prototype.destroy = function () {
            this.removeEventListener();
            if (this.autoFillElement) {
                this.autoFillElement.remove();
            }
            this.autoFillElement = null;
            this.autoFillElementPosition = null;
            this.autoFillCell = null;
            if (this.autoFillDropDown) {
                this.autoFillDropDown.destroy();
            }
            this.autoFillDropDown = null;
            this.isVerticalFill = null;
            this.fillOptionIndex = null;
            if (this.splitBtnElem) {
                index_1.removeAllChildren(this.splitBtnElem);
                this.splitBtnElem.remove();
            }
            this.splitBtnElem = null;
            this.parent = null;
        };
        AutoFill.prototype.getModuleName = function () {
            return 'autofill';
        };
        return AutoFill;
    }());
    exports.AutoFill = AutoFill;
});