import type { DslDefinition } from '@oinone/kunlun-dsl'; import { type RuntimeContext, RuntimeContextManager, type RuntimeModelField, type RuntimeView, type RuntimeViewAction, SubmitValue, type WidgetConstructor, type WidgetProps } from '@oinone/kunlun-engine'; import { ViewType } from '@oinone/kunlun-meta'; import { CallChaining } from '@oinone/kunlun-shared'; import { ActiveRecordsWidget, Widget } from '@oinone/kunlun-vue-widget'; import { createRuntimeContextByFieldSubview, createRuntimeContextByView, createRuntimeContextByViewAction } from '../../tags/context'; import MetadataView from './MetadataView.vue'; import type { MetadataViewWidgetProps } from './MetadataViewWidget'; export interface MetadataViewWithActiveRecordsWidgetProps extends MetadataViewWidgetProps { mountedCallChaining?: CallChaining; refreshCallChaining?: CallChaining; submitCallChaining?: CallChaining; validatorCallChaining?: CallChaining; } export class MetadataViewWithActiveRecordsWidget< Props extends MetadataViewWithActiveRecordsWidgetProps = MetadataViewWithActiveRecordsWidgetProps > extends ActiveRecordsWidget { private viewAction: RuntimeViewAction | undefined; protected runtimeContext: RuntimeContext | undefined; @Widget.Reactive() protected isVirtual = false; @Widget.Reactive() protected inline = false; @Widget.Reactive() protected viewType: ViewType | undefined; @Widget.Reactive() protected modelModel: string | undefined; @Widget.Reactive() protected modelName: string | undefined; @Widget.Reactive() protected moduleModule: string | undefined; @Widget.Reactive() protected moduleName: string | undefined; @Widget.Reactive() protected viewLayout: DslDefinition | undefined; @Widget.Reactive() protected viewDsl: DslDefinition | undefined; @Widget.Reactive() protected viewTemplate: DslDefinition | undefined; @Widget.Reactive() @Widget.Inject('mountedCallChaining') protected parentMountedCallChaining: CallChaining | undefined; @Widget.Reactive() protected currentMountedCallChaining: CallChaining | undefined; @Widget.Reactive() @Widget.Provide() protected get mountedCallChaining(): CallChaining | undefined { return this.currentMountedCallChaining || this.parentMountedCallChaining; } @Widget.Reactive() @Widget.Inject('refreshCallChaining') protected parentRefreshCallChaining: CallChaining | undefined; @Widget.Reactive() protected currentRefreshCallChaining: CallChaining | undefined; @Widget.Reactive() @Widget.Provide() protected get refreshCallChaining(): CallChaining | undefined { return this.currentRefreshCallChaining || this.parentRefreshCallChaining; } @Widget.Reactive() @Widget.Inject('submitCallChaining') protected parentSubmitCallChaining: CallChaining | undefined; @Widget.Reactive() protected currentSubmitCallChaining: CallChaining | undefined; @Widget.Reactive() @Widget.Provide() protected get submitCallChaining(): CallChaining | undefined { return this.currentSubmitCallChaining || this.parentSubmitCallChaining; } @Widget.Reactive() @Widget.Inject('validatorCallChaining') protected parentValidatorCallChaining: CallChaining | undefined; @Widget.Reactive() protected currentValidatorCallChaining: CallChaining | undefined; @Widget.Reactive() @Widget.Provide() protected get validatorCallChaining(): CallChaining | undefined { return this.currentValidatorCallChaining || this.parentValidatorCallChaining; } public initialize(props: Props) { this.isVirtual = props.isVirtual || false; if (this.isVirtual) { props.slotNames = []; } super.initialize(props); this.setComponent(MetadataView); this.viewAction = props.viewAction; this.inline = props.inline || false; this.currentMountedCallChaining = props.mountedCallChaining; this.currentRefreshCallChaining = props.refreshCallChaining; this.currentSubmitCallChaining = props.submitCallChaining; this.currentValidatorCallChaining = props.validatorCallChaining; return this; } /** * 使用指定运行时上下文进行上下文初始化 * @param runtimeContext 指定运行时上下文 */ public initContext(runtimeContext: RuntimeContext): RuntimeContext { this.viewAction = runtimeContext.viewAction; this.runtimeContext = runtimeContext; const { viewLayout, viewDsl, viewTemplate } = runtimeContext; const { type, model, modelName, module, moduleName } = runtimeContext.view; this.viewType = type; this.modelModel = model; this.modelName = modelName; this.moduleModule = module; this.moduleName = moduleName; this.viewLayout = viewLayout; this.viewDsl = viewDsl; this.viewTemplate = viewTemplate; return this.runtimeContext; } /** * 使用指定跳转动作进行上下文初始化 * @param viewAction 指定跳转动作,默认使用当前视图动作 */ public initContextByViewAction(viewAction?: RuntimeViewAction): RuntimeContext { const finalViewAction = viewAction || this.viewAction; if (!finalViewAction) { throw new Error('Invalid view action define.'); } const runtimeContext = createRuntimeContextByViewAction( finalViewAction, this.inline, this.currentHandle, this.rootHandle ); if (!runtimeContext) { throw new Error('Invalid runtime context.'); } this.initContext(runtimeContext); return runtimeContext; } /** * 使用指定视图进行上下文初始化 * @param view 指定视图 */ public initContextByView(view: RuntimeView) { if (!view) { throw new Error('Invalid view define'); } const runtimeContext = createRuntimeContextByView(view, this.inline, this.currentHandle, this.rootHandle); this.initContext(runtimeContext); return runtimeContext; } /** * 使用指定视图和字段进行上下文初始化 * @param view 指定视图 * @param field 指定字段 */ public initContextByViewForField(view: RuntimeView, field: RuntimeModelField) { if (!view) { throw new Error('Invalid view define'); } if (!field) { throw new Error('Invalid field define'); } const runtimeContext = createRuntimeContextByFieldSubview( view, field, this.inline, this.currentHandle, this.rootHandle ); this.initContext(runtimeContext); return runtimeContext; } public createWidget>( constructor: WidgetConstructor, slotName?: string, initConfig: T['config'] = {} as any, specifiedIndex?: number, resolveNewCode = false ): T { const automatic = initConfig.automatic; if (!automatic) { return super.createWidget( constructor, slotName, { ...initConfig, metadataHandle: this.currentHandle, rootHandle: this.currentHandle }, specifiedIndex, resolveNewCode ); } return super.createWidget(constructor, slotName, initConfig, specifiedIndex, resolveNewCode); } protected $$unmountedAfterProperties() { super.$$unmountedAfterProperties(); RuntimeContextManager.delete(this.currentHandle); } }