import { getControlById } from '../../../utils/core.js'; import type FlexCommand from 'sap/ui/rta/command/FlexCommand'; import type { QuickActionContext } from '../../../cpe/quick-actions/quick-action-definition.js'; import CommandFactory from 'sap/ui/rta/command/CommandFactory'; import { getV4AppComponent, getPageName, getReference, isMacroTable } from '../../../utils/fe-v4.js'; import UI5Element from 'sap/ui/core/Element'; import type AppComponent from 'sap/fe/core/AppComponent'; import { Manifest } from 'sap/ui/rta/RuntimeAuthoring'; interface ViewDataType { stableId: string; } export function getPageId(context: QuickActionContext): string | undefined { return (context.view.getViewData() as ViewDataType)?.stableId.split('::').pop(); } /** * Returns the app component and page ID for use in appDescriptor, or undefined if either is missing. */ export function getAppDescriptorBase( context: QuickActionContext ): { appComponent: AppComponent; pageId: string } | undefined { const appComponent = getV4AppComponent(context.view); const pageId = getPageId(context); if (!appComponent || !pageId) { return undefined; } return { appComponent, pageId }; } export async function executeToggleAction( context: QuickActionContext, isButtonEnabled: boolean, controlType: string, propertyPath: string ): Promise { const controls = context.controlIndex[controlType] ?? []; const control = controls[0]; if (control) { const modifiedControl = getControlById(control.controlId); if (!modifiedControl) { return []; } const { flexSettings } = context; const parent = modifiedControl.getParent(); if (!parent) { return []; } const modifiedValue = { reference: getReference(modifiedControl), appComponent: getV4AppComponent(modifiedControl), changeType: 'appdescr_fe_changePageConfiguration', parameters: { page: getPageName(parent), entityPropertyChange: { propertyPath: propertyPath, propertyValue: !isButtonEnabled, operation: 'UPSERT' } } }; const command = await CommandFactory.getCommandFor( modifiedControl, 'appDescriptor', modifiedValue, null, flexSettings ); return [command]; } return []; } const PATTERN_SUFFIX = ':?query:'; /** * Generates the pattern for a new route based on the input. * * @param sourceRoutePattern source page route pattern * @param navProperty navigation property name (used to build nav pattern for nested OP ) * @param targetEntitySet navigation target entity set * @returns the generated pattern as string */ export function generateRoutePattern(sourceRoutePattern: string, navProperty: string, targetEntitySet: string): string { const parts: string[] = []; const basePattern = sourceRoutePattern.replace(PATTERN_SUFFIX, ''); if (basePattern) { parts.push(basePattern); parts.push('/'); parts.push(navProperty); } else { parts.push(targetEntitySet); } parts.push(`({${targetEntitySet}Key})`); parts.push(PATTERN_SUFFIX); return parts.join(''); } /** * Get LineItem annotation - tries to use design-time helper if available, falls back to local implementation. * * @param table - table control * @returns LineItem annotation string */ export function getLineItemAnnotation(table: UI5Element): string | undefined { try { const helper = sap.ui.require('sap/fe/macros/table/designtime/Table.designtime.helper'); if (helper && typeof helper.getLineItemAnnotation === 'function') { return helper.getLineItemAnnotation(table); } } catch { // Module not available or error occurred } return getLineItemAnnotationForTable(table); } /** * Get property path for table action. * * @param table - table control * @returns string */ export function getPropertyPath(table: UI5Element, property: 'actions' | 'columns' = 'actions'): string | undefined { const macroTable = table.getParent(); const configPath = ''; if (isMacroTable(macroTable)) { const lineItemAnnotation = getLineItemAnnotation(macroTable); const navigationPath = macroTable.metaPath.split(macroTable.getProperty('contextPath'))[1]; if (!lineItemAnnotation) { throw new Error('Line item annotation could not be determined for the table.'); } if (navigationPath) { return configPath.concat( 'controlConfiguration/', navigationPath.split('@')[0], lineItemAnnotation, `/${property}/` ); } else { let contextString = macroTable.metaPath; const firstSlash = contextString.indexOf('/'); if (firstSlash >= 0) { contextString = contextString.substring(firstSlash + 1); } const secondSlash = contextString.indexOf('/'); if (secondSlash >= 0) { contextString = contextString.substring(0, secondSlash); } return configPath.concat( 'controlConfiguration/', '/', contextString, '/', lineItemAnnotation, `/${property}/` ); } } return undefined; } export function hasRouteForNavProperty( manifest: Manifest, sourcePageId: string, navigationProperty: string ): boolean { const targets = manifest['sap.ui5']?.routing?.targets ?? {}; const sourceTarget = targets[sourcePageId]; // Check if a route exists from the source page for the specified navigation property. return !!sourceTarget?.options?.settings?.navigation?.[navigationProperty]?.detail?.route; } /** * Return the line item annotation that defines the table. * This may come from a Presentation Variant, a Selection Presentation Variant or the default. * @param table - The table control * @returns The line item annotation used to define the table */ function getLineItemAnnotationForTable(macroTable: UI5Element): string | undefined { let lineItemAnnotation: string | undefined = ''; if (isMacroTable(macroTable)) { const presentation = macroTable.getModel()?.getMetaModel()?.getObject(macroTable.metaPath); // default line item annotation if (!presentation.Visualizations && !presentation.PresentationVariant) { lineItemAnnotation = macroTable.metaPath.split('/').pop(); } else if (presentation.Visualizations) { lineItemAnnotation = presentation.Visualizations[0].$AnnotationPath; } else if (presentation.PresentationVariant) { if (presentation.PresentationVariant.Visualizations) { lineItemAnnotation = presentation.PresentationVariant.Visualizations[0].$AnnotationPath; } else { const contextPath = macroTable.metaPath.startsWith('/') ? macroTable.metaPath.split('@')[0] : macroTable.contextPath; const pathForLineItems = contextPath + presentation.PresentationVariant.$Path; const presentationVariantType = macroTable.getModel()?.getMetaModel()?.getObject(pathForLineItems); lineItemAnnotation = presentationVariantType.Visualizations[0].$AnnotationPath; } } } return lineItemAnnotation; }