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

98.42% Statements 187/190
86.03% Branches 117/136
100% Functions 29/29
98.4% Lines 184/187
11 statements, 6 functions, 9 branches Ignored     
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                              11×                         19×   21× 21× 21× 21× 21×   20× 20× 20×     19× 19× 19× 19× 19×               19× 19× 19× 19× 19×   18× 18× 18× 18×         18× 18× 18× 18× 18×   18× 18× 18× 18× 18× 18× 18× 18×     11× 11× 11×       11× 11×   10× 10× 10× 10×   10× 10× 10× 10×     15× 15× 15×     15× 15× 15× 15×                           15× 15×   14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14×     14× 14× 14× 14× 14× 14×   14×           13×           14×       13×   14× 14×   15× 15×                                           11×        
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../base/util", "../base/constant", "../base/string-literals", "./batch-edit", "./data", "./normal-edit"], function (require, exports, ej2_base_1, ej2_base_2, util_1, events, literals, batch_edit_1, data_1, normal_edit_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var CellEdit = (function (_super) {
        __extends(CellEdit, _super);
        function CellEdit(parent, serviceLocator, renderer) {
            var _this = _super.call(this, parent, serviceLocator, renderer) || this;
            _this.data = new data_1.Data(parent, serviceLocator);
            _this.renderer = renderer;
            _this.cellEditModule = new normal_edit_1.NormalEdit(parent, serviceLocator, renderer);
            _this.addEventListener();
            return _this;
        }
        CellEdit.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.evtHandlers = [
                { event: events.click, handler: this.clickHandler },
                { event: events.dblclick, handler: this.dblClickHandler },
                { event: events.cellFocused, handler: _super.prototype.onCellFocused },
                { event: events.destroy, handler: this.destroy },
                { event: events.deleteComplete, handler: this.editComplete },
                { event: events.saveComplete, handler: this.editComplete }
            ];
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, true, this);
        };
        CellEdit.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, false);
        };
        CellEdit.prototype.clickHandler = function (e) {
            var target = e.target;
            Eif (!util_1.parentsUntil(target, this.parent.element.id + '_add', true)) {
                Eif (util_1.parentsUntil(target, literals.gridContent) &&
                    util_1.parentsUntil(util_1.parentsUntil(target, literals.gridContent), 'e-grid').id === this.parent.element.id
                    && !util_1.parentsUntil(target, 'e-unboundcelldiv')) {
                    if (this.parent.isEdit) {
                        this.endEdit();
                    }
                }
            }
        };
        CellEdit.prototype.dblClickHandler = function (e) {
            var target = util_1.parentsUntil(e.target, literals.rowCell);
            var tr = util_1.parentsUntil(e.target, literals.row);
            var rowIndex = tr && parseInt(tr.getAttribute(literals.ariaRowIndex), 10) - 1;
            var colIndex = target && parseInt(target.getAttribute(literals.ariaColIndex), 10) - 1;
            if (!ej2_base_2.isNullOrUndefined(target) && !ej2_base_2.isNullOrUndefined(rowIndex) && !isNaN(colIndex)
                && !target.parentElement.classList.contains(literals.editedRow) &&
                this.parent.getColumns()[parseInt(colIndex.toString(), 10)].allowEditing) {
                this.editCell(rowIndex, this.parent.getColumns()[parseInt(colIndex.toString(), 10)].field);
            }
        };
        CellEdit.prototype.editCell = function (rowIndex, field) {
            _super.prototype.editCell.call(this, rowIndex, field);
        };
        CellEdit.prototype.editCellExtend = function (rowIndex, field) {
            var _this = this;
            var gObj = this.parent;
            var column = gObj.getColumnByField(field);
            var keys = gObj.getPrimaryKeyFieldNames();
            if (gObj.isEdit) {
                return;
            }
            var rowData = ej2_base_1.extend({}, {}, this.getDataByIndex(rowIndex), true);
            var row = gObj.getRowByIndex(rowIndex);
            if (keys[0] === column.field || column.columns ||
                (column.isPrimaryKey && column.isIdentity) || column.commands) {
                this.parent.isLastCellPrimaryKey = true;
                return;
            }
            this.parent.isLastCellPrimaryKey = false;
            this.parent.element.classList.add('e-editing');
            var rowObj = gObj.getRowObjectFromUID(row.getAttribute('data-uid'));
            var cells = [].slice.apply(row.cells);
            var args = {
                columnName: column.field, isForeignKey: !ej2_base_2.isNullOrUndefined(column.foreignKeyValue),
                primaryKey: keys, rowData: rowData,
                validationRules: ej2_base_1.extend({}, column.validationRules ? column.validationRules : {}),
                value: util_1.getObject(column.field, rowData), requestType: 'beginEdit',
                type: 'edit', cancel: false,
                foreignKeyData: rowObj && rowObj.foreignKeyData
            };
            args.cell = cells[this.getColIndex(cells, this.getCellIdx(column.uid))];
            args.row = row;
            args.columnObject = column;
            gObj.trigger(events.actionBegin, args, function (cellEditArgs) {
                if (cellEditArgs.cancel) {
                    return;
                }
                cellEditArgs.cell = cellEditArgs.cell ? cellEditArgs.cell : cells[_this.getColIndex(cells, _this.getCellIdx(column.uid))];
                cellEditArgs.row = cellEditArgs.row ? cellEditArgs.row : row;
                cellEditArgs.columnObject = cellEditArgs.columnObject ? cellEditArgs.columnObject : column;
                _this.cellDetails = {
                    rowData: rowData, column: column, value: cellEditArgs.value, isForeignKey: cellEditArgs.isForeignKey, rowIndex: rowIndex,
                    cellIndex: parseInt(cellEditArgs.cell.getAttribute(literals.ariaColIndex), 10) - 1,
                    foreignKeyData: cellEditArgs.foreignKeyData
                };
                gObj.isEdit = true;
                var checkSelect = !ej2_base_2.isNullOrUndefined(cellEditArgs.row.querySelector('.e-selectionbackground')) ? true : false;
                gObj.clearSelection();
                Eif ((!gObj.isCheckBoxSelection || !gObj.isPersistSelection) && (checkSelect || !gObj.selectionSettings.checkboxOnly)) {
                    gObj.selectRow(_this.cellDetails.rowIndex, true);
                }
                _this.renderer.update(cellEditArgs);
                _this.parent.notify(events.batchEditFormRendered, cellEditArgs);
                _this.form = ej2_base_1.select('#' + gObj.element.id + 'EditForm', gObj.element);
                _this.args = cellEditArgs;
                gObj.editModule.applyFormValidation([column]);
                _this.parent.element.querySelector('.e-gridpopup').style.display = 'none';
                _this.parent.notify(events.toolbarRefresh, {});
                gObj.trigger(events.actionComplete, cellEditArgs);
            });
        };
        CellEdit.prototype.updateCell = function (rowIndex, field, value) {
            var _this = this;
            var rowElement = this.parent.getRowByIndex(rowIndex);
            var args = {
                requestType: 'save', action: 'edit', type: events.actionBegin, cancel: false,
                rowData: this.parent.getCurrentViewRecords()[parseInt(rowIndex.toString(), 10)], row: rowElement
            };
            this.parent.trigger(events.actionBegin, args, function (updateCellArgs) {
                if (updateCellArgs.cancel) {
                    return;
                }
                _super.prototype.updateCell.call(_this, rowIndex, field, value);
                var rowObj = _this.parent.getRowObjectFromUID(rowElement.getAttribute('data-uid'));
                Eif (rowObj && 'changes' in rowObj) {
                    delete rowObj.changes;
                }
                ej2_base_1.setValue(field, value, args.rowData);
                _this.parent.notify(events.updateData, args);
                args.type = events.actionComplete;
                _this.parent.trigger(events.actionComplete, args);
            });
        };
        CellEdit.prototype.saveCell = function () {
            var _this = this;
            var gObj = this.parent;
            Iif (this.parent.isEdit && this.validateFormObj()) {
                return;
            }
            var cellSaveArgs = _super.prototype.generateCellArgs.call(this);
            var rowIndex = this.cellDetails.rowIndex;
            var columnName = cellSaveArgs.columnName;
            var saveArgs = ej2_base_1.extend({}, {
                requestType: 'save',
                type: events.actionBegin,
                data: cellSaveArgs.rowData,
                cancel: false,
                previousData: cellSaveArgs.rowData,
                selectedRow: gObj.selectedRowIndex,
                action: 'edit',
                cell: cellSaveArgs.cell,
                columnName: columnName,
                foreignKeyData: {},
                rowIndex: rowIndex,
                index: rowIndex
            });
            gObj.trigger(events.actionBegin, saveArgs, function (updateArgs) {
                if (updateArgs.cancel) {
                    return;
                }
                ej2_base_1.setValue(columnName, cellSaveArgs.value, cellSaveArgs.rowData);
                gObj.isEdit = false;
                gObj.element.classList.remove('e-editing');
                var rowElement = cellSaveArgs.cell.parentElement;
                var column = _this.cellDetails.column;
                Eif (rowElement) {
                    var cellElement = cellSaveArgs.cell;
                    Eif (!ej2_base_2.isNullOrUndefined(cellElement)) {
                        cellElement.innerHTML = '';
                        var displayValue = String(cellSaveArgs.value || '');
                        cellElement.textContent = displayValue;
                        ej2_base_1.removeClass([rowElement], [literals.editedRow]);
                        ej2_base_1.removeClass([cellElement], ['e-editedcell', 'e-boolcell']);
                    }
                }
                gObj.notify(events.updateData, updateArgs);
                gObj.editModule.destroyWidgets([column]);
                gObj.editModule.destroyForm();
                var rowObj = gObj.getRowObjectFromUID(rowElement.getAttribute('data-uid'));
                _this.refreshTD(cellSaveArgs.cell, column, rowObj, cellSaveArgs.value);
                var isReactChild = _this.parent.parentDetails && _this.parent.parentDetails.parentInstObj &&
                    _this.parent.parentDetails.parentInstObj.isReact;
                if (((_this.parent.isReact && _this.parent.requireTemplateRef) || (isReactChild &&
                    _this.parent.parentDetails.parentInstObj.requireTemplateRef)) && column.template) {
                    var thisRef_1 = _this;
                    var newReactTd_1 = _this.newReactTd;
                    thisRef_1.parent.renderTemplates(function () {
                        thisRef_1.parent.trigger(events.queryCellInfo, {
                            cell: newReactTd_1 || cellSaveArgs.cell, column: column, data: cellSaveArgs.rowData
                        });
                    });
                }
                else Iif ((_this.parent.isReact || isReactChild) && column.template) {
                    _this.parent.renderTemplates();
                    _this.parent.trigger(events.queryCellInfo, {
                        cell: _this.newReactTd || cellSaveArgs.cell, column: column, data: cellSaveArgs.rowData
                    });
                }
                if (!_this.parent.groupSettings.enableLazyLoading && _this.parent.aggregates.length &&
                    (_this.parent.enableInfiniteScrolling || _this.parent.enableVirtualization ||
                        (_this.parent.allowPaging && _this.parent.groupSettings.disablePageWiseAggregates))) {
                    _this.parent.notify(events.modelChanged, { requestType: 'refresh-aggregate-on-save', action: 'update' });
                }
                else if (_this.parent.aggregates.length) {
                    _this.parent.aggregateModule.refresh(cellSaveArgs.rowData, _this.parent.groupSettings.enableLazyLoading ? rowElement : undefined);
                }
                saveArgs.type = events.actionComplete;
                gObj.trigger(events.actionComplete, saveArgs);
            });
            this.preventSaveCell = false;
            if (this.editNext) {
                this.editNext = false;
                if (this.cellDetails.rowIndex === this.index && this.cellDetails.column.field === this.field && this.prevEditedBatchCell) {
                    return;
                }
                var column = gObj.getColumnByField(this.field);
                Eif (column && column.allowEditing) {
                    this.editCellExtend(this.index, this.field);
                }
            }
        };
        CellEdit.prototype.endEdit = function () {
            if (this.parent.isEdit && this.validateFormObj()) {
                return;
            }
            if ([].slice.call(this.parent.element.getElementsByClassName(literals.addedRow)).length) {
                this.cellEditModule.endEdit();
            }
            else {
                this.saveCell();
            }
        };
        CellEdit.prototype.closeEdit = function () {
            if ([].slice.call(this.parent.element.getElementsByClassName(literals.addedRow)).length) {
                this.cellEditModule.closeEdit();
            }
            else {
                this.closeCellEdit();
            }
        };
        CellEdit.prototype.closeCellEdit = function () {
            var _this = this;
            var gObj = this.parent;
            var args = ej2_base_1.extend(this.args, {
                requestType: 'cancel', type: events.actionBegin, cancel: false, data: this.cellDetails.rowData, selectedRow: gObj.selectedRowIndex
            });
            gObj.notify(events.virtualScrollEditCancel, args);
            gObj.trigger(events.actionBegin, args, function (closeEditArgs) {
                if (closeEditArgs.cancel) {
                    return;
                }
                closeEditArgs.type = events.actionComplete;
                var rowObj = _this.parent.getRowObjectFromUID(_this.args.row.getAttribute('data-uid'));
                _this.removeBatchElementChanges(rowObj, true);
                _this.refreshRowIdx();
                gObj.isEdit = false;
                gObj.trigger(events.actionComplete, closeEditArgs);
            });
        };
        CellEdit.prototype.addRecord = function (data, index) {
            this.cellEditModule.addRecord(data, index);
        };
        CellEdit.prototype.deleteRecord = function (fieldname, data) {
            this.cellEditModule.deleteRecord(fieldname, data);
        };
        CellEdit.prototype.editComplete = function (e) {
            this.cellEditModule.editComplete(e);
        };
        CellEdit.prototype.destroy = function () {
            this.removeEventListener();
        };
        return CellEdit;
    }(batch_edit_1.BatchEdit));
    exports.CellEdit = CellEdit;
});