/** sap.m */ import Button from 'sap/m/Button'; import type Dialog from 'sap/m/Dialog'; import Input from 'sap/m/Input'; import type RadioButtonGroup from 'sap/m/RadioButtonGroup'; /** sap.ui.core */ import type UI5Element from 'sap/ui/core/Element'; import { ValueState } from 'sap/ui/core/library'; /** sap.ui.base */ import type Event from 'sap/ui/base/Event'; /** sap.ui.model */ import JSONModel from 'sap/ui/model/json/JSONModel'; /** sap.ui.rta */ import type RuntimeAuthoring from 'sap/ui/rta/RuntimeAuthoring'; /** sap.ui.dt */ import type ElementOverlay from 'sap/ui/dt/ElementOverlay'; import { MessageBarType } from '@sap-ux-private/control-property-editor-common'; import { QuickActionTelemetryData } from '../../cpe/quick-actions/quick-action-definition.js'; import { getResourceModel, getTextBundle, TextBundle } from '../../i18n.js'; import { getControlById } from '../../utils/core.js'; import { getError } from '../../utils/error.js'; import { sendInfoCenterMessage } from '../../utils/info-center-message.js'; import { getUi5Version, isLowerThanMinimalUi5Version, type Ui5VersionInfo } from '../../utils/version.js'; import type { CodeExtResponse, ControllersResponse } from '../api-handler.js'; import { getExistingController, readControllers, writeChange, writeController } from '../api-handler.js'; import CommandExecutor from '../command-executor.js'; import type { DeferredExtendControllerData, ExtendControllerData } from '../extend-controller.js'; import { checkForExistingChange, getControllerInfo, getPendingCodeExtViewIds } from '../utils.js'; import BaseDialog from './BaseDialog.controller.js'; interface ControllerExtensionService { add: (codeRef: string, viewId: string, includeViewId?: boolean) => Promise<{ creation: string }>; } type ControllerList = { /** * File name without extension */ controllerName: string; }[]; type ControllerModel = JSONModel & { getProperty(sPath: '/controllersList'): ControllerList; getProperty(sPath: '/controllerExists'): boolean; getProperty(sPath: '/newControllerName'): string; getProperty(sPath: '/viewId'): string; getProperty(sPath: '/baseControllerPath'): string; getProperty(sPath: '/instanceControllerPath'): string; getProperty(sPath: '/controllerExtension'): string; getProperty(sPath: '/isInstanceSpecific'): boolean; getProperty(sPath: '/instanceSpecificVisibility'): boolean; getProperty(sPath: '/baseControllerEnabled'): boolean; getProperty(sPath: '/instanceControllerEnabled'): boolean; getProperty(sPath: '/controllerTypeSelectedIndex'): number; }; /** * @namespace open.ux.preview.client.adp.controllers */ export default class ControllerExtension extends BaseDialog { /* The minimum version of UI5 framework which supports controller extensions. */ private static readonly CONTROLLER_EXT_MIN_UI5_VERSION = { major: 1, minor: 135 }; /* The minimum version of UI5 framework which supports instance-specific controller extensions. */ private static readonly INSTANCE_SPECIFIC_MIN_UI5_VERSION = { major: 1, minor: 143 }; public readonly data?: ExtendControllerData; private bundle: TextBundle; private ui5Version: Ui5VersionInfo; constructor( name: string, overlays: UI5Element, rta: RuntimeAuthoring, data?: ExtendControllerData, telemetryData?: QuickActionTelemetryData ) { super(name, telemetryData); this.rta = rta; this.overlays = overlays; this.model = new JSONModel(); this.data = data; } /** * Setups the Dialog and the JSON Model * * @param {Dialog} dialog - Dialog instance */ async setup(dialog: Dialog): Promise { this.dialog = dialog; this.setEscapeHandler(); const resourceModel = await getResourceModel('open.ux.preview.client'); this.bundle = await getTextBundle(); this.ui5Version = await getUi5Version(); await this.buildDialogData(); this.dialog.setModel(resourceModel, 'i18n'); this.dialog.setModel(this.model); this.dialog.open(); } /** * Handles fragment name input change * * @param event Event */ onControllerNameInputChange(event: Event) { const input = event.getSource(); const beginBtn = this.dialog.getBeginButton(); const controllerName: string = input.getValue(); const controllerList = this.model.getProperty('/controllersList'); const updateDialogState = (valueState: ValueState, valueStateText = '') => { input.setValueState(valueState).setValueStateText(valueStateText); beginBtn.setEnabled(valueState === ValueState.Success); }; if (controllerName.length <= 0) { updateDialogState(ValueState.None); this.model.setProperty('/newControllerName', null); return; } const fileExists = controllerList.some((f) => f.controllerName === controllerName); const pendingChangeExists = checkForExistingChange( this.rta, 'codeExt', 'content.codeRef', `${controllerName}.js` ); if (fileExists) { updateDialogState( ValueState.Error, 'Enter a different name. The controller name that you entered already exists in your project.' ); return; } if (pendingChangeExists) { updateDialogState( ValueState.Error, 'Enter a different name. The controller name that you entered already exists as a pending change.' ); return; } const isValidName = /^[a-zA-Z_][a-zA-Z0-9_-]*$/.test(controllerName); if (!isValidName) { updateDialogState( ValueState.Error, 'The controller name cannot contain white spaces or special characters.' ); return; } if (controllerName.length > 64) { updateDialogState(ValueState.Error, 'A controller file name cannot contain more than 64 characters.'); return; } updateDialogState(ValueState.Success); this.model.setProperty('/newControllerName', controllerName); } /** * Handles create button press * * @param event Event */ async onCreateBtnPress(event: Event) { const source = event.getSource