UNPKG

@syncfusion/ej2-treegrid

Version:
412 lines (411 loc) 22.4 kB
import { isNullOrUndefined, extend, getValue } from '@syncfusion/ej2-base'; import { DataManager } from '@syncfusion/ej2-data'; import { extendArray, getPlainData, getParentData } from '../utils'; /** * Performs CRUD update to Tree Grid data source * * @param {{value: ITreeData, action: string }} details - Gets modified record value and CRUD action type * @param {TreeGrid} details.value - Gets modified record value * @param {string} details.action - CRUD action type * @param {TreeGrid} control - Tree Grid instance * @param {boolean} isSelfReference - Denotes whether Self Referential data binding * @param {number} addRowIndex - New add row index * @param {number} selectedIndex - Selected Row index * @param {string} columnName - Column field name * @param {ITreeData} addRowRecord - Newly added record * @returns {void} */ export 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 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 = extendArray(value); } else { modifiedData.push(extend({}, value)); } if (!isSkip && (action !== 'add' || (control.editSettings.newRowPosition !== 'Top' && control.editSettings.newRowPosition !== 'Bottom'))) { for (var k = 0; k < modifiedData.length; k++) { if (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 (!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' || (!isNullOrUndefined(batchChanges) && batchChanges["" + changedRecords].length === 0)) || keys[parseInt(j.toString(), 10)] === columnName)) { var editedData = 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)]]; if (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 if (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 (!isNullOrUndefined(originalData.parentItem)) { updateParentRow(key, treeData[i + 1], action, control, isSelfReference, originalData); } } else if (!addRowIndex) { index = 0; treeData.splice(index, 0, originalData.taskData); } else if (control.editSettings.newRowPosition === 'Above') { treeData.splice(i, 0, originalData.taskData); if (!isNullOrUndefined(originalData.parentItem)) { updateParentRow(key, treeData[parseInt(i.toString(), 10)], action, control, isSelfReference, originalData); } } } return "break"; } } else if (!isNullOrUndefined(treeData[parseInt(i.toString(), 10)]["" + control.childMapping])) { if (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; } } } } /** * Performs Add action to Tree Grid data source * * @param {{value: ITreeData, action: string }} details - Gets modified record value and CRUD action type * @param {TreeGrid} details.value - Gets modified record value * @param {string} details.action - CRUD action type * @param {Object[]} treeData - Tree Grid data source * @param {TreeGrid} control - Tree Grid instance * @param {boolean} isSelfReference - Denotes whether Self Referential data binding * @param {number} addRowIndex - New add row index * @param {number} selectedIndex - Selected Row index * @param {ITreeData} addRowRecord - Newly added record * @returns {void} */ export function addAction(details, treeData, control, isSelfReference, addRowIndex, selectedIndex, addRowRecord) { var value; var isSkip = false; var currentViewRecords = control.grid.getCurrentViewRecords(); value = extend({}, details.value); value = 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 (!isNullOrUndefined(addRowRecord)) { value = extend({}, addRowRecord); value = getPlainData(value); } else { value = extend({}, currentViewRecords[addRowIndex + 1]); value = getPlainData(value); } break; case 'Below': case 'Child': if (!isNullOrUndefined(addRowRecord)) { value = extend({}, addRowRecord); value = getPlainData(value); } else { var primaryKeys = control.grid.getPrimaryKeyFieldNames()[0]; var currentdata = currentViewRecords[parseInt(addRowIndex.toString(), 10)]; if (!isNullOrUndefined(currentdata) && currentdata["" + primaryKeys] === details.value["" + primaryKeys] || selectedIndex !== -1) { value = extend({}, currentdata); } else { value = extend({}, details.value); } value = getPlainData(value); var internalProperty = 'internalProperties'; control.editModule["" + internalProperty].taskData = value; } if (selectedIndex === -1) { treeData.unshift(value); isSkip = true; } } return { value: value, isSkip: isSkip }; } /** * @param {ITreeData[]} childRecords - Child Records collection * @param {Object} modifiedData - Modified data in crud action * @param {string} action - crud action type * @param {string} key - Primary key field name * @param {TreeGrid} control - Tree Grid instance * @param {boolean} isSelfReference - Specified whether Self Referential data binding * @param {ITreeData} originalData - Non updated data from data source, of edited data * @param {string} columnName - column field name * @returns {boolean} Returns whether child records exists */ export 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 = 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' && !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') { if (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 (!isNullOrUndefined(originalData.parentItem)) { updateParentRow(key, childRecords[parseInt(j.toString(), 10)], action, control, isSelfReference, originalData); } } else if (control.editSettings.newRowPosition === 'Below') { childRecords.splice(j + 1, 0, originalData.taskData); if (!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 (!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; } /** * @param {string} key - Primary key field name * @param {ITreeData} record - Parent Record which has to be updated * @param {string} action - CRUD action type * @param {TreeGrid} control - Tree Grid instance * @param {boolean} isSelfReference - Specified whether self referential data binding * @param {ITreeData} child - Specifies child record * @returns {void} */ export function updateParentRow(key, record, action, control, isSelfReference, child) { if ((control.editSettings.newRowPosition === 'Above' || control.editSettings.newRowPosition === 'Below') && ((action === 'add' || action === 'batchsave')) && !isNullOrUndefined(child.parentItem)) { var parentData = 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; } }); if (control.enableVirtualization && isNullOrUndefined(index_1)) { var updatedParent = getValue('uniqueIDCollection.' + child.parentUniqueID, control); record = updatedParent; } if (!isNullOrUndefined(index_1)) { record = currentRecords[parseInt(index_1.toString(), 10)]; } if (control.enableVirtualization && isNullOrUndefined(record) && !isNullOrUndefined(child)) { record = getValue('uniqueIDCollection.' + child.parentUniqueID, control); } if (!isSelfReference && !isNullOrUndefined(record.childRecords) && record.childRecords.length) { record.hasChildRecords = true; } else { record.hasChildRecords = false; } if (action === 'add' || action === 'batchsave') { record.expanded = true; record.hasChildRecords = true; if (control.sortSettings.columns.length && 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]; if (control.editSettings.newRowPosition !== 'Below') { if (!Object.prototype.hasOwnProperty.call(record, 'childRecords')) { record.childRecords = []; } else { if (!isNullOrUndefined(child) && record["" + key] !== child["" + key]) { if (!record.childRecords.some(function (records) { return records.uniqueID === child.uniqueID; })) { 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 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 && !isNullOrUndefined(row) || !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 }); } } } }