import ManagedObject from 'sap/ui/base/ManagedObject'; import TemplateComponent from 'sap/fe/core/TemplateComponent'; import Component from 'sap/ui/core/Component'; import AppComponent from 'sap/fe/core/AppComponent'; import XMLView from 'sap/ui/core/mvc/XMLView'; import type { FlexSettings, Manifest } from 'sap/ui/rta/RuntimeAuthoring'; import { isA } from './core.js'; import { getOverlay } from '../cpe/utils.js'; import UI5Element from 'sap/ui/core/Element'; import type FlexCommand from 'sap/ui/rta/command/FlexCommand'; import TableAPI from 'sap/fe/macros/table/TableAPI'; export type MacroTable = TableAPI & { metaPath: string; contextPath: string; }; /** * Gets app component of a v4 project. * * @param control - ManagedObject. * @returns AppComponent. */ export function getV4AppComponent(control: ManagedObject): AppComponent | undefined { const ownerComponent = Component.getOwnerComponentFor(control); if (ownerComponent?.isA('sap.fe.core.TemplateComponent')) { if (ownerComponent.getAppComponent) { return ownerComponent.getAppComponent(); } else if ((ownerComponent as unknown as { oAppComponent: AppComponent })?.oAppComponent) { return (ownerComponent as unknown as { oAppComponent: AppComponent }).oAppComponent; } } return undefined; } /** * Gets reference id of the app. * * @param control - ManagedObject. * @returns string. */ export function getReference(control: ManagedObject): string { const manifest = getV4AppComponent(control)?.getManifest() as Manifest; return manifest?.['sap.app']?.id ?? ''; } /** * Determines the page type of v4 app. * * @param control - ManagedObject. * @returns 'ObjectPage' | 'ListReport' | undefined. */ export function getV4PageType(control: ManagedObject): 'ObjectPage' | 'ListReport' | undefined { const component = Component.getOwnerComponentFor(control); if (!component?.isA('sap.fe.core.TemplateComponent')) { return undefined; } const view = component.getRootControl(); const name = (view as XMLView).getViewName(); if (name === 'sap.fe.templates.ObjectPage.ObjectPage') { return 'ObjectPage'; } if (name === 'sap.fe.templates.ListReport.ListReport') { return 'ListReport'; } return undefined; } /** * Get the containing page name of a control. * * @param control - UI5 control instance. * @returns Page name to which the control belongs. */ export function getPageName(control: ManagedObject): string | undefined { const component = Component.getOwnerComponentFor(control); if (!isA('sap.fe.core.TemplateComponent', component)) { return undefined; } const view = component.getRootControl(); return view.getId().split('::').pop(); } export function getConfigMapControlIdMap(page: string | undefined, propertyPathSegments: string[]): string { if (page && !propertyPathSegments.length) { return page; } if (page) { return `${page}-${propertyPathSegments.join('/')}`; } return propertyPathSegments.join('/'); } /** * Get the modified value for a control. * @param modifiedControl - The modified control. * @param flexSettings - Flex Settings of the control. * @param propertyChanges - The change object * @param propertyPathExtraSegments - optional path segments which are added to the default modified control manifest path * * @returns A Promise resolving to an array of FlexCommand objects. */ export async function createManifestPropertyChange( modifiedControl: UI5Element, flexSettings: FlexSettings, propertyChanges: Record, propertyPathExtraSegments?: string[] ): Promise { const overlay = await getOverlay(modifiedControl); if (!overlay) { return undefined; } const overlayData = overlay.getDesignTimeMetadata().getData(); const settings = overlayData.manifestSettings(modifiedControl); let manifestPropertyPath = overlayData.manifestPropertyPath(modifiedControl); if (propertyPathExtraSegments) { manifestPropertyPath += '/' + propertyPathExtraSegments.join('/'); } const adjustedChanges: Record = {}; for (const [key, value] of Object.entries(propertyChanges)) { const setting = settings.find((s) => s.id === key); if (setting) { adjustedChanges[setting.path ?? setting.id] = value; } else { adjustedChanges[key] = value; } } const [manifestPropertyChange] = overlayData.manifestPropertyChange( adjustedChanges, manifestPropertyPath, modifiedControl ); const modifiedValue = { reference: getReference(modifiedControl), appComponent: manifestPropertyChange.appComponent, changeType: manifestPropertyChange.changeSpecificData.appDescriptorChangeType, parameters: manifestPropertyChange.changeSpecificData.content.parameters, selector: manifestPropertyChange.selector }; const CommandFactory = (await import('sap/ui/rta/command/CommandFactory')).default; const command = await CommandFactory.getCommandFor( modifiedControl, 'appDescriptor', modifiedValue, null, flexSettings ); return command; } /** * Returns application object page definitions found in manifest * * @param manifest - manifest object * @returns array with page descriptors */ export function getV4ApplicationPages(manifest: Manifest): { id: string; entitySet?: string; contextPath?: string }[] { const result: { id: string; entitySet?: string; contextPath?: string }[] = []; const targets = manifest['sap.ui5'].routing?.targets ?? {}; for (const target of Object.values(targets)) { if (target.name === 'sap.fe.templates.ObjectPage') { result.push({ id: target.id, entitySet: target.options?.settings?.entitySet, contextPath: target.options?.settings?.contextPath }); } } return result; } /** * Checks if the given control is inside a table and returns the table instance. * For UI5 version greater than 1.144 the table is 'sap.fe.macros.Table' and for lower versions it is 'sap.fe.macros.table.TableAPI' * @param control - UI5 control instance. * @returns Table instance if the control is inside a table, otherwise undefined. */ export function isMacroTable(table: ManagedObject | null): table is MacroTable { if (table && (isA('sap.fe.macros.Table', table) || isA('sap.fe.macros.table.TableAPI', table))) { return true; } return false; }