all files / treegrid/actions/ edit.js

90.21% Statements 608/674
85.74% Branches 517/603
89.36% Functions 42/47
90.19% Lines 607/673
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 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981   229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229×   11395×   229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229× 229×   73× 73×   73×         161× 56×     22× 15× 15×     221× 220×     220×       559× 559× 559× 559×   559×                               559×   559×                       96×                     72× 72×     72×   70× 70×     47× 47× 47×   47×   23×     137× 137× 137× 137×   90× 32× 32×     10×   10×   10×     67× 67× 67× 67×   129× 129× 129× 129× 129×   129× 128× 128× 56×   72×   128× 128×       129×           129× 59×   56×   59×       129×     92× 91× 91× 91×     22× 15× 15×   15× 15×         15× 15×   22×     68×         98× 98× 35× 35× 35× 35× 35× 35×       35×   35× 35× 35×                 35×   35× 12× 12×             23×       35× 35× 35× 35× 35× 35× 35× 35×     35×   35×               35× 35× 35×                                                         98×     35×   35× 35× 35× 31× 31×   35× 35× 35× 35× 35×   35× 35×     35×       35×   35×   98×                 35× 35×   126× 125× 125×   125× 18842× 18842× 18842× 95× 66× 66× 66× 66× 29× 29×   66×     18842× 18842× 18842×     18842× 4240×     125× 117× 10× 10×       147× 3925× 3925× 3923× 3923× 3923× 3257× 3257×       147× 147× 147× 36× 36× 108× 24× 24×       147× 3925× 3925×   3925× 3925× 3923× 3923× 3923× 3923× 3257×   3923× 3923× 3923× 9690× 9690× 9690× 9690× 9690× 9690× 3869×   9690×       3923× 3923×                 84× 84× 84× 84× 43× 43× 43×   16×     84× 84× 84× 84× 84× 84× 84× 15×   69×   57× 57×   18×       10× 10×       84×     67×   67× 67× 67× 67× 67× 67× 67× 38× 38×   67× 29× 29×     29×   67×     67× 67× 67×   67×         84×                     298×       298×       298×     295× 31× 31× 72×           31× 31× 31× 31× 36×   31×     295× 70×   68×   70×                         67×           65×     70× 70×         66× 36×     295× 18× 18×   295×   26×   295×       295× 295×       302× 302× 74× 74× 74× 74× 74×   72×     65×   74×   74× 74× 74× 74× 74× 74× 74× 74× 74× 74× 74× 74× 74×   74× 72× 72× 72× 38×   72×   74× 63× 63× 14× 14×   49× 21× 21× 21× 21×   28× 20× 20× 16× 16× 16× 16×   20× 20× 20× 16×   20× 20×             63× 35× 21× 21× 21× 21×   35× 35× 15× 15×       63× 44×   63×       74× 23×   74× 74× 74×   302× 74×       302× 31× 31× 67× 62× 62× 25× 25×         302×   31× 26×   31× 10× 10× 10× 10× 10× 10× 10×   10× 10× 10× 10× 10×     21× 17× 17× 17×           17× 17×   17×                   436×     129× 129× 20× 12×         10× 10×   10×              
define(["require", "exports", "@syncfusion/ej2-grids", "@syncfusion/ej2-grids", "../base/constant", "@syncfusion/ej2-base", "@syncfusion/ej2-data", "../utils", "./crud-actions", "./batch-edit"], function (require, exports, ej2_grids_1, ej2_grids_2, events, ej2_base_1, ej2_data_1, utils_1, crud_actions_1, batch_edit_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Edit = (function () {
        function Edit(parent) {
            this.addedRecords = 'addedRecords';
            this.deletedRecords = 'deletedRecords';
            this.prevAriaRowIndex = '-1';
            this.isAddedRowByMethod = false;
            this.isAddedRowByContextMenu = false;
            ej2_grids_1.Grid.Inject(ej2_grids_1.Edit);
            this.parent = parent;
            this.isSelfReference = !ej2_base_1.isNullOrUndefined(parent.parentIdMapping);
            this.previousNewRowPosition = null;
            this.internalProperties = {};
            this.batchEditModule = new batch_edit_1.BatchEdit(this.parent);
            this.addEventListener();
        }
        Edit.prototype.getModuleName = function () {
            return 'edit';
        };
        Edit.prototype.addEventListener = function () {
            this.parent.on(events.crudAction, this.crudAction, this);
            this.parent.on(events.beginEdit, this.beginEdit, this);
            this.parent.on(events.beginAdd, this.beginAdd, this);
            this.parent.on(events.recordDoubleClick, this.recordDoubleClick, this);
            this.parent.on(events.cellSave, this.cellSave, this);
            this.parent.on(events.batchCancel, this.batchCancel, this);
            this.parent.grid.on(events.keyPressed, this.keyPressed, this);
            this.parent.grid.on('batchedit-form', this.lastCellTab, this);
            this.parent.grid.on('content-ready', this.contentready, this);
            this.parent.on(events.cellEdit, this.cellEdit, this);
            this.parent.on('actionBegin', this.editActionEvents, this);
            this.parent.on('actionComplete', this.editActionEvents, this);
            this.parent.grid.on(events.doubleTap, this.recordDoubleClick, this);
            this.parent.grid.on('dblclick', this.gridDblClick, this);
            this.parent.grid.on('recordAdded', this.customCellSave, this);
            this.parent.on('savePreviousRowPosition', this.savePreviousRowPosition, this);
            this.parent.grid.on(events.beforeStartEdit, this.beforeStartEdit, this);
            this.parent.grid.on(events.beforeBatchCancel, this.beforeBatchCancel, this);
            this.parent.grid.on('reset-edit-props', this.resetIsOnBatch, this);
            this.parent.grid.on('get-row-position', this.getRowPosition, this);
        };
        Edit.prototype.gridDblClick = function (e) {
            this.doubleClickTarget = e.target;
            if (e.target.classList.contains('e-frame') && this.parent.getCurrentViewRecords().length === 0) {
                this.doubleClickTarget = null;
            }
            if (e.target.classList.contains('e-treegridcollapse') || e.target.classList.contains('e-treegridexpand')) {
                var tr = ej2_grids_2.parentsUntil(e.target, 'e-row');
                var rowIndex = tr && parseInt(tr.getAttribute('data-rowindex'), 10);
                if (!ej2_base_1.isNullOrUndefined(rowIndex) && rowIndex >= 0 && this.parent.allowPaging) {
                    this.parent.grid.getDataRows()[rowIndex].dataset.uid = this.parent.grid.contentModule.getRows()[rowIndex].uid;
                }
            }
        };
        Edit.prototype.getRowPosition = function (addArgs) {
            addArgs.newRowPosition = this.parent.editSettings.newRowPosition;
            addArgs.addRowIndex = this.addRowIndex;
            addArgs.dataRowIndex = +this.prevAriaRowIndex;
        };
        Edit.prototype.beforeStartEdit = function (args) {
            if (this.parent.editSettings.mode === 'Cell') {
                this.parent.trigger(events.actionBegin, args);
            }
        };
        Edit.prototype.beforeBatchCancel = function (args) {
            if (this.parent.editSettings.mode === 'Cell') {
                args['requestType'] = 'cancel';
                this.parent.trigger(events.actionComplete, args);
            }
        };
        Edit.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.crudAction, this.crudAction);
            this.parent.off(events.beginEdit, this.beginEdit);
            this.parent.off(events.beginAdd, this.beginAdd);
            this.parent.off(events.recordDoubleClick, this.recordDoubleClick);
            this.parent.off(events.batchCancel, this.batchCancel);
            this.parent.grid.off(events.keyPressed, this.keyPressed);
            this.parent.grid.off('batchedit-form', this.lastCellTab);
            this.parent.grid.off('content-ready', this.contentready);
            this.parent.off(events.cellEdit, this.cellEdit);
            this.parent.off('actionBegin', this.editActionEvents);
            this.parent.off('actionComplete', this.editActionEvents);
            this.parent.grid.off('recordAdded', this.customCellSave);
            this.parent.grid.off(events.doubleTap, this.recordDoubleClick);
            this.parent.off('savePreviousRowPosition', this.savePreviousRowPosition);
            this.parent.grid.off(events.beforeStartEdit, this.beforeStartEdit);
            this.parent.grid.off(events.beforeBatchCancel, this.beforeBatchCancel);
            this.parent.grid.off('dblclick', this.gridDblClick);
            this.parent.grid.off('reset-edit-props', this.resetIsOnBatch);
            this.parent.grid.off('get-row-position', this.getRowPosition);
        };
        Edit.prototype.destroy = function () {
            this.removeEventListener();
        };
        Edit.prototype.applyFormValidation = function (cols) {
            this.parent.grid.editModule.applyFormValidation(cols);
        };
        Edit.prototype.editActionEvents = function (args) {
            var eventArgs = ej2_grids_1.getObject('editAction', args);
            var eventName = ej2_grids_1.getObject('name', eventArgs);
            var treeObj = this.parent;
            var adaptor = !ej2_base_1.isNullOrUndefined(treeObj.dataSource)
                && treeObj.dataSource.adaptor;
            if (!ej2_base_1.isNullOrUndefined(adaptor) && (utils_1.isRemoteData(treeObj) || adaptor instanceof ej2_data_1.RemoteSaveAdaptor) &&
                (eventArgs.requestType === 'save' && eventArgs.action === 'add') &&
                (treeObj.editSettings.newRowPosition === 'Child' || treeObj.editSettings.newRowPosition === 'Below'
                    || treeObj.editSettings.newRowPosition === 'Above')) {
                Eif (eventName === 'actionBegin') {
                    var rowIndex = ej2_base_1.isNullOrUndefined(eventArgs.row) || !Object.keys(eventArgs.row).length ? this.selectedIndex :
                        eventArgs.row.rowIndex - 1;
                    var keyData = (!ej2_base_1.isNullOrUndefined(rowIndex) && rowIndex !== -1) ?
                        treeObj.getCurrentViewRecords()[parseInt(rowIndex.toString(), 10)][treeObj.getPrimaryKeyFieldNames()[0]] : -1;
                    treeObj.grid.query.addParams('relationalKey', keyData);
                }
                else if (eventName === 'actionComplete') {
                    var paramsLength = treeObj.grid.query.params.length;
                    for (var i = 0; i < paramsLength; i++) {
                        if (treeObj.grid.query.params[parseInt(i.toString(), 10)].key === 'relationalKey') {
                            treeObj.grid.query.params.splice(i);
                        }
                    }
                }
            }
            if (this.parent.enableInfiniteScrolling && eventName === 'actionComplete') {
                this.infiniteAddAction(eventArgs);
            }
            if (this.parent.editSettings.mode === 'Batch' && eventArgs.requestType === 'paging') {
                this.parent.notify('batchPageAction', {});
            }
        };
        Edit.prototype.infiniteAddAction = function (args) {
            Eif ((args.requestType === 'save' && args.action === 'add') || args.requestType === 'delete') {
                Eif (this.parent.editSettings.newRowPosition !== 'Top' && this.selectedIndex !== -1
                    && (args.requestType === 'save' && args.action === 'add')) {
                    var rowObjects = this.parent.grid.getRowsObject();
                    var newRowObject = rowObjects.splice(0, 1)[0];
                    var newRowObjectIndex = this.addRowIndex;
                    var currentData = this.parent.getCurrentViewRecords();
                    Eif (this.parent.editSettings.newRowPosition === 'Below' || this.parent.editSettings.newRowPosition === 'Child') {
                        newRowObjectIndex += utils_1.findChildrenRecords(currentData[newRowObjectIndex + 1]).length;
                    }
                    newRowObjectIndex = this.parent.editSettings.newRowPosition === 'Below' ? newRowObjectIndex + 1 : newRowObjectIndex;
                    rowObjects.splice(newRowObjectIndex, 0, newRowObject);
                    var newRecord = currentData.splice(0, 1)[0];
                    currentData.splice(newRowObjectIndex, 0, newRecord);
                    this.updateInfiniteCurrentViewData(newRecord, this.addRowIndex);
                }
                var movableRows = this.parent.grid.getRows();
                var movableRowsObject = this.parent.grid.getRowsObject();
                var isCache = this.parent.infiniteScrollSettings.enableCache;
                Eif (!isCache) {
                    ej2_grids_1.resetRowIndex(this.parent.grid, this.parent.grid.getRowsObject(), this.parent.grid.getRows(), 0);
                    this.updateIndex(this.parent.grid.dataSource, this.parent.getRows(), this.parent.getCurrentViewRecords());
                }
                Iif (!isCache && this.parent.getFrozenColumns() > 0) {
                    ej2_grids_1.resetRowIndex(this.parent.grid, movableRowsObject, movableRows, 0);
                    this.updateIndex(this.parent.grid.dataSource, movableRows, this.parent.getCurrentViewRecords());
                }
            }
        };
        Edit.prototype.updateInfiniteCurrentViewData = function (newRecord, newRowIndex) {
            var _this = this;
            var infiniteData = 'infiniteCurrentViewData';
            var updateCurrentViewData = 'updateCurrentViewData';
            var size = Math.ceil(newRowIndex / this.parent.grid.pageSettings.pageSize);
            var page = size > 0 ? size : 1;
            var dataIndex = newRowIndex - ((page - 1) * this.parent.pageSettings.pageSize);
            var infiniteCurrentViewData = this.parent.grid.infiniteScrollModule["" + infiniteData];
            infiniteCurrentViewData[1].splice(0, 1);
            var data = infiniteCurrentViewData[parseInt(page.toString(), 10)];
            Eif (!ej2_base_1.isNullOrUndefined(this.addRowRecord)) {
                data.filter(function (e, index) {
                    if (e.uniqueID === _this.addRowRecord.uniqueID) {
                        dataIndex = index;
                    }
                });
                if (this.addRowRecord.hasChildRecords && this.addRowRecord.childRecords.length &&
                    this.parent.editSettings.newRowPosition === 'Below' || this.parent.editSettings.newRowPosition === 'Child') {
                    dataIndex += utils_1.findChildrenRecords(this.addRowRecord).length;
                }
            }
            Iif (dataIndex >= this.parent.pageSettings.pageSize) {
                page += 1;
                data = infiniteCurrentViewData[parseInt(page.toString(), 10)];
                dataIndex = dataIndex - this.parent.pageSettings.pageSize >= 0 ? dataIndex - this.parent.pageSettings.pageSize : 0;
            }
            dataIndex = this.parent.editSettings.newRowPosition === 'Below' ? dataIndex + 1 : dataIndex;
            data.splice(dataIndex, 0, newRecord);
            this.parent.grid.infiniteScrollModule["" + updateCurrentViewData]();
        };
        Edit.prototype.recordDoubleClick = function (args) {
            var target = args.target;
            Iif (ej2_base_1.isNullOrUndefined(target.closest('td.e-rowcell'))) {
                return;
            }
            if (!(this.parent.grid.editSettings.allowEditing) || this.parent.grid.isEdit) {
                return;
            }
            var column = this.parent.grid.getColumnByIndex(+target.closest('td.e-rowcell').getAttribute('data-colindex'));
            if (this.parent.editSettings.mode === 'Cell' && !this.isOnBatch && column && !column.isPrimaryKey &&
                this.parent.editSettings.allowEditing && column.allowEditing && !(target.classList.contains('e-treegridexpand') ||
                target.classList.contains('e-treegridcollapse')) && this.parent.editSettings.allowEditOnDblClick) {
                this.isOnBatch = true;
                this.parent.grid.setProperties({ selectedRowIndex: args.rowIndex }, true);
                if (this.parent.enableVirtualization) {
                    var tr = ej2_grids_2.parentsUntil(args.target, 'e-row');
                    this.prevAriaRowIndex = tr.getAttribute('data-rowindex');
                    tr.setAttribute('data-rowindex', tr.rowIndex + '');
                }
                this.updateGridEditMode('Batch');
            }
            else if (this.parent.editSettings.mode === 'Cell' && (!column.allowEditing || column.isPrimaryKey)) {
                this.isOnBatch = true;
                this.updateGridEditMode('Batch');
            }
        };
        Edit.prototype.updateGridEditMode = function (mode) {
            this.parent.grid.setProperties({ editSettings: { mode: mode } }, true);
            var updateMethod = ej2_grids_1.getObject('updateEditObj', this.parent.grid.editModule);
            updateMethod.apply(this.parent.grid.editModule);
            this.parent.grid.isEdit = false;
        };
        Edit.prototype.resetIsOnBatch = function () {
            if (this.parent.enableVirtualization && this.parent.editSettings.mode === 'Cell') {
                this.isOnBatch = false;
                this.updateGridEditMode('Normal');
            }
        };
        Edit.prototype.keyPressed = function (args) {
            if (this.isOnBatch) {
                this.keyPress = args.action;
            }
            if (args.action === 'f2') {
                this.recordDoubleClick(args);
            }
            if (args.action === 'escape') {
                this.closeEdit();
            }
        };
        Edit.prototype.deleteUniqueID = function (value) {
            var idFilter = 'uniqueIDFilterCollection';
            delete this.parent["" + idFilter]["" + value];
            var id = 'uniqueIDCollection';
            delete this.parent["" + id]["" + value];
        };
        Edit.prototype.cellEdit = function (args) {
            var _this = this;
            var promise = 'promise';
            var prom = args["" + promise];
            delete args["" + promise];
            if (this.parent.enableVirtualization && !ej2_base_1.isNullOrUndefined(this.prevAriaRowIndex) && this.prevAriaRowIndex !== '-1') {
                args.row.setAttribute('data-rowindex', this.prevAriaRowIndex);
                this.prevAriaRowIndex = undefined;
            }
            if (this.keyPress !== 'enter') {
                this.parent.trigger(events.cellEdit, args, function (celleditArgs) {
                    if (!celleditArgs.cancel && _this.parent.editSettings.mode === 'Cell') {
                        _this.enableToolbarItems('edit');
                    }
                    else if (celleditArgs.cancel && _this.parent.editSettings.mode === 'Cell') {
                        _this.isOnBatch = false;
                        _this.updateGridEditMode('Normal');
                    }
                    Eif (!ej2_base_1.isNullOrUndefined(prom)) {
                        prom.resolve(celleditArgs);
                    }
                });
            }
            Iif (this.doubleClickTarget && (this.doubleClickTarget.classList.contains('e-treegridexpand') ||
                this.doubleClickTarget.classList.contains('e-treegridcollapse') || this.doubleClickTarget.classList.contains('e-summarycell'))) {
                args.cancel = true;
                this.doubleClickTarget = null;
                return;
            }
            if (this.parent.editSettings.mode === 'Cell') {
                if (this.keyPress === 'tab' || this.keyPress === 'shiftTab') {
                    this.keyPress = null;
                }
                else if (this.keyPress === 'enter') {
                    args.cancel = true;
                    this.keyPress = null;
                    ej2_base_1.setValue('isEditCollapse', false, this.parent);
                }
                Iif (!args.columnObject.allowEditing) {
                    args.cancel = true;
                }
            }
            if (this.parent.enableVirtualization) {
                this.parent.grid.contentModule['editedRowIndex'] = this.parent.grid.editModule.editModule['index'];
            }
        };
        Edit.prototype.enableToolbarItems = function (request) {
            if (!ej2_base_1.isNullOrUndefined(this.parent.grid.toolbarModule)) {
                var toolbarID = this.parent.element.id + '_gridcontrol_';
                this.parent.grid.toolbarModule.enableItems([toolbarID + 'add', toolbarID + 'edit', toolbarID + 'delete'], request === 'save');
                this.parent.grid.toolbarModule.enableItems([toolbarID + 'update', toolbarID + 'cancel'], request === 'edit');
            }
        };
        Edit.prototype.batchCancel = function () {
            if (this.parent.editSettings.mode === 'Cell') {
                var cellDetails = ej2_base_1.getValue('editModule.cellDetails', this.parent.grid.editModule);
                if (!ej2_base_1.isNullOrUndefined(this.editedRowIndex)) {
                    cellDetails.rowIndex = this.editedRowIndex;
                }
                var treeCell = this.parent.getCellFromIndex(cellDetails.rowIndex, this.parent.treeColumnIndex);
                this.parent.renderModule.cellRender({
                    data: cellDetails.rowData,
                    cell: treeCell,
                    column: this.parent.grid.getColumns()[this.parent.treeColumnIndex]
                });
                this.updateGridEditMode('Normal');
                this.isOnBatch = false;
            }
            if (this.parent.editSettings.mode === 'Batch') {
                this.parent.notify('batchCancelAction', {});
            }
        };
        Edit.prototype.customCellSave = function (args) {
            Iif (utils_1.isCountRequired(this.parent) && this.parent.editSettings.mode === 'Cell' && args.action === 'edit') {
                this.updateCell(args, args.rowIndex);
                this.afterCellSave(args, args.row);
            }
        };
        Edit.prototype.cellSave = function (args) {
            var _this = this;
            if (this.parent.editSettings.mode === 'Cell' && this.parent.element.querySelector('form')) {
                args.cancel = true;
                var editModule = 'editModule';
                ej2_base_1.setValue('isEditCollapse', true, this.parent);
                args.rowData[args.columnName] = args.value;
                var row_1;
                Iif (ej2_base_1.isNullOrUndefined(args.cell)) {
                    row_1 = this.parent.grid.editModule["" + editModule].form.parentElement.parentNode;
                }
                else {
                    row_1 = args.cell.parentNode;
                }
                var rowIndex_1;
                var primaryKeys_1 = this.parent.getPrimaryKeyFieldNames();
                Iif (ej2_base_1.isNullOrUndefined(row_1)) {
                    this.parent.grid.getCurrentViewRecords().filter(function (e, i) {
                        if (e[primaryKeys_1[0]] === args.rowData[primaryKeys_1[0]]) {
                            rowIndex_1 = i;
                            return;
                        }
                    });
                }
                else {
                    var freeze = (this.parent.getFrozenLeftColumnsCount() > 0 ||
                        this.parent.getFrozenRightColumnsCount() > 0) ? true : false;
                    if (freeze) {
                        Eif (this.parent.getRows().indexOf(row_1) !== -1) {
                            rowIndex_1 = this.parent.getRows().indexOf(row_1);
                        }
                        else {
                            rowIndex_1 = this.parent.getRows().indexOf(row_1);
                        }
                    }
                    else {
                        rowIndex_1 = (this.parent.getRows().indexOf(row_1) === -1 && (this.parent.getFrozenColumns() > 0)) ?
                            this.parent.grid.getRows().indexOf(row_1) : this.parent.getRows().indexOf(row_1);
                    }
                }
                var arg = {};
                ej2_base_1.extend(arg, args);
                arg.cancel = false;
                arg.type = 'save';
                row_1 = this.parent.grid.getRows()[row_1.rowIndex];
                this.parent.trigger(events.actionBegin, arg);
                Eif (!arg.cancel) {
                    Iif ((row_1.rowIndex === this.parent.getCurrentViewRecords().length - 1) && this.keyPress === 'tab') {
                        this.isTabLastRow = true;
                    }
                    Eif (!utils_1.isRemoteData(this.parent) &&
                        !(this.parent.dataSource instanceof ej2_data_1.DataManager && this.parent.dataSource.adaptor instanceof ej2_data_1.RemoteSaveAdaptor)) {
                        Iif (utils_1.isCountRequired(this.parent)) {
                            var eventPromise = 'eventPromise';
                            var editArgs = { requestType: 'save', data: args.rowData, action: 'edit', row: row_1,
                                rowIndex: rowIndex_1, rowData: args.rowData, columnName: args.columnName,
                                filterChoiceCount: null, excelSearchOperator: null };
                            this.parent.grid.getDataModule()["" + eventPromise](editArgs, this.parent.grid.query);
                        }
                        else {
                            this.updateCell(args, rowIndex_1);
                            ej2_base_1.setValue('isEdit', false, this.parent.grid);
                            this.afterCellSave(args, row_1);
                        }
                    }
                    else if (utils_1.isRemoteData(this.parent) ||
                        (this.parent.dataSource instanceof ej2_data_1.DataManager && this.parent.dataSource.adaptor instanceof ej2_data_1.RemoteSaveAdaptor)) {
                        var query = this.parent.grid.query;
                        if (this.parent['isGantt'] && this.parent.loadChildOnDemand) {
                            this.updateCell(args, rowIndex_1);
                            ej2_base_1.setValue('isEdit', false, this.parent.grid);
                            this.afterCellSave(args, row_1);
                        }
                        else {
                            var crud = null;
                            crud = this.parent.grid.dataSource.update(primaryKeys_1[0], args.rowData, query.fromTable, query, args.previousValue);
                            crud.then(function (e) {
                                if (!ej2_base_1.isNullOrUndefined(e)) {
                                    args.rowData[args.columnName] = e[args.columnName];
                                }
                                _this.updateCell(args, rowIndex_1);
                                ej2_base_1.setValue('isEdit', false, _this.parent.grid);
                                _this.afterCellSave(args, row_1);
                            });
                        }
                    }
                }
                else {
                    this.parent.grid.isEdit = true;
                }
            }
            if (this.parent.enableVirtualization) {
                this.parent.grid.contentModule['virtualData'] = {};
            }
        };
        Edit.prototype.afterCellSave = function (args, row) {
            if (this.parent.grid.aggregateModule) {
                this.parent.grid.aggregateModule.refresh(args.rowData);
            }
            this.parent.grid.editModule.destroyWidgets([this.parent.grid.getColumnByField(args.columnName)]);
            this.parent.grid.editModule.formObj.destroy();
            if (this.keyPress !== 'tab' && this.keyPress !== 'shiftTab') {
                this.updateGridEditMode('Normal');
                this.isOnBatch = false;
            }
            this.enableToolbarItems('save');
            ej2_base_1.removeClass([row], ['e-editedrow', 'e-batchrow']);
            ej2_base_1.removeClass(row.querySelectorAll('.e-rowcell'), ['e-editedbatchcell', 'e-updatedtd']);
            Eif (this.parent['isCellSaveFocus'] !== false) {
                this.parent.grid.focusModule.restoreFocus();
            }
            crud_actions_1.editAction({ value: args.rowData, action: 'edit' }, this.parent, this.isSelfReference, this.addRowIndex, this.selectedIndex, args.columnName);
            Iif ((row.rowIndex === this.parent.getCurrentViewRecords().length - 1) && this.keyPress === 'enter') {
                this.keyPress = null;
            }
            var saveArgs = {
                type: 'save', column: this.parent.getColumnByField(args.columnName), data: args.rowData,
                previousData: args.previousValue, row: row, target: args.cell
            };
            if (this.parent.aggregates.map(function (ag) { return ag.showChildSummary === true; }).length) {
                this.parent.grid.refresh();
            }
            this.parent.trigger(events.actionComplete, saveArgs);
        };
        Edit.prototype.lastCellTab = function () {
            if (!this.parent.grid.isEdit && this.isOnBatch && this.keyPress === 'tab' && this.parent.editSettings.mode === 'Cell') {
                Iif (!this.parent.editSettings.allowNextRowEdit) {
                    this.updateGridEditMode('Normal');
                    this.isOnBatch = false;
                    this.keyPress = null;
                }
                else {
                    this.enableToolbarItems('edit');
                }
            }
        };
        Edit.prototype.updateCell = function (args, rowIndex) {
            this.parent.grid.editModule.updateCell(rowIndex, args.columnName, args.rowData[args.columnName]);
            this.parent.grid.getRowsObject()[parseInt(rowIndex.toString(), 10)].data = args.rowData;
        };
        Edit.prototype.crudAction = function (details, columnName) {
            crud_actions_1.editAction(details, this.parent, this.isSelfReference, this.addRowIndex, this.selectedIndex, columnName, this.addRowRecord);
            this.parent.parentData = [];
            var data = this.parent.grid.dataSource instanceof ej2_data_1.DataManager ?
                this.parent.grid.dataSource.dataSource.json : this.parent.grid.dataSource;
            for (var i = 0; i < data.length; i++) {
                data[parseInt(i.toString(), 10)].index = i;
                var key = this.parent.grid.getPrimaryKeyFieldNames()[0];
                if (details.value["" + key] === data[parseInt(i.toString(), 10)]["" + key]) {
                    if (details.action === 'add') {
                        data[parseInt(i.toString(), 10)].level = this.internalProperties.level;
                        data[parseInt(i.toString(), 10)].taskData = this.internalProperties.taskData;
                        data[parseInt(i.toString(), 10)].uniqueID = this.internalProperties.uniqueID;
                        if (!ej2_base_1.isNullOrUndefined(this.internalProperties.parentItem)) {
                            data[parseInt(i.toString(), 10)].parentItem = this.internalProperties.parentItem;
                            data[parseInt(i.toString(), 10)].parentUniqueID = this.internalProperties.parentUniqueID;
                        }
                        data[parseInt(i.toString(), 10)].childRecords = this.internalProperties.childRecords;
                    }
                }
                ej2_base_1.setValue('uniqueIDCollection.' + data[parseInt(i.toString(), 10)].uniqueID + '.index', i, this.parent);
                var adaptor = this.parent.dataSource.adaptor;
                Iif ((utils_1.isRemoteData(this.parent) || adaptor instanceof ej2_data_1.RemoteSaveAdaptor)) {
                    ej2_base_1.setValue('uniqueIDCollection.' + data[parseInt(i.toString(), 10)].uniqueID, data[parseInt(i.toString(), 10)], this.parent);
                }
                if (!data[parseInt(i.toString(), 10)].level) {
                    this.parent.parentData.push(data[parseInt(i.toString(), 10)]);
                }
            }
            if (!this.parent.enableInfiniteScrolling) {
                if (details.action === 'add' && this.previousNewRowPosition != null) {
                    this.parent.setProperties({ editSettings: { newRowPosition: this.previousNewRowPosition } }, true);
                    this.previousNewRowPosition = null;
                }
            }
        };
        Edit.prototype.updateIndex = function (data, rows, records) {
            for (var j = 0; j < this.parent.getDataRows().length; j++) {
                var data1 = records[parseInt(j.toString(), 10)];
                if (!ej2_base_1.isNullOrUndefined(data1)) {
                    var index = ej2_base_1.getValue('uniqueIDCollection.' + data1.uniqueID + '.index', this.parent);
                    data1.index = index;
                    if (!ej2_base_1.isNullOrUndefined(data1.parentItem)) {
                        var parentIndex = ej2_base_1.getValue('uniqueIDCollection.' + data1.parentItem.uniqueID + '.index', this.parent);
                        data1.parentItem.index = parentIndex;
                    }
                }
            }
            var count = -1;
            var treeColIndex = this.parent.treeColumnIndex;
            if (this.parent.getFrozenColumns() > 0) {
                var cells = rows[0].querySelectorAll('.e-rowcell');
                for (var l = 0; l < cells.length; l++) {
                    if (cells[parseInt(l.toString(), 10)].classList.contains('e-gridrowindex0level0')) {
                        treeColIndex = l;
                        break;
                    }
                }
            }
            for (var k = 0; k < this.parent.getRows().length; k++) {
                Eif (!rows[parseInt(k.toString(), 10)].classList.contains('e-detailrow')) {
                    count++;
                }
                var data2 = records[parseInt(count.toString(), 10)];
                if (!ej2_base_1.isNullOrUndefined(data2)) {
                    var index = data2.index;
                    var level = data2.level;
                    var row = rows[parseInt(k.toString(), 10)];
                    if (!ej2_base_1.isNullOrUndefined(data2.parentItem)) {
                        index = ej2_base_1.getValue('uniqueIDCollection.' + data2.parentItem.uniqueID + '.index', this.parent);
                    }
                    var treecell = row.cells[parseInt(treeColIndex.toString(), 10)];
                    Eif (!ej2_base_1.isNullOrUndefined(treecell)) {
                        for (var l = 0; l < treecell.classList.length; l++) {
                            var value = treecell.classList[parseInt(l.toString(), 10)];
                            var remove = /e-gridrowindex/i;
                            var removed = /e-griddetailrowindex/i;
                            var result = value.match(remove);
                            var results = value.match(removed);
                            if (result != null) {
                                ej2_base_1.removeClass([treecell], value);
                            }
                            Iif (results != null) {
                                ej2_base_1.removeClass([treecell], value);
                            }
                        }
                        Eif (!rows[parseInt(k.toString(), 10)].classList.contains('e-detailrow')) {
                            ej2_base_1.addClass([treecell], 'e-gridrowindex' + index + 'level' + level);
                        }
                        else {
                            ej2_base_1.addClass([treecell], 'e-griddetailrowindex' + index + 'level' + level);
                        }
                    }
                }
            }
        };
        Edit.prototype.beginAdd = function () {
            var position;
            var index = this.addRowIndex;
            var records = this.parent.grid.getCurrentViewRecords();
            if (this.parent.editSettings.mode === 'Batch') {
                index = this.batchEditModule.getAddRowIndex();
                this.selectedIndex = this.batchEditModule.getSelectedIndex();
                if (this.parent.getBatchChanges()[this.addedRecords].length > 1
                    || this.parent.getBatchChanges()[this.deletedRecords].length) {
                    records = this.batchEditModule.getBatchRecords();
                }
            }
            var rows = this.parent.grid.getDataRows();
            var firstAriaIndex = rows.length ? +rows[0].getAttribute('data-rowindex') : 0;
            var lastAriaIndex = rows.length ? +rows[rows.length - 1].getAttribute('data-rowindex') : 0;
            var withinRange = this.selectedIndex >= firstAriaIndex && this.selectedIndex <= lastAriaIndex;
            var isVirtualization = this.parent.enableVirtualization && this.addRowIndex > -1 && this.prevAriaRowIndex !== '-1';
            Eif (this.parent.editSettings.mode !== 'Dialog') {
                if (this.parent.editSettings.newRowPosition === 'Above') {
                    position = 'before';
                }
                else if ((this.parent.editSettings.newRowPosition === 'Below' || this.parent.editSettings.newRowPosition === 'Child')
                    && (this.selectedIndex > -1 || isVirtualization) && withinRange) {
                    position = 'after';
                    if (!ej2_base_1.isNullOrUndefined(records[parseInt(index.toString(), 10)]) &&
                        records[parseInt(index.toString(), 10)].expanded) {
                        if (this.parent.editSettings.mode === 'Batch' && (this.parent.getBatchChanges()[this.addedRecords].length > 1
                            || this.parent.getBatchChanges()[this.deletedRecords].length)) {
                            index += utils_1.findChildrenRecords(records[parseInt(index.toString(), 10)]).length;
                            if (this.parent.editSettings.newRowPosition !== 'Child') {
                                var batchChildCount = this.batchEditModule.getBatchChildCount();
                                index = index + batchChildCount;
                            }
                        }
                        else Eif (!this.parent.enableVirtualization) {
                            index += utils_1.findChildrenRecords(records[parseInt(index.toString(), 10)]).length;
                        }
                    }
                }
                if ((this.selectedIndex > -1 || isVirtualization) && withinRange
                    && (index || (this.parent.editSettings.newRowPosition === 'Child'
                        || this.parent.editSettings.newRowPosition === 'Below'))) {
                    if (index >= rows.length - 1) {
                        index = rows.length - 2;
                    }
                    var r = 'rows';
                    var newRowObject = this.parent.grid.contentModule["" + r][0];
                    var focussedElement = document.activeElement;
                    rows[index + 1]["" + position](rows[0]);
                    ej2_base_1.setValue('batchIndex', index + 1, this.batchEditModule);
                    var rowObjectIndex = this.parent.editSettings.newRowPosition === 'Above' ? index : index + 1;
                    if (this.parent.editSettings.mode === 'Batch') {
                        this.parent.grid.contentModule["" + r].splice(0, 1);
                        this.parent.grid.contentModule["" + r].splice(rowObjectIndex, 0, newRowObject);
                    }
                    if (this.parent.editSettings.mode === 'Row' || this.parent.editSettings.mode === 'Cell') {
                        var errors = this.parent.grid.getContentTable().querySelectorAll('.e-griderror');
                        for (var i = 0; i < errors.length; i++) {
                            errors[parseInt(i.toString(), 10)].remove();
                        }
                        ej2_base_1.setValue('errorRules', [], this.parent.grid.editModule.formObj);
                    }
                    Iif (isVirtualization) {
                        this.prevAriaRowIndex = '-1';
                    }
                    Eif (!this.parent.enableVirtualization || this.parent.enableVirtualization && !Object.keys(this.parent.grid.contentModule['emptyRowData']).length) {
                        this.isScrollByFocus = true;
                        focussedElement.focus();
                    }
                    Iif (this.parent.enableVirtualization && !Object.keys(this.parent.grid.contentModule['emptyRowData']).length) {
                        this.parent.grid.contentModule['createEmptyRowdata']();
                    }
                }
            }
            if (this.parent.editSettings.mode === 'Batch' && !ej2_base_1.isNullOrUndefined(this.addRowIndex) && this.addRowIndex !== -1 && this['isAddedRowByMethod'] && !this.isAddedRowByContextMenu) {
                index = this.batchEditModule.getAddRowIndex();
                this.selectedIndex = this.batchEditModule.getSelectedIndex();
                var batchAddedRecords = this.parent.getBatchChanges()['addedRecords'];
                var newlyAddedRecord = void 0;
                Eif (batchAddedRecords.length) {
                    for (var i = 0; i < batchAddedRecords.length; i++) {
                        if (ej2_base_1.isNullOrUndefined(batchAddedRecords[parseInt(i.toString(), 10)].uniqueID)) {
                            newlyAddedRecord = batchAddedRecords[parseInt(i.toString(), 10)];
                        }
                    }
                }
                var args = {
                    action: 'add',
                    data: newlyAddedRecord,
                    index: index,
                    seletedRow: 0
                };
                this.beginAddEdit(args);
                this.batchEditModule['batchAddRowRecord'].push(this.batchEditModule['addRowRecord']);
                this.batchEditModule['batchAddedRecords'].push(args['data']);
            }
        };
        Edit.prototype.beginEdit = function (args) {
            Iif (args.requestType === 'refresh' && this.isOnBatch) {
                args.cancel = true;
                return;
            }
            Iif (this.parent.editSettings.mode === 'Cell' && args.requestType === 'beginEdit') {
                args.cancel = true;
                return;
            }
            if (this.doubleClickTarget && args.requestType !== 'delete' && (this.doubleClickTarget.classList.contains('e-treegridexpand') ||
                this.doubleClickTarget.classList.contains('e-treegridcollapse') || this.doubleClickTarget.classList.contains('e-frame'))) {
                args.cancel = true;
                this.doubleClickTarget = null;
                return;
            }
            if (args.requestType === 'delete') {
                var data_1 = args.data;
                if (ej2_base_1.isNullOrUndefined(args.data[0].uniqueID)) {
                    var primaryKeys_2 = this.parent.getPrimaryKeyFieldNames();
                    var _loop_1 = function (i) {
                        this_1.parent.flatData.filter(function (e) {
                            if (e["" + primaryKeys_2[0]] === args.data[parseInt(i.toString(), 10)][primaryKeys_2[0]]) {
                                data_1[parseInt(i.toString(), 10)] = e;
                            }
                        });
                    };
                    var this_1 = this;
                    for (var i = 0; i < data_1.length; i++) {
                        _loop_1(i);
                    }
                }
                for (var i = 0; i < data_1.length; i++) {
                    this.deleteUniqueID(data_1[parseInt(i.toString(), 10)].uniqueID);
                    var childs = utils_1.findChildrenRecords(data_1[parseInt(i.toString(), 10)]);
                    for (var c = 0; c < childs.length; c++) {
                        this.deleteUniqueID(childs[parseInt(c.toString(), 10)].uniqueID);
                    }
                    args.data = args.data.concat(childs);
                }
            }
            if (args.requestType === 'add' || (this.isAddedRowByMethod && (this.parent.enableVirtualization || this.parent.enableInfiniteScrolling))) {
                if (!(this.parent.grid.selectedRowIndex === -1 && this.isAddedRowByMethod)
                    && args.index === this.parent.grid.selectedRowIndex || args.index === 0) {
                    this.selectedIndex = this.parent.grid.selectedRowIndex;
                }
                if (this.parent.enableVirtualization) {
                    var selector = '.e-row[data-rowindex="' + this.selectedIndex + '"]';
                    var row = void 0;
                    if (this.selectedIndex > -1 && this.parent.editSettings.newRowPosition !== 'Top' &&
                        this.parent.editSettings.newRowPosition !== 'Bottom') {
                        this.prevAriaRowIndex = this.selectedIndex.toString();
                        row = this.parent.getContent().querySelector(selector);
                        this.addRowIndex = row ? row.rowIndex : 0;
                    }
                    else {
                        Iif (this.prevAriaRowIndex && this.prevAriaRowIndex !== '-1') {
                            selector = '.e-row[data-rowindex="' + this.prevAriaRowIndex + '"]';
                            row = this.parent.getContent().querySelector(selector);
                            this.addRowIndex = row ? row.rowIndex : 0;
                        }
                        else {
                            this.addRowIndex = 0;
                        }
                    }
                }
                else {
                    if (this.isAddedRowByMethod && (this.parent.enableVirtualization || this.parent.enableInfiniteScrolling)) {
                        if (args.index !== 0) {
                            this.addRowIndex = args.index;
                        }
                        else {
                            this.addRowIndex = this.parent.grid.selectedRowIndex;
                        }
                    }
                    else {
                        this.addRowIndex = this.parent.grid.selectedRowIndex > -1 ? this.parent.grid.selectedRowIndex : 0;
                    }
                }
                var selectedRecords = this.parent.getSelectedRecords()[0];
                if ((this.isAddedRowByMethod || (this.isAddedRowByContextMenu && this.parent.grid.selectedRowIndex !== -1)) &&
                    (this.parent.enableVirtualization || this.parent.enableInfiniteScrolling)) {
                    this.addRowRecord = this.parent.flatData[this.parent.grid.selectedRowIndex];
                    Iif (this.parent.enableVirtualization && this.isAddedRowByContextMenu) {
                        this.addRowRecord = this.parent.getCurrentViewRecords()[this.addRowIndex];
                    }
                }
                else if (!ej2_base_1.isNullOrUndefined(selectedRecords)) {
                    this.addRowRecord = selectedRecords;
                }
            }
            if (this.isAddedRowByMethod && args.index !== 0) {
                this.addRowRecord = this.parent.flatData[args.index];
                this.addRowIndex = args.index;
            }
            if (this.parent.editSettings.newRowPosition === 'Child' &&
                !ej2_base_1.isNullOrUndefined(this.parent.getSelectedRecords()[0])) {
                this.addRowRecord = this.parent.getSelectedRecords()[0];
            }
            Iif (ej2_base_1.isNullOrUndefined(this.addRowRecord) && this.parent.getCurrentViewRecords().length > this.addRowIndex &&
                args.requestType === 'save' && this.parent.getSelectedRecords().length !== 0) {
                this.addRowRecord = this.parent.getCurrentViewRecords()[this.addRowIndex];
            }
            this.isAddedRowByMethod = false;
            args = this.beginAddEdit(args);
        };
        Edit.prototype.savePreviousRowPosition = function () {
            Eif (this.previousNewRowPosition === null) {
                this.previousNewRowPosition = this.parent.editSettings.newRowPosition;
            }
        };
        Edit.prototype.beginAddEdit = function (args) {
            var value = args.data;
            if (args.action === 'add') {
                var key = this.parent.grid.getPrimaryKeyFieldNames()[0];
                var position = null;
                value.taskData = ej2_base_1.isNullOrUndefined(value.taskData) ? ej2_base_1.extend({}, args.data) : value.taskData;
                var currentData = void 0;
                if (this.parent.enableVirtualization && args.index !== 0) {
                    currentData = this.parent.flatData;
                }
                else if (this.parent.editSettings.mode === 'Batch' && this['isAddedRowByMethod'] && !ej2_base_1.isNullOrUndefined(this.addRowIndex)) {
                    currentData = this.batchEditModule['batchRecords'];
                }
                else {
                    currentData = this.parent.grid.getCurrentViewRecords();
                }
                if (this.parent.enableVirtualization && args.index !== 0) {
                    this.addRowIndex = this.parent.flatData.indexOf(this.addRowRecord);
                    this.selectedIndex = this.addRowIndex;
                }
                var index = this.addRowIndex;
                value.uniqueID = ej2_grids_1.getUid(this.parent.element.id + '_data_');
                ej2_base_1.setValue('uniqueIDCollection.' + value.uniqueID, value, this.parent);
                var level = 0;
                var idMapping = void 0;
                var parentUniqueID = void 0;
                var parentItem = void 0;
                var parentIdMapping = void 0;
                var isVirtualization = this.parent.enableVirtualization && this.addRowIndex > -1 && this.prevAriaRowIndex !== '-1';
                var rows = this.parent.getRows();
                var firstAriaIndex = rows.length ? currentData.indexOf(currentData[0]) : 0;
                var lastAriaIndex = rows.length ? +rows[rows.length - 1].getAttribute('data-rowindex') : 0;
                var withinRange = this.parent.enableVirtualization && args.index !== 0 ? true :
                    this.selectedIndex >= firstAriaIndex && this.selectedIndex <= lastAriaIndex;
                if (currentData.length) {
                    idMapping = currentData[this.addRowIndex][this.parent.idMapping];
                    parentIdMapping = currentData[this.addRowIndex][this.parent.parentIdMapping];
                    if (currentData[this.addRowIndex].parentItem) {
                        parentUniqueID = currentData[this.addRowIndex].parentItem.uniqueID;
                    }
                    parentItem = currentData[this.addRowIndex].parentItem;
                }
                if (this.parent.editSettings.newRowPosition !== 'Top' && currentData.length) {
                    level = currentData[this.addRowIndex].level;
                    if (this.parent.editSettings.newRowPosition === 'Above') {
                        position = 'before';
                        index = currentData[this.addRowIndex].index;
                    }
                    else if (this.parent.editSettings.newRowPosition === 'Below') {
                        position = 'after';
                        var childRecordCount = utils_1.findChildrenRecords(currentData[this.addRowIndex]).length;
                        var currentDataIndex = currentData[this.addRowIndex].index;
                        index = (childRecordCount > 0) ? (currentDataIndex + childRecordCount) : (currentDataIndex);
                    }
                    else if (this.parent.editSettings.newRowPosition === 'Child') {
                        position = 'after';
                        if ((this.selectedIndex > -1 || isVirtualization) && withinRange) {
                            value.parentItem = ej2_base_1.extend({}, currentData[this.addRowIndex]);
                            value.parentUniqueID = value.parentItem.uniqueID;
                            delete value.parentItem.childRecords;
                            delete value.parentItem[this.parent.childMapping];
                        }
                        var childRecordCount1 = utils_1.findChildrenRecords(currentData[this.addRowIndex]).length;
                        var currentDataIndex1 = currentData[this.addRowIndex].index;
                        if (this.selectedIndex >= 0) {
                            value.level = level + 1;
                        }
                        index = (childRecordCount1 > 0) ? (currentDataIndex1 + childRecordCount1) : (currentDataIndex1);
                        if (this.isSelfReference) {
                            if (!this.parent.isLocalData && this.parent.editModule.selectedIndex === -1) {
                                value.taskData[this.parent.parentIdMapping] = value[this.parent.parentIdMapping] = null;
                            }
                            else {
                                value.taskData[this.parent.parentIdMapping] = value[this.parent.parentIdMapping] = idMapping;
                            }
                            if (!ej2_base_1.isNullOrUndefined(value.parentItem)) {
                                crud_actions_1.updateParentRow(key, value.parentItem, 'add', this.parent, this.isSelfReference, value);
                            }
                        }
                    }
                    if (this.parent.editSettings.newRowPosition === 'Above' || this.parent.editSettings.newRowPosition === 'Below') {
                        if ((this.selectedIndex > -1 || isVirtualization) && level && withinRange) {
                            value.parentUniqueID = parentUniqueID;
                            value.parentItem = ej2_base_1.extend({}, parentItem);
                            delete value.parentItem.childRecords;
                            delete value.parentItem[this.parent.childMapping];
                        }
                        value.level = level;
                        if (this.isSelfReference) {
                            value.taskData[this.parent.parentIdMapping] = value[this.parent.parentIdMapping] = parentIdMapping;
                            if (!ej2_base_1.isNullOrUndefined(value.parentItem)) {
                                crud_actions_1.updateParentRow(key, value.parentItem, 'add', this.parent, this.isSelfReference, value);
                            }
                        }
                    }
                    if (position != null && (this.selectedIndex > -1 || isVirtualization) && withinRange) {
                        args.index = position === 'before' ? index : index + 1;
                    }
                    if (this.parent.editSettings.newRowPosition === 'Bottom') {
                        level = 0;
                        var dataSource = (this.parent.grid.dataSource instanceof ej2_data_1.DataManager ?
                            this.parent.grid.dataSource.dataSource.json : this.parent.grid.dataSource);
                        args.index = dataSource.length;
                    }
                }
                if (ej2_base_1.isNullOrUndefined(value.level)) {
                    value.level = level;
                }
                value.hasChildRecords = false;
                value.childRecords = [];
                value.index = 0;
            }
            if (args.action === 'add') {
                this.internalProperties = { level: value.level, parentItem: value.parentItem, uniqueID: value.uniqueID,
                    taskData: value.taskData, parentUniqueID: ej2_base_1.isNullOrUndefined(value.parentItem) ? undefined : value.parentItem.uniqueID,
                    childRecords: value.childRecords };
            }
            if (args.requestType === 'delete') {
                var deletedValues = args.data;
                for (var i = 0; i < deletedValues.length; i++) {
                    if (deletedValues[parseInt(i.toString(), 10)].parentItem) {
                        var parentItem = utils_1.getParentData(this.parent, deletedValues[parseInt(i.toString(), 10)].parentItem.uniqueID);
                        if (!ej2_base_1.isNullOrUndefined(parentItem) && parentItem.hasChildRecords) {
                            var childIndex = parentItem.childRecords.indexOf(deletedValues[parseInt(i.toString(), 10)]);
                            parentItem.childRecords.splice(childIndex, 1);
                        }
                    }
                }
            }
            return args;
        };
        Edit.prototype.addRecord = function (data, index, position) {
            if (this.parent.editSettings.newRowPosition === this.previousNewRowPosition || this.previousNewRowPosition === null) {
                this.previousNewRowPosition = this.parent.editSettings.newRowPosition;
            }
            if (!this.isSelfReference && !ej2_base_1.isNullOrUndefined(data) && Object.hasOwnProperty.call(data, this.parent.childMapping)) {
                var addRecords = [];
                var previousEditMode = this.parent.editSettings.mode;
                var previousGridEditMode = this.parent.grid.editSettings.mode;
                addRecords.push(data);
                this.parent.setProperties({ editSettings: { mode: 'Batch' } }, true);
                this.parent.grid.setProperties({ editSettings: { mode: 'Batch' } }, true);
                if (!ej2_base_1.isNullOrUndefined(position)) {
                    this.parent.setProperties({ editSettings: { newRowPosition: position } }, true);
                }
                var updatedRecords = { addedRecords: addRecords, changedRecords: [], deletedRecords: [] };
                this.parent.notify(events.batchSave, { updatedRecords: updatedRecords, index: index });
                this.parent.setProperties({ editSettings: { mode: previousEditMode } }, true);
                this.parent.grid.setProperties({ editSettings: { mode: previousGridEditMode } }, true);
                this.parent.refresh();
            }
            else {
                if (data) {
                    Eif (index > -1) {
                        this.selectedIndex = index;
                        this.addRowIndex = index;
                    }
                    else {
                        this.selectedIndex = this.parent.selectedRowIndex;
                        this.addRowIndex = this.parent.selectedRowIndex;
                    }
                    Eif (position) {
                        this.parent.setProperties({ editSettings: { newRowPosition: position } }, true);
                    }
                    this.parent.grid.editModule.addRecord(data, index);
                }
                else {
                    this.parent.grid.editModule.addRecord(data, index);
                }
            }
        };
        Edit.prototype.editFormValidate = function () {
            return this.parent.grid.editModule.editFormValidate();
        };
        Edit.prototype.destroyForm = function () {
            this.parent.grid.editModule.destroyForm();
        };
        Edit.prototype.contentready = function (e) {
            if (!ej2_base_1.isNullOrUndefined(e.args.requestType)
                && (e.args.requestType.toString() === 'delete' || e.args.requestType.toString() === 'save'
                    || (this.parent.editSettings.mode === 'Batch' && e.args.requestType.toString() === 'batchsave'))) {
                this.updateIndex(this.parent.grid.dataSource, this.parent.getRows(), this.parent.getCurrentViewRecords());
                if (this.parent.frozenRows || this.parent.getFrozenColumns() || this.parent.frozenColumns) {
                    if (this.parent.grid.dataSource.length === this.parent.getDataRows().length) {
                        this.updateIndex(this.parent.grid.dataSource, this.parent.getDataRows(), this.parent.getCurrentViewRecords());
                    }
                }
            }
        };
        Edit.prototype.editCell = function (rowIndex, field) {
            Eif (this.parent.editSettings.mode === 'Cell' || this.parent.editSettings.mode === 'Batch') {
                if (this.parent.editSettings.mode !== 'Batch') {
                    this.isOnBatch = true;
                    this.updateGridEditMode('Batch');
                }
                this.parent.grid.editModule.editCell(rowIndex, field);
            }
        };
        Edit.prototype.closeEdit = function () {
            if (this.parent.enableVirtualization && this.parent.grid.editSettings.mode === 'Batch' && this.parent.grid.pageSettings.currentPage > 1) {
                this.editedRowIndex = this.parent.grid.editModule.editModule['cellDetails'].rowIndex;
                this.parent.grid.editModule.editModule['cellDetails'].rowIndex = parseInt(this.parent.getRows()[this.parent.grid.editModule.editModule['cellDetails'].rowIndex].getAttribute('data-rowIndex'), 10);
            }
            this.parent.grid.editModule.closeEdit();
        };
        return Edit;
    }());
    exports.Edit = Edit;
});