all files / spreadsheet/actions/ find-and-replace.js

98.22% Statements 552/562
91.27% Branches 230/252
100% Functions 53/53
98.22% Lines 551/561
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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 8551×   1× 1× 1× 1523× 1523× 1523× 1523× 1523× 1523× 1523× 1523×   1× 1524× 1524× 1524× 1524× 1524× 1524× 1524× 1524× 1524×   1× 1526× 39× 39× 39× 39× 39× 39× 39× 39× 39× 39×           1× 1535× 1535× 12× 12× 12×   12× 12×   1535× 14×     1× 67× 67× 102× 102× 92×     92× 91× 13×     78×   91×       67× 67× 20×   67× 67× 67× 67× 67× 33×   2×   31× 31× 4× 4× 4×       27× 27× 3× 3×     24×   24× 24× 24×   27× 27×     67× 23× 9× 7× 7× 7× 7× 7×   9× 6× 6× 4× 4×     2×   6×       14× 14×       44× 44× 44× 19×   44× 44× 44× 44× 44× 26× 1×     1×     25×     44× 44× 44×                     44×   67× 29× 29×   38× 7× 7×   31× 20× 20×         44× 176×     44× 44× 44× 44× 44× 44×     44× 44×   17×   17× 17× 17× 17× 17×     27×   44× 44× 44× 44× 41× 41× 41× 41×   44×   44× 13×     44×   44× 1× 1×   44× 44× 44× 44× 44×   44× 44×     44× 44× 44× 44× 44×     44× 44× 44× 44× 44×   44× 44× 44× 44×   44× 44× 44×     44×         44×   44× 44× 44× 44× 13× 13×   44×     1× 30× 30× 30×   1× 153× 153× 4× 4×     1× 40× 40× 40× 8× 8× 3×       32× 32× 3×     40× 40×   1× 80× 12×     1× 25× 25× 25× 25× 25×       25× 25× 25× 1×     24× 24× 24×             21× 21×           34× 34×           11× 11×           9× 9×       24× 24× 13× 13× 13× 13×   24× 24×   24× 3× 3× 3× 3×   3× 3× 3× 3× 3×           25×           1× 17× 17× 16× 16×   17× 17× 32× 32× 32×     17× 17× 32× 32× 32×     17× 17× 17× 17× 17× 17× 17×   1× 76× 7×   69× 7×     1× 16× 16× 16× 16× 16× 14×       14×       14× 14× 2×     12× 12× 12×               14× 4×         12×   13×   14×     2×     1× 12× 12× 12× 12× 1×     12×   1× 95× 95× 40× 40×       95× 95× 95× 95×     95× 95× 95× 95× 40×     55× 55×   95× 95× 95× 40×     55× 55×   95× 95× 95× 40×     55× 55×   95× 95× 95× 40×     55× 55×   95×       95×     1× 20× 20× 20× 20× 20× 20× 20× 20×     1× 150× 150× 136×     14× 14× 5× 1× 1×     14× 14× 2×   14× 14× 1×   14× 14× 14× 7× 7×     7× 7× 7× 2× 2×   5× 1× 1×     4×       150×   1× 9× 9× 9× 9× 2×   9×   1× 22×     22× 22× 10× 10× 10×     1× 41×     41× 41× 2×   41× 41× 41× 41× 11×     1× 48× 32× 32× 31× 31× 31× 31×     1× 1× 1×     48× 48× 48× 48× 48× 24× 24× 24×       24× 24×       1× 24× 20×   24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24×           24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24×     24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24×       24×           24× 24× 24×     24× 24× 24× 24× 24× 24× 24× 24× 24× 24×       24×           24× 24× 24×     24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24× 24×         24×     24× 24× 24× 24× 24×         24× 24×     24× 24× 24× 24× 24× 24×   1× 12× 12× 12× 12× 12× 12× 12× 12×       12× 12× 12× 12× 12× 12× 12×   1× 1522× 1522× 1522× 10×   1522× 8×   1522× 8×   1522× 8×   1522× 8×   1522× 8×   1522× 1522× 1522× 1522×   1× 234058×   1×   1×    
define(["require", "exports", "../common/index", "@syncfusion/ej2-base", "../../workbook/index", "../../workbook/common/index", "@syncfusion/ej2-buttons", "@syncfusion/ej2-dropdowns", "@syncfusion/ej2-inputs", "@syncfusion/ej2-navigations", "@syncfusion/ej2-popups"], function (require, exports, index_1, ej2_base_1, index_2, index_3, ej2_buttons_1, ej2_dropdowns_1, ej2_inputs_1, ej2_navigations_1, ej2_popups_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var FindAndReplace = (function () {
        function FindAndReplace(parent) {
            this.shortValue = '';
            this.divElements = [];
            this.paraElements = [];
            this.inputElements = [];
            this.textBoxElements = [];
            this.dropDownListElements = [];
            this.parent = parent;
            this.addEventListener();
        }
        FindAndReplace.prototype.addEventListener = function () {
            this.parent.on(index_1.findDlg, this.renderFindDlg, this);
            this.parent.on(index_1.gotoDlg, this.renderGotoDlg, this);
            this.parent.on(index_2.goto, this.gotoHandler, this);
            this.parent.on(index_1.findHandler, this.findHandler, this);
            this.parent.on(index_2.showFindAlert, this.showFindAlert, this);
            this.parent.on(index_2.replaceAllDialog, this.replaceAllDialog, this);
            this.parent.on(index_2.findKeyUp, this.findKeyUp, this);
            this.parent.on(index_3.findToolDlg, this.findToolDlg, this);
            this.parent.on(index_1.activeSheetChanged, this.refreshFindDlg, this);
        };
        FindAndReplace.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_1.findDlg, this.renderFindDlg);
                this.parent.off(index_1.gotoDlg, this.renderGotoDlg);
                this.parent.off(index_2.goto, this.gotoHandler);
                this.parent.off(index_1.findHandler, this.findHandler);
                this.parent.off(index_2.showFindAlert, this.showFindAlert);
                this.parent.off(index_2.replaceAllDialog, this.replaceAllDialog);
                this.parent.off(index_2.findKeyUp, this.findKeyUp);
                this.parent.off(index_3.findToolDlg, this.findToolDlg);
                this.parent.off(index_1.activeSheetChanged, this.refreshFindDlg);
                Iif (this.timeoutHandler) {
                    clearTimeout(this.timeoutHandler);
                    this.timeoutHandler = null;
                }
            }
        };
        FindAndReplace.prototype.destroyGoToDlgContent = function () {
            var dlgCont = this.parent.element.querySelector('.e-dialog.e-goto-dlg .e-dlg-content');
            if (this.gotoTextBox) {
                Eif (this.gotoTextBox.element && this.textFocusHandler) {
                    this.gotoTextBox.element.removeEventListener('focus', this.textFocusHandler);
                    this.textFocusHandler = null;
                }
                this.gotoTextBox.destroy();
                this.gotoTextBox = null;
            }
            if (dlgCont) {
                dlgCont.innerHTML = '';
            }
        };
        FindAndReplace.prototype.findToolDlg = function (args) {
            var _this = this;
            var updateDisableState = function (disable) {
                var ribbon = _this.parent.showRibbon && _this.parent.element.querySelector('.e-ribbon');
                if (ribbon) {
                    var findBtn = (args.event && args.event.target ?
                        ej2_base_1.closest(args.event.target, "#" + _this.parent.element.id + "_findbtn") :
                        ej2_base_1.select("#" + _this.parent.element.id + "_findbtn", ribbon));
                    if (findBtn) {
                        if (disable) {
                            findBtn.classList.add('e-disabled');
                        }
                        else {
                            findBtn.classList.remove('e-disabled');
                        }
                        findBtn.disabled = disable;
                    }
                }
            };
            var dialogDiv = args.dialogEle || this.parent.element.getElementsByClassName('e-findtool-dlg')[0];
            if (args.isPublic) {
                this.findValue = args.findValue;
            }
            var sheet = this.parent.getActiveSheet();
            var toolbarObj;
            var findTextInput;
            var findSpan;
            var findHandlerFn = function (e) {
                if (!findTextInput || (sheet.isProtected && !sheet.protectSettings.selectCells &&
                    !sheet.protectSettings.selectUnLockedCells)) {
                    return;
                }
                var inputValue = findTextInput.value;
                if (e && e.keyCode === 13) {
                    Eif (findTextInput.value && findSpan.textContent !== '0 of 0') {
                        _this.parent.notify(index_1.findHandler, { findOption: e.shiftKey ? 'prev' : 'next' });
                        _this.updateCount(findSpan, e.shiftKey);
                    }
                }
                else {
                    var enable = void 0;
                    if (inputValue === '') {
                        findSpan.textContent = '0 of 0';
                        enable = false;
                    }
                    else {
                        var countArgs = { value: inputValue, mode: 'Sheet',
                            isCSen: false, sheetIndex: _this.parent.activeSheetIndex, isEMatch: false, searchBy: 'By Row' };
                        _this.parent.notify(index_3.count, countArgs);
                        findSpan.textContent = countArgs.findCount;
                        enable = countArgs.findCount !== '0 of 0';
                    }
                    toolbarObj.enableItems(1, enable);
                    toolbarObj.enableItems(2, enable);
                }
            };
            if (dialogDiv) {
                if (args.isPublic || args.refreshPosition) {
                    if (args.isPublic) {
                        findTextInput = dialogDiv.querySelector('.e-text-findNext-short');
                        findTextInput.value = this.findValue;
                        findSpan = dialogDiv.querySelector('.e-input-group-icon');
                        toolbarObj = ej2_base_1.getComponent(dialogDiv.querySelector('.e-find-toolbar'), 'toolbar');
                        findHandlerFn();
                    }
                    if (args.refreshPosition) {
                        var headerHgt = void 0;
                        if (this.parent.getActiveSheet().showHeaders) {
                            var hdrPanel = this.parent.getColumnHeaderContent().parentElement;
                            headerHgt = ((hdrPanel && hdrPanel.offsetHeight) || 30) + 1;
                        }
                        else {
                            headerHgt = 1;
                        }
                        dialogDiv.style.top = headerHgt + "px";
                    }
                }
                else {
                    updateDisableState(true);
                    this.findDialog.hide();
                }
            }
            else {
                var findTextElement = this.parent.createElement('div', { className: 'e-input-group' });
                findTextInput = this.parent.createElement('input', { className: 'e-input e-text-findNext-short', attrs: { 'type': 'Text' } });
                if (this.findValue) {
                    findTextInput.value = this.findValue;
                }
                var l10n = this.parent.serviceLocator.getService(index_1.locale);
                findTextInput.setAttribute('placeholder', l10n.getConstant('FindValue'));
                findSpan = this.parent.createElement('span', { className: 'e-input-group-icon' });
                var largeData_1 = (sheet.usedRange.rowIndex * sheet.usedRange.colIndex) > 100;
                findTextInput.onkeyup = function (e) {
                    if (largeData_1) {
                        Iif (_this.timeoutHandler) {
                            clearTimeout(_this.timeoutHandler);
                        }
                        _this.timeoutHandler = setTimeout(findHandlerFn.bind(_this, e), 500);
                    }
                    else {
                        findHandlerFn(e);
                    }
                };
                findTextElement.appendChild(findTextInput);
                findTextElement.appendChild(findSpan);
                var toolItemModel = [
                    { type: 'Input', template: findTextElement },
                    {
                        prefixIcon: 'e-icons e-prev-icon', tooltipText: l10n.getConstant('FindPreviousBtn'), type: 'Button', cssClass: 'e-findRib-prev',
                        disabled: true
                    },
                    { prefixIcon: 'e-icons e-next-icon', tooltipText: l10n.getConstant('FindNextBtn'), type: 'Button', cssClass: 'e-findRib-next', disabled: true },
                    { type: 'Separator' },
                    { prefixIcon: 'e-icons e-option-icon', tooltipText: l10n.getConstant('MoreOptions'), type: 'Button', cssClass: 'e-findRib-more' },
                    { prefixIcon: 'e-icons e-close', tooltipText: l10n.getConstant('Close'), type: 'Button', cssClass: 'e-findRib-close' }
                ];
                toolbarObj = new ej2_navigations_1.Toolbar({
                    clicked: function (args) {
                        if (args.item.cssClass === 'e-findRib-next') {
                            _this.parent.notify(index_1.findHandler, { findOption: 'next' });
                            _this.updateCount(findSpan);
                        }
                        else if (args.item.cssClass === 'e-findRib-prev') {
                            _this.parent.notify(index_1.findHandler, { findOption: 'prev' });
                            _this.updateCount(findSpan, true);
                        }
                        else if (args.item.cssClass === 'e-findRib-more') {
                            _this.renderFindDlg();
                            _this.hideFindDlg();
                        }
                    }, width: 'auto', height: 'auto', items: toolItemModel,
                    cssClass: 'e-find-toolObj', enableRtl: this.parent.enableRtl,
                    created: function () {
                        var tbarBtns = toolbarObj.element.querySelectorAll('.e-toolbar-item .e-tbar-btn');
                        tbarBtns.forEach(function (tbarBtn) { return tbarBtn.removeAttribute('tabindex'); });
                    }
                });
                var tbarEle = this.parent.createElement('div', { className: 'e-find-toolbar', attrs: { 'tabindex': '-1' } });
                toolbarObj.createElement = this.parent.createElement;
                toolbarObj.appendTo(tbarEle);
                dialogDiv = this.parent.createElement('div', { className: 'e-dlg-div', attrs: { 'aria-label': l10n.getConstant('FindValue') } });
                var sheetPanel_1 = this.parent.element.getElementsByClassName('e-sheet-panel')[0];
                var findDlgModel = {
                    cssClass: 'e-findtool-dlg', visible: false, enableRtl: this.parent.enableRtl, target: sheetPanel_1,
                    open: function () {
                        ej2_base_1.EventHandler.add(document, 'click', _this.closeDialog, _this);
                        if (_this.findValue && (!sheet.isProtected || sheet.protectSettings.selectCells ||
                            sheet.protectSettings.selectUnLockedCells)) {
                            var countArgs = { value: _this.findValue, mode: 'Sheet',
                                isCSen: false, sheetIndex: _this.parent.activeSheetIndex, isEMatch: false, searchBy: 'By Row' };
                            _this.parent.notify(index_3.count, countArgs);
                            findSpan.textContent = countArgs.findCount;
                            var enable = countArgs.findCount !== '0 of 0';
                            toolbarObj.enableItems(1, enable);
                            toolbarObj.enableItems(2, enable);
                        }
                        else {
                            findSpan.textContent = '0 of 0';
                        }
                        updateDisableState(false);
                        var inputContainer = toolbarObj.element.querySelector('.e-input-group');
                        Eif (inputContainer) {
                            _this.findToolInputFocusHandler = function () {
                                var textInput = toolbarObj.element.querySelector('.e-text-findNext-short');
                                index_1.focus(textInput);
                                textInput.classList.add('e-input-focus');
                                (textInput).setSelectionRange(0, textInput.value.length);
                            };
                            inputContainer.addEventListener('focus', _this.findToolInputFocusHandler);
                        }
                        if (animationSettings_1) {
                            _this.findDialog.setProperties({ animationSettings: animationSettings_1 }, true);
                        }
                    },
                    beforeOpen: function () { return index_1.focus(_this.parent.element); },
                    beforeClose: function () {
                        if (_this.timeoutHandler) {
                            clearTimeout(_this.timeoutHandler);
                            _this.timeoutHandler = null;
                        }
                        _this.findValue = findTextInput.value || null;
                        var inputContainer = toolbarObj.element.querySelector('.e-input-group');
                        Eif (inputContainer && _this.findToolInputFocusHandler) {
                            inputContainer.removeEventListener('focus', _this.findToolInputFocusHandler);
                            _this.findToolInputFocusHandler = null;
                        }
                        toolbarObj.destroy();
                        ej2_base_1.EventHandler.remove(document, 'click', _this.closeDialog);
                    },
                    close: function () {
                        _this.findDialog.destroy();
                        _this.findDialog = null;
                        ej2_base_1.detach(dialogDiv);
                        sheetPanel_1.style.position = '';
                        updateDisableState(false);
                    },
                    created: function () {
                        sheetPanel_1.style.position = 'relative';
                        dialogDiv.style.width = _this.parent.getMainContent().offsetWidth + 'px';
                        dialogDiv.style.visibility = 'hidden';
                        dialogDiv.style.display = 'block';
                        _this.findDialog.width = (parseInt(getComputedStyle(dialogDiv).borderLeftWidth, 10) * 2) +
                            dialogDiv.querySelector('.e-toolbar-items').getBoundingClientRect().width + 'px';
                        dialogDiv.style.display = '';
                        dialogDiv.style.width = '';
                        dialogDiv.style.visibility = '';
                        dialogDiv.style.top =
                            ((args && args.headerHgt) || (_this.parent.getColumnHeaderContent().parentElement.offsetHeight + 1)) + "px";
                        dialogDiv.style.left = '';
                        dialogDiv.style[_this.parent.enableRtl ? 'left' : 'right'] = _this.parent.sheetModule.getScrollSize() + "px";
                        _this.findDialog.show();
                    }
                };
                Iif (ej2_base_1.Browser.isDevice) {
                    findDlgModel.header = tbarEle;
                    findDlgModel.allowDragging = true;
                }
                else {
                    findDlgModel.content = tbarEle;
                }
                this.findDialog = new ej2_popups_1.Dialog(findDlgModel);
                this.findDialog.createElement = this.parent.createElement;
                var animationSettings_1;
                if (args && args.isPublic) {
                    animationSettings_1 = { effect: this.findDialog.animationSettings.effect };
                    this.findDialog.setProperties({ animationSettings: { effect: 'None' } }, true);
                }
                this.findDialog.appendTo(dialogDiv);
            }
        };
        FindAndReplace.prototype.hideFindDlg = function () {
            this.findDialog.animationSettings.effect = 'None';
            this.findDialog.setProperties({ animationSettings: this.findDialog.animationSettings }, true);
            this.findDialog.hide();
        };
        FindAndReplace.prototype.refreshFindDlg = function () {
            var findDialog = this.findDialog && this.parent.element.getElementsByClassName('e-findtool-dlg')[0];
            if (findDialog) {
                var findToolInput = findDialog.querySelector('.e-text-findNext-short');
                this.findToolDlg({ findValue: findToolInput.value, isPublic: true, refreshPosition: true, dialogEle: findDialog });
            }
        };
        FindAndReplace.prototype.updateCount = function (countEle, isPrev) {
            var values = countEle.textContent.split(' ');
            var newStart;
            if (isPrev) {
                newStart = Number(values[0]) - 1;
                if (newStart < 1) {
                    newStart = Number(values[2]);
                }
            }
            else {
                newStart = Number(values[0]) + 1;
                if (newStart > Number(values[2])) {
                    newStart = 1;
                }
            }
            values[0] = newStart.toString();
            countEle.textContent = values.join(' ');
        };
        FindAndReplace.prototype.closeDialog = function (e) {
            if ((ej2_base_1.closest(e.target, '.e-findRib-close') || !ej2_base_1.closest(e.target, '.e-spreadsheet')) && this.findDialog) {
                this.findToolDlg({});
            }
        };
        FindAndReplace.prototype.renderFindDlg = function (args) {
            var _this = this;
            var dialogInst = this.parent.serviceLocator.getService(index_1.dialog);
            Eif (!this.parent.element.querySelector('.e-find-dlg')) {
                var l10n = this.parent.serviceLocator.getService(index_1.locale);
                var dlg = {
                    isModal: false, showCloseIcon: true, cssClass: 'e-find-dlg', enableRtl: this.parent.enableRtl,
                    header: l10n.getConstant('FindAndReplace'),
                    beforeOpen: function (args) {
                        var dlgArgs = { dialogName: 'FindAndReplaceDialog', element: args.element, target: args.target, cancel: args.cancel };
                        _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                        if (dlgArgs.cancel) {
                            args.cancel = true;
                        }
                        else {
                            dialogInst.dialogInstance.content = _this.findandreplaceContent();
                            dialogInst.dialogInstance.dataBind();
                            index_1.focus(_this.parent.element);
                        }
                    },
                    buttons: [{
                            buttonModel: { content: l10n.getConstant('FindPreviousBtn'), isPrimary: true, cssClass: 'e-btn-findPrevious',
                                disabled: true },
                            click: function (e) {
                                _this.dialogMessage();
                                _this.findHandler({ findOption: e && e.keyCode === 13 ? 'next' : 'prev' });
                            }
                        },
                        {
                            buttonModel: { content: l10n.getConstant('FindNextBtn'), isPrimary: true, cssClass: 'e-btn-findNext', disabled: true },
                            click: function () {
                                _this.dialogMessage();
                                _this.findHandler({ findOption: 'next' });
                            }
                        },
                        {
                            buttonModel: { content: l10n.getConstant('ReplaceBtn'), isPrimary: true, cssClass: 'e-btn-replace', disabled: true },
                            click: function () {
                                _this.dialogMessage();
                                _this.replaceHandler(index_2.replace);
                            }
                        },
                        {
                            buttonModel: { content: l10n.getConstant('ReplaceAllBtn'), isPrimary: true, cssClass: 'e-btn-replaceAll', disabled: true },
                            click: function () {
                                _this.dialogMessage();
                                _this.replaceHandler(index_2.replaceAll);
                            }
                        }],
                    open: function () {
                        var findInput = _this.parent.element.querySelector('.e-text-findNext');
                        if (findInput.value) {
                            var prevButton = _this.parent.element.querySelector('.e-btn-findPrevious');
                            var prevButtonObj = ej2_base_1.getComponent(prevButton, 'btn');
                            prevButtonObj.disabled = false;
                            ej2_base_1.getComponent(_this.parent.element.querySelector('.e-btn-findNext'), 'btn').disabled = false;
                        }
                        index_1.getUpdateUsingRaf(function () {
                            index_1.focus(findInput);
                        });
                        if (args && args.showDialog) {
                            Eif (args.value) {
                                findInput.value = args.value;
                                ej2_base_1.getComponent(_this.parent.element.querySelector('.e-btn-findPrevious'), 'btn').disabled = false;
                                ej2_base_1.getComponent(_this.parent.element.querySelector('.e-btn-findNext'), 'btn').disabled = false;
                            }
                            Eif (args.replaceValue) {
                                var replaceInput = _this.parent.element.querySelector('.e-text-replaceInp');
                                replaceInput.value = args.replaceValue;
                                ej2_base_1.getComponent(_this.parent.element.querySelector('.e-btn-replace'), 'btn').disabled = false;
                                ej2_base_1.getComponent(_this.parent.element.querySelector('.e-btn-replaceAll'), 'btn').disabled = false;
                            }
                        }
                    },
                    beforeClose: this.dialogBeforeClose.bind(this)
                };
                dialogInst.show(dlg);
            }
            else {
                dialogInst.hide();
            }
        };
        FindAndReplace.prototype.dialogBeforeClose = function () {
            var checkBox = this.checkBoxElements;
            if (checkBox && checkBox.element) {
                checkBox.destroy();
                checkBox.element.remove();
            }
            this.checkBoxElements = null;
            this.textBoxElements.forEach(function (textBox) {
                Eif (textBox && textBox.element) {
                    textBox.destroy();
                    textBox.element.remove();
                }
            });
            this.textBoxElements = [];
            this.dropDownListElements.forEach(function (dropDownList) {
                Eif (dropDownList && dropDownList.element) {
                    dropDownList.destroy();
                    dropDownList.element.remove();
                }
            });
            this.dropDownListElements = [];
            index_1.removeElements(this.paraElements);
            this.paraElements = [];
            index_1.removeElements(this.inputElements);
            this.inputElements = [];
            index_1.removeElements(this.divElements);
            this.divElements = [];
        };
        FindAndReplace.prototype.dialogMessage = function () {
            if (this.parent.element.querySelector('.e-replace-alert-span')) {
                this.parent.element.querySelector('.e-replace-alert-span').remove();
            }
            else if (this.parent.element.querySelector('.e-find-alert-span')) {
                this.parent.element.querySelector('.e-find-alert-span').remove();
            }
        };
        FindAndReplace.prototype.renderGotoDlg = function () {
            var _this = this;
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            var dialogInst = this.parent.serviceLocator.getService(index_1.dialog);
            var cancelBtn = false;
            if (ej2_base_1.isNullOrUndefined(this.parent.element.querySelector('.e-goto-dlg'))) {
                var dlg = {
                    width: 300, isModal: false, showCloseIcon: true, cssClass: 'e-goto-dlg',
                    header: l10n.getConstant('GotoHeader'), enableRtl: this.parent.enableRtl,
                    beforeOpen: function (args) {
                        var dlgArgs = {
                            dialogName: 'GoToDialog',
                            element: args.element, target: args.target, cancel: args.cancel
                        };
                        _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                        if (dlgArgs.cancel) {
                            args.cancel = true;
                        }
                        else {
                            dialogInst.dialogInstance.content = _this.GotoContent();
                            dialogInst.dialogInstance.dataBind();
                            index_1.focus(_this.parent.element);
                        }
                    },
                    buttons: [{
                            buttonModel: {
                                content: l10n.getConstant('Ok'), isPrimary: true, cssClass: 'e-btn-goto-ok'
                            },
                            click: function () {
                                if (_this.gotoHandler()) {
                                    dialogInst.hide();
                                }
                            }
                        }],
                    open: function () {
                        _this.textFocus();
                    },
                    beforeClose: function () { return _this.destroyGoToDlgContent(); }
                };
                dialogInst.show(dlg, cancelBtn);
            }
            else {
                dialogInst.hide();
            }
        };
        FindAndReplace.prototype.textFocus = function () {
            var _this = this;
            var element = this.parent.element.querySelector('.e-text-goto');
            this.textFocusHandler = function () {
                if (_this.parent.element.querySelector('.e-goto-alert-span')) {
                    _this.parent.element.querySelector('.e-goto-alert-span').remove();
                }
            };
            element.addEventListener('focus', this.textFocusHandler);
        };
        FindAndReplace.prototype.findHandler = function (findOpt) {
            var findInput = this.parent.element.querySelector('.e-text-findNext');
            if (!findInput) {
                findInput = this.parent.element.querySelector('.e-text-findNext-short');
                Iif (!findInput) {
                    this.gotoAlert();
                }
            }
            var value = findInput.value;
            Eif (findInput.value !== '') {
                var sheet = this.parent.getActiveSheet();
                Iif (sheet.isProtected && !sheet.protectSettings.selectCells && !sheet.protectSettings.selectUnLockedCells) {
                    return;
                }
                var sheetIndex = this.parent.activeSheetIndex;
                var checkCase = this.parent.element.querySelector('.e-findnreplace-checkcase');
                var isCSen = void 0;
                if (!checkCase) {
                    isCSen = false;
                }
                else {
                    var caseCheckbox = ej2_base_1.getComponent(checkCase, 'checkbox');
                    isCSen = caseCheckbox.checked;
                }
                var checkmatch = this.parent.element.querySelector('.e-findnreplace-checkmatch');
                var isEMatch = void 0;
                if (!checkmatch) {
                    isEMatch = false;
                }
                else {
                    var entireMatchCheckbox = ej2_base_1.getComponent(checkmatch, 'checkbox');
                    isEMatch = entireMatchCheckbox.checked;
                }
                var searchitem = this.parent.element.querySelector('.e-findnreplace-searchby');
                var searchBy = void 0;
                if (!searchitem) {
                    searchBy = 'By Row';
                }
                else {
                    var searchDDL = ej2_base_1.getComponent(searchitem, 'dropdownlist');
                    searchBy = searchDDL.value.toString();
                }
                var modeitem = this.parent.element.querySelector('.e-findnreplace-searchwithin');
                var mode = void 0;
                if (!modeitem) {
                    mode = 'Sheet';
                }
                else {
                    var modeDDL = ej2_base_1.getComponent(modeitem, 'dropdownlist');
                    mode = modeDDL.value.toString();
                }
                var args = {
                    value: value, sheetIndex: sheetIndex, findOpt: findOpt.findOption, mode: mode, isCSen: isCSen,
                    isEMatch: isEMatch, searchBy: searchBy, isAction: true
                };
                this.parent.find(args);
            }
        };
        FindAndReplace.prototype.replaceHandler = function (action) {
            var dlg = this.parent.element.querySelector('.e-find-dlg');
            var findValue = dlg.querySelector('.e-text-findNext').value;
            var replaceValue = this.parent.element.querySelector('.e-text-replaceInp').value;
            var checkCase = this.parent.element.querySelector('.e-findnreplace-checkcase').checked;
            var checkmatch = this.parent.element.querySelector('.e-findnreplace-checkmatch').checked;
            var searchInValue = this.parent.element.querySelector('.e-search-within .e-ddl-hidden').value;
            var searchByValue = this.parent.element.querySelector('.e-searchby .e-ddl-hidden').value;
            this.parent.notify(action, { value: findValue, mode: searchInValue, isCSen: checkCase, isEMatch: checkmatch, searchBy: searchByValue,
                findOpt: 'next', replaceValue: replaceValue, replaceBy: action, sheetIndex: this.parent.activeSheetIndex, isAction: true });
        };
        FindAndReplace.prototype.gotoHandler = function (address) {
            var isNotAlertShown = true;
            if (address) {
                this.parent.goTo(address.address);
            }
            else {
                var gotoAddress = this.parent.element.querySelector('.e-text-goto').value;
                for (var nameIdx = 0; nameIdx < this.parent.definedNames.length; nameIdx++) {
                    if (this.parent.definedNames[nameIdx].name === gotoAddress) {
                        gotoAddress = this.parent.definedNames[nameIdx].refersTo.slice(1);
                        break;
                    }
                }
                var addr = gotoAddress;
                if (gotoAddress.includes('!')) {
                    addr = gotoAddress.substring(gotoAddress.lastIndexOf('!') + 1);
                }
                addr = addr.split('$').join('');
                if (addr.includes(':')) {
                    addr = addr.split(':')[0];
                }
                var rowMatch = addr.match(/\d+/);
                var colMatch = addr.match(/[A-Z]+/i);
                if (!rowMatch || !colMatch || colMatch.index !== 0) {
                    this.gotoAlert();
                    isNotAlertShown = false;
                }
                else {
                    var indexes = index_3.getSwapRange(index_3.getRangeIndexes(addr));
                    var sheet = this.parent.getActiveSheet();
                    if (indexes[2] >= 1048576 || indexes[3] >= 16384) {
                        this.gotoAlert();
                        isNotAlertShown = false;
                    }
                    else if (this.parent.scrollSettings.isFinite && (sheet.rowCount < indexes[2] + 1 || sheet.colCount < indexes[3] + 1)) {
                        this.parent.notify(index_1.finiteAlert, null);
                        isNotAlertShown = false;
                    }
                    else {
                        this.parent.goTo(gotoAddress);
                    }
                }
            }
            return isNotAlertShown;
        };
        FindAndReplace.prototype.gotoAlert = function () {
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            var gotoSpan = this.parent.createElement('span', { className: 'e-goto-alert-span' });
            gotoSpan.innerText = l10n.getConstant('InsertingEmptyValue');
            if (this.parent.element.querySelector('.e-goto-alert-span')) {
                this.parent.element.querySelector('.e-goto-alert-span').remove();
            }
            (this.parent.element.querySelector('.e-goto-dlg').querySelector('.e-dlg-content')).appendChild(gotoSpan);
        };
        FindAndReplace.prototype.showFindAlert = function () {
            Iif (this.parent.element.querySelector('.e-replace-alert-span')) {
                this.parent.element.querySelector('.e-replace-alert-span').remove();
            }
            var replaceDlgCont = this.parent.element.querySelector('.e-find-dlg .e-dlg-content');
            if (replaceDlgCont) {
                var findSpan = this.parent.createElement('span', { className: 'e-find-alert-span' });
                findSpan.innerText = this.parent.serviceLocator.getService(index_1.locale).getConstant('NoElements');
                replaceDlgCont.appendChild(findSpan);
            }
        };
        FindAndReplace.prototype.replaceAllDialog = function (options) {
            Iif (this.parent.element.querySelector('.e-find-alert-span')) {
                this.parent.element.querySelector('.e-find-alert-span').remove();
            }
            var replaceAlert = this.parent.element.querySelector('.e-replace-alert-span');
            if (replaceAlert) {
                replaceAlert.remove();
            }
            var l10n = (this.parent.serviceLocator.getService(index_1.locale));
            var replaceSpan = this.parent.createElement('span', { className: 'e-replace-alert-span' });
            replaceSpan.innerText = options.count + l10n.getConstant('ReplaceAllEnd') + options.replaceValue;
            if (this.parent.element.querySelector('.e-find-dlg')) {
                (this.parent.element.querySelector('.e-find-dlg').querySelector('.e-dlg-content')).appendChild(replaceSpan);
            }
        };
        FindAndReplace.prototype.findKeyUp = function (e) {
            if (e.target.classList.contains('e-text-findNext')) {
                var findValue = this.parent.element.querySelector('.e-text-findNext').value;
                if (!ej2_base_1.isNullOrUndefined(findValue) && findValue !== '') {
                    var prevButton = this.parent.element.querySelector('.e-btn-findPrevious');
                    var prevButtonObj = ej2_base_1.getComponent(prevButton, 'btn');
                    prevButtonObj.disabled = false;
                    ej2_base_1.getComponent(this.parent.element.querySelector('.e-btn-findNext'), 'btn').disabled = false;
                }
                else {
                    ej2_base_1.getComponent(this.parent.element.querySelector('.e-btn-findPrevious'), 'btn').disabled = true;
                    ej2_base_1.getComponent(this.parent.element.querySelector('.e-btn-findNext'), 'btn').disabled = true;
                    this.dialogMessage();
                }
            }
            var findInput = this.parent.element.querySelector('.e-text-findNext');
            Eif (findInput) {
                var findValue = this.parent.element.querySelector('.e-text-findNext').value;
                var replaceValue = this.parent.element.querySelector('.e-text-replaceInp').value;
                if (!ej2_base_1.isNullOrUndefined(findValue) && !ej2_base_1.isNullOrUndefined(replaceValue) && (findValue !== '') && (replaceValue !== '')) {
                    Eif (!this.parent.getActiveSheet().isProtected) {
                        ej2_base_1.getComponent(this.parent.element.querySelector('.e-btn-replace'), 'btn').disabled = false;
                        ej2_base_1.getComponent(this.parent.element.querySelector('.e-btn-replaceAll'), 'btn').disabled = false;
                    }
                }
                else {
                    ej2_base_1.getComponent(this.parent.element.querySelector('.e-btn-replace'), 'btn').disabled = true;
                    ej2_base_1.getComponent(this.parent.element.querySelector('.e-btn-replaceAll'), 'btn').disabled = true;
                }
            }
        };
        FindAndReplace.prototype.findandreplaceContent = function () {
            if (this.parent.element.querySelector('.e-text-findNext-short')) {
                this.shortValue = this.parent.element.querySelector('.e-text-findNext-short').value;
            }
            var dialogElem = this.parent.createElement('div', { className: 'e-link-dialog' });
            var findElem = this.parent.createElement('div', { className: 'e-find' });
            var findCheck = this.parent.createElement('div', { className: 'e-findCheck' });
            this.divElements.push(dialogElem);
            this.divElements.push(findElem);
            this.divElements.push(findCheck);
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            dialogElem.appendChild(findElem);
            var findTextE = this.parent.createElement('div', { className: 'e-cont' });
            var findTextH = this.parent.createElement('p', { className: 'e-header' });
            findTextH.innerText = l10n.getConstant('FindWhat');
            var findTextIp = this.parent.createElement('input', {
                className: 'e-input e-text-findNext', attrs: {
                    'type': 'Text', 'placeholder': l10n.getConstant('FindValue'),
                    'value': this.shortValue
                }
            });
            this.divElements.push(findTextE);
            this.paraElements.push(findTextH);
            this.inputElements.push(findTextIp);
            findTextE.appendChild(findTextIp);
            findTextE.insertBefore(findTextH, findTextIp);
            findElem.appendChild(findTextE);
            var findTextBox = new ej2_inputs_1.TextBox({ width: '100%', enableRtl: this.parent.enableRtl });
            this.textBoxElements.push(findTextBox);
            findTextBox.createElement = this.parent.createElement;
            findTextBox.appendTo(findTextIp);
            var replaceTextE = this.parent.createElement('div', { className: 'e-cont' });
            var replaceTextH = this.parent.createElement('p', { className: 'e-header' });
            replaceTextH.innerText = l10n.getConstant('ReplaceWith');
            var replaceTextIp = this.parent.createElement('input', {
                className: 'e-input e-text-replaceInp', attrs: { 'type': 'Text', 'placeholder': l10n.getConstant('ReplaceValue') }
            });
            this.divElements.push(replaceTextE);
            this.paraElements.push(replaceTextH);
            this.inputElements.push(replaceTextIp);
            replaceTextE.appendChild(replaceTextIp);
            replaceTextE.insertBefore(replaceTextH, replaceTextIp);
            findElem.appendChild(replaceTextE);
            var replaceTextBox = new ej2_inputs_1.TextBox({ width: '100%', enableRtl: this.parent.enableRtl });
            this.textBoxElements.push(replaceTextBox);
            replaceTextBox.createElement = this.parent.createElement;
            replaceTextBox.appendTo(replaceTextIp);
            var searchContainer = this.parent.createElement('div', { className: 'e-search-options' });
            var withinData = [
                { Id: 'Sheet', Within: l10n.getConstant('Sheet') },
                { Id: 'Workbook', Within: l10n.getConstant('Workbook') }
            ];
            var withInDDL = new ej2_dropdowns_1.DropDownList({
                dataSource: withinData,
                cssClass: 'e-search-within',
                enableRtl: this.parent.enableRtl,
                fields: { value: 'Id', text: 'Within' }, width: '100%', index: 0
            });
            this.dropDownListElements.push(withInDDL);
            var label = l10n.getConstant('SearchWithin');
            var withIn = this.parent.createElement('input', {
                className: 'e-findnreplace-searchwithin', attrs: { type: 'select', label: label }
            });
            var withinTextH = this.parent.createElement('p', { className: 'e-header' });
            withinTextH.innerText = label;
            this.inputElements.push(withIn);
            this.paraElements.push(withinTextH);
            var withinContainer = this.parent.createElement('div', { className: 'e-search-options-container' });
            withinContainer.appendChild(withinTextH);
            withinContainer.appendChild(withIn);
            withInDDL.createElement = this.parent.createElement;
            withInDDL.appendTo(withIn);
            var searchData = [
                { Id: 'By Row', Search: l10n.getConstant('ByRow') },
                { Id: 'By Column', Search: l10n.getConstant('ByColumn') }
            ];
            var searchDDL = new ej2_dropdowns_1.DropDownList({
                dataSource: searchData,
                cssClass: 'e-searchby',
                enableRtl: this.parent.enableRtl,
                fields: { value: 'Id', text: 'Search' }, width: '100%', index: 0
            });
            this.dropDownListElements.push(searchDDL);
            label = l10n.getConstant('SearchBy');
            var searchIn = this.parent.createElement('input', {
                className: 'e-findnreplace-searchby', attrs: { type: 'select', label: label }
            });
            var searchTextH = this.parent.createElement('p', { className: 'e-header' });
            searchTextH.innerText = label;
            this.inputElements.push(searchIn);
            this.paraElements.push(searchTextH);
            var searchByContainer = this.parent.createElement('div', { className: 'e-search-options-container' });
            searchByContainer.appendChild(searchTextH);
            searchByContainer.appendChild(searchIn);
            searchContainer.appendChild(withinContainer);
            searchContainer.appendChild(searchByContainer);
            findElem.appendChild(searchContainer);
            searchDDL.createElement = this.parent.createElement;
            searchDDL.appendTo(searchIn);
            var isCSen = new ej2_buttons_1.CheckBox({
                label: l10n.getConstant('MatchCase'), checked: false,
                cssClass: 'e-findnreplace-casecheckbox',
                enableRtl: this.parent.enableRtl
            });
            var caaseCheckbox = this.parent.createElement('input', {
                className: 'e-findnreplace-checkcase', attrs: { type: 'checkbox' }
            });
            this.inputElements.push(caaseCheckbox);
            findCheck.appendChild(caaseCheckbox);
            isCSen.createElement = this.parent.createElement;
            isCSen.appendTo(caaseCheckbox);
            var isEMatch = new ej2_buttons_1.CheckBox({
                label: l10n.getConstant('MatchExactCellElements'), checked: false,
                cssClass: 'e-findnreplace-exactmatchcheckbox',
                enableRtl: this.parent.enableRtl
            });
            this.checkBoxElements = isEMatch;
            var entirematchCheckbox = this.parent.createElement('input', {
                className: 'e-findnreplace-checkmatch', attrs: { type: 'checkbox' }
            });
            this.inputElements.push(entirematchCheckbox);
            findCheck.appendChild(entirematchCheckbox);
            isEMatch.createElement = this.parent.createElement;
            isEMatch.appendTo(entirematchCheckbox);
            findElem.appendChild(findCheck);
            return dialogElem;
        };
        FindAndReplace.prototype.GotoContent = function () {
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            var dialogElem = this.parent.createElement('div', { className: 'e-link-dialog' });
            var gotoElem = this.parent.createElement('div', { className: 'e-goto' });
            dialogElem.appendChild(gotoElem);
            var gotoTextE = this.parent.createElement('div', { className: 'e-cont' });
            var gotoTextH = this.parent.createElement('p', { className: 'e-header' });
            gotoTextH.innerText = l10n.getConstant('Reference');
            this.gotoTextBox = new ej2_inputs_1.TextBox({
                placeholder: l10n.getConstant('EnterCellAddress'),
                enableRtl: this.parent.enableRtl
            });
            var gotoTextIp = this.parent.createElement('input', { className: 'e-text-goto', attrs: { 'type': 'Text' } });
            gotoTextE.appendChild(gotoTextIp);
            gotoTextE.insertBefore(gotoTextH, gotoTextIp);
            gotoElem.appendChild(gotoTextE);
            this.gotoTextBox.createElement = this.parent.createElement;
            this.gotoTextBox.appendTo(gotoTextIp);
            return dialogElem;
        };
        FindAndReplace.prototype.destroy = function () {
            this.removeEventListener();
            this.destroyGoToDlgContent();
            if (this.findDialog) {
                this.hideFindDlg();
            }
            if (this.divElements && this.divElements.length) {
                this.divElements.length = 0;
            }
            if (this.paraElements && this.paraElements.length) {
                this.paraElements.length = 0;
            }
            if (this.inputElements && this.inputElements.length) {
                this.inputElements.length = 0;
            }
            if (this.textBoxElements && this.textBoxElements.length) {
                this.textBoxElements.length = 0;
            }
            if (this.dropDownListElements && this.dropDownListElements.length) {
                this.dropDownListElements.length = 0;
            }
            this.checkBoxElements = null;
            this.timeoutHandler = null;
            this.findValue = null;
            this.parent = null;
        };
        FindAndReplace.prototype.getModuleName = function () {
            return 'findAndReplace';
        };
        return FindAndReplace;
    }());
    exports.FindAndReplace = FindAndReplace;
});