all files / grid/actions/ batch-edit.js

95.29% Statements 486/510
87.36% Branches 311/356
95.45% Functions 42/44
95.29% Lines 486/510
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   27× 27× 27× 27× 27× 27×   27×     27× 27× 27× 27× 27× 27× 27× 27× 27× 27×   26× 25×     51×   26×   69× 64× 64× 64× 15×             285×             274× 274× 274× 274× 274× 240×   34× 34×   34×     34× 34× 34× 34×     22× 22×   22×               27×   25× 25× 25×       52×                                                         20× 20×   20× 20× 20×       18×   10×     349×   11× 11× 11×   10× 10× 10× 10×     25×         25× 25× 25× 25× 251× 251× 12×   251× 49×     11× 11×   30×       25×   12×                                 16×   20× 20× 20× 20×           20×     20× 20×   19× 19× 19×     16× 16× 16× 16× 16×         13×       19× 19× 19× 19× 19× 19×   40× 40× 40× 40× 40× 15×       40×   40× 40× 431× 403× 403× 52× 52× 39×     403×     28× 28×           19× 19×   18×   18×     18× 18×         18× 18×   16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16×       16× 16× 16×   16×             16×   16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 10× 10× 10×   16×   16×       16×         42× 42× 42× 42× 207× 207×     207×   17×     25×       12× 12× 12× 12× 121× 121×   12×   15× 15× 12×   15× 15×   95× 95× 95× 95× 92×   26× 26×     91×   86× 86× 86× 86× 86× 86×     84× 84×   86×     82× 82× 82× 82×                 82×     82× 82×   81×         81× 27× 27×   81× 81× 81× 81×   81× 81× 81× 81×     16× 16× 16× 16× 16× 16×         92× 46×   92× 92× 70×         361× 361×     361×   92× 92× 92× 92× 92× 92×       92×   92× 92× 92×   82× 82× 56×   82×     82× 326× 326× 82×         74× 74× 11× 39×   32× 32× 32×           166× 166× 89×   77× 77× 77× 77× 77× 77× 77× 77×                 77× 74×   77×   76× 76× 76× 76× 76× 76× 76× 76×       27×   76× 76× 76× 76× 11× 11×       85× 85×   41× 15× 15×                
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../base/enum", "../base/util", "../base/constant", "../renderer/row-renderer", "../renderer/cell-renderer", "../models/row", "../models/cell", "../services/row-model-generator"], function (require, exports, ej2_base_1, ej2_base_2, ej2_base_3, enum_1, util_1, events, row_renderer_1, cell_renderer_1, row_1, cell_1, row_model_generator_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BatchEdit = (function () {
        function BatchEdit(parent, serviceLocator, renderer) {
            this.cellDetails = {};
            this.parent = parent;
            this.serviceLocator = serviceLocator;
            this.renderer = renderer;
            this.focus = serviceLocator.getService('focus');
            this.addEventListener();
        }
        BatchEdit.prototype.addEventListener = function () {
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.click, this.clickHandler, this);
            this.parent.on(events.dblclick, this.dblClickHandler, this);
            this.parent.on(events.beforeCellFocused, this.onBeforeCellFocused, this);
            this.parent.on(events.cellFocused, this.onCellFocused, this);
            this.dataBoundFunction = this.dataBound.bind(this);
            this.parent.addEventListener(events.dataBound, this.dataBoundFunction);
            this.parent.on(events.doubleTap, this.dblClickHandler, this);
            this.parent.on(events.keyPressed, this.keyDownHandler, this);
            this.parent.on(events.editNextValCell, this.editNextValCell, this);
            this.parent.off(events.editNextValCell, this.editNextValCell);
        };
        BatchEdit.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.click, this.clickHandler);
            this.parent.off(events.dblclick, this.dblClickHandler);
            this.parent.off(events.beforeCellFocused, this.onBeforeCellFocused);
            this.parent.off(events.cellFocused, this.onCellFocused);
            this.parent.removeEventListener(events.dataBound, this.dataBoundFunction);
            this.parent.off(events.doubleTap, this.dblClickHandler);
            this.parent.off(events.keyPressed, this.keyDownHandler);
        };
        BatchEdit.prototype.dataBound = function () {
            this.parent.notify(events.toolbarRefresh, {});
        };
        BatchEdit.prototype.destroy = function () {
            this.removeEventListener();
        };
        BatchEdit.prototype.clickHandler = function (e) {
            if (!util_1.parentsUntil(e.target, this.parent.element.id + '_add', true)) {
                this.saveCell();
                this.editNextValCell();
                if (util_1.parentsUntil(e.target, 'e-rowcell') && !this.parent.isEdit) {
                    this.setCellIdx(e.target);
                }
            }
        };
        BatchEdit.prototype.dblClickHandler = function (e) {
            var target = util_1.parentsUntil(e.target, 'e-rowcell');
            var tr = util_1.parentsUntil(e.target, 'e-row');
            if (target && tr && !isNaN(parseInt(target.getAttribute('aria-colindex'), 10))
                && !target.parentElement.classList.contains('e-editedrow')) {
                this.editCell(parseInt(tr.getAttribute('aria-rowindex'), 10), this.parent.getColumns()[parseInt(target.getAttribute('aria-colindex'), 10)].field, this.isAddRow(parseInt(tr.getAttribute('aria-rowindex'), 10)));
            }
        };
        BatchEdit.prototype.onBeforeCellFocused = function (e) {
            if (this.parent.isEdit && this.validateFormObj() &&
                (e.byClick || (['tab', 'shiftTab', 'enter', 'shiftEnter'].indexOf(e.keyArgs.action) > -1))) {
                e.cancel = true;
                if (e.byClick) {
                    e.clickArgs.preventDefault();
                }
                else {
                    e.keyArgs.preventDefault();
                }
            }
        };
        BatchEdit.prototype.onCellFocused = function (e) {
            var frzCols = this.parent.getFrozenColumns();
            var mCont = this.parent.getContent().querySelector('.e-movablecontent');
            var mHdr = this.parent.getHeaderContent().querySelector('.e-movableheader');
            var clear = (!e.container.isContent || !e.container.isDataCell) && !(this.parent.frozenRows && e.container.isHeader);
            if (!e.byKey || clear) {
                return;
            }
            var _a = e.container.indexes, rowIndex = _a[0], cellIndex = _a[1];
            if (frzCols && (mCont.contains(e.element) || (this.parent.frozenRows && mHdr.contains(e.element)))) {
                cellIndex += frzCols;
            }
            Iif (this.parent.frozenRows && e.container.isContent) {
                rowIndex += this.parent.frozenRows;
            }
            var isEdit = this.parent.isEdit;
            Eif (!this.parent.element.querySelectorAll('.e-popup-open').length) {
                isEdit = isEdit && !this.validateFormObj();
                switch (e.keyArgs.action) {
                    case 'tab':
                    case 'shiftTab':
                        Eif (isEdit || this.parent.isLastCellPrimaryKey) {
                            this.editCellFromIndex(rowIndex, cellIndex);
                        }
                        break;
                    case 'enter':
                    case 'shiftEnter':
                        e.keyArgs.preventDefault();
                        Eif (isEdit) {
                            this.editCell(rowIndex, this.cellDetails.column.field);
                        }
                        break;
                    case 'f2':
                        this.editCellFromIndex(rowIndex, cellIndex);
                        this.focus.focus();
                        break;
                }
            }
        };
        BatchEdit.prototype.isAddRow = function (index) {
            return this.parent.getDataRows()[index].classList.contains('e-insertedrow');
        };
        BatchEdit.prototype.editCellFromIndex = function (rowIdx, cellIdx) {
            this.cellDetails.rowIndex = rowIdx;
            this.cellDetails.cellIndex = cellIdx;
            this.editCell(rowIdx, this.parent.getColumns()[cellIdx].field, this.isAddRow(rowIdx));
        };
        BatchEdit.prototype.closeEdit = function () {
            var gObj = this.parent;
            var rows = this.parent.getRowsObject();
            if (gObj.frozenColumns && rows.length < this.parent.currentViewData.length * 2) {
                rows.push.apply(rows, this.parent.getMovableRowsObject());
            }
            var rowRenderer = new row_renderer_1.RowRenderer(this.serviceLocator, null, this.parent);
            var tr;
            var mTr;
            var movObj;
            if (gObj.isEdit) {
                this.saveCell(true);
            }
            this.isAdded = false;
            gObj.clearSelection();
            for (var i = 0; i < rows.length; i++) {
                if (rows[i].isDirty) {
                    if (gObj.frozenColumns) {
                        movObj = gObj.getMovableRowsObject()[rows[i].index];
                        movObj.isDirty = true;
                    }
                    tr = gObj.getContentTable().querySelector('[data-uid=' + rows[i].uid + ']');
                    if (gObj.frozenRows) {
                        tr = gObj.getHeaderContent().querySelector('[data-uid=' + rows[i].uid + ']');
                    }
                    if (gObj.frozenColumns) {
                        Eif (gObj.frozenRows) {
                            mTr = gObj.getHeaderContent().querySelector('.e-movableheader')
                                .querySelector('[data-uid=' + rows[i].uid + ']');
                        }
                        else {
                            mTr = gObj.getContent().querySelector('.e-movablecontent')
                                .querySelector('[data-uid=' + rows[i].uid + ']');
                        }
                    }
                    Eif (tr || mTr) {
                        if (tr && tr.classList.contains('e-insertedrow') || mTr && mTr.classList.contains('e-insertedrow')) {
                            Eif (tr) {
                                ej2_base_2.remove(tr);
                            }
                            Iif (mTr && (gObj.frozenColumns || gObj.frozenRows)) {
                                ej2_base_2.remove(mTr);
                            }
                            this.removeRowObjectFromUID(rows[i].uid);
                            i--;
                        }
                        else {
                            util_1.refreshForeignData(rows[i], this.parent.getForeignKeyColumns(), rows[i].data);
                            delete rows[i].changes;
                            rows[i].isDirty = false;
                            var ftr = mTr ? mTr : tr;
                            ej2_base_2.classList(ftr, [], ['e-hiddenrow', 'e-updatedtd']);
                            rowRenderer.refresh(rows[i], gObj.getColumns(), false);
                            if (this.parent.aggregates.length > 0) {
                                this.parent.notify(events.refreshFooterRenderer, {});
                            }
                        }
                    }
                }
            }
            if (gObj.getContentTable().querySelector('tr.e-emptyrow') &&
                !gObj.getContentTable().querySelector('tr.e-row')) {
                gObj.getContentTable().querySelector('tr.e-emptyrow').classList.remove('e-hide');
            }
            var args = {
                requestType: 'batchCancel', rows: this.parent.getRowsObject()
            };
            gObj.notify(events.batchCancel, {
                rows: this.parent.getRowsObject().length ? this.parent.getRowsObject() :
                    [new row_1.Row({ isDataRow: true, cells: [new cell_1.Cell({ isDataCell: true, visible: true })] })]
            });
            gObj.selectRow(this.cellDetails.rowIndex);
            this.refreshRowIdx();
            gObj.notify(events.toolbarRefresh, {});
            this.parent.notify(events.tooltipDestroy, {});
            args = { requestType: 'batchCancel', rows: this.parent.getRowsObject() };
            gObj.trigger(events.batchCancel, args);
            if (gObj.frozenColumns) {
                rows.splice(this.parent.getMovableRowsObject().length, rows.length);
            }
        };
        BatchEdit.prototype.deleteRecord = function (fieldname, data) {
            this.saveCell();
            if (this.validateFormObj()) {
                this.saveCell(true);
            }
            this.isAdded = false;
            this.bulkDelete(fieldname, data);
            if (this.parent.aggregates.length > 0) {
                this.parent.notify(events.refreshFooterRenderer, {});
                if (this.parent.groupSettings.columns.length > 0) {
                    this.parent.notify(events.groupAggregates, {});
                }
            }
        };
        BatchEdit.prototype.addRecord = function (data) {
            this.bulkAddRow(data);
        };
        BatchEdit.prototype.endEdit = function (data) {
            if (this.parent.isEdit && this.validateFormObj()) {
                return;
            }
            this.batchSave();
        };
        BatchEdit.prototype.validateFormObj = function () {
            return this.parent.editModule.formObj && !this.parent.editModule.formObj.validate();
        };
        BatchEdit.prototype.batchSave = function () {
            var gObj = this.parent;
            this.saveCell();
            if (gObj.isEdit || this.editNextValCell() || gObj.isEdit) {
                return;
            }
            var changes = this.getBatchChanges();
            var args = { batchChanges: changes, cancel: false };
            gObj.trigger(events.beforeBatchSave, args);
            if (args.cancel) {
                return;
            }
            gObj.showSpinner();
            gObj.notify(events.bulkSave, { changes: changes });
        };
        BatchEdit.prototype.getBatchChanges = function () {
            var changes = {
                addedRecords: [],
                deletedRecords: [],
                changedRecords: []
            };
            var rows = this.parent.getRowsObject();
            var mRows = this.parent.getMovableRowsObject();
            var frzCols = this.parent.getFrozenColumns();
            for (var _i = 0, rows_1 = rows; _i < rows_1.length; _i++) {
                var row = rows_1[_i];
                if (frzCols) {
                    this.mergeBatchChanges(row, mRows[row.index], frzCols);
                }
                if (row.isDirty) {
                    switch (row.edit) {
                        case 'add':
                            changes.addedRecords.push(row.changes);
                            break;
                        case 'delete':
                            changes.deletedRecords.push(row.data);
                            break;
                        default:
                            changes.changedRecords.push(row.changes);
                    }
                }
            }
            return changes;
        };
        BatchEdit.prototype.mergeBatchChanges = function (row, mRow, frzCols) {
            if (row.isDirty) {
                Iif (mRow.isDirty) {
                    var i_1 = 0;
                    Object.keys(row.changes).forEach(function (key) {
                        if (i_1 >= frzCols) {
                            row.changes[key] = mRow.changes[key];
                        }
                        i_1++;
                    });
                }
            }
            else Iif (mRow.isDirty) {
                row.changes = mRow.changes;
                row.isDirty = mRow.isDirty;
            }
        };
        BatchEdit.prototype.removeRowObjectFromUID = function (uid) {
            var rows = this.parent.getRowsObject();
            var i = 0;
            for (var len = rows.length; i < len; i++) {
                Eif (rows[i].uid === uid) {
                    break;
                }
            }
            rows.splice(i, 1);
        };
        BatchEdit.prototype.addRowObject = function (row) {
            this.parent.getRowsObject().unshift(row);
        };
        BatchEdit.prototype.bulkDelete = function (fieldname, data) {
            var gObj = this.parent;
            var index = data ? this.getIndexFromData(data) : gObj.selectedRowIndex;
            var selectedRows = gObj.getSelectedRows();
            var args = {
                primaryKey: this.parent.getPrimaryKeyFieldNames(),
                rowIndex: index,
                rowData: data ? data : gObj.getSelectedRecords()[0],
                row: data ? gObj.getRows()[index] : selectedRows[0], cancel: false
            };
            Iif (!args.row) {
                return;
            }
            gObj.trigger(events.beforeBatchDelete, args);
            if (args.cancel) {
                return;
            }
            gObj.clearSelection();
            var uid = args.row.getAttribute('data-uid');
            if (args.row.classList.contains('e-insertedrow')) {
                this.removeRowObjectFromUID(uid);
                ej2_base_2.remove(args.row);
            }
            else {
                var rowObj = gObj.getRowObjectFromUID(uid);
                rowObj.isDirty = true;
                rowObj.edit = 'delete';
                ej2_base_2.classList(args.row, ['e-hiddenrow', 'e-updatedtd'], []);
                if (gObj.getFrozenColumns()) {
                    ej2_base_2.classList(data ? gObj.getMovableRows()[index] : selectedRows[1], ['e-hiddenrow', 'e-updatedtd'], []);
                    if (gObj.frozenRows && index < gObj.frozenRows) {
                        gObj.getHeaderContent().querySelector('.e-movableheader').querySelector('tbody')
                            .appendChild(gObj.getMovableRowByIndex(gObj.frozenRows - 1));
                        gObj.getHeaderContent().querySelector('.e-frozenheader').querySelector('tbody')
                            .appendChild(gObj.getRowByIndex(gObj.frozenRows - 1));
                    }
                }
                else Iif (gObj.frozenRows && index < gObj.frozenRows) {
                    gObj.getHeaderContent().querySelector('tbody').appendChild(gObj.getRowByIndex(gObj.frozenRows - 1));
                }
            }
            this.refreshRowIdx();
            gObj.selectRow(index);
            delete args.row;
            gObj.trigger(events.batchDelete, args);
            gObj.notify(events.batchDelete, { rows: this.parent.getRowsObject() });
            gObj.notify(events.toolbarRefresh, {});
        };
        BatchEdit.prototype.refreshRowIdx = function () {
            var rows = [];
            var mRows = [];
            var nonMovableRows = [];
            var frzCols = this.parent.getFrozenColumns();
            if (this.parent.frozenRows) {
                rows = [].slice.call(this.parent.getHeaderTable().querySelector('tbody').children);
                Eif (frzCols) {
                    mRows = [].slice.call(this.parent.getHeaderContent().querySelector('.e-movableheader').querySelector('tbody').children);
                    for (var i = 0; i < mRows.length; i++) {
                        nonMovableRows[i] = ej2_base_2.createElement('tr', { className: 'emptynonmv' });
                    }
                }
            }
            if (frzCols) {
                mRows = mRows.concat([].slice.call(this.parent.getContentTable().querySelector('tbody').children));
                nonMovableRows = nonMovableRows.concat([].slice.call(this.parent.element.querySelector('.e-movablecontent').querySelector('tbody').children));
            }
            rows = rows.concat([].slice.call(this.parent.getContentTable().querySelector('tbody').children));
            for (var i = 0, j = 0, len = rows.length; i < len; i++) {
                if (rows[i].classList.contains('e-row') && !rows[i].classList.contains('e-hiddenrow')) {
                    rows[i].setAttribute('aria-rowindex', j.toString());
                    if (frzCols) {
                        mRows[i].setAttribute('aria-rowindex', j.toString());
                        if (nonMovableRows[i].classList.contains('e-row')) {
                            nonMovableRows[i].setAttribute('aria-rowindex', j.toString());
                        }
                    }
                    j++;
                }
                else {
                    rows[i].removeAttribute('aria-rowindex');
                    if (frzCols) {
                        mRows[i].removeAttribute('aria-rowindex');
                    }
                }
            }
        };
        BatchEdit.prototype.getIndexFromData = function (data) {
            return util_1.inArray(data, this.parent.getCurrentViewRecords());
        };
        BatchEdit.prototype.bulkAddRow = function (data) {
            var gObj = this.parent;
            if (!gObj.editSettings.allowAdding) {
                return;
            }
            if (gObj.isEdit) {
                this.saveCell();
            }
            Iif (gObj.isEdit) {
                return;
            }
            var defaultData = data ? data : this.getDefaultData();
            var args = {
                defaultData: defaultData,
                primaryKey: gObj.getPrimaryKeyFieldNames(),
                cancel: false
            };
            gObj.trigger(events.beforeBatchAdd, args);
            if (args.cancel) {
                return;
            }
            this.isAdded = true;
            gObj.clearSelection();
            var mTr;
            var mTbody;
            var row = new row_renderer_1.RowRenderer(this.serviceLocator, null, this.parent);
            var model = new row_model_generator_1.RowModelGenerator(this.parent);
            var modelData = model.generateRows([args.defaultData]);
            var tr = row.render(modelData[0], gObj.getColumns());
            var col;
            var index;
            for (var i = 0; i < this.parent.groupSettings.columns.length; i++) {
                tr.insertBefore(ej2_base_2.createElement('td', { className: 'e-indentcell' }), tr.firstChild);
                modelData[0].cells.unshift(new cell_1.Cell({ cellType: enum_1.CellType.Indent }));
            }
            var tbody = gObj.getContentTable().querySelector('tbody');
            tr.classList.add('e-insertedrow');
            if (tbody.querySelector('.e-emptyrow')) {
                tbody.querySelector('.e-emptyrow').classList.add('e-hide');
            }
            if (gObj.getFrozenColumns()) {
                mTr = this.renderMovable(tr);
                Eif (gObj.frozenRows) {
                    mTbody = gObj.getHeaderContent().querySelector('.e-movableheader').querySelector('tbody');
                }
                else {
                    mTbody = gObj.getContent().querySelector('.e-movablecontent').querySelector('tbody');
                }
                mTbody.insertBefore(mTr, mTbody.firstChild);
                ej2_base_1.addClass(mTr.querySelectorAll('.e-rowcell'), ['e-updatedtd']);
                Eif (this.parent.height === 'auto') {
                    this.parent.notify(events.frozenHeight, {});
                }
            }
            if (gObj.frozenRows) {
                tbody = gObj.getHeaderContent().querySelector('tbody');
            }
            tbody.insertBefore(tr, tbody.firstChild);
            ej2_base_1.addClass(tr.querySelectorAll('.e-rowcell'), ['e-updatedtd']);
            modelData[0].isDirty = true;
            modelData[0].changes = ej2_base_1.extend({}, modelData[0].data);
            modelData[0].edit = 'add';
            this.addRowObject(modelData[0]);
            this.refreshRowIdx();
            this.focus.forgetPrevious();
            gObj.notify(events.batchAdd, { rows: this.parent.getRowsObject() });
            gObj.selectRow(0);
            if (!data) {
                index = this.findNextEditableCell(0, true);
                col = gObj.getColumns()[index];
                this.editCell(0, col.field, true);
            }
            if (this.parent.aggregates.length > 0 && data) {
                this.parent.notify(events.refreshFooterRenderer, {});
            }
            var args1 = {
                defaultData: args.defaultData, row: tr,
                columnObject: col, columnIndex: index, primaryKey: args.primaryKey, cell: tr.cells[index]
            };
            gObj.trigger(events.batchAdd, args1);
        };
        BatchEdit.prototype.renderMovable = function (ele) {
            var mEle = ele.cloneNode(true);
            for (var i = 0; i < this.parent.frozenColumns; i++) {
                mEle.removeChild(mEle.children[0]);
            }
            for (var i = this.parent.frozenColumns, len = ele.childElementCount; i < len; i++) {
                ele.removeChild(ele.children[ele.childElementCount - 1]);
            }
            return mEle;
        };
        BatchEdit.prototype.findNextEditableCell = function (columnIndex, isAdd, isValOnly) {
            var cols = this.parent.getColumns();
            var endIndex = cols.length;
            var validation;
            for (var i = columnIndex; i < endIndex; i++) {
                validation = isValOnly ? ej2_base_3.isNullOrUndefined(cols[i].validationRules) : false;
                Iif (!isAdd && this.checkNPCell(cols[i])) {
                    return i;
                }
                else if (isAdd && !cols[i].template && cols[i].visible && cols[i].allowEditing &&
                    !(cols[i].isIdentity && cols[i].isPrimaryKey) && !validation) {
                    return i;
                }
            }
            return -1;
        };
        BatchEdit.prototype.checkNPCell = function (col) {
            return !col.template && col.visible && !col.isPrimaryKey && !col.isIdentity && col.allowEditing;
        };
        BatchEdit.prototype.getDefaultData = function () {
            var gObj = this.parent;
            var data = {};
            var dValues = { 'number': 0, 'string': null, 'boolean': false, 'date': null, 'datetime': null };
            for (var _i = 0, _a = gObj.columns; _i < _a.length; _i++) {
                var col = _a[_i];
                ej2_base_1.setValue(col.field, col.defaultValue ? col.defaultValue : dValues[col.type], data);
            }
            return data;
        };
        BatchEdit.prototype.setCellIdx = function (target) {
            var gLen = 0;
            if (this.parent.allowGrouping) {
                gLen = this.parent.groupSettings.columns.length;
            }
            this.cellDetails.cellIndex = target.cellIndex - gLen;
            this.cellDetails.rowIndex = parseInt(target.parentElement.getAttribute('aria-rowindex'), 10);
        };
        BatchEdit.prototype.editCell = function (index, field, isAdd) {
            var gObj = this.parent;
            var col = gObj.getColumnByField(field);
            var keys = gObj.getPrimaryKeyFieldNames();
            if (gObj.editSettings.allowEditing && col.allowEditing) {
                if (gObj.isEdit && !(this.cellDetails.column.field === field
                    && (this.cellDetails.rowIndex === index && this.parent.getDataRows().length - 1 !== index))) {
                    this.saveCell();
                    if (this.cellDetails.rowIndex === index && this.cellDetails.column.field === field) {
                        return;
                    }
                }
                if (gObj.isEdit) {
                    return;
                }
                var row = void 0;
                var rowData = void 0;
                var mRowData = void 0;
                var colIdx = gObj.getColumnIndexByField(field);
                var frzCols = gObj.getFrozenColumns();
                if (frzCols && colIdx >= frzCols) {
                    row = gObj.getMovableDataRows()[index];
                    mRowData = this.parent.getRowObjectFromUID(this.parent.getMovableDataRows()[index].getAttribute('data-uid'));
                    rowData = mRowData.changes ? ej2_base_1.extend({}, mRowData.changes) : ej2_base_1.extend({}, this.getDataByIndex(index));
                }
                else {
                    row = gObj.getDataRows()[index];
                    rowData = ej2_base_1.extend({}, this.getDataByIndex(index));
                }
                if ((keys[0] === col.field && !row.classList.contains('e-insertedrow')) || col.columns ||
                    (col.isPrimaryKey && col.isIdentity)) {
                    this.parent.isLastCellPrimaryKey = true;
                    return;
                }
                this.parent.isLastCellPrimaryKey = false;
                var rowObj = gObj.getRowObjectFromUID(row.getAttribute('data-uid'));
                var cells = [].slice.apply(row.cells);
                var args = {
                    cell: cells[this.getColIndex(cells, this.getCellIdx(col.uid))], row: row,
                    columnName: col.field, columnObject: col, isForeignKey: !ej2_base_3.isNullOrUndefined(col.foreignKeyValue),
                    primaryKey: keys, rowData: rowData,
                    validationRules: ej2_base_1.extend({}, col.validationRules ? col.validationRules : {}),
                    value: util_1.getObject(col.field, rowData),
                    type: !isAdd ? 'edit' : 'add', cancel: false,
                    foreignKeyData: rowObj && rowObj.foreignKeyData
                };
                Iif (!args.cell) {
                    return;
                }
                gObj.trigger(events.cellEdit, args);
                if (args.cancel) {
                    return;
                }
                this.cellDetails = {
                    rowData: rowData, column: col, value: args.value, isForeignKey: args.isForeignKey, rowIndex: index,
                    cellIndex: parseInt(args.cell.getAttribute('aria-colindex'), 10),
                    foreignKeyData: args.foreignKeyData
                };
                if (args.cell.classList.contains('e-updatedtd')) {
                    this.isColored = true;
                    args.cell.classList.remove('e-updatedtd');
                }
                gObj.isEdit = true;
                gObj.clearSelection();
                Eif (!gObj.isCheckBoxSelection || !gObj.isPersistSelection) {
                    gObj.selectRow(this.cellDetails.rowIndex, true);
                }
                this.renderer.update(args);
                this.form = gObj.element.querySelector('#' + gObj.element.id + 'EditForm');
                gObj.editModule.applyFormValidation([col]);
                this.parent.element.querySelector('.e-gridpopup').style.display = 'none';
            }
        };
        BatchEdit.prototype.updateCell = function (rowIndex, field, value) {
            var col = this.parent.getColumnByField(field);
            Eif (col && !col.isPrimaryKey) {
                var td = this.parent.getDataRows()[rowIndex].cells[this.parent.getColumnIndexByField(field)];
                var rowObj = this.parent.getRowObjectFromUID(td.parentElement.getAttribute('data-uid'));
                this.refreshTD(td, col, rowObj, value);
                this.parent.trigger(events.queryCellInfo, {
                    cell: td, column: col, data: rowObj.changes
                });
            }
        };
        BatchEdit.prototype.setChanges = function (rowObj, field, value) {
            if (!rowObj.changes) {
                rowObj.changes = ej2_base_1.extend({}, rowObj.data);
            }
            ej2_base_1.setValue(field, ej2_base_1.isUndefined(value) ? null : value, rowObj.changes);
            if (rowObj.data[field] !== value) {
                rowObj.isDirty = true;
            }
        };
        BatchEdit.prototype.updateRow = function (index, data) {
            var keys = Object.keys(data);
            for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
                var col = keys_1[_i];
                this.updateCell(index, col, data[col]);
            }
        };
        BatchEdit.prototype.getCellIdx = function (uid) {
            var cIdx = this.parent.getColumnIndexByUid(uid) + this.parent.groupSettings.columns.length;
            Iif (!ej2_base_3.isNullOrUndefined(this.parent.detailTemplate) || !ej2_base_3.isNullOrUndefined(this.parent.childGrid)) {
                cIdx++;
            }
            return cIdx;
        };
        BatchEdit.prototype.refreshTD = function (td, column, rowObj, value) {
            var cell = new cell_renderer_1.CellRenderer(this.parent, this.serviceLocator);
            var rowcell;
            this.setChanges(rowObj, column.field, value);
            var frzCols = this.parent.getFrozenColumns();
            util_1.refreshForeignData(rowObj, this.parent.getForeignKeyColumns(), rowObj.changes);
            Iif (frzCols && this.getCellIdx(column.uid) >= frzCols && this.parent.getColumns().length === rowObj.cells.length) {
                rowcell = rowObj.cells.slice(frzCols, rowObj.cells.length);
            }
            else {
                rowcell = rowObj.cells;
            }
            cell.refreshTD(td, rowcell[this.getCellIdx(column.uid) - (this.getCellIdx(column.uid) >= frzCols ? frzCols : 0)], rowObj.changes, { 'index': this.getCellIdx(column.uid) });
            td.classList.add('e-updatedtd');
            this.parent.notify(events.toolbarRefresh, {});
        };
        BatchEdit.prototype.getColIndex = function (cells, index) {
            var cIdx = 0;
            if (this.parent.allowGrouping && this.parent.groupSettings.columns) {
                cIdx = this.parent.groupSettings.columns.length;
            }
            Iif (!ej2_base_3.isNullOrUndefined(this.parent.detailTemplate) || !ej2_base_3.isNullOrUndefined(this.parent.childGrid)) {
                cIdx++;
            }
            for (var m = 0; m < cells.length; m++) {
                var colIndex = parseInt(cells[m].getAttribute('aria-colindex'), 10);
                if (colIndex === index - cIdx) {
                    return m;
                }
            }
            return -1;
        };
        BatchEdit.prototype.editNextValCell = function () {
            var gObj = this.parent;
            if (this.isAdded && !gObj.isEdit) {
                for (var i = this.cellDetails.cellIndex; i < gObj.getColumns().length; i++) {
                    if (gObj.isEdit) {
                        return;
                    }
                    var index = this.findNextEditableCell(this.cellDetails.cellIndex + 1, true, true);
                    var col = gObj.getColumns()[index];
                    if (col) {
                        this.editCell(0, col.field, true);
                        this.saveCell();
                    }
                }
                Eif (!gObj.isEdit) {
                    this.isAdded = false;
                }
            }
        };
        BatchEdit.prototype.saveCell = function (isForceSave) {
            var gObj = this.parent;
            if (!isForceSave && (!gObj.isEdit || this.validateFormObj())) {
                return;
            }
            var tr = util_1.parentsUntil(this.form, 'e-row');
            var column = this.cellDetails.column;
            var editedData = gObj.editModule.getCurrentEditedData(this.form, {});
            var cloneEditedData = ej2_base_1.extend({}, editedData);
            editedData = ej2_base_1.extend({}, editedData, this.cellDetails.rowData);
            var value = util_1.getObject(column.field, cloneEditedData);
            ej2_base_1.setValue(column.field, ej2_base_1.isUndefined(value) ? null : value, editedData);
            var args = {
                columnName: column.field,
                value: util_1.getObject(column.field, editedData),
                rowData: this.cellDetails.rowData,
                previousValue: this.cellDetails.value,
                columnObject: column,
                cell: this.form.parentElement,
                isForeignKey: this.cellDetails.isForeignKey, cancel: false
            };
            if (!isForceSave) {
                gObj.trigger(events.cellSave, args);
            }
            if (args.cancel) {
                return;
            }
            gObj.editModule.destroyForm();
            gObj.isEdit = false;
            gObj.editModule.destroyWidgets([column]);
            this.parent.notify(events.tooltipDestroy, {});
            this.refreshTD(args.cell, column, gObj.getRowObjectFromUID(tr.getAttribute('data-uid')), args.value);
            ej2_base_1.removeClass([tr], ['e-editedrow', 'e-batchrow']);
            ej2_base_1.removeClass([args.cell], ['e-editedbatchcell', 'e-boolcell']);
            if (!ej2_base_3.isNullOrUndefined(args.value) && args.value.toString() ===
                (!ej2_base_3.isNullOrUndefined(this.cellDetails.value) ? this.cellDetails.value : '').toString() && !this.isColored
                || (ej2_base_3.isNullOrUndefined(args.value) && ej2_base_3.isNullOrUndefined(this.cellDetails.value) &&
                    !args.cell.parentElement.classList.contains('e-insertedrow'))) {
                args.cell.classList.remove('e-updatedtd');
            }
            gObj.trigger(events.cellSaved, args);
            gObj.notify(events.toolbarRefresh, {});
            this.isColored = false;
            if (this.parent.aggregates.length > 0) {
                this.parent.notify(events.refreshFooterRenderer, {});
                if (this.parent.groupSettings.columns.length > 0 && !this.isAddRow(this.cellDetails.rowIndex)) {
                    this.parent.notify(events.groupAggregates, {});
                }
            }
        };
        BatchEdit.prototype.getDataByIndex = function (index) {
            var row = this.parent.getRowObjectFromUID(this.parent.getDataRows()[index].getAttribute('data-uid'));
            return row.changes ? row.changes : row.data;
        };
        BatchEdit.prototype.keyDownHandler = function (e) {
            if (e.action === 'tab' && this.parent.isEdit) {
                var rowcell = util_1.parentsUntil(e.target, 'e-rowcell');
                if (rowcell) {
                    var cell = rowcell.querySelector('.e-field');
                    if (cell) {
                        var visibleColumns = this.parent.getVisibleColumns();
                        if (visibleColumns[visibleColumns.length - 1].field === cell.getAttribute('name')) {
                            this.saveCell();
                        }
                    }
                }
            }
        };
        return BatchEdit;
    }());
    exports.BatchEdit = BatchEdit;
});