import type FlexCommand from 'sap/ui/rta/command/FlexCommand'; import OverlayRegistry from 'sap/ui/dt/OverlayRegistry'; import { QuickActionContext, NestedQuickActionDefinition } from '../../../cpe/quick-actions/quick-action-definition.js'; import { DialogFactory, DialogNames } from '../../dialog-factory.js'; import { SMART_TABLE_TYPE, GRID_TABLE_TYPE, MDC_TABLE_TYPE, TREE_TABLE_TYPE } from '../control-types.js'; import { TableQuickActionDefinitionBase } from '../table-quick-action-base.js'; import { DIALOG_ENABLEMENT_VALIDATOR } from '../dialog-enablement-validator.js'; import Table from 'sap/ui/mdc/Table'; import { isMacroTable } from '../../../utils/fe-v4.js'; import { getLineItemAnnotation, getPropertyPath, getAppDescriptorBase } from './utils.js'; import { getUi5Version, isLowerThanMinimalUi5Version } from '../../../utils/version.js'; import { isA } from '../../../utils/core.js'; import UI5Element from 'sap/ui/core/Element'; export const CREATE_TABLE_CUSTOM_COLUMN = 'create-table-custom-column'; const regexForAnnotationPath = /controlConfiguration\/(?:entity\/)?@com\.sap\.vocabularies\.UI\.v1\.LineItem(?:#[^/]+)?\/columns\//; export const CONTROL_TYPES = [SMART_TABLE_TYPE, MDC_TABLE_TYPE, TREE_TABLE_TYPE, GRID_TABLE_TYPE]; export class AddTableCustomColumnQuickAction extends TableQuickActionDefinitionBase implements NestedQuickActionDefinition { protected appDescriptor: ReturnType; constructor(context: QuickActionContext) { super( CREATE_TABLE_CUSTOM_COLUMN, CONTROL_TYPES, 'QUICK_ACTION_ADD_CUSTOM_TABLE_COLUMN', context, { validateTableColumns: true }, [DIALOG_ENABLEMENT_VALIDATOR] ); } async initialize(): Promise { this.appDescriptor = getAppDescriptorBase(this.context); const version = await getUi5Version(); if (isLowerThanMinimalUi5Version(version, { major: 1, minor: 120 })) { return; } await super.initialize(); } async execute(path: string): Promise { if (!this.appDescriptor) { return []; } const { table } = this.tableMap[path]; if (!table) { return []; } if (table) { const overlay = OverlayRegistry.getOverlay(table) || []; const propertyPath = `${getPropertyPath(table, 'columns')}`; const anchor = findAnchor(table); await DialogFactory.createDialog( overlay, this.context.rta, DialogNames.ADD_CUSTOM_FRAGMENT, undefined, { title: 'QUICK_ACTION_ADD_CUSTOM_TABLE_COLUMN', type: 'tableColumn', appDescriptor: { ...this.appDescriptor, appType: 'fe-v4', projectId: this.context.flexSettings.projectId, anchor }, validateId: (columnId) => { const customColumnInPending = [ ...this.context.changeService.getAllPendingConfigPropertyPath() ].filter((path) => regexForAnnotationPath.test(path)); const idInPendingChanges = customColumnInPending.includes(`${propertyPath}${columnId}`); if (idInPendingChanges) { return false; } if ( isA(MDC_TABLE_TYPE, table) && (table as Table) .getColumns() .every((col) => !col.getId().endsWith(`CustomColumn::${columnId}`)) ) { return true; } return false; }, propertyPath }, { actionName: this.type, telemetryEventIdentifier: this.getTelemetryIdentifier() } ); } return []; } } function findAnchor(table: UI5Element): string { const macroTable = table.getParent(); let anchor: string = ''; if (isMacroTable(macroTable)) { const metaPath = macroTable.metaPath.includes('LineItem') ? macroTable.metaPath : (() => { const segments = macroTable.metaPath.split('/'); segments.pop(); const path = segments.join('/'); return `${path}/${getLineItemAnnotation(macroTable)}`; })(); if (!metaPath) { return ''; } const columns = macroTable.getModel()?.getMetaModel()?.getObject(metaPath); const filteredColumns = columns.filter( (col: { $Type: string; Inline?: boolean; SemanticObject?: string; Action?: string }) => [ 'com.sap.vocabularies.UI.v1.DataField', 'com.sap.vocabularies.UI.v1.DataFieldForIntentBasedNavigation', 'com.sap.vocabularies.UI.v1.DataFieldForAnnotation' ].includes(col.$Type) || (col.$Type === 'com.sap.vocabularies.UI.v1.DataFieldForAction' && col.Inline) ) as { $Type: string; Inline?: boolean; Value: { $Path: string }; Action: string; SemanticObject?: string; Target?: { $AnnotationPath: string }; }[]; const lastColumn = filteredColumns.at(-1); if (!lastColumn) { return ''; } anchor = buildColumnAnchor(lastColumn); } return anchor; } /** * Builds anchor string for different column types * @param column - The column object from metadata * @returns The anchor string for the column */ function buildColumnAnchor(column: { $Type: string; Value?: { $Path: string }; Action?: string; SemanticObject?: string; Target?: { $AnnotationPath: string }; }): string { if (column.$Type === 'com.sap.vocabularies.UI.v1.DataFieldForAction') { return `DataFieldForAction::${column.Action}`; } if (column.$Type === 'com.sap.vocabularies.UI.v1.DataField') { return `DataField::${column.Value?.$Path}`; } if (column.$Type === 'com.sap.vocabularies.UI.v1.DataFieldForIntentBasedNavigation') { return `DataFieldForIntentBasedNavigation::${column.SemanticObject}::${column.Action}`; } if (column.$Type === 'com.sap.vocabularies.UI.v1.DataFieldForAnnotation') { const annotationPath = column.Target?.$AnnotationPath; if (!annotationPath) { return ''; } const annotation = annotationPath.split('.').pop(); return `DataFieldForAnnotation::${annotation?.split('#').join('::')}`; } return ''; }