@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
138 lines (137 loc) • 6.39 kB
JavaScript
import { AdaptableModuleBase } from './AdaptableModuleBase';
import * as ModuleConstants from '../Utilities/Constants/ModuleConstants';
import { PreviewHelper } from '../Utilities/Helpers/PreviewHelper';
import StringExtensions from '../Utilities/Extensions/StringExtensions';
import ArrayExtensions from '../Utilities/Extensions/ArrayExtensions';
import ObjectFactory from '../Utilities/ObjectFactory';
import { ACCESS_LEVEL_FULL } from '../Utilities/Constants/GeneralConstants';
export class BulkUpdateModule extends AdaptableModuleBase {
constructor(api) {
super(ModuleConstants.BulkUpdateModuleId, ModuleConstants.BulkUpdateFriendlyName, 'edit-table', 'BulkUpdatePopup', 'Update multiple cell simultaneously with a new or existing value', api);
}
getViewAccessLevel() {
return ACCESS_LEVEL_FULL;
}
createContextMenuItems(menuContext) {
let menuItemShowPopup = undefined;
if (!menuContext.isRowGroupColumn && this.isModuleEditable()) {
if (menuContext.adaptableColumn &&
menuContext.isSelectedCell &&
menuContext.isSingleSelectedColumn &&
this.api.gridApi.isEveryCellEditable(menuContext.selectedCellInfo.gridCells)) {
let popUpParams = {
source: 'ContextMenu',
};
menuItemShowPopup = this.createMainMenuItemShowPopup({
Name: 'bulk-update-apply',
Label: 'Apply Bulk Update',
ComponentName: this.moduleInfo.Popup,
Icon: this.moduleInfo.Glyph,
PopupParams: popUpParams,
});
}
}
return [menuItemShowPopup];
}
checkCorrectCellSelection() {
let selectedCellInfo = this.api.gridApi.getSelectedCellInfo();
if (this.api.layoutApi.isCurrentLayoutPivot()) {
return {
IsValid: false,
Alert: {
alertType: 'generic',
header: 'Bulk Update',
message: 'Editing is not available in Pivot Mode.',
alertDefinition: ObjectFactory.CreateInternalAlertDefinitionForMessages('Error'),
},
};
}
if (selectedCellInfo == null || ArrayExtensions.IsNullOrEmpty(selectedCellInfo.columns)) {
return {
IsValid: false,
Alert: {
alertType: 'generic',
header: 'Bulk Update',
message: 'No cells selected. Please select one or more cells to continue.',
alertDefinition: ObjectFactory.CreateInternalAlertDefinitionForMessages('Error'),
},
};
}
if (ArrayExtensions.NotCorrectLength(selectedCellInfo.columns, 1)) {
return {
IsValid: false,
Alert: {
alertType: 'generic',
header: 'Bulk Update',
message: 'Bulk Update supports editing a single column at a time. Please adjust your selection.',
alertDefinition: ObjectFactory.CreateInternalAlertDefinitionForMessages('Error'),
},
};
}
let selectedColumn = selectedCellInfo.columns[0];
if (ArrayExtensions.IsNotNullOrEmpty(selectedCellInfo.gridCells) &&
!this.api.gridApi.isEveryCellEditable(selectedCellInfo.gridCells)) {
return {
IsValid: false,
Alert: {
alertType: 'generic',
header: 'Bulk Update',
message: 'Bulk Update is not available for read-only cells. Please adjust your selection.',
alertDefinition: ObjectFactory.CreateInternalAlertDefinitionForMessages('Error'),
},
};
}
return { IsValid: true, Column: selectedColumn };
}
buildPreviewValues(bulkUpdateValue) {
let previewResults = [];
if (StringExtensions.IsNullOrEmpty(String(bulkUpdateValue))) {
return null;
}
let selectedCellInfo = this.api.gridApi.getSelectedCellInfo();
let column;
if (!this.api.layoutApi.isCurrentLayoutPivot()) {
if (selectedCellInfo != null && selectedCellInfo.columns.length > 0) {
column = this.api.columnApi.getColumnWithColumnId(selectedCellInfo.columns[0].columnId);
let typedBulkUpdateValue;
switch (column.dataType) {
case 'number':
typedBulkUpdateValue = Number(bulkUpdateValue);
break;
case 'text':
typedBulkUpdateValue = bulkUpdateValue;
break;
case 'date':
typedBulkUpdateValue = new Date(bulkUpdateValue);
break;
}
selectedCellInfo.gridCells.forEach((selectedCell) => {
const cellDataChangedInfo = this.api.internalApi.buildCellDataChangedInfo({
oldValue: selectedCell.rawValue,
newValue: typedBulkUpdateValue,
column: selectedCell.column,
primaryKeyValue: selectedCell.primaryKeyValue,
rowNode: selectedCell.rowNode,
trigger: 'edit',
});
const validationRules = this.api.internalApi
.getValidationService()
.getValidationRulesForDataChange(cellDataChangedInfo);
const previewResult = {
id: selectedCell.primaryKeyValue,
initialValue: selectedCell.rawValue,
computedValue: typedBulkUpdateValue,
validationRules: validationRules,
rowNode: selectedCell.rowNode,
};
previewResults.push(previewResult);
});
}
}
return {
column: column,
previewResults: previewResults,
previewValidationSummary: PreviewHelper.GetPreviewValidationSummary(previewResults),
};
}
}