import { baseActionTokenSymbol, type GetRequestModelFieldsOptions, ModelCache, RelationUpdateType, type RequestModelField, type RuntimeAction, SubmitType, SubmitValue } from '@oinone/kunlun-engine'; import { ActionType, ViewActionTarget, ViewMode, ViewType } from '@oinone/kunlun-meta'; import { type Matched, Router, useMatched } from '@oinone/kunlun-router'; import { CallChaining, type Constructor } from '@oinone/kunlun-shared'; import { SPI, type SPIOptions, type SPISingleSelector, type SPITokenFactory } from '@oinone/kunlun-spi'; import { useRouter } from '@oinone/kunlun-vue-router'; import { type ActiveRecordsWidgetProps, InnerWidgetType, type OioActionBarState, useOioState, Widget } from '@oinone/kunlun-vue-widget'; import { PopupScene } from '../../typing'; import { BaseRuntimePropertiesWidget } from '../common'; /** * Action组件注册可选项 */ export interface BaseActionOptions extends SPIOptions { /** * 指定动作类型 */ actionType?: ActionType | ActionType[]; /** * 指定跳转动作路由类型 */ target?: ViewActionTarget | string | string[]; /** * 指定动作名称 */ name?: string | string[]; /** * 指定模型 */ model?: string[] | string; /** * 指定视图类型 */ viewType?: ViewType | ViewType[]; /** * 指定视图名称 */ viewName?: string | string[]; /** * 指定组件名称或别称 */ widget?: string[] | string; } export interface BaseActionWidgetProps extends ActiveRecordsWidgetProps { viewType?: ViewType; action?: Action; } @SPI.Base(baseActionTokenSymbol, ['viewType', 'actionType', 'target', 'widget', 'model', 'viewName', 'name']) export class BaseActionWidget< Action extends RuntimeAction = RuntimeAction, Props extends BaseActionWidgetProps = BaseActionWidgetProps > extends BaseRuntimePropertiesWidget { protected $$innerWidgetType = InnerWidgetType.Action; public static Token: SPITokenFactory; public static Selector: SPISingleSelector>; protected $matched: Matched | undefined; protected $router: Router | undefined; @Widget.Reactive() @Widget.Inject() protected viewType: ViewType | undefined; @Widget.Reactive() @Widget.Inject('viewMode') protected parentViewMode: ViewMode | undefined; @Widget.Reactive() @Widget.Provide() protected get viewMode(): ViewMode | undefined { return this.parentViewMode; } @Widget.Reactive() @Widget.Inject() protected submitType: SubmitType | undefined; @Widget.Reactive() @Widget.Inject() protected relationUpdateType: RelationUpdateType | undefined; private runtimeAction: Action | undefined; @Widget.Reactive() public get action(): Action { const action = this.runtimeAction; if (!action) { throw new Error('Invalid action define.'); } return action; } @Widget.Reactive() protected get isAsync() { return true; } @Widget.Reactive() @Widget.Inject() protected popupScene: PopupScene | string | undefined; @Widget.Reactive() protected get isDialog() { return this.popupScene === PopupScene.dialog; } @Widget.Reactive() protected get isDrawer() { return this.popupScene === PopupScene.drawer; } @Widget.Reactive() protected get isInnerPopup() { return this.popupScene === PopupScene.inner; } @Widget.Inject() @Widget.Reactive() public rowIndex: number | undefined; @Widget.Reactive() protected actionBarState: OioActionBarState | undefined; /** * 数据提交 * @protected */ @Widget.Reactive() @Widget.Inject() protected submitCallChaining: CallChaining | undefined; /** * 数据校验 * @protected */ @Widget.Reactive() @Widget.Inject() protected validatorCallChaining: CallChaining | undefined; /** * 刷新时 * @protected */ @Widget.Reactive() @Widget.Inject() protected refreshCallChaining: CallChaining | undefined; public initialize(props: Props) { super.initialize(props); this.runtimeAction = props.action; return this; } public async load(fn: (...args) => R, ...args): Promise { if (this.isAsync) { return super.load(fn, ...args); } return fn(...args); } protected async getRequestModelFields(options?: GetRequestModelFieldsOptions): Promise { const { viewType } = this; if (viewType === ViewType.Tree) { const runtimeModel = await ModelCache.get(this.model.model); if (runtimeModel) { return runtimeModel.modelFields.map((field) => ({ field })); } return []; } if (this.popupScene) { return this.seekPopupMainRuntimeContext().getRequestModelFields(options); } return this.rootRuntimeContext.getRequestModelFields(options); } protected $$beforeMount() { super.$$beforeMount(); if (!this.viewState && this.popupScene) { this.viewState = useOioState(this.seekPopupMainRuntimeContext().handle).viewState; if (this.viewState) { this.$$initViewState(this.viewState); } } if (!this.actionBarState) { this.actionBarState = this.viewState?.getActionBarState(this.rowIndex); } if (!this.$matched) { const { matched } = useMatched(); this.$matched = matched; } if (!this.$router) { this.$router = useRouter().router as Router; } } protected $$mounted() { super.$$mounted(); this.viewState?.pushAction(this.currentHandle, this.rowIndex); } protected $$unmounted() { super.$$unmounted(); this.viewState?.popAction(this.currentHandle, this.rowIndex); } }