import { type ActiveRecords, ModelCache, type RuntimeModel, type RuntimeRelationField } from '@oinone/kunlun-engine'; import { isEmptyValue } from '@oinone/kunlun-meta'; import { CallChaining } from '@oinone/kunlun-shared'; import { autoFillByLabel, autoFillByLabelFields } from '@oinone/kunlun-vue-admin-layout'; import { Widget } from '@oinone/kunlun-vue-widget'; import { BaseTableFieldWidget, type BaseTableFieldWidgetProps } from '../../../table-column'; export class TableComplexFieldWidget< Value = ActiveRecords, Field extends RuntimeRelationField = RuntimeRelationField, Props extends BaseTableFieldWidgetProps = BaseTableFieldWidgetProps > extends BaseTableFieldWidget { @Widget.Reactive() protected get referencesModel(): RuntimeModel | undefined { return this.field.referencesModel; } @Widget.Reactive() @Widget.Inject('mountedCallChaining') protected parentMountedCallChaining: CallChaining | undefined; @Widget.Reactive() public get labelFields(): string[] { return this.referencesModel?.labelFields || []; } @Widget.Reactive() public get searchFields(): string[] { return this.getDsl().searchFields || this.referencesModel?.labelFields; } @Widget.Reactive() public get separator() { return this.getDsl().separator || ', '; } @Widget.Reactive() protected get relationFieldKey(): string { // fixme @zbh 20230114 改版 使用string[] | undefined return this.referencesModel?.pks?.[0] || 'id'; } @Widget.Reactive() protected get defaultSortable() { return false; } protected handleTableLabel(dataEntity) { const realLabel = this.optionLabel || this.referencesModel?.label; let showValue; if (isEmptyValue(realLabel)) { showValue = autoFillByLabelFields(this.relationFieldKey, dataEntity, this.labelFields, this.separator); } else { showValue = autoFillByLabel(this.relationFieldKey, dataEntity, realLabel, this.optionLabelContextArgs); } return showValue.label; } @Widget.Reactive() protected get optionLabel() { const _optionLabel = this.getDsl().optionLabel; if (_optionLabel) { return _optionLabel; } return ''; } @Widget.Reactive() protected get optionLabelContextArgs() { const _optionLabelContextArgs = this.getDsl().optionLabelContextArgs; if (_optionLabelContextArgs) { return _optionLabelContextArgs; } return ''; } /** * 当所有referencesModel都能在field中进行获取后移除该方法 * @protected */ protected async resolveReferenceModel() { let { referencesModel } = this; if (!referencesModel) { const references = this.field.references; if (references) { referencesModel = await ModelCache.get(references); if (referencesModel) { this.field!.referencesModel = referencesModel; } } } } protected $$mounted() { super.$$mounted(); this.parentMountedCallChaining?.hook(this.path, async () => { await this.resolveReferenceModel(); }); } protected $$unmounted() { super.$$unmounted(); this.parentMountedCallChaining?.unhook(this.path); } }