import type UI5Element from 'sap/ui/core/Element'; import { NESTED_QUICK_ACTION_KIND } from '@sap-ux-private/control-property-editor-common'; import type IconTabBar from 'sap/m/IconTabBar'; import type IconTabFilter from 'sap/m/IconTabFilter'; import type Table from 'sap/m/Table'; import type MdcTable from 'sap/ui/mdc/Table'; import type SmartTable from 'sap/ui/comp/smarttable/SmartTable'; import type { QuickActionContext } from '../../cpe/quick-actions/quick-action-definition.js'; import OverlayUtil from 'sap/ui/dt/OverlayUtil'; import type { NestedQuickActionChild, NestedQuickAction } from '@sap-ux-private/control-property-editor-common'; import { getParentContainer, getRelevantControlFromActivePage } from '../../cpe/quick-actions/utils.js'; import { getControlById, isA, isManagedObject } from '../../utils/core.js'; import { getUi5Version, isLowerThanMinimalUi5Version } from '../../utils/version.js'; import type ObjectPageSection from 'sap/uxap/ObjectPageSection'; import type ObjectPageSubSection from 'sap/uxap/ObjectPageSubSection'; import type ObjectPageLayout from 'sap/uxap/ObjectPageLayout'; import type ManagedObject from 'sap/ui/base/ManagedObject'; import type { EnablementValidator } from './enablement-validator.js'; import { QuickActionDefinitionBase } from './quick-action-base.js'; import { ANALYTICAL_TABLE_TYPE, GRID_TABLE_TYPE, M_TABLE_TYPE, MDC_TABLE_TYPE, SMART_TABLE_TYPE, TREE_TABLE_TYPE } from './control-types.js'; import { isVariantManagementEnabledOPPage } from './fe-v2/utils.js'; import { isMacroTable } from '../../utils/fe-v4.js'; const SMART_TABLE_ACTION_ID = 'CTX_COMP_VARIANT_CONTENT'; const M_TABLE_ACTION_ID = 'CTX_ADD_ELEMENTS_AS_CHILD'; const SETTINGS_ID = 'CTX_SETTINGS'; const REARRANGE_TOOLBAR_SETTINGS_ID = 'CTX_SETTINGS0'; const ICON_TAB_BAR_TYPE = 'sap.m.IconTabBar'; async function getActionId(table: UI5Element): Promise { const { major, minor } = await getUi5Version(); if (isA(SMART_TABLE_TYPE, table)) { if (major === 1 && minor === 96) { return [SETTINGS_ID]; } else { return [SMART_TABLE_ACTION_ID]; } } return [M_TABLE_ACTION_ID, SETTINGS_ID]; } async function getRearrangeToolbarContentActionId(): Promise { const { major, minor } = await getUi5Version(); if (major === 1 && minor <= 127) { return SETTINGS_ID; } return REARRANGE_TOOLBAR_SETTINGS_ID; } export type TableQuickActionsOptions = { includeServiceAction?: boolean; areTableRowsRequired?: boolean; validatePageVariantManagement?: boolean; validateTableColumns?: boolean; }; /** * Base class for table quick actions. */ export abstract class TableQuickActionDefinitionBase extends QuickActionDefinitionBase< typeof NESTED_QUICK_ACTION_KIND > { public isApplicable = false; public children: NestedQuickActionChild[] = []; public tableMap: Record< string, { table: UI5Element; tableUpdateEventAttachedOnce: boolean; iconTabBarFilterKey?: string; changeColumnActionId?: string; changeToolbarContentAction?: { id: string; enabled: boolean }; sectionInfo?: { section: ObjectPageSection; subSection: ObjectPageSubSection; layout?: ObjectPageLayout; }; } > = {}; public iconTabBar: IconTabBar | undefined; protected get textKey(): string { return this.defaultTextKey; } protected control: UI5Element | undefined; constructor( public readonly type: string, protected readonly controlTypes: string[], protected readonly defaultTextKey: string, protected readonly context: QuickActionContext, protected options: TableQuickActionsOptions = {}, protected readonly enablementValidators: EnablementValidator[] = [] ) { super(type, NESTED_QUICK_ACTION_KIND, defaultTextKey, context, enablementValidators); } /** * Adds action id to the table map entry, if the service actions are needed. * * @param table - table element * @param tableMapKey - map key */ protected async addSettingsActionId(table: UI5Element, tableMapKey: string): Promise { if (this.options.includeServiceAction) { const actions = await this.context.actionService.get(table.getId()); const actionsIds = await getActionId(table); const changeColumnActionId = actionsIds.find( (actionId) => actions.findIndex((action) => action.id === actionId) > -1 ); this.tableMap[tableMapKey].changeColumnActionId = changeColumnActionId; const changeToolbarContentActionId = await getRearrangeToolbarContentActionId(); const changeToolbarContentAction = actions.find((action) => action.id === changeToolbarContentActionId); this.tableMap[tableMapKey].changeToolbarContentAction = changeToolbarContentAction ? { id: changeToolbarContentAction.id, enabled: changeToolbarContentAction.enabled } : undefined; } } /** * Builds a map of custom tab keys to their associated table building blocks. * * @param iconTabBarFilterMap - A map of icon tab bar filter keys to their labels. * @returns A map where each key is a combination of contextPath and metaPath, and the value is the corresponding custom tab key. */ private getCustomViewTableBuildingBlocksMap(iconTabBarFilterMap: { [key: string]: string }): { [key: string]: string; } { const customTabTableBuildingBlockMap: { [key: string]: string } = {}; const customTabs = Object.keys(iconTabBarFilterMap).filter((key) => /^fe::CustomTab::\d+$/.test(key)); for (const key of customTabs) { const items = (this.iconTabBar?.getItems() ?? []) as unknown as ManagedObject[]; const filterItem = items.find( (item) => isA('sap.m.IconTabFilter', item) && item.getKey() === key ); if (filterItem && isA('sap.m.IconTabFilter', filterItem)) { filterItem.getContent().forEach((content) => { if (isMacroTable(content)) { const { metaPath, contextPath } = content; const path = metaPath.startsWith(contextPath) ? metaPath : `${contextPath}${metaPath}`; customTabTableBuildingBlockMap[path] = key; } }); } } return customTabTableBuildingBlockMap; } /** * Initializes action object instance */ async initialize(): Promise { // No action found in control design time for version < 1.96 const version = await getUi5Version(); if (isLowerThanMinimalUi5Version(version, { major: 1, minor: 96 })) { this.isApplicable = false; return; } const iconTabBarFilterMap = this.buildIconTabBarFilterMap(); const customViewTableBuildingBlockMap = this.getCustomViewTableBuildingBlocksMap(iconTabBarFilterMap); for (const table of getRelevantControlFromActivePage( this.context.controlIndex, this.context.view, this.controlTypes )) { const tabKey = Object.keys(iconTabBarFilterMap).find((key) => table.getId().endsWith(key)); const section = getParentContainer(table, 'sap.uxap.ObjectPageSection'); if (section) { await this.collectChildrenInSection(section, table); } else if (this.iconTabBar && tabKey) { const label = `'${iconTabBarFilterMap[tabKey]}' table`; const tableMapKey = this.children.length.toString(); const child = this.createChild(label, table, tableMapKey, customViewTableBuildingBlockMap); this.children.push(child); this.tableMap[tableMapKey] = { table, iconTabBarFilterKey: tabKey, tableUpdateEventAttachedOnce: false }; await this.addSettingsActionId(table, tableMapKey); } else { await this.processTable(table); } } if (this.children.length > 0) { this.isApplicable = true; } } /** * Retrieves the internal table control from a UI5Element. * * @param table - The UI5Element instance to analyze. * @returns The internal table otherwise undefined. */ protected getInternalTable(table: UI5Element): UI5Element | undefined { try { let tableInternal: ManagedObject | undefined; if (isA(SMART_TABLE_TYPE, table)) { const itemsAggregation = table.getAggregation('items') as ManagedObject[]; tableInternal = itemsAggregation.find((item) => [M_TABLE_TYPE, TREE_TABLE_TYPE, ANALYTICAL_TABLE_TYPE, GRID_TABLE_TYPE].some((tType) => isA(tType, item) ) ); } return tableInternal as UI5Element | undefined; } catch { return undefined; } } /** * Determines table label for the given table element * * @param table - table element * @returns table label if found or 'Unnamed table' */ private getTableLabel(table: UI5Element): string { if (isA(SMART_TABLE_TYPE, table) || isA(MDC_TABLE_TYPE, table)) { const header = table.getHeader(); if (header) { return `'${header}' table`; } } else if (isA(M_TABLE_TYPE, table)) { const title = table?.getHeaderToolbar()?.getTitleControl()?.getText(); if (title) { return `'${title}' table`; } } return 'Unnamed table'; } /** * Builds a map kay/tab_name for ICON_TAB_BAR control of the active page, if such exists * * @returns built map */ protected buildIconTabBarFilterMap(): { [key: string]: string } { const iconTabBarFilterMap: { [key: string]: string } = {}; // Assumption only a tab bar control per page. const tabBar = getRelevantControlFromActivePage(this.context.controlIndex, this.context.view, [ ICON_TAB_BAR_TYPE ])[0]; if (tabBar) { const control = getControlById(tabBar.getId()); if (isA(ICON_TAB_BAR_TYPE, control)) { this.iconTabBar = control; for (const item of control.getItems()) { if (isManagedObject(item) && isA('sap.m.IconTabFilter', item)) { iconTabBarFilterMap[item.getKey()] = item.getText(); } } } } return iconTabBarFilterMap; } /** * Collects subsection data in the table map for the given section and table * * @param section - object page section * @param table - table element */ private async collectChildrenInSection(section: ObjectPageSection, table: UI5Element): Promise { const layout = getParentContainer(table, 'sap.uxap.ObjectPageLayout'); const subSections = section.getSubSections(); const subSection = getParentContainer(table, 'sap.uxap.ObjectPageSubSection'); if (subSection) { if (subSections?.length === 1) { await this.processTable(table, { section, subSection: subSections[0], layout }); } else if (subSections.length > 1) { const existingChildIdx = this.children.findIndex( (val) => val.label === `'${section.getTitle()}' section` ); let tableMapIndex; const label = this.getTableLabel(table); if (existingChildIdx < 0) { tableMapIndex = `${this.children.length}/0`; const child = this.createChild(label, table, tableMapIndex); this.children.push({ path: this.children.length.toString(), label: `'${section?.getTitle()}' section`, enabled: true, children: [child] }); } else { tableMapIndex = `${existingChildIdx}/${this.children[existingChildIdx].children.length}`; const child = this.createChild(label, table, tableMapIndex); this.children[existingChildIdx].children.push(child); } this.tableMap[tableMapIndex] = { table, sectionInfo: { section, subSection, layout }, tableUpdateEventAttachedOnce: false }; await this.addSettingsActionId(table, tableMapIndex); } } } /** * Processes table element and pushes table data to the children array * * @param table - table element * @param sectionInfo - section info object * @param sectionInfo.section * @param sectionInfo.subSection * @param sectionInfo.layout */ private async processTable( table: UI5Element, sectionInfo?: { section: ObjectPageSection; subSection: ObjectPageSubSection; layout?: ObjectPageLayout } ): Promise { const tableMapKey = this.children.length.toString(); if ( [ SMART_TABLE_TYPE, M_TABLE_TYPE, MDC_TABLE_TYPE, TREE_TABLE_TYPE, GRID_TABLE_TYPE, ANALYTICAL_TABLE_TYPE ].some((type) => isA(type, table)) ) { const label = this.getTableLabel(table); const child = this.createChild(label, table, tableMapKey); this.children.push(child); } this.tableMap[tableMapKey] = { table, sectionInfo: sectionInfo, tableUpdateEventAttachedOnce: false }; await this.addSettingsActionId(table, tableMapKey); } /** * Selects closest overlay for the given table element * * @param table - table element */ protected selectOverlay(table: UI5Element): void { const controlOverlay = OverlayUtil.getClosestOverlayFor(table); if (controlOverlay) { controlOverlay.setSelected(true); } } /** * Prepares nested quick action object * * @returns action instance */ getActionObject(): NestedQuickAction { return { kind: NESTED_QUICK_ACTION_KIND, id: this.id, enabled: !this.isDisabled, tooltip: this.tooltip, title: this.context.resourceBundle.getText(this.textKey), children: this.children }; } createChild( label: string, table: UI5Element, path: string, customViewTableBuildingBlockMap?: { [key: string]: string; } ): NestedQuickActionChild { const child: NestedQuickActionChild = { path, label, enabled: true, children: [] }; const parent = table.getParent(); const macroTable = isMacroTable(parent) ? parent : undefined; if (this.options.validateTableColumns && macroTable) { const { metaPath, contextPath } = macroTable; const finalPath = metaPath.startsWith(contextPath) ? metaPath : `${contextPath}${metaPath}`; if (customViewTableBuildingBlockMap?.[finalPath]) { child.enabled = false; child.tooltip = this.context.resourceBundle.getText('CUSTOM_COLUMNS_NOT_SUPPORTED'); return child; } } if (this.options.validatePageVariantManagement) { const variantEnabledV2 = isVariantManagementEnabledOPPage(this.context, table); if (variantEnabledV2 === false) { child.enabled = false; child.tooltip = this.context.resourceBundle.getText( 'TABLE_ACTION_DISABLED_VARIANT_MANAGEMENT_NOT_AVAILABLE' ); return child; } } if (!this.options.areTableRowsRequired) { return child; } const innerTable = this.getInternalTable(table); const tableRows = innerTable?.getAggregation('items') ?? []; if (isA(M_TABLE_TYPE, innerTable) && Array.isArray(tableRows) && tableRows.length === 0) { child.enabled = false; child.tooltip = this.context.resourceBundle.getText('TABLE_ACTION_DISABLED_ROWS_NOT_AVAILABLE'); } return child; } }