all files / treegrid/actions/ crud-actions.js

94.74% Statements 216/228
88.11% Branches 200/227
88.89% Functions 8/9
94.71% Lines 215/227
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   391× 391× 391× 391× 391× 391× 391× 391× 391×   391× 391× 391× 391× 157×   391×   119× 119× 119×   391× 31×     360×   391×   351× 391×     391×     391× 391× 9779× 140× 38× 38× 38× 13× 11× 157×   11× 11× 12×           13×       102× 60× 520×     222× 222×   222× 222×           42× 42× 42× 12×           30× 17× 17×     13×         101×     9639× 8009×         391× 9779× 9778× 114×         119× 119× 119× 119× 119× 119×   22× 22× 22×   11× 11× 11×   19× 13× 13×       19×     67× 59× 59×             67×     119×   9322× 9322× 9322× 36419×   138× 35× 35× 35× 582×   155×       155× 18× 18× 18×       35×   103× 37× 11×           11×   11× 11×     26×     20× 20× 20× 15×         66× 66×       36281× 1313×       9322×   95×   45× 45×     50× 50× 1208× 90× 90×   50×       50× 50×   50×     50× 12×     38×   50× 36× 36× 36×                   36× 36× 36× 19×     17× 17×     36× 18×   36×           50× 50×   50× 274× 50× 50×     50× 49× 49× 23× 22×         49× 49×   49× 47× 47×   47×           47×                
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-data", "../utils"], function (require, exports, ej2_base_1, ej2_data_1, utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function editAction(details, control, isSelfReference, addRowIndex, selectedIndex, columnName, addRowRecord) {
        var value = details.value;
        var action = details.action;
        var changedRecords = 'changedRecords';
        var i;
        var j;
        var addedRecords = 'addedRecords';
        var batchChanges;
        var key = control.grid.getPrimaryKeyFieldNames()[0];
        var treeData = control.dataSource instanceof ej2_data_1.DataManager ?
            control.dataSource.dataSource.json : control.dataSource;
        var modifiedData = [];
        var originalData = value;
        var isSkip = false;
        if (control.editSettings.mode === 'Batch') {
            batchChanges = control.grid.editModule.getBatchChanges();
        }
        if (action === 'add' || (action === 'batchsave' && (control.editSettings.mode === 'Batch'
            && batchChanges["" + addedRecords].length))) {
            var addAct = addAction(details, treeData, control, isSelfReference, addRowIndex, selectedIndex, addRowRecord);
            value = addAct.value;
            isSkip = addAct.isSkip;
        }
        if (value instanceof Array) {
            modifiedData = utils_1.extendArray(value);
        }
        else {
            modifiedData.push(ej2_base_1.extend({}, value));
        }
        if (!isSkip && (action !== 'add' ||
            (control.editSettings.newRowPosition !== 'Top' && control.editSettings.newRowPosition !== 'Bottom'))) {
            for (var k = 0; k < modifiedData.length; k++) {
                Iif (typeof (modifiedData[parseInt(k.toString(), 10)]["" + key]) === 'object') {
                    modifiedData[parseInt(k.toString(), 10)] = modifiedData[parseInt(k.toString(), 10)]["" + key];
                }
                var keys = modifiedData[parseInt(k.toString(), 10)].taskData ?
                    Object.keys(modifiedData[parseInt(k.toString(), 10)].taskData) :
                    Object.keys(modifiedData[parseInt(k.toString(), 10)]);
                i = treeData.length;
                var _loop_1 = function () {
                    if (treeData[parseInt(i.toString(), 10)]["" + key] === modifiedData[parseInt(k.toString(), 10)]["" + key]) {
                        if (action === 'delete') {
                            var currentData_1 = treeData[parseInt(i.toString(), 10)];
                            treeData.splice(i, 1);
                            if (isSelfReference) {
                                if (!ej2_base_1.isNullOrUndefined(currentData_1["" + control.parentIdMapping])) {
                                    var parentData = control.flatData.filter(function (e) {
                                        return e["" + control.idMapping] === currentData_1["" + control.parentIdMapping];
                                    })[0];
                                    var childRecords = parentData ? parentData["" + control.childMapping] : [];
                                    for (var p = childRecords.length - 1; p >= 0; p--) {
                                        if (childRecords[parseInt(p.toString(), 10)]["" + control.idMapping] === currentData_1["" + control.idMapping]) {
                                            if (!control.enableImmutableMode && parentData.childRecords.length === parentData['Children'].length) {
                                                parentData['childRecords'].splice(p, 1);
                                            }
                                            childRecords.splice(p, 1);
                                            if (!childRecords.length) {
                                                parentData.hasChildRecords = false;
                                                updateParentRow(key, parentData, action, control, isSelfReference);
                                            }
                                            break;
                                        }
                                    }
                                }
                                return "break";
                            }
                        }
                        else {
                            if (action === 'edit') {
                                for (j = 0; j < keys.length; j++) {
                                    if (Object.prototype.hasOwnProperty.call(treeData[parseInt(i.toString(), 10)], keys[parseInt(j.toString(), 10)]) && ((control.editSettings.mode !== 'Cell'
                                        || (!ej2_base_1.isNullOrUndefined(batchChanges) && batchChanges["" + changedRecords].length === 0))
                                        || keys[parseInt(j.toString(), 10)] === columnName)) {
                                        var editedData = utils_1.getParentData(control, modifiedData[parseInt(k.toString(), 10)].uniqueID);
                                        treeData[parseInt(i.toString(), 10)][keys[parseInt(j.toString(), 10)]] =
                                            modifiedData[parseInt(k.toString(), 10)][keys[parseInt(j.toString(), 10)]];
                                        Eif (editedData && editedData.taskData) {
                                            editedData.taskData[keys[parseInt(j.toString(), 10)]] = editedData[keys[parseInt(j.toString(), 10)]]
                                                = treeData[parseInt(i.toString(), 10)][keys[parseInt(j.toString(), 10)]];
                                        }
                                    }
                                }
                            }
                            else Eif (action === 'add' || action === 'batchsave') {
                                var index = void 0;
                                if (control.editSettings.newRowPosition === 'Child') {
                                    if (isSelfReference) {
                                        originalData.taskData["" + control.parentIdMapping] = treeData[parseInt(i.toString(), 10)]["" + control.idMapping];
                                        treeData.splice(i + 1, 0, originalData.taskData);
                                    }
                                    else {
                                        if (!Object.prototype.hasOwnProperty.call(treeData[parseInt(i.toString(), 10)], control.childMapping)) {
                                            treeData[parseInt(i.toString(), 10)]["" + control.childMapping] = [];
                                        }
                                        treeData[parseInt(i.toString(), 10)]["" + control.childMapping].push(originalData.taskData);
                                        updateParentRow(key, treeData[parseInt(i.toString(), 10)], action, control, isSelfReference, originalData);
                                    }
                                }
                                else if (control.editSettings.newRowPosition === 'Below') {
                                    treeData.splice(i + 1, 0, originalData.taskData);
                                    if (!ej2_base_1.isNullOrUndefined(originalData.parentItem)) {
                                        updateParentRow(key, treeData[i + 1], action, control, isSelfReference, originalData);
                                    }
                                }
                                else if (!addRowIndex) {
                                    index = 0;
                                    treeData.splice(index, 0, originalData.taskData);
                                }
                                else Eif (control.editSettings.newRowPosition === 'Above') {
                                    treeData.splice(i, 0, originalData.taskData);
                                    if (!ej2_base_1.isNullOrUndefined(originalData.parentItem)) {
                                        updateParentRow(key, treeData[parseInt(i.toString(), 10)], action, control, isSelfReference, originalData);
                                    }
                                }
                            }
                            return "break";
                        }
                    }
                    else if (!ej2_base_1.isNullOrUndefined(treeData[parseInt(i.toString(), 10)]["" + control.childMapping])) {
                        Iif (removeChildRecords(treeData[parseInt(i.toString(), 10)]["" + control.childMapping], modifiedData[parseInt(k.toString(), 10)], action, key, control, isSelfReference, originalData, columnName)) {
                            updateParentRow(key, treeData[parseInt(i.toString(), 10)], action, control, isSelfReference);
                        }
                    }
                };
                while (i-- && i >= 0) {
                    var state_1 = _loop_1();
                    if (state_1 === "break")
                        break;
                }
            }
        }
    }
    exports.editAction = editAction;
    function addAction(details, treeData, control, isSelfReference, addRowIndex, selectedIndex, addRowRecord) {
        var value;
        var isSkip = false;
        var currentViewRecords = control.grid.getCurrentViewRecords();
        value = ej2_base_1.extend({}, details.value);
        value = utils_1.getPlainData(value);
        switch (control.editSettings.newRowPosition) {
            case 'Top':
                treeData.unshift(value);
                isSkip = true;
                break;
            case 'Bottom':
                treeData.push(value);
                isSkip = true;
                break;
            case 'Above':
                if (!ej2_base_1.isNullOrUndefined(addRowRecord)) {
                    value = ej2_base_1.extend({}, addRowRecord);
                    value = utils_1.getPlainData(value);
                }
                else {
                    value = ej2_base_1.extend({}, currentViewRecords[addRowIndex + 1]);
                    value = utils_1.getPlainData(value);
                }
                break;
            case 'Below':
            case 'Child':
                if (!ej2_base_1.isNullOrUndefined(addRowRecord)) {
                    value = ej2_base_1.extend({}, addRowRecord);
                    value = utils_1.getPlainData(value);
                }
                else {
                    var primaryKeys = control.grid.getPrimaryKeyFieldNames()[0];
                    var currentdata = currentViewRecords[parseInt(addRowIndex.toString(), 10)];
                    if (!ej2_base_1.isNullOrUndefined(currentdata) && currentdata["" + primaryKeys] === details.value["" + primaryKeys] || selectedIndex !== -1) {
                        value = ej2_base_1.extend({}, currentdata);
                    }
                    else {
                        value = ej2_base_1.extend({}, details.value);
                    }
                    value = utils_1.getPlainData(value);
                    var internalProperty = 'internalProperties';
                    control.editModule["" + internalProperty].taskData = value;
                }
                if (selectedIndex === -1) {
                    treeData.unshift(value);
                    isSkip = true;
                }
        }
        return { value: value, isSkip: isSkip };
    }
    exports.addAction = addAction;
    function removeChildRecords(childRecords, modifiedData, action, key, control, isSelfReference, originalData, columnName) {
        var isChildAll = false;
        var j = childRecords.length;
        while (j-- && j >= 0) {
            if (childRecords[parseInt(j.toString(), 10)]["" + key] === modifiedData["" + key] ||
                (isSelfReference && childRecords[parseInt(j.toString(), 10)][control.parentIdMapping] === modifiedData[control.idMapping])) {
                if (action === 'edit') {
                    var keys = Object.keys(modifiedData);
                    var editedData = utils_1.getParentData(control, modifiedData.uniqueID);
                    for (var i = 0; i < keys.length; i++) {
                        if (Object.prototype.hasOwnProperty.call(childRecords[parseInt(j.toString(), 10)], keys[parseInt(i.toString(), 10)]) &&
                            (control.editSettings.mode !== 'Cell' || keys[parseInt(i.toString(), 10)] === columnName)) {
                            editedData[keys[parseInt(i.toString(), 10)]] =
                                editedData.taskData[keys[parseInt(i.toString(), 10)]] =
                                    childRecords[parseInt(j.toString(), 10)][keys[parseInt(i.toString(), 10)]] =
                                        modifiedData[keys[parseInt(i.toString(), 10)]];
                            if (control.grid.editSettings.mode === 'Normal' && control.editSettings.mode === 'Cell' && !ej2_base_1.isNullOrUndefined(control.grid.editModule)) {
                                var editModule = 'editModule';
                                control.grid.editModule["" + editModule].editRowIndex = modifiedData.index;
                                control.grid.editModule["" + editModule].updateCurrentViewData(modifiedData);
                            }
                        }
                    }
                    break;
                }
                else if (action === 'add' || action === 'batchsave') {
                    if (control.editSettings.newRowPosition === 'Child') {
                        Iif (isSelfReference) {
                            originalData["" + control.parentIdMapping] = childRecords[parseInt(j.toString(), 10)][control.idMapping];
                            childRecords.splice(j + 1, 0, originalData);
                            updateParentRow(key, childRecords[parseInt(j.toString(), 10)], action, control, isSelfReference, originalData);
                        }
                        else {
                            if (!Object.prototype.hasOwnProperty.call(childRecords[parseInt(j.toString(), 10)], control.childMapping)) {
                                childRecords[parseInt(j.toString(), 10)][control.childMapping] = [];
                            }
                            childRecords[parseInt(j.toString(), 10)][control.childMapping].push(originalData.taskData);
                            updateParentRow(key, childRecords[parseInt(j.toString(), 10)], action, control, isSelfReference, originalData);
                        }
                    }
                    else if (control.editSettings.newRowPosition === 'Above') {
                        childRecords.splice(j, 0, originalData.taskData);
                        if (!ej2_base_1.isNullOrUndefined(originalData.parentItem)) {
                            updateParentRow(key, childRecords[parseInt(j.toString(), 10)], action, control, isSelfReference, originalData);
                        }
                    }
                    else Eif (control.editSettings.newRowPosition === 'Below') {
                        childRecords.splice(j + 1, 0, originalData.taskData);
                        if (!ej2_base_1.isNullOrUndefined(originalData.parentItem)) {
                            updateParentRow(key, childRecords[parseInt(j.toString(), 10)], action, control, isSelfReference, originalData);
                        }
                    }
                }
                else {
                    childRecords.splice(j, 1);
                    if (!childRecords.length) {
                        isChildAll = true;
                    }
                }
            }
            else if (!ej2_base_1.isNullOrUndefined(childRecords[parseInt(j.toString(), 10)][control.childMapping])) {
                if (removeChildRecords(childRecords[parseInt(j.toString(), 10)][control.childMapping], modifiedData, action, key, control, isSelfReference, originalData, columnName)) {
                    updateParentRow(key, childRecords[parseInt(j.toString(), 10)], action, control, isSelfReference);
                }
            }
        }
        return isChildAll;
    }
    exports.removeChildRecords = removeChildRecords;
    function updateParentRow(key, record, action, control, isSelfReference, child) {
        if ((control.editSettings.newRowPosition === 'Above' || control.editSettings.newRowPosition === 'Below')
            && ((action === 'add' || action === 'batchsave')) && !ej2_base_1.isNullOrUndefined(child.parentItem)) {
            var parentData = utils_1.getParentData(control, child.parentItem.uniqueID);
            parentData.childRecords.push(child);
        }
        else {
            var currentRecords = control.grid.getCurrentViewRecords();
            var index_1;
            currentRecords.map(function (e, i) { if (e["" + key] === record["" + key]) {
                index_1 = i;
                return;
            } });
            Iif (control.enableVirtualization && ej2_base_1.isNullOrUndefined(index_1)) {
                var updatedParent = ej2_base_1.getValue('uniqueIDCollection.' + child.parentUniqueID, control);
                record = updatedParent;
            }
            Eif (!ej2_base_1.isNullOrUndefined(index_1)) {
                record = currentRecords[parseInt(index_1.toString(), 10)];
            }
            Iif (control.enableVirtualization && ej2_base_1.isNullOrUndefined(record) && !ej2_base_1.isNullOrUndefined(child)) {
                record = ej2_base_1.getValue('uniqueIDCollection.' + child.parentUniqueID, control);
            }
            if (!isSelfReference && !ej2_base_1.isNullOrUndefined(record.childRecords) && record.childRecords.length) {
                record.hasChildRecords = true;
            }
            else {
                record.hasChildRecords = false;
            }
            if (action === 'add' || action === 'batchsave') {
                record.expanded = true;
                record.hasChildRecords = true;
                Iif (control.sortSettings.columns.length && ej2_base_1.isNullOrUndefined(child)) {
                    child = currentRecords.filter(function (e) {
                        if (e.parentUniqueID === record.uniqueID) {
                            return e;
                        }
                        else {
                            return null;
                        }
                    });
                }
                var childRecords = child ? child instanceof Array ? child[0] : child : currentRecords[index_1 + 1];
                Eif (control.editSettings.newRowPosition !== 'Below') {
                    if (!Object.prototype.hasOwnProperty.call(record, 'childRecords')) {
                        record.childRecords = [];
                    }
                    else {
                        Eif (!ej2_base_1.isNullOrUndefined(child) && record["" + key] !== child["" + key]) {
                            record.childRecords.push(child);
                        }
                    }
                    if (record.childRecords.indexOf(childRecords) === -1 && record["" + key] !== child["" + key]) {
                        record.childRecords.unshift(childRecords);
                    }
                    if (isSelfReference) {
                        if (!Object.prototype.hasOwnProperty.call(record, control.childMapping)) {
                            record[control.childMapping] = [];
                        }
                        if (record["" + control.childMapping].indexOf(childRecords) === -1 && record["" + key] !== child["" + key]) {
                            record[control.childMapping].unshift(childRecords);
                        }
                    }
                }
            }
            var primaryKeys = control.grid.getPrimaryKeyFieldNames()[0];
            var data = control.grid.dataSource instanceof ej2_data_1.DataManager ?
                control.grid.dataSource.dataSource.json : control.grid.dataSource;
            for (var i = 0; i < data.length; i++) {
                if (data[parseInt(i.toString(), 10)]["" + primaryKeys] === record["" + primaryKeys]) {
                    data[parseInt(i.toString(), 10)] = record;
                    break;
                }
            }
            control.grid.setRowData(key, record);
            var row = control.getRowByIndex(index_1);
            if (control.editSettings.mode === 'Batch') {
                if (action === 'add') {
                    row = control.getRows()[control.grid.getCurrentViewRecords().indexOf(record)];
                }
                else {
                    row = control.getRows()[control.grid.getRowIndexByPrimaryKey(record["" + key])];
                }
            }
            var movableRow = void 0;
            if (control.frozenRows || control.getFrozenColumns()) {
                movableRow = control.getRowByIndex(index_1);
            }
            if (!control.enableVirtualization && !ej2_base_1.isNullOrUndefined(row) || !ej2_base_1.isNullOrUndefined(movableRow)) {
                var index_2 = control.treeColumnIndex;
                if (control.allowRowDragAndDrop && control.enableImmutableMode) {
                    index_2 = index_2 + 1;
                }
                control.renderModule.cellRender({
                    data: record, cell: row.cells[parseInt(index_2.toString(), 10)] ? row.cells[parseInt(index_2.toString(), 10)]
                        : movableRow.cells[index_2 - control.getFrozenColumns()],
                    column: control.grid.getColumns()[control.treeColumnIndex],
                    requestType: action
                });
                if (control.enableImmutableMode && control['action'] === 'indenting' || control['action'] === 'outdenting') {
                    control.renderModule.RowModifier({
                        data: record, row: row
                    });
                }
            }
        }
    }
    exports.updateParentRow = updateParentRow;
});